blob: b6a26907e11f1a8721def730ae0085e4c75c8616 [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;
229 case 64: /* Target bytes > Source bytes during compression */
230 case 13: /* Output buffer too small */
231 dev_dbg(dev, "%s: Compression output larger than input\n",
232 __func__);
233 return -ENOSPC;
234 case 66: /* Input data contains an illegal template field */
235 case 67: /* Template indicates data past the end of the input stream */
236 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
237 __func__, csb->completion_code);
238 return -EINVAL;
239 default:
240 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
241 __func__, csb->completion_code);
242 return -EIO;
243 }
244
245 /* Hardware sanity check */
246 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
247 dev_err(dev, "%s: No error returned by hardware, but "
248 "data returned is unusable, contact support.\n"
249 "(Additional info: csbcbp->processed bytes "
250 "does not specify processed bytes for the "
251 "target buffer.)\n", __func__);
252 return -EIO;
253 }
254
255 return 0;
256}
257
258/**
Dan Streetman7011a122015-05-07 13:49:17 -0400259 * nx842_pseries_compress - Compress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500260 *
261 * Compression provide by the NX842 coprocessor on IBM Power systems.
262 * The input buffer is compressed and the result is stored in the
263 * provided output buffer.
264 *
265 * Upon return from this function @outlen contains the length of the
266 * compressed data. If there is an error then @outlen will be 0 and an
267 * error will be specified by the return code from this function.
268 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400269 * @in: Pointer to input buffer
270 * @inlen: Length of input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500271 * @out: Pointer to output buffer
272 * @outlen: Length of output buffer
273 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400274 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500275 *
276 * Returns:
277 * 0 Success, output of length @outlen stored in the buffer at @out
278 * -ENOMEM Unable to allocate internal buffers
279 * -ENOSPC Output buffer is to small
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500280 * -EIO Internal error
281 * -ENODEV Hardware unavailable
282 */
Dan Streetman7011a122015-05-07 13:49:17 -0400283static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
284 unsigned char *out, unsigned int *outlen,
285 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500286{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500287 struct nx842_devdata *local_devdata;
288 struct device *dev = NULL;
289 struct nx842_workmem *workmem;
290 struct nx842_scatterlist slin, slout;
291 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400292 int ret = 0, max_sync_size;
293 unsigned long inbuf, outbuf;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500294 struct vio_pfo_op op = {
295 .done = NULL,
296 .handle = 0,
297 .timeout = 0,
298 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400299 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500300
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500301 inbuf = (unsigned long)in;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400302 if (check_constraints(inbuf, &inlen, true))
303 return -EINVAL;
304
305 outbuf = (unsigned long)out;
306 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500307 return -EINVAL;
308
309 rcu_read_lock();
310 local_devdata = rcu_dereference(devdata);
311 if (!local_devdata || !local_devdata->dev) {
312 rcu_read_unlock();
313 return -ENODEV;
314 }
315 max_sync_size = local_devdata->max_sync_size;
316 dev = local_devdata->dev;
317
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500318 /* Init scatterlist */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400319 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500320 slin.entries = (struct nx842_slentry *)workmem->slin;
321 slout.entries = (struct nx842_slentry *)workmem->slout;
322
323 /* Init operation */
324 op.flags = NX842_OP_COMPRESS;
325 csbcpb = &workmem->csbcpb;
326 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600327 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500328
Dan Streetmanb8e041872015-05-07 13:49:20 -0400329 if ((inbuf & NX842_HW_PAGE_MASK) ==
330 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
331 /* Create direct DDE */
332 op.in = nx842_get_pa((void *)inbuf);
333 op.inlen = inlen;
334 } else {
335 /* Create indirect DDE (scatterlist) */
336 nx842_build_scatterlist(inbuf, inlen, &slin);
337 op.in = nx842_get_pa(slin.entries);
338 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500339 }
340
Dan Streetmanb8e041872015-05-07 13:49:20 -0400341 if ((outbuf & NX842_HW_PAGE_MASK) ==
342 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
343 /* Create direct DDE */
344 op.out = nx842_get_pa((void *)outbuf);
345 op.outlen = *outlen;
346 } else {
347 /* Create indirect DDE (scatterlist) */
348 nx842_build_scatterlist(outbuf, *outlen, &slout);
349 op.out = nx842_get_pa(slout.entries);
350 op.outlen = -nx842_get_scatterlist_size(&slout);
351 }
352
Dan Streetmanc47d6302015-06-18 12:05:30 -0400353 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
354 __func__, (unsigned long)op.in, (long)op.inlen,
355 (unsigned long)op.out, (long)op.outlen);
356
Dan Streetmanb8e041872015-05-07 13:49:20 -0400357 /* Send request to pHyp */
358 ret = vio_h_cop_sync(local_devdata->vdev, &op);
359
360 /* Check for pHyp error */
361 if (ret) {
362 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
363 __func__, ret, op.hcall_err);
364 ret = -EIO;
365 goto unlock;
366 }
367
368 /* Check for hardware error */
369 ret = nx842_validate_result(dev, &csbcpb->csb);
370 if (ret)
371 goto unlock;
372
Dan Streetmanc47d6302015-06-18 12:05:30 -0400373 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400374 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500375
376unlock:
377 if (ret)
378 nx842_inc_comp_failed(local_devdata);
379 else {
380 nx842_inc_comp_complete(local_devdata);
381 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400382 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500383 }
384 rcu_read_unlock();
385 return ret;
386}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500387
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500388/**
Dan Streetman7011a122015-05-07 13:49:17 -0400389 * nx842_pseries_decompress - Decompress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500390 *
391 * Decompression provide by the NX842 coprocessor on IBM Power systems.
392 * The input buffer is decompressed and the result is stored in the
393 * provided output buffer. The size allocated to the output buffer is
394 * provided by the caller of this function in @outlen. Upon return from
395 * this function @outlen contains the length of the decompressed data.
396 * If there is an error then @outlen will be 0 and an error will be
397 * specified by the return code from this function.
398 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400399 * @in: Pointer to input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500400 * @inlen: Length of input buffer
Dan Streetmanb8e041872015-05-07 13:49:20 -0400401 * @out: Pointer to output buffer
402 * @outlen: Length of output buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500403 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400404 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500405 *
406 * Returns:
407 * 0 Success, output of length @outlen stored in the buffer at @out
408 * -ENODEV Hardware decompression device is unavailable
409 * -ENOMEM Unable to allocate internal buffers
410 * -ENOSPC Output buffer is to small
411 * -EINVAL Bad input data encountered when attempting decompress
412 * -EIO Internal error
413 */
Dan Streetman7011a122015-05-07 13:49:17 -0400414static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
415 unsigned char *out, unsigned int *outlen,
416 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500417{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500418 struct nx842_devdata *local_devdata;
419 struct device *dev = NULL;
420 struct nx842_workmem *workmem;
421 struct nx842_scatterlist slin, slout;
422 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400423 int ret = 0, max_sync_size;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500424 unsigned long inbuf, outbuf;
425 struct vio_pfo_op op = {
426 .done = NULL,
427 .handle = 0,
428 .timeout = 0,
429 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400430 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500431
432 /* Ensure page alignment and size */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400433 inbuf = (unsigned long)in;
434 if (check_constraints(inbuf, &inlen, true))
435 return -EINVAL;
436
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500437 outbuf = (unsigned long)out;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400438 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500439 return -EINVAL;
440
441 rcu_read_lock();
442 local_devdata = rcu_dereference(devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400443 if (!local_devdata || !local_devdata->dev) {
444 rcu_read_unlock();
445 return -ENODEV;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500446 }
Dan Streetmanb8e041872015-05-07 13:49:20 -0400447 max_sync_size = local_devdata->max_sync_size;
448 dev = local_devdata->dev;
449
450 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500451
452 /* Init scatterlist */
453 slin.entries = (struct nx842_slentry *)workmem->slin;
454 slout.entries = (struct nx842_slentry *)workmem->slout;
455
456 /* Init operation */
457 op.flags = NX842_OP_DECOMPRESS;
458 csbcpb = &workmem->csbcpb;
459 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600460 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500461
Dan Streetmanb8e041872015-05-07 13:49:20 -0400462 if ((inbuf & NX842_HW_PAGE_MASK) ==
463 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
464 /* Create direct DDE */
465 op.in = nx842_get_pa((void *)inbuf);
466 op.inlen = inlen;
467 } else {
468 /* Create indirect DDE (scatterlist) */
469 nx842_build_scatterlist(inbuf, inlen, &slin);
470 op.in = nx842_get_pa(slin.entries);
471 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500472 }
473
Dan Streetmanb8e041872015-05-07 13:49:20 -0400474 if ((outbuf & NX842_HW_PAGE_MASK) ==
475 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
476 /* Create direct DDE */
477 op.out = nx842_get_pa((void *)outbuf);
478 op.outlen = *outlen;
479 } else {
480 /* Create indirect DDE (scatterlist) */
481 nx842_build_scatterlist(outbuf, *outlen, &slout);
482 op.out = nx842_get_pa(slout.entries);
483 op.outlen = -nx842_get_scatterlist_size(&slout);
484 }
485
Dan Streetmanc47d6302015-06-18 12:05:30 -0400486 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
487 __func__, (unsigned long)op.in, (long)op.inlen,
488 (unsigned long)op.out, (long)op.outlen);
489
Dan Streetmanb8e041872015-05-07 13:49:20 -0400490 /* Send request to pHyp */
491 ret = vio_h_cop_sync(local_devdata->vdev, &op);
492
493 /* Check for pHyp error */
494 if (ret) {
495 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
496 __func__, ret, op.hcall_err);
497 goto unlock;
498 }
499
500 /* Check for hardware error */
501 ret = nx842_validate_result(dev, &csbcpb->csb);
502 if (ret)
503 goto unlock;
504
Dan Streetmanc47d6302015-06-18 12:05:30 -0400505 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500506
507unlock:
508 if (ret)
509 /* decompress fail */
510 nx842_inc_decomp_failed(local_devdata);
511 else {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500512 nx842_inc_decomp_complete(local_devdata);
513 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400514 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500515 }
516
517 rcu_read_unlock();
518 return ret;
519}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500520
521/**
522 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
523 *
524 * @devdata - struct nx842_devdata to update
525 *
526 * Returns:
527 * 0 on success
528 * -ENOENT if @devdata ptr is NULL
529 */
530static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
531{
532 if (devdata) {
533 devdata->max_sync_size = 0;
534 devdata->max_sync_sg = 0;
535 devdata->max_sg_len = 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500536 return 0;
537 } else
538 return -ENOENT;
539}
540
541/**
Dan Streetman90fd73f2015-07-22 14:26:32 -0400542 * nx842_OF_upd_status -- Check the device info from OF status prop
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500543 *
544 * The status property indicates if the accelerator is enabled. If the
545 * device is in the OF tree it indicates that the hardware is present.
546 * The status field indicates if the device is enabled when the status
547 * is 'okay'. Otherwise the device driver will be disabled.
548 *
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500549 * @prop - struct property point containing the maxsyncop for the update
550 *
551 * Returns:
552 * 0 - Device is available
Nishanth Aravamudanfa9a9a02015-07-02 15:38:48 -0700553 * -ENODEV - Device is not available
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500554 */
Dan Streetman90fd73f2015-07-22 14:26:32 -0400555static int nx842_OF_upd_status(struct property *prop)
556{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500557 const char *status = (const char *)prop->value;
558
Dan Streetman90fd73f2015-07-22 14:26:32 -0400559 if (!strncmp(status, "okay", (size_t)prop->length))
560 return 0;
561 if (!strncmp(status, "disabled", (size_t)prop->length))
562 return -ENODEV;
563 dev_info(devdata->dev, "%s: unknown status '%s'\n", __func__, status);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500564
Dan Streetman90fd73f2015-07-22 14:26:32 -0400565 return -EINVAL;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500566}
567
568/**
569 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
570 *
571 * Definition of the 'ibm,max-sg-len' OF property:
572 * This field indicates the maximum byte length of a scatter list
573 * for the platform facility. It is a single cell encoded as with encode-int.
574 *
575 * Example:
576 * # od -x ibm,max-sg-len
577 * 0000000 0000 0ff0
578 *
579 * In this example, the maximum byte length of a scatter list is
580 * 0x0ff0 (4,080).
581 *
582 * @devdata - struct nx842_devdata to update
583 * @prop - struct property point containing the maxsyncop for the update
584 *
585 * Returns:
586 * 0 on success
587 * -EINVAL on failure
588 */
589static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
590 struct property *prop) {
591 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400592 const unsigned int maxsglen = of_read_number(prop->value, 1);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500593
Dan Streetmanc47d6302015-06-18 12:05:30 -0400594 if (prop->length != sizeof(maxsglen)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500595 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
596 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 -0400597 prop->length, sizeof(maxsglen));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500598 ret = -EINVAL;
599 } else {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400600 devdata->max_sg_len = min_t(unsigned int,
601 maxsglen, NX842_HW_PAGE_SIZE);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500602 }
603
604 return ret;
605}
606
607/**
608 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
609 *
610 * Definition of the 'ibm,max-sync-cop' OF property:
611 * Two series of cells. The first series of cells represents the maximums
612 * that can be synchronously compressed. The second series of cells
613 * represents the maximums that can be synchronously decompressed.
614 * 1. The first cell in each series contains the count of the number of
615 * data length, scatter list elements pairs that follow – each being
616 * of the form
617 * a. One cell data byte length
618 * b. One cell total number of scatter list elements
619 *
620 * Example:
621 * # od -x ibm,max-sync-cop
622 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
623 * 0000020 0000 1000 0000 01fe
624 *
625 * In this example, compression supports 0x1000 (4,096) data byte length
626 * and 0x1fe (510) total scatter list elements. Decompression supports
627 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
628 * elements.
629 *
630 * @devdata - struct nx842_devdata to update
631 * @prop - struct property point containing the maxsyncop for the update
632 *
633 * Returns:
634 * 0 on success
635 * -EINVAL on failure
636 */
637static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
638 struct property *prop) {
639 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400640 unsigned int comp_data_limit, decomp_data_limit;
641 unsigned int comp_sg_limit, decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500642 const struct maxsynccop_t {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400643 __be32 comp_elements;
644 __be32 comp_data_limit;
645 __be32 comp_sg_limit;
646 __be32 decomp_elements;
647 __be32 decomp_data_limit;
648 __be32 decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500649 } *maxsynccop;
650
651 if (prop->length != sizeof(*maxsynccop)) {
652 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
653 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
654 sizeof(*maxsynccop));
655 ret = -EINVAL;
656 goto out;
657 }
658
659 maxsynccop = (const struct maxsynccop_t *)prop->value;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400660 comp_data_limit = be32_to_cpu(maxsynccop->comp_data_limit);
661 comp_sg_limit = be32_to_cpu(maxsynccop->comp_sg_limit);
662 decomp_data_limit = be32_to_cpu(maxsynccop->decomp_data_limit);
663 decomp_sg_limit = be32_to_cpu(maxsynccop->decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500664
665 /* Use one limit rather than separate limits for compression and
666 * decompression. Set a maximum for this so as not to exceed the
667 * size that the header can support and round the value down to
668 * the hardware page size (4K) */
Dan Streetmanc47d6302015-06-18 12:05:30 -0400669 devdata->max_sync_size = min(comp_data_limit, decomp_data_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500670
671 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400672 65536);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500673
Dan Streetmanb8e041872015-05-07 13:49:20 -0400674 if (devdata->max_sync_size < 4096) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500675 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
676 "less than the driver minimum, unable to use "
677 "the hardware device\n",
678 __func__, devdata->max_sync_size);
679 ret = -EINVAL;
680 goto out;
681 }
682
Dan Streetman959e6652015-05-07 13:49:18 -0400683 nx842_pseries_constraints.maximum = devdata->max_sync_size;
684
Dan Streetmanc47d6302015-06-18 12:05:30 -0400685 devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500686 if (devdata->max_sync_sg < 1) {
687 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
688 "less than the driver minimum, unable to use "
689 "the hardware device\n",
690 __func__, devdata->max_sync_sg);
691 ret = -EINVAL;
692 goto out;
693 }
694
695out:
696 return ret;
697}
698
699/**
700 *
701 * nx842_OF_upd -- Handle OF properties updates for the device.
702 *
703 * Set all properties from the OF tree. Optionally, a new property
704 * can be provided by the @new_prop pointer to overwrite an existing value.
705 * The device will remain disabled until all values are valid, this function
706 * will return an error for updates unless all values are valid.
707 *
708 * @new_prop: If not NULL, this property is being updated. If NULL, update
709 * all properties from the current values in the OF tree.
710 *
711 * Returns:
712 * 0 - Success
713 * -ENOMEM - Could not allocate memory for new devdata structure
714 * -EINVAL - property value not found, new_prop is not a recognized
715 * property for the device or property value is not valid.
716 * -ENODEV - Device is not available
717 */
718static int nx842_OF_upd(struct property *new_prop)
719{
720 struct nx842_devdata *old_devdata = NULL;
721 struct nx842_devdata *new_devdata = NULL;
722 struct device_node *of_node = NULL;
723 struct property *status = NULL;
724 struct property *maxsglen = NULL;
725 struct property *maxsyncop = NULL;
726 int ret = 0;
727 unsigned long flags;
728
Dan Streetman7f6e3aa2015-07-22 14:26:33 -0400729 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
730 if (!new_devdata)
731 return -ENOMEM;
732
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500733 spin_lock_irqsave(&devdata_mutex, flags);
734 old_devdata = rcu_dereference_check(devdata,
735 lockdep_is_held(&devdata_mutex));
736 if (old_devdata)
737 of_node = old_devdata->dev->of_node;
738
739 if (!old_devdata || !of_node) {
740 pr_err("%s: device is not available\n", __func__);
741 spin_unlock_irqrestore(&devdata_mutex, flags);
Dan Streetman7f6e3aa2015-07-22 14:26:33 -0400742 kfree(new_devdata);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500743 return -ENODEV;
744 }
745
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500746 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
747 new_devdata->counters = old_devdata->counters;
748
749 /* Set ptrs for existing properties */
750 status = of_find_property(of_node, "status", NULL);
751 maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
752 maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
753 if (!status || !maxsglen || !maxsyncop) {
754 dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
755 ret = -EINVAL;
756 goto error_out;
757 }
758
Grant Likely259092a2014-07-16 12:48:23 -0600759 /*
760 * If this is a property update, there are only certain properties that
761 * we care about. Bail if it isn't in the below list
762 */
763 if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
764 strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
765 strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
766 goto out;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500767
768 /* Perform property updates */
Dan Streetman90fd73f2015-07-22 14:26:32 -0400769 ret = nx842_OF_upd_status(status);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500770 if (ret)
771 goto error_out;
772
773 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
774 if (ret)
775 goto error_out;
776
777 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
778 if (ret)
779 goto error_out;
780
781out:
782 dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
783 __func__, new_devdata->max_sync_size,
784 old_devdata->max_sync_size);
785 dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
786 __func__, new_devdata->max_sync_sg,
787 old_devdata->max_sync_sg);
788 dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
789 __func__, new_devdata->max_sg_len,
790 old_devdata->max_sg_len);
791
792 rcu_assign_pointer(devdata, new_devdata);
793 spin_unlock_irqrestore(&devdata_mutex, flags);
794 synchronize_rcu();
795 dev_set_drvdata(new_devdata->dev, new_devdata);
796 kfree(old_devdata);
797 return 0;
798
799error_out:
800 if (new_devdata) {
801 dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
802 nx842_OF_set_defaults(new_devdata);
803 rcu_assign_pointer(devdata, new_devdata);
804 spin_unlock_irqrestore(&devdata_mutex, flags);
805 synchronize_rcu();
806 dev_set_drvdata(new_devdata->dev, new_devdata);
807 kfree(old_devdata);
808 } else {
809 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
810 spin_unlock_irqrestore(&devdata_mutex, flags);
811 }
812
813 if (!ret)
814 ret = -EINVAL;
815 return ret;
816}
817
818/**
819 * nx842_OF_notifier - Process updates to OF properties for the device
820 *
821 * @np: notifier block
822 * @action: notifier action
823 * @update: struct pSeries_reconfig_prop_update pointer if action is
824 * PSERIES_UPDATE_PROPERTY
825 *
826 * Returns:
827 * NOTIFY_OK on success
828 * NOTIFY_BAD encoded with error number on failure, use
829 * notifier_to_errno() to decode this value
830 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000831static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
Grant Likelyf5242e52014-11-24 17:58:01 +0000832 void *data)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500833{
Grant Likelyf5242e52014-11-24 17:58:01 +0000834 struct of_reconfig_data *upd = data;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500835 struct nx842_devdata *local_devdata;
836 struct device_node *node = NULL;
837
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500838 rcu_read_lock();
839 local_devdata = rcu_dereference(devdata);
840 if (local_devdata)
841 node = local_devdata->dev->of_node;
842
843 if (local_devdata &&
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000844 action == OF_RECONFIG_UPDATE_PROPERTY &&
845 !strcmp(upd->dn->name, node->name)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500846 rcu_read_unlock();
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000847 nx842_OF_upd(upd->prop);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500848 } else
849 rcu_read_unlock();
850
851 return NOTIFY_OK;
852}
853
854static struct notifier_block nx842_of_nb = {
855 .notifier_call = nx842_OF_notifier,
856};
857
858#define nx842_counter_read(_name) \
859static ssize_t nx842_##_name##_show(struct device *dev, \
860 struct device_attribute *attr, \
861 char *buf) { \
862 struct nx842_devdata *local_devdata; \
863 int p = 0; \
864 rcu_read_lock(); \
865 local_devdata = rcu_dereference(devdata); \
866 if (local_devdata) \
867 p = snprintf(buf, PAGE_SIZE, "%ld\n", \
868 atomic64_read(&local_devdata->counters->_name)); \
869 rcu_read_unlock(); \
870 return p; \
871}
872
873#define NX842DEV_COUNTER_ATTR_RO(_name) \
874 nx842_counter_read(_name); \
875 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
876 0444, \
877 nx842_##_name##_show,\
878 NULL);
879
880NX842DEV_COUNTER_ATTR_RO(comp_complete);
881NX842DEV_COUNTER_ATTR_RO(comp_failed);
882NX842DEV_COUNTER_ATTR_RO(decomp_complete);
883NX842DEV_COUNTER_ATTR_RO(decomp_failed);
884NX842DEV_COUNTER_ATTR_RO(swdecomp);
885
886static ssize_t nx842_timehist_show(struct device *,
887 struct device_attribute *, char *);
888
889static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
890 nx842_timehist_show, NULL);
891static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
892 0444, nx842_timehist_show, NULL);
893
894static ssize_t nx842_timehist_show(struct device *dev,
895 struct device_attribute *attr, char *buf) {
896 char *p = buf;
897 struct nx842_devdata *local_devdata;
898 atomic64_t *times;
899 int bytes_remain = PAGE_SIZE;
900 int bytes;
901 int i;
902
903 rcu_read_lock();
904 local_devdata = rcu_dereference(devdata);
905 if (!local_devdata) {
906 rcu_read_unlock();
907 return 0;
908 }
909
910 if (attr == &dev_attr_comp_times)
911 times = local_devdata->counters->comp_times;
912 else if (attr == &dev_attr_decomp_times)
913 times = local_devdata->counters->decomp_times;
914 else {
915 rcu_read_unlock();
916 return 0;
917 }
918
919 for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
920 bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n",
921 i ? (2<<(i-1)) : 0, (2<<i)-1,
922 atomic64_read(&times[i]));
923 bytes_remain -= bytes;
924 p += bytes;
925 }
926 /* The last bucket holds everything over
927 * 2<<(NX842_HIST_SLOTS - 2) us */
928 bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n",
929 2<<(NX842_HIST_SLOTS - 2),
930 atomic64_read(&times[(NX842_HIST_SLOTS - 1)]));
931 p += bytes;
932
933 rcu_read_unlock();
934 return p - buf;
935}
936
937static struct attribute *nx842_sysfs_entries[] = {
938 &dev_attr_comp_complete.attr,
939 &dev_attr_comp_failed.attr,
940 &dev_attr_decomp_complete.attr,
941 &dev_attr_decomp_failed.attr,
942 &dev_attr_swdecomp.attr,
943 &dev_attr_comp_times.attr,
944 &dev_attr_decomp_times.attr,
945 NULL,
946};
947
948static struct attribute_group nx842_attribute_group = {
949 .name = NULL, /* put in device directory */
950 .attrs = nx842_sysfs_entries,
951};
952
Dan Streetman7011a122015-05-07 13:49:17 -0400953static struct nx842_driver nx842_pseries_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -0400954 .name = KBUILD_MODNAME,
Dan Streetman7011a122015-05-07 13:49:17 -0400955 .owner = THIS_MODULE,
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400956 .workmem_size = sizeof(struct nx842_workmem),
Dan Streetman959e6652015-05-07 13:49:18 -0400957 .constraints = &nx842_pseries_constraints,
Dan Streetman7011a122015-05-07 13:49:17 -0400958 .compress = nx842_pseries_compress,
959 .decompress = nx842_pseries_decompress,
960};
961
Dan Streetman03952d92015-07-22 14:26:38 -0400962static int nx842_pseries_crypto_init(struct crypto_tfm *tfm)
963{
964 return nx842_crypto_init(tfm, &nx842_pseries_driver);
965}
966
967static struct crypto_alg nx842_pseries_alg = {
968 .cra_name = "842",
969 .cra_driver_name = "842-nx",
970 .cra_priority = 300,
971 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
972 .cra_ctxsize = sizeof(struct nx842_crypto_ctx),
973 .cra_module = THIS_MODULE,
974 .cra_init = nx842_pseries_crypto_init,
975 .cra_exit = nx842_crypto_exit,
976 .cra_u = { .compress = {
977 .coa_compress = nx842_crypto_compress,
978 .coa_decompress = nx842_crypto_decompress } }
979};
980
Dan Streetman039af962015-07-22 14:26:31 -0400981static int nx842_probe(struct vio_dev *viodev,
982 const struct vio_device_id *id)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500983{
984 struct nx842_devdata *old_devdata, *new_devdata = NULL;
985 unsigned long flags;
986 int ret = 0;
987
Dan Streetman7f6e3aa2015-07-22 14:26:33 -0400988 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
989 if (!new_devdata)
990 return -ENOMEM;
991
992 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
993 GFP_NOFS);
994 if (!new_devdata->counters) {
995 kfree(new_devdata);
996 return -ENOMEM;
997 }
998
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500999 spin_lock_irqsave(&devdata_mutex, flags);
1000 old_devdata = rcu_dereference_check(devdata,
1001 lockdep_is_held(&devdata_mutex));
1002
1003 if (old_devdata && old_devdata->vdev != NULL) {
1004 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
1005 ret = -1;
1006 goto error_unlock;
1007 }
1008
1009 dev_set_drvdata(&viodev->dev, NULL);
1010
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001011 new_devdata->vdev = viodev;
1012 new_devdata->dev = &viodev->dev;
1013 nx842_OF_set_defaults(new_devdata);
1014
1015 rcu_assign_pointer(devdata, new_devdata);
1016 spin_unlock_irqrestore(&devdata_mutex, flags);
1017 synchronize_rcu();
1018 kfree(old_devdata);
1019
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001020 of_reconfig_notifier_register(&nx842_of_nb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001021
1022 ret = nx842_OF_upd(NULL);
Dan Streetmanee781b72015-07-22 14:26:34 -04001023 if (ret)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001024 goto error;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001025
Dan Streetman03952d92015-07-22 14:26:38 -04001026 ret = crypto_register_alg(&nx842_pseries_alg);
1027 if (ret) {
1028 dev_err(&viodev->dev, "could not register comp alg: %d\n", ret);
1029 goto error;
1030 }
1031
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001032 rcu_read_lock();
Jean Delvarecda43572014-05-28 14:02:24 +02001033 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001034 rcu_read_unlock();
1035
1036 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1037 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1038 ret = -1;
1039 goto error;
1040 }
1041
1042 return 0;
1043
1044error_unlock:
1045 spin_unlock_irqrestore(&devdata_mutex, flags);
1046 if (new_devdata)
1047 kfree(new_devdata->counters);
1048 kfree(new_devdata);
1049error:
1050 return ret;
1051}
1052
Dan Streetman039af962015-07-22 14:26:31 -04001053static int nx842_remove(struct vio_dev *viodev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001054{
1055 struct nx842_devdata *old_devdata;
1056 unsigned long flags;
1057
1058 pr_info("Removing IBM Power 842 compression device\n");
1059 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1060
Dan Streetman03952d92015-07-22 14:26:38 -04001061 crypto_unregister_alg(&nx842_pseries_alg);
1062
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001063 spin_lock_irqsave(&devdata_mutex, flags);
1064 old_devdata = rcu_dereference_check(devdata,
1065 lockdep_is_held(&devdata_mutex));
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001066 of_reconfig_notifier_unregister(&nx842_of_nb);
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301067 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001068 spin_unlock_irqrestore(&devdata_mutex, flags);
1069 synchronize_rcu();
1070 dev_set_drvdata(&viodev->dev, NULL);
1071 if (old_devdata)
1072 kfree(old_devdata->counters);
1073 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001074
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001075 return 0;
1076}
1077
Dan Streetmanb8e041872015-05-07 13:49:20 -04001078static struct vio_device_id nx842_vio_driver_ids[] = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001079 {"ibm,compression-v1", "ibm,compression"},
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001080 {"", ""},
1081};
1082
Dan Streetmanb8e041872015-05-07 13:49:20 -04001083static struct vio_driver nx842_vio_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001084 .name = KBUILD_MODNAME,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001085 .probe = nx842_probe,
Dan Streetman039af962015-07-22 14:26:31 -04001086 .remove = nx842_remove,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001087 .get_desired_dma = nx842_get_desired_dma,
Dan Streetmanb8e041872015-05-07 13:49:20 -04001088 .id_table = nx842_vio_driver_ids,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001089};
1090
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001091static int __init nx842_pseries_init(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001092{
1093 struct nx842_devdata *new_devdata;
Dan Streetman3e648cb2015-05-28 16:21:31 -04001094 int ret;
1095
Dan Streetman3e648cb2015-05-28 16:21:31 -04001096 if (!of_find_compatible_node(NULL, NULL, "ibm,compression"))
1097 return -ENODEV;
1098
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001099 RCU_INIT_POINTER(devdata, NULL);
1100 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1101 if (!new_devdata) {
1102 pr_err("Could not allocate memory for device data\n");
1103 return -ENOMEM;
1104 }
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001105 RCU_INIT_POINTER(devdata, new_devdata);
1106
Dan Streetman3e648cb2015-05-28 16:21:31 -04001107 ret = vio_register_driver(&nx842_vio_driver);
1108 if (ret) {
1109 pr_err("Could not register VIO driver %d\n", ret);
1110
1111 kfree(new_devdata);
1112 return ret;
1113 }
1114
Dan Streetman3e648cb2015-05-28 16:21:31 -04001115 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001116}
1117
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001118module_init(nx842_pseries_init);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001119
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001120static void __exit nx842_pseries_exit(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001121{
1122 struct nx842_devdata *old_devdata;
1123 unsigned long flags;
1124
Dan Streetman03952d92015-07-22 14:26:38 -04001125 crypto_unregister_alg(&nx842_pseries_alg);
1126
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001127 spin_lock_irqsave(&devdata_mutex, flags);
1128 old_devdata = rcu_dereference_check(devdata,
1129 lockdep_is_held(&devdata_mutex));
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301130 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001131 spin_unlock_irqrestore(&devdata_mutex, flags);
1132 synchronize_rcu();
Dan Streetmanb8e041872015-05-07 13:49:20 -04001133 if (old_devdata && old_devdata->dev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001134 dev_set_drvdata(old_devdata->dev, NULL);
1135 kfree(old_devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -04001136 vio_unregister_driver(&nx842_vio_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001137}
1138
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001139module_exit(nx842_pseries_exit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001140