blob: f4cbde03c6adda03c497f0ce08a358081f78d6d6 [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/vio.h>
Seth Jennings0e16aaf2012-07-19 09:42:40 -050025
Dan Streetman7011a122015-05-07 13:49:17 -040026#include "nx-842.h"
Seth Jennings0e16aaf2012-07-19 09:42:40 -050027#include "nx_csbcpb.h" /* struct nx_csbcpb */
28
Seth Jennings0e16aaf2012-07-19 09:42:40 -050029MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Robert Jennings <rcj@linux.vnet.ibm.com>");
31MODULE_DESCRIPTION("842 H/W Compression driver for IBM Power processors");
Dan Streetman03952d92015-07-22 14:26:38 -040032MODULE_ALIAS_CRYPTO("842");
33MODULE_ALIAS_CRYPTO("842-nx");
Seth Jennings0e16aaf2012-07-19 09:42:40 -050034
Dan Streetman959e6652015-05-07 13:49:18 -040035static struct nx842_constraints nx842_pseries_constraints = {
Dan Streetman3154de72015-06-02 15:22:10 -040036 .alignment = DDE_BUFFER_ALIGN,
Dan Streetman959e6652015-05-07 13:49:18 -040037 .multiple = DDE_BUFFER_LAST_MULT,
Dan Streetman3154de72015-06-02 15:22:10 -040038 .minimum = DDE_BUFFER_LAST_MULT,
Dan Streetman959e6652015-05-07 13:49:18 -040039 .maximum = PAGE_SIZE, /* dynamic, max_sync_size */
40};
41
Dan Streetmanb8e041872015-05-07 13:49:20 -040042static int check_constraints(unsigned long buf, unsigned int *len, bool in)
Seth Jennings0e16aaf2012-07-19 09:42:40 -050043{
Dan Streetmanb8e041872015-05-07 13:49:20 -040044 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);
48 return -EINVAL;
49 }
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);
54 if (in)
55 return -EINVAL;
56 *len = round_down(*len, nx842_pseries_constraints.multiple);
57 }
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);
62 return -EINVAL;
63 }
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);
68 if (in)
69 return -EINVAL;
70 *len = nx842_pseries_constraints.maximum;
71 }
72 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -050073}
74
Dan Streetmanb8e041872015-05-07 13:49:20 -040075/* I assume we need to align the CSB? */
76#define WORKMEM_ALIGN (256)
77
78struct nx842_workmem {
79 /* scatterlist */
80 char slin[4096];
81 char slout[4096];
82 /* coprocessor status/parameter block */
83 struct nx_csbcpb csbcpb;
84
85 char padding[WORKMEM_ALIGN];
86} __aligned(WORKMEM_ALIGN);
87
Seth Jennings0e16aaf2012-07-19 09:42:40 -050088/* 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))
91
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))
99
100/* The NX unit accepts data only on 4K page boundaries */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400101#define NX842_HW_PAGE_SIZE (4096)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500102#define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1))
103
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500104struct ibm_nx842_counters {
105 atomic64_t comp_complete;
106 atomic64_t comp_failed;
107 atomic64_t decomp_complete;
108 atomic64_t decomp_failed;
109 atomic64_t swdecomp;
110 atomic64_t comp_times[32];
111 atomic64_t decomp_times[32];
112};
113
114static struct nx842_devdata {
115 struct vio_dev *vdev;
116 struct device *dev;
117 struct ibm_nx842_counters *counters;
118 unsigned int max_sg_len;
119 unsigned int max_sync_size;
120 unsigned int max_sync_sg;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500121} __rcu *devdata;
122static DEFINE_SPINLOCK(devdata_mutex);
123
124#define NX842_COUNTER_INC(_x) \
125static inline void nx842_inc_##_x( \
126 const struct nx842_devdata *dev) { \
127 if (dev) \
128 atomic64_inc(&dev->counters->_x); \
129}
130NX842_COUNTER_INC(comp_complete);
131NX842_COUNTER_INC(comp_failed);
132NX842_COUNTER_INC(decomp_complete);
133NX842_COUNTER_INC(decomp_failed);
134NX842_COUNTER_INC(swdecomp);
135
136#define NX842_HIST_SLOTS 16
137
138static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
139{
140 int bucket = fls(time);
141
142 if (bucket)
143 bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
144
145 atomic64_inc(&times[bucket]);
146}
147
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)
157
158static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
159{
160 /* No use of DMA mappings within the driver. */
161 return 0;
162}
163
164struct nx842_slentry {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400165 __be64 ptr; /* Real address (use __pa()) */
166 __be64 len;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500167};
168
169/* pHyp scatterlist entry */
170struct nx842_scatterlist {
171 int entry_nr; /* number of slentries */
172 struct nx842_slentry *entries; /* ptr to array of slentries */
173};
174
175/* Does not include sizeof(entry_nr) in the size */
176static inline unsigned long nx842_get_scatterlist_size(
177 struct nx842_scatterlist *sl)
178{
179 return sl->entry_nr * sizeof(struct nx842_slentry);
180}
181
182static int nx842_build_scatterlist(unsigned long buf, int len,
183 struct nx842_scatterlist *sl)
184{
Dan Streetmanc47d6302015-06-18 12:05:30 -0400185 unsigned long entrylen;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500186 struct nx842_slentry *entry;
187
188 sl->entry_nr = 0;
189
190 entry = sl->entries;
191 while (len) {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400192 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);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500196
Dan Streetmanc47d6302015-06-18 12:05:30 -0400197 len -= entrylen;
198 buf += entrylen;
199
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500200 sl->entry_nr++;
201 entry++;
202 }
203
204 return 0;
205}
206
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500207static int nx842_validate_result(struct device *dev,
208 struct cop_status_block *csb)
209{
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",
213 __func__);
214 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
215 csb->valid,
216 csb->crb_seq_number,
217 csb->completion_code,
218 csb->completion_extension);
219 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
Dan Streetmanc47d6302015-06-18 12:05:30 -0400220 be32_to_cpu(csb->processed_byte_count),
221 (unsigned long)be64_to_cpu(csb->address));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500222 return -EIO;
223 }
224
225 /* Check return values from the hardware in the CSB */
226 switch (csb->completion_code) {
227 case 0: /* Completed without error */
228 break;
Dan Streetman7371c0a2015-07-29 19:42:09 -0400229 case 64: /* Compression ok, but output larger than input */
230 dev_dbg(dev, "%s: output size larger than input size\n",
231 __func__);
232 break;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500233 case 13: /* Output buffer too small */
Dan Streetman7371c0a2015-07-29 19:42:09 -0400234 dev_dbg(dev, "%s: Out of space in output buffer\n",
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500235 __func__);
236 return -ENOSPC;
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);
241 return -EINVAL;
242 default:
243 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
244 __func__, csb->completion_code);
245 return -EIO;
246 }
247
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__);
255 return -EIO;
256 }
257
258 return 0;
259}
260
261/**
Dan Streetman7011a122015-05-07 13:49:17 -0400262 * nx842_pseries_compress - Compress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500263 *
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.
267 *
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.
271 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400272 * @in: Pointer to input buffer
273 * @inlen: Length of input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500274 * @out: Pointer to output buffer
275 * @outlen: Length of output buffer
276 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400277 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500278 *
279 * Returns:
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
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500283 * -EIO Internal error
284 * -ENODEV Hardware unavailable
285 */
Dan Streetman7011a122015-05-07 13:49:17 -0400286static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
287 unsigned char *out, unsigned int *outlen,
288 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500289{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500290 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;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400295 int ret = 0, max_sync_size;
296 unsigned long inbuf, outbuf;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500297 struct vio_pfo_op op = {
298 .done = NULL,
299 .handle = 0,
300 .timeout = 0,
301 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400302 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500303
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500304 inbuf = (unsigned long)in;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400305 if (check_constraints(inbuf, &inlen, true))
306 return -EINVAL;
307
308 outbuf = (unsigned long)out;
309 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500310 return -EINVAL;
311
312 rcu_read_lock();
313 local_devdata = rcu_dereference(devdata);
314 if (!local_devdata || !local_devdata->dev) {
315 rcu_read_unlock();
316 return -ENODEV;
317 }
318 max_sync_size = local_devdata->max_sync_size;
319 dev = local_devdata->dev;
320
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500321 /* Init scatterlist */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400322 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500323 slin.entries = (struct nx842_slentry *)workmem->slin;
324 slout.entries = (struct nx842_slentry *)workmem->slout;
325
326 /* Init operation */
327 op.flags = NX842_OP_COMPRESS;
328 csbcpb = &workmem->csbcpb;
329 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600330 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500331
Dan Streetmanb8e041872015-05-07 13:49:20 -0400332 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);
336 op.inlen = inlen;
337 } else {
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);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500342 }
343
Dan Streetmanb8e041872015-05-07 13:49:20 -0400344 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);
348 op.outlen = *outlen;
349 } else {
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);
354 }
355
Dan Streetmanc47d6302015-06-18 12:05:30 -0400356 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);
359
Dan Streetmanb8e041872015-05-07 13:49:20 -0400360 /* Send request to pHyp */
361 ret = vio_h_cop_sync(local_devdata->vdev, &op);
362
363 /* Check for pHyp error */
364 if (ret) {
365 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
366 __func__, ret, op.hcall_err);
367 ret = -EIO;
368 goto unlock;
369 }
370
371 /* Check for hardware error */
372 ret = nx842_validate_result(dev, &csbcpb->csb);
373 if (ret)
374 goto unlock;
375
Dan Streetmanc47d6302015-06-18 12:05:30 -0400376 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400377 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500378
379unlock:
380 if (ret)
381 nx842_inc_comp_failed(local_devdata);
382 else {
383 nx842_inc_comp_complete(local_devdata);
384 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400385 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500386 }
387 rcu_read_unlock();
388 return ret;
389}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500390
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500391/**
Dan Streetman7011a122015-05-07 13:49:17 -0400392 * nx842_pseries_decompress - Decompress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500393 *
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.
401 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400402 * @in: Pointer to input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500403 * @inlen: Length of input buffer
Dan Streetmanb8e041872015-05-07 13:49:20 -0400404 * @out: Pointer to output buffer
405 * @outlen: Length of output buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500406 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400407 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500408 *
409 * Returns:
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
416 */
Dan Streetman7011a122015-05-07 13:49:17 -0400417static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
418 unsigned char *out, unsigned int *outlen,
419 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500420{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500421 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;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400426 int ret = 0, max_sync_size;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500427 unsigned long inbuf, outbuf;
428 struct vio_pfo_op op = {
429 .done = NULL,
430 .handle = 0,
431 .timeout = 0,
432 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400433 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500434
435 /* Ensure page alignment and size */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400436 inbuf = (unsigned long)in;
437 if (check_constraints(inbuf, &inlen, true))
438 return -EINVAL;
439
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500440 outbuf = (unsigned long)out;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400441 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500442 return -EINVAL;
443
444 rcu_read_lock();
445 local_devdata = rcu_dereference(devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400446 if (!local_devdata || !local_devdata->dev) {
447 rcu_read_unlock();
448 return -ENODEV;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500449 }
Dan Streetmanb8e041872015-05-07 13:49:20 -0400450 max_sync_size = local_devdata->max_sync_size;
451 dev = local_devdata->dev;
452
453 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500454
455 /* Init scatterlist */
456 slin.entries = (struct nx842_slentry *)workmem->slin;
457 slout.entries = (struct nx842_slentry *)workmem->slout;
458
459 /* Init operation */
460 op.flags = NX842_OP_DECOMPRESS;
461 csbcpb = &workmem->csbcpb;
462 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600463 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500464
Dan Streetmanb8e041872015-05-07 13:49:20 -0400465 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);
469 op.inlen = inlen;
470 } else {
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);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500475 }
476
Dan Streetmanb8e041872015-05-07 13:49:20 -0400477 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);
481 op.outlen = *outlen;
482 } else {
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);
487 }
488
Dan Streetmanc47d6302015-06-18 12:05:30 -0400489 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);
492
Dan Streetmanb8e041872015-05-07 13:49:20 -0400493 /* Send request to pHyp */
494 ret = vio_h_cop_sync(local_devdata->vdev, &op);
495
496 /* Check for pHyp error */
497 if (ret) {
498 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
499 __func__, ret, op.hcall_err);
500 goto unlock;
501 }
502
503 /* Check for hardware error */
504 ret = nx842_validate_result(dev, &csbcpb->csb);
505 if (ret)
506 goto unlock;
507
Dan Streetmanc47d6302015-06-18 12:05:30 -0400508 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500509
510unlock:
511 if (ret)
512 /* decompress fail */
513 nx842_inc_decomp_failed(local_devdata);
514 else {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500515 nx842_inc_decomp_complete(local_devdata);
516 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400517 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500518 }
519
520 rcu_read_unlock();
521 return ret;
522}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500523
524/**
525 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
526 *
527 * @devdata - struct nx842_devdata to update
528 *
529 * Returns:
530 * 0 on success
531 * -ENOENT if @devdata ptr is NULL
532 */
533static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
534{
535 if (devdata) {
536 devdata->max_sync_size = 0;
537 devdata->max_sync_sg = 0;
538 devdata->max_sg_len = 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500539 return 0;
540 } else
541 return -ENOENT;
542}
543
544/**
Dan Streetman90fd73f2015-07-22 14:26:32 -0400545 * nx842_OF_upd_status -- Check the device info from OF status prop
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500546 *
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.
551 *
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500552 * @prop - struct property point containing the maxsyncop for the update
553 *
554 * Returns:
555 * 0 - Device is available
Nishanth Aravamudanfa9a9a02015-07-02 15:38:48 -0700556 * -ENODEV - Device is not available
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500557 */
Dan Streetman90fd73f2015-07-22 14:26:32 -0400558static int nx842_OF_upd_status(struct property *prop)
559{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500560 const char *status = (const char *)prop->value;
561
Dan Streetman90fd73f2015-07-22 14:26:32 -0400562 if (!strncmp(status, "okay", (size_t)prop->length))
563 return 0;
564 if (!strncmp(status, "disabled", (size_t)prop->length))
565 return -ENODEV;
566 dev_info(devdata->dev, "%s: unknown status '%s'\n", __func__, status);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500567
Dan Streetman90fd73f2015-07-22 14:26:32 -0400568 return -EINVAL;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500569}
570
571/**
572 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
573 *
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.
577 *
578 * Example:
579 * # od -x ibm,max-sg-len
580 * 0000000 0000 0ff0
581 *
582 * In this example, the maximum byte length of a scatter list is
583 * 0x0ff0 (4,080).
584 *
585 * @devdata - struct nx842_devdata to update
586 * @prop - struct property point containing the maxsyncop for the update
587 *
588 * Returns:
589 * 0 on success
590 * -EINVAL on failure
591 */
592static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
593 struct property *prop) {
594 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400595 const unsigned int maxsglen = of_read_number(prop->value, 1);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500596
Dan Streetmanc47d6302015-06-18 12:05:30 -0400597 if (prop->length != sizeof(maxsglen)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500598 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__,
Dan Streetmanc47d6302015-06-18 12:05:30 -0400600 prop->length, sizeof(maxsglen));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500601 ret = -EINVAL;
602 } else {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400603 devdata->max_sg_len = min_t(unsigned int,
604 maxsglen, NX842_HW_PAGE_SIZE);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500605 }
606
607 return ret;
608}
609
610/**
611 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
612 *
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
619 * of the form
620 * a. One cell data byte length
621 * b. One cell total number of scatter list elements
622 *
623 * Example:
624 * # od -x ibm,max-sync-cop
625 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
626 * 0000020 0000 1000 0000 01fe
627 *
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
631 * elements.
632 *
633 * @devdata - struct nx842_devdata to update
634 * @prop - struct property point containing the maxsyncop for the update
635 *
636 * Returns:
637 * 0 on success
638 * -EINVAL on failure
639 */
640static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
641 struct property *prop) {
642 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400643 unsigned int comp_data_limit, decomp_data_limit;
644 unsigned int comp_sg_limit, decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500645 const struct maxsynccop_t {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400646 __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;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500652 } *maxsynccop;
653
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));
658 ret = -EINVAL;
659 goto out;
660 }
661
662 maxsynccop = (const struct maxsynccop_t *)prop->value;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400663 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);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500667
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) */
Dan Streetmanc47d6302015-06-18 12:05:30 -0400672 devdata->max_sync_size = min(comp_data_limit, decomp_data_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500673
674 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400675 65536);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500676
Dan Streetmanb8e041872015-05-07 13:49:20 -0400677 if (devdata->max_sync_size < 4096) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500678 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);
682 ret = -EINVAL;
683 goto out;
684 }
685
Dan Streetman959e6652015-05-07 13:49:18 -0400686 nx842_pseries_constraints.maximum = devdata->max_sync_size;
687
Dan Streetmanc47d6302015-06-18 12:05:30 -0400688 devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500689 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);
694 ret = -EINVAL;
695 goto out;
696 }
697
698out:
699 return ret;
700}
701
702/**
703 *
704 * nx842_OF_upd -- Handle OF properties updates for the device.
705 *
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.
710 *
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.
713 *
714 * Returns:
715 * 0 - Success
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
720 */
721static int nx842_OF_upd(struct property *new_prop)
722{
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;
729 int ret = 0;
730 unsigned long flags;
731
Dan Streetman7f6e3aa2015-07-22 14:26:33 -0400732 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
733 if (!new_devdata)
734 return -ENOMEM;
735
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500736 spin_lock_irqsave(&devdata_mutex, flags);
737 old_devdata = rcu_dereference_check(devdata,
738 lockdep_is_held(&devdata_mutex));
739 if (old_devdata)
740 of_node = old_devdata->dev->of_node;
741
742 if (!old_devdata || !of_node) {
743 pr_err("%s: device is not available\n", __func__);
744 spin_unlock_irqrestore(&devdata_mutex, flags);
Dan Streetman7f6e3aa2015-07-22 14:26:33 -0400745 kfree(new_devdata);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500746 return -ENODEV;
747 }
748
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500749 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
750 new_devdata->counters = old_devdata->counters;
751
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__);
758 ret = -EINVAL;
759 goto error_out;
760 }
761
Grant Likely259092a2014-07-16 12:48:23 -0600762 /*
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
765 */
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)))
769 goto out;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500770
771 /* Perform property updates */
Dan Streetman90fd73f2015-07-22 14:26:32 -0400772 ret = nx842_OF_upd_status(status);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500773 if (ret)
774 goto error_out;
775
776 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
777 if (ret)
778 goto error_out;
779
780 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
781 if (ret)
782 goto error_out;
783
784out:
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);
794
795 rcu_assign_pointer(devdata, new_devdata);
796 spin_unlock_irqrestore(&devdata_mutex, flags);
797 synchronize_rcu();
798 dev_set_drvdata(new_devdata->dev, new_devdata);
799 kfree(old_devdata);
800 return 0;
801
802error_out:
803 if (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);
808 synchronize_rcu();
809 dev_set_drvdata(new_devdata->dev, new_devdata);
810 kfree(old_devdata);
811 } else {
812 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
813 spin_unlock_irqrestore(&devdata_mutex, flags);
814 }
815
816 if (!ret)
817 ret = -EINVAL;
818 return ret;
819}
820
821/**
822 * nx842_OF_notifier - Process updates to OF properties for the device
823 *
824 * @np: notifier block
825 * @action: notifier action
826 * @update: struct pSeries_reconfig_prop_update pointer if action is
827 * PSERIES_UPDATE_PROPERTY
828 *
829 * Returns:
830 * NOTIFY_OK on success
831 * NOTIFY_BAD encoded with error number on failure, use
832 * notifier_to_errno() to decode this value
833 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000834static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
Grant Likelyf5242e52014-11-24 17:58:01 +0000835 void *data)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500836{
Grant Likelyf5242e52014-11-24 17:58:01 +0000837 struct of_reconfig_data *upd = data;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500838 struct nx842_devdata *local_devdata;
839 struct device_node *node = NULL;
840
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500841 rcu_read_lock();
842 local_devdata = rcu_dereference(devdata);
843 if (local_devdata)
844 node = local_devdata->dev->of_node;
845
846 if (local_devdata &&
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000847 action == OF_RECONFIG_UPDATE_PROPERTY &&
848 !strcmp(upd->dn->name, node->name)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500849 rcu_read_unlock();
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000850 nx842_OF_upd(upd->prop);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500851 } else
852 rcu_read_unlock();
853
854 return NOTIFY_OK;
855}
856
857static struct notifier_block nx842_of_nb = {
858 .notifier_call = nx842_OF_notifier,
859};
860
861#define nx842_counter_read(_name) \
862static ssize_t nx842_##_name##_show(struct device *dev, \
863 struct device_attribute *attr, \
864 char *buf) { \
865 struct nx842_devdata *local_devdata; \
866 int p = 0; \
867 rcu_read_lock(); \
868 local_devdata = rcu_dereference(devdata); \
869 if (local_devdata) \
870 p = snprintf(buf, PAGE_SIZE, "%ld\n", \
871 atomic64_read(&local_devdata->counters->_name)); \
872 rcu_read_unlock(); \
873 return p; \
874}
875
876#define NX842DEV_COUNTER_ATTR_RO(_name) \
877 nx842_counter_read(_name); \
878 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
879 0444, \
880 nx842_##_name##_show,\
881 NULL);
882
883NX842DEV_COUNTER_ATTR_RO(comp_complete);
884NX842DEV_COUNTER_ATTR_RO(comp_failed);
885NX842DEV_COUNTER_ATTR_RO(decomp_complete);
886NX842DEV_COUNTER_ATTR_RO(decomp_failed);
887NX842DEV_COUNTER_ATTR_RO(swdecomp);
888
889static ssize_t nx842_timehist_show(struct device *,
890 struct device_attribute *, char *);
891
892static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
893 nx842_timehist_show, NULL);
894static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
895 0444, nx842_timehist_show, NULL);
896
897static ssize_t nx842_timehist_show(struct device *dev,
898 struct device_attribute *attr, char *buf) {
899 char *p = buf;
900 struct nx842_devdata *local_devdata;
901 atomic64_t *times;
902 int bytes_remain = PAGE_SIZE;
903 int bytes;
904 int i;
905
906 rcu_read_lock();
907 local_devdata = rcu_dereference(devdata);
908 if (!local_devdata) {
909 rcu_read_unlock();
910 return 0;
911 }
912
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;
917 else {
918 rcu_read_unlock();
919 return 0;
920 }
921
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(&times[i]));
926 bytes_remain -= bytes;
927 p += bytes;
928 }
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(&times[(NX842_HIST_SLOTS - 1)]));
934 p += bytes;
935
936 rcu_read_unlock();
937 return p - buf;
938}
939
940static 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,
948 NULL,
949};
950
951static struct attribute_group nx842_attribute_group = {
952 .name = NULL, /* put in device directory */
953 .attrs = nx842_sysfs_entries,
954};
955
Dan Streetman7011a122015-05-07 13:49:17 -0400956static struct nx842_driver nx842_pseries_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -0400957 .name = KBUILD_MODNAME,
Dan Streetman7011a122015-05-07 13:49:17 -0400958 .owner = THIS_MODULE,
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400959 .workmem_size = sizeof(struct nx842_workmem),
Dan Streetman959e6652015-05-07 13:49:18 -0400960 .constraints = &nx842_pseries_constraints,
Dan Streetman7011a122015-05-07 13:49:17 -0400961 .compress = nx842_pseries_compress,
962 .decompress = nx842_pseries_decompress,
963};
964
Dan Streetman03952d92015-07-22 14:26:38 -0400965static int nx842_pseries_crypto_init(struct crypto_tfm *tfm)
966{
967 return nx842_crypto_init(tfm, &nx842_pseries_driver);
968}
969
970static struct crypto_alg nx842_pseries_alg = {
971 .cra_name = "842",
972 .cra_driver_name = "842-nx",
973 .cra_priority = 300,
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 } }
982};
983
Dan Streetman039af962015-07-22 14:26:31 -0400984static int nx842_probe(struct vio_dev *viodev,
985 const struct vio_device_id *id)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500986{
987 struct nx842_devdata *old_devdata, *new_devdata = NULL;
988 unsigned long flags;
989 int ret = 0;
990
Dan Streetman7f6e3aa2015-07-22 14:26:33 -0400991 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
992 if (!new_devdata)
993 return -ENOMEM;
994
995 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
996 GFP_NOFS);
997 if (!new_devdata->counters) {
998 kfree(new_devdata);
999 return -ENOMEM;
1000 }
1001
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001002 spin_lock_irqsave(&devdata_mutex, flags);
1003 old_devdata = rcu_dereference_check(devdata,
1004 lockdep_is_held(&devdata_mutex));
1005
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__);
1008 ret = -1;
1009 goto error_unlock;
1010 }
1011
1012 dev_set_drvdata(&viodev->dev, NULL);
1013
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001014 new_devdata->vdev = viodev;
1015 new_devdata->dev = &viodev->dev;
1016 nx842_OF_set_defaults(new_devdata);
1017
1018 rcu_assign_pointer(devdata, new_devdata);
1019 spin_unlock_irqrestore(&devdata_mutex, flags);
1020 synchronize_rcu();
1021 kfree(old_devdata);
1022
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001023 of_reconfig_notifier_register(&nx842_of_nb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001024
1025 ret = nx842_OF_upd(NULL);
Dan Streetmanee781b72015-07-22 14:26:34 -04001026 if (ret)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001027 goto error;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001028
Dan Streetman03952d92015-07-22 14:26:38 -04001029 ret = crypto_register_alg(&nx842_pseries_alg);
1030 if (ret) {
1031 dev_err(&viodev->dev, "could not register comp alg: %d\n", ret);
1032 goto error;
1033 }
1034
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001035 rcu_read_lock();
Jean Delvarecda43572014-05-28 14:02:24 +02001036 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001037 rcu_read_unlock();
1038
1039 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1040 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1041 ret = -1;
1042 goto error;
1043 }
1044
1045 return 0;
1046
1047error_unlock:
1048 spin_unlock_irqrestore(&devdata_mutex, flags);
1049 if (new_devdata)
1050 kfree(new_devdata->counters);
1051 kfree(new_devdata);
1052error:
1053 return ret;
1054}
1055
Dan Streetman039af962015-07-22 14:26:31 -04001056static int nx842_remove(struct vio_dev *viodev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001057{
1058 struct nx842_devdata *old_devdata;
1059 unsigned long flags;
1060
1061 pr_info("Removing IBM Power 842 compression device\n");
1062 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1063
Dan Streetman03952d92015-07-22 14:26:38 -04001064 crypto_unregister_alg(&nx842_pseries_alg);
1065
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001066 spin_lock_irqsave(&devdata_mutex, flags);
1067 old_devdata = rcu_dereference_check(devdata,
1068 lockdep_is_held(&devdata_mutex));
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001069 of_reconfig_notifier_unregister(&nx842_of_nb);
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301070 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001071 spin_unlock_irqrestore(&devdata_mutex, flags);
1072 synchronize_rcu();
1073 dev_set_drvdata(&viodev->dev, NULL);
1074 if (old_devdata)
1075 kfree(old_devdata->counters);
1076 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001077
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001078 return 0;
1079}
1080
Dan Streetmanb8e041872015-05-07 13:49:20 -04001081static struct vio_device_id nx842_vio_driver_ids[] = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001082 {"ibm,compression-v1", "ibm,compression"},
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001083 {"", ""},
1084};
1085
Dan Streetmanb8e041872015-05-07 13:49:20 -04001086static struct vio_driver nx842_vio_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001087 .name = KBUILD_MODNAME,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001088 .probe = nx842_probe,
Dan Streetman039af962015-07-22 14:26:31 -04001089 .remove = nx842_remove,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001090 .get_desired_dma = nx842_get_desired_dma,
Dan Streetmanb8e041872015-05-07 13:49:20 -04001091 .id_table = nx842_vio_driver_ids,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001092};
1093
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001094static int __init nx842_pseries_init(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001095{
1096 struct nx842_devdata *new_devdata;
Dan Streetman3e648cb2015-05-28 16:21:31 -04001097 int ret;
1098
Dan Streetman3e648cb2015-05-28 16:21:31 -04001099 if (!of_find_compatible_node(NULL, NULL, "ibm,compression"))
1100 return -ENODEV;
1101
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001102 RCU_INIT_POINTER(devdata, NULL);
1103 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1104 if (!new_devdata) {
1105 pr_err("Could not allocate memory for device data\n");
1106 return -ENOMEM;
1107 }
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001108 RCU_INIT_POINTER(devdata, new_devdata);
1109
Dan Streetman3e648cb2015-05-28 16:21:31 -04001110 ret = vio_register_driver(&nx842_vio_driver);
1111 if (ret) {
1112 pr_err("Could not register VIO driver %d\n", ret);
1113
1114 kfree(new_devdata);
1115 return ret;
1116 }
1117
Dan Streetman3e648cb2015-05-28 16:21:31 -04001118 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001119}
1120
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001121module_init(nx842_pseries_init);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001122
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001123static void __exit nx842_pseries_exit(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001124{
1125 struct nx842_devdata *old_devdata;
1126 unsigned long flags;
1127
Dan Streetman03952d92015-07-22 14:26:38 -04001128 crypto_unregister_alg(&nx842_pseries_alg);
1129
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001130 spin_lock_irqsave(&devdata_mutex, flags);
1131 old_devdata = rcu_dereference_check(devdata,
1132 lockdep_is_held(&devdata_mutex));
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301133 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001134 spin_unlock_irqrestore(&devdata_mutex, flags);
1135 synchronize_rcu();
Dan Streetmanb8e041872015-05-07 13:49:20 -04001136 if (old_devdata && old_devdata->dev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001137 dev_set_drvdata(old_devdata->dev, NULL);
1138 kfree(old_devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -04001139 vio_unregister_driver(&nx842_vio_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001140}
1141
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001142module_exit(nx842_pseries_exit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001143