blob: da52d8edefb37ef8104bc4fe49145c7349ae7ad9 [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");
32
Dan Streetman959e6652015-05-07 13:49:18 -040033static struct nx842_constraints nx842_pseries_constraints = {
Dan Streetman3154de72015-06-02 15:22:10 -040034 .alignment = DDE_BUFFER_ALIGN,
Dan Streetman959e6652015-05-07 13:49:18 -040035 .multiple = DDE_BUFFER_LAST_MULT,
Dan Streetman3154de72015-06-02 15:22:10 -040036 .minimum = DDE_BUFFER_LAST_MULT,
Dan Streetman959e6652015-05-07 13:49:18 -040037 .maximum = PAGE_SIZE, /* dynamic, max_sync_size */
38};
39
Dan Streetmanb8e041872015-05-07 13:49:20 -040040static int check_constraints(unsigned long buf, unsigned int *len, bool in)
Seth Jennings0e16aaf2012-07-19 09:42:40 -050041{
Dan Streetmanb8e041872015-05-07 13:49:20 -040042 if (!IS_ALIGNED(buf, nx842_pseries_constraints.alignment)) {
43 pr_debug("%s buffer 0x%lx not aligned to 0x%x\n",
44 in ? "input" : "output", buf,
45 nx842_pseries_constraints.alignment);
46 return -EINVAL;
47 }
48 if (*len % nx842_pseries_constraints.multiple) {
49 pr_debug("%s buffer len 0x%x not multiple of 0x%x\n",
50 in ? "input" : "output", *len,
51 nx842_pseries_constraints.multiple);
52 if (in)
53 return -EINVAL;
54 *len = round_down(*len, nx842_pseries_constraints.multiple);
55 }
56 if (*len < nx842_pseries_constraints.minimum) {
57 pr_debug("%s buffer len 0x%x under minimum 0x%x\n",
58 in ? "input" : "output", *len,
59 nx842_pseries_constraints.minimum);
60 return -EINVAL;
61 }
62 if (*len > nx842_pseries_constraints.maximum) {
63 pr_debug("%s buffer len 0x%x over maximum 0x%x\n",
64 in ? "input" : "output", *len,
65 nx842_pseries_constraints.maximum);
66 if (in)
67 return -EINVAL;
68 *len = nx842_pseries_constraints.maximum;
69 }
70 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -050071}
72
Dan Streetmanb8e041872015-05-07 13:49:20 -040073/* I assume we need to align the CSB? */
74#define WORKMEM_ALIGN (256)
75
76struct nx842_workmem {
77 /* scatterlist */
78 char slin[4096];
79 char slout[4096];
80 /* coprocessor status/parameter block */
81 struct nx_csbcpb csbcpb;
82
83 char padding[WORKMEM_ALIGN];
84} __aligned(WORKMEM_ALIGN);
85
Seth Jennings0e16aaf2012-07-19 09:42:40 -050086/* Macros for fields within nx_csbcpb */
87/* Check the valid bit within the csbcpb valid field */
88#define NX842_CSBCBP_VALID_CHK(x) (x & BIT_MASK(7))
89
90/* CE macros operate on the completion_extension field bits in the csbcpb.
91 * CE0 0=full completion, 1=partial completion
92 * CE1 0=CE0 indicates completion, 1=termination (output may be modified)
93 * CE2 0=processed_bytes is source bytes, 1=processed_bytes is target bytes */
94#define NX842_CSBCPB_CE0(x) (x & BIT_MASK(7))
95#define NX842_CSBCPB_CE1(x) (x & BIT_MASK(6))
96#define NX842_CSBCPB_CE2(x) (x & BIT_MASK(5))
97
98/* The NX unit accepts data only on 4K page boundaries */
Dan Streetmanb8e041872015-05-07 13:49:20 -040099#define NX842_HW_PAGE_SIZE (4096)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500100#define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1))
101
102enum nx842_status {
103 UNAVAILABLE,
104 AVAILABLE
105};
106
107struct ibm_nx842_counters {
108 atomic64_t comp_complete;
109 atomic64_t comp_failed;
110 atomic64_t decomp_complete;
111 atomic64_t decomp_failed;
112 atomic64_t swdecomp;
113 atomic64_t comp_times[32];
114 atomic64_t decomp_times[32];
115};
116
117static struct nx842_devdata {
118 struct vio_dev *vdev;
119 struct device *dev;
120 struct ibm_nx842_counters *counters;
121 unsigned int max_sg_len;
122 unsigned int max_sync_size;
123 unsigned int max_sync_sg;
124 enum nx842_status status;
125} __rcu *devdata;
126static DEFINE_SPINLOCK(devdata_mutex);
127
128#define NX842_COUNTER_INC(_x) \
129static inline void nx842_inc_##_x( \
130 const struct nx842_devdata *dev) { \
131 if (dev) \
132 atomic64_inc(&dev->counters->_x); \
133}
134NX842_COUNTER_INC(comp_complete);
135NX842_COUNTER_INC(comp_failed);
136NX842_COUNTER_INC(decomp_complete);
137NX842_COUNTER_INC(decomp_failed);
138NX842_COUNTER_INC(swdecomp);
139
140#define NX842_HIST_SLOTS 16
141
142static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
143{
144 int bucket = fls(time);
145
146 if (bucket)
147 bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
148
149 atomic64_inc(&times[bucket]);
150}
151
152/* NX unit operation flags */
153#define NX842_OP_COMPRESS 0x0
154#define NX842_OP_CRC 0x1
155#define NX842_OP_DECOMPRESS 0x2
156#define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC)
157#define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC)
158#define NX842_OP_ASYNC (1<<23)
159#define NX842_OP_NOTIFY (1<<22)
160#define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8)
161
162static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
163{
164 /* No use of DMA mappings within the driver. */
165 return 0;
166}
167
168struct nx842_slentry {
Michael Ellerman33b58b012012-08-03 12:23:15 +1000169 unsigned long ptr; /* Real address (use __pa()) */
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500170 unsigned long len;
171};
172
173/* pHyp scatterlist entry */
174struct nx842_scatterlist {
175 int entry_nr; /* number of slentries */
176 struct nx842_slentry *entries; /* ptr to array of slentries */
177};
178
179/* Does not include sizeof(entry_nr) in the size */
180static inline unsigned long nx842_get_scatterlist_size(
181 struct nx842_scatterlist *sl)
182{
183 return sl->entry_nr * sizeof(struct nx842_slentry);
184}
185
186static int nx842_build_scatterlist(unsigned long buf, int len,
187 struct nx842_scatterlist *sl)
188{
189 unsigned long nextpage;
190 struct nx842_slentry *entry;
191
192 sl->entry_nr = 0;
193
194 entry = sl->entries;
195 while (len) {
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600196 entry->ptr = nx842_get_pa((void *)buf);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500197 nextpage = ALIGN(buf + 1, NX842_HW_PAGE_SIZE);
198 if (nextpage < buf + len) {
199 /* we aren't at the end yet */
200 if (IS_ALIGNED(buf, NX842_HW_PAGE_SIZE))
201 /* we are in the middle (or beginning) */
202 entry->len = NX842_HW_PAGE_SIZE;
203 else
204 /* we are at the beginning */
205 entry->len = nextpage - buf;
206 } else {
207 /* at the end */
208 entry->len = len;
209 }
210
211 len -= entry->len;
212 buf += entry->len;
213 sl->entry_nr++;
214 entry++;
215 }
216
217 return 0;
218}
219
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500220static int nx842_validate_result(struct device *dev,
221 struct cop_status_block *csb)
222{
223 /* The csb must be valid after returning from vio_h_cop_sync */
224 if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
225 dev_err(dev, "%s: cspcbp not valid upon completion.\n",
226 __func__);
227 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
228 csb->valid,
229 csb->crb_seq_number,
230 csb->completion_code,
231 csb->completion_extension);
232 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
233 csb->processed_byte_count,
234 (unsigned long)csb->address);
235 return -EIO;
236 }
237
238 /* Check return values from the hardware in the CSB */
239 switch (csb->completion_code) {
240 case 0: /* Completed without error */
241 break;
242 case 64: /* Target bytes > Source bytes during compression */
243 case 13: /* Output buffer too small */
244 dev_dbg(dev, "%s: Compression output larger than input\n",
245 __func__);
246 return -ENOSPC;
247 case 66: /* Input data contains an illegal template field */
248 case 67: /* Template indicates data past the end of the input stream */
249 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
250 __func__, csb->completion_code);
251 return -EINVAL;
252 default:
253 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
254 __func__, csb->completion_code);
255 return -EIO;
256 }
257
258 /* Hardware sanity check */
259 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
260 dev_err(dev, "%s: No error returned by hardware, but "
261 "data returned is unusable, contact support.\n"
262 "(Additional info: csbcbp->processed bytes "
263 "does not specify processed bytes for the "
264 "target buffer.)\n", __func__);
265 return -EIO;
266 }
267
268 return 0;
269}
270
271/**
Dan Streetman7011a122015-05-07 13:49:17 -0400272 * nx842_pseries_compress - Compress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500273 *
274 * Compression provide by the NX842 coprocessor on IBM Power systems.
275 * The input buffer is compressed and the result is stored in the
276 * provided output buffer.
277 *
278 * Upon return from this function @outlen contains the length of the
279 * compressed data. If there is an error then @outlen will be 0 and an
280 * error will be specified by the return code from this function.
281 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400282 * @in: Pointer to input buffer
283 * @inlen: Length of input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500284 * @out: Pointer to output buffer
285 * @outlen: Length of output buffer
286 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400287 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500288 *
289 * Returns:
290 * 0 Success, output of length @outlen stored in the buffer at @out
291 * -ENOMEM Unable to allocate internal buffers
292 * -ENOSPC Output buffer is to small
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500293 * -EIO Internal error
294 * -ENODEV Hardware unavailable
295 */
Dan Streetman7011a122015-05-07 13:49:17 -0400296static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
297 unsigned char *out, unsigned int *outlen,
298 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500299{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500300 struct nx842_devdata *local_devdata;
301 struct device *dev = NULL;
302 struct nx842_workmem *workmem;
303 struct nx842_scatterlist slin, slout;
304 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400305 int ret = 0, max_sync_size;
306 unsigned long inbuf, outbuf;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500307 struct vio_pfo_op op = {
308 .done = NULL,
309 .handle = 0,
310 .timeout = 0,
311 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400312 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500313
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500314 inbuf = (unsigned long)in;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400315 if (check_constraints(inbuf, &inlen, true))
316 return -EINVAL;
317
318 outbuf = (unsigned long)out;
319 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500320 return -EINVAL;
321
322 rcu_read_lock();
323 local_devdata = rcu_dereference(devdata);
324 if (!local_devdata || !local_devdata->dev) {
325 rcu_read_unlock();
326 return -ENODEV;
327 }
328 max_sync_size = local_devdata->max_sync_size;
329 dev = local_devdata->dev;
330
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500331 /* Init scatterlist */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400332 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500333 slin.entries = (struct nx842_slentry *)workmem->slin;
334 slout.entries = (struct nx842_slentry *)workmem->slout;
335
336 /* Init operation */
337 op.flags = NX842_OP_COMPRESS;
338 csbcpb = &workmem->csbcpb;
339 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600340 op.csbcpb = nx842_get_pa(csbcpb);
341 op.out = nx842_get_pa(slout.entries);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500342
Dan Streetmanb8e041872015-05-07 13:49:20 -0400343 if ((inbuf & NX842_HW_PAGE_MASK) ==
344 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
345 /* Create direct DDE */
346 op.in = nx842_get_pa((void *)inbuf);
347 op.inlen = inlen;
348 } else {
349 /* Create indirect DDE (scatterlist) */
350 nx842_build_scatterlist(inbuf, inlen, &slin);
351 op.in = nx842_get_pa(slin.entries);
352 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500353 }
354
Dan Streetmanb8e041872015-05-07 13:49:20 -0400355 if ((outbuf & NX842_HW_PAGE_MASK) ==
356 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
357 /* Create direct DDE */
358 op.out = nx842_get_pa((void *)outbuf);
359 op.outlen = *outlen;
360 } else {
361 /* Create indirect DDE (scatterlist) */
362 nx842_build_scatterlist(outbuf, *outlen, &slout);
363 op.out = nx842_get_pa(slout.entries);
364 op.outlen = -nx842_get_scatterlist_size(&slout);
365 }
366
367 /* Send request to pHyp */
368 ret = vio_h_cop_sync(local_devdata->vdev, &op);
369
370 /* Check for pHyp error */
371 if (ret) {
372 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
373 __func__, ret, op.hcall_err);
374 ret = -EIO;
375 goto unlock;
376 }
377
378 /* Check for hardware error */
379 ret = nx842_validate_result(dev, &csbcpb->csb);
380 if (ret)
381 goto unlock;
382
383 *outlen = csbcpb->csb.processed_byte_count;
384 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500385
386unlock:
387 if (ret)
388 nx842_inc_comp_failed(local_devdata);
389 else {
390 nx842_inc_comp_complete(local_devdata);
391 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400392 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500393 }
394 rcu_read_unlock();
395 return ret;
396}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500397
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500398/**
Dan Streetman7011a122015-05-07 13:49:17 -0400399 * nx842_pseries_decompress - Decompress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500400 *
401 * Decompression provide by the NX842 coprocessor on IBM Power systems.
402 * The input buffer is decompressed and the result is stored in the
403 * provided output buffer. The size allocated to the output buffer is
404 * provided by the caller of this function in @outlen. Upon return from
405 * this function @outlen contains the length of the decompressed data.
406 * If there is an error then @outlen will be 0 and an error will be
407 * specified by the return code from this function.
408 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400409 * @in: Pointer to input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500410 * @inlen: Length of input buffer
Dan Streetmanb8e041872015-05-07 13:49:20 -0400411 * @out: Pointer to output buffer
412 * @outlen: Length of output buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500413 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400414 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500415 *
416 * Returns:
417 * 0 Success, output of length @outlen stored in the buffer at @out
418 * -ENODEV Hardware decompression device is unavailable
419 * -ENOMEM Unable to allocate internal buffers
420 * -ENOSPC Output buffer is to small
421 * -EINVAL Bad input data encountered when attempting decompress
422 * -EIO Internal error
423 */
Dan Streetman7011a122015-05-07 13:49:17 -0400424static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
425 unsigned char *out, unsigned int *outlen,
426 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500427{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500428 struct nx842_devdata *local_devdata;
429 struct device *dev = NULL;
430 struct nx842_workmem *workmem;
431 struct nx842_scatterlist slin, slout;
432 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400433 int ret = 0, max_sync_size;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500434 unsigned long inbuf, outbuf;
435 struct vio_pfo_op op = {
436 .done = NULL,
437 .handle = 0,
438 .timeout = 0,
439 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400440 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500441
442 /* Ensure page alignment and size */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400443 inbuf = (unsigned long)in;
444 if (check_constraints(inbuf, &inlen, true))
445 return -EINVAL;
446
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500447 outbuf = (unsigned long)out;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400448 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500449 return -EINVAL;
450
451 rcu_read_lock();
452 local_devdata = rcu_dereference(devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400453 if (!local_devdata || !local_devdata->dev) {
454 rcu_read_unlock();
455 return -ENODEV;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500456 }
Dan Streetmanb8e041872015-05-07 13:49:20 -0400457 max_sync_size = local_devdata->max_sync_size;
458 dev = local_devdata->dev;
459
460 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500461
462 /* Init scatterlist */
463 slin.entries = (struct nx842_slentry *)workmem->slin;
464 slout.entries = (struct nx842_slentry *)workmem->slout;
465
466 /* Init operation */
467 op.flags = NX842_OP_DECOMPRESS;
468 csbcpb = &workmem->csbcpb;
469 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600470 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500471
Dan Streetmanb8e041872015-05-07 13:49:20 -0400472 if ((inbuf & NX842_HW_PAGE_MASK) ==
473 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
474 /* Create direct DDE */
475 op.in = nx842_get_pa((void *)inbuf);
476 op.inlen = inlen;
477 } else {
478 /* Create indirect DDE (scatterlist) */
479 nx842_build_scatterlist(inbuf, inlen, &slin);
480 op.in = nx842_get_pa(slin.entries);
481 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500482 }
483
Dan Streetmanb8e041872015-05-07 13:49:20 -0400484 if ((outbuf & NX842_HW_PAGE_MASK) ==
485 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
486 /* Create direct DDE */
487 op.out = nx842_get_pa((void *)outbuf);
488 op.outlen = *outlen;
489 } else {
490 /* Create indirect DDE (scatterlist) */
491 nx842_build_scatterlist(outbuf, *outlen, &slout);
492 op.out = nx842_get_pa(slout.entries);
493 op.outlen = -nx842_get_scatterlist_size(&slout);
494 }
495
496 /* Send request to pHyp */
497 ret = vio_h_cop_sync(local_devdata->vdev, &op);
498
499 /* Check for pHyp error */
500 if (ret) {
501 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
502 __func__, ret, op.hcall_err);
503 goto unlock;
504 }
505
506 /* Check for hardware error */
507 ret = nx842_validate_result(dev, &csbcpb->csb);
508 if (ret)
509 goto unlock;
510
511 *outlen = csbcpb->csb.processed_byte_count;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500512
513unlock:
514 if (ret)
515 /* decompress fail */
516 nx842_inc_decomp_failed(local_devdata);
517 else {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500518 nx842_inc_decomp_complete(local_devdata);
519 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400520 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500521 }
522
523 rcu_read_unlock();
524 return ret;
525}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500526
527/**
528 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
529 *
530 * @devdata - struct nx842_devdata to update
531 *
532 * Returns:
533 * 0 on success
534 * -ENOENT if @devdata ptr is NULL
535 */
536static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
537{
538 if (devdata) {
539 devdata->max_sync_size = 0;
540 devdata->max_sync_sg = 0;
541 devdata->max_sg_len = 0;
542 devdata->status = UNAVAILABLE;
543 return 0;
544 } else
545 return -ENOENT;
546}
547
548/**
549 * nx842_OF_upd_status -- Update the device info from OF status prop
550 *
551 * The status property indicates if the accelerator is enabled. If the
552 * device is in the OF tree it indicates that the hardware is present.
553 * The status field indicates if the device is enabled when the status
554 * is 'okay'. Otherwise the device driver will be disabled.
555 *
556 * @devdata - struct nx842_devdata to update
557 * @prop - struct property point containing the maxsyncop for the update
558 *
559 * Returns:
560 * 0 - Device is available
561 * -EINVAL - Device is not available
562 */
563static int nx842_OF_upd_status(struct nx842_devdata *devdata,
564 struct property *prop) {
565 int ret = 0;
566 const char *status = (const char *)prop->value;
567
568 if (!strncmp(status, "okay", (size_t)prop->length)) {
569 devdata->status = AVAILABLE;
570 } else {
571 dev_info(devdata->dev, "%s: status '%s' is not 'okay'\n",
572 __func__, status);
573 devdata->status = UNAVAILABLE;
574 }
575
576 return ret;
577}
578
579/**
580 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
581 *
582 * Definition of the 'ibm,max-sg-len' OF property:
583 * This field indicates the maximum byte length of a scatter list
584 * for the platform facility. It is a single cell encoded as with encode-int.
585 *
586 * Example:
587 * # od -x ibm,max-sg-len
588 * 0000000 0000 0ff0
589 *
590 * In this example, the maximum byte length of a scatter list is
591 * 0x0ff0 (4,080).
592 *
593 * @devdata - struct nx842_devdata to update
594 * @prop - struct property point containing the maxsyncop for the update
595 *
596 * Returns:
597 * 0 on success
598 * -EINVAL on failure
599 */
600static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
601 struct property *prop) {
602 int ret = 0;
603 const int *maxsglen = prop->value;
604
605 if (prop->length != sizeof(*maxsglen)) {
606 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
607 dev_dbg(devdata->dev, "%s: ibm,max-sg-len is %d bytes long, expected %lu bytes\n", __func__,
608 prop->length, sizeof(*maxsglen));
609 ret = -EINVAL;
610 } else {
611 devdata->max_sg_len = (unsigned int)min(*maxsglen,
612 (int)NX842_HW_PAGE_SIZE);
613 }
614
615 return ret;
616}
617
618/**
619 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
620 *
621 * Definition of the 'ibm,max-sync-cop' OF property:
622 * Two series of cells. The first series of cells represents the maximums
623 * that can be synchronously compressed. The second series of cells
624 * represents the maximums that can be synchronously decompressed.
625 * 1. The first cell in each series contains the count of the number of
626 * data length, scatter list elements pairs that follow – each being
627 * of the form
628 * a. One cell data byte length
629 * b. One cell total number of scatter list elements
630 *
631 * Example:
632 * # od -x ibm,max-sync-cop
633 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
634 * 0000020 0000 1000 0000 01fe
635 *
636 * In this example, compression supports 0x1000 (4,096) data byte length
637 * and 0x1fe (510) total scatter list elements. Decompression supports
638 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
639 * elements.
640 *
641 * @devdata - struct nx842_devdata to update
642 * @prop - struct property point containing the maxsyncop for the update
643 *
644 * Returns:
645 * 0 on success
646 * -EINVAL on failure
647 */
648static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
649 struct property *prop) {
650 int ret = 0;
651 const struct maxsynccop_t {
652 int comp_elements;
653 int comp_data_limit;
654 int comp_sg_limit;
655 int decomp_elements;
656 int decomp_data_limit;
657 int decomp_sg_limit;
658 } *maxsynccop;
659
660 if (prop->length != sizeof(*maxsynccop)) {
661 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
662 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
663 sizeof(*maxsynccop));
664 ret = -EINVAL;
665 goto out;
666 }
667
668 maxsynccop = (const struct maxsynccop_t *)prop->value;
669
670 /* Use one limit rather than separate limits for compression and
671 * decompression. Set a maximum for this so as not to exceed the
672 * size that the header can support and round the value down to
673 * the hardware page size (4K) */
674 devdata->max_sync_size =
675 (unsigned int)min(maxsynccop->comp_data_limit,
676 maxsynccop->decomp_data_limit);
677
678 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400679 65536);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500680
Dan Streetmanb8e041872015-05-07 13:49:20 -0400681 if (devdata->max_sync_size < 4096) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500682 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
683 "less than the driver minimum, unable to use "
684 "the hardware device\n",
685 __func__, devdata->max_sync_size);
686 ret = -EINVAL;
687 goto out;
688 }
689
Dan Streetman959e6652015-05-07 13:49:18 -0400690 nx842_pseries_constraints.maximum = devdata->max_sync_size;
691
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500692 devdata->max_sync_sg = (unsigned int)min(maxsynccop->comp_sg_limit,
693 maxsynccop->decomp_sg_limit);
694 if (devdata->max_sync_sg < 1) {
695 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
696 "less than the driver minimum, unable to use "
697 "the hardware device\n",
698 __func__, devdata->max_sync_sg);
699 ret = -EINVAL;
700 goto out;
701 }
702
703out:
704 return ret;
705}
706
707/**
708 *
709 * nx842_OF_upd -- Handle OF properties updates for the device.
710 *
711 * Set all properties from the OF tree. Optionally, a new property
712 * can be provided by the @new_prop pointer to overwrite an existing value.
713 * The device will remain disabled until all values are valid, this function
714 * will return an error for updates unless all values are valid.
715 *
716 * @new_prop: If not NULL, this property is being updated. If NULL, update
717 * all properties from the current values in the OF tree.
718 *
719 * Returns:
720 * 0 - Success
721 * -ENOMEM - Could not allocate memory for new devdata structure
722 * -EINVAL - property value not found, new_prop is not a recognized
723 * property for the device or property value is not valid.
724 * -ENODEV - Device is not available
725 */
726static int nx842_OF_upd(struct property *new_prop)
727{
728 struct nx842_devdata *old_devdata = NULL;
729 struct nx842_devdata *new_devdata = NULL;
730 struct device_node *of_node = NULL;
731 struct property *status = NULL;
732 struct property *maxsglen = NULL;
733 struct property *maxsyncop = NULL;
734 int ret = 0;
735 unsigned long flags;
736
737 spin_lock_irqsave(&devdata_mutex, flags);
738 old_devdata = rcu_dereference_check(devdata,
739 lockdep_is_held(&devdata_mutex));
740 if (old_devdata)
741 of_node = old_devdata->dev->of_node;
742
743 if (!old_devdata || !of_node) {
744 pr_err("%s: device is not available\n", __func__);
745 spin_unlock_irqrestore(&devdata_mutex, flags);
746 return -ENODEV;
747 }
748
749 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
750 if (!new_devdata) {
751 dev_err(old_devdata->dev, "%s: Could not allocate memory for device data\n", __func__);
752 ret = -ENOMEM;
753 goto error_out;
754 }
755
756 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
757 new_devdata->counters = old_devdata->counters;
758
759 /* Set ptrs for existing properties */
760 status = of_find_property(of_node, "status", NULL);
761 maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
762 maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
763 if (!status || !maxsglen || !maxsyncop) {
764 dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
765 ret = -EINVAL;
766 goto error_out;
767 }
768
Grant Likely259092a2014-07-16 12:48:23 -0600769 /*
770 * If this is a property update, there are only certain properties that
771 * we care about. Bail if it isn't in the below list
772 */
773 if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
774 strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
775 strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
776 goto out;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500777
778 /* Perform property updates */
779 ret = nx842_OF_upd_status(new_devdata, status);
780 if (ret)
781 goto error_out;
782
783 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
784 if (ret)
785 goto error_out;
786
787 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
788 if (ret)
789 goto error_out;
790
791out:
792 dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
793 __func__, new_devdata->max_sync_size,
794 old_devdata->max_sync_size);
795 dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
796 __func__, new_devdata->max_sync_sg,
797 old_devdata->max_sync_sg);
798 dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
799 __func__, new_devdata->max_sg_len,
800 old_devdata->max_sg_len);
801
802 rcu_assign_pointer(devdata, new_devdata);
803 spin_unlock_irqrestore(&devdata_mutex, flags);
804 synchronize_rcu();
805 dev_set_drvdata(new_devdata->dev, new_devdata);
806 kfree(old_devdata);
807 return 0;
808
809error_out:
810 if (new_devdata) {
811 dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
812 nx842_OF_set_defaults(new_devdata);
813 rcu_assign_pointer(devdata, new_devdata);
814 spin_unlock_irqrestore(&devdata_mutex, flags);
815 synchronize_rcu();
816 dev_set_drvdata(new_devdata->dev, new_devdata);
817 kfree(old_devdata);
818 } else {
819 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
820 spin_unlock_irqrestore(&devdata_mutex, flags);
821 }
822
823 if (!ret)
824 ret = -EINVAL;
825 return ret;
826}
827
828/**
829 * nx842_OF_notifier - Process updates to OF properties for the device
830 *
831 * @np: notifier block
832 * @action: notifier action
833 * @update: struct pSeries_reconfig_prop_update pointer if action is
834 * PSERIES_UPDATE_PROPERTY
835 *
836 * Returns:
837 * NOTIFY_OK on success
838 * NOTIFY_BAD encoded with error number on failure, use
839 * notifier_to_errno() to decode this value
840 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000841static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
Grant Likelyf5242e52014-11-24 17:58:01 +0000842 void *data)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500843{
Grant Likelyf5242e52014-11-24 17:58:01 +0000844 struct of_reconfig_data *upd = data;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500845 struct nx842_devdata *local_devdata;
846 struct device_node *node = NULL;
847
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500848 rcu_read_lock();
849 local_devdata = rcu_dereference(devdata);
850 if (local_devdata)
851 node = local_devdata->dev->of_node;
852
853 if (local_devdata &&
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000854 action == OF_RECONFIG_UPDATE_PROPERTY &&
855 !strcmp(upd->dn->name, node->name)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500856 rcu_read_unlock();
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000857 nx842_OF_upd(upd->prop);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500858 } else
859 rcu_read_unlock();
860
861 return NOTIFY_OK;
862}
863
864static struct notifier_block nx842_of_nb = {
865 .notifier_call = nx842_OF_notifier,
866};
867
868#define nx842_counter_read(_name) \
869static ssize_t nx842_##_name##_show(struct device *dev, \
870 struct device_attribute *attr, \
871 char *buf) { \
872 struct nx842_devdata *local_devdata; \
873 int p = 0; \
874 rcu_read_lock(); \
875 local_devdata = rcu_dereference(devdata); \
876 if (local_devdata) \
877 p = snprintf(buf, PAGE_SIZE, "%ld\n", \
878 atomic64_read(&local_devdata->counters->_name)); \
879 rcu_read_unlock(); \
880 return p; \
881}
882
883#define NX842DEV_COUNTER_ATTR_RO(_name) \
884 nx842_counter_read(_name); \
885 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
886 0444, \
887 nx842_##_name##_show,\
888 NULL);
889
890NX842DEV_COUNTER_ATTR_RO(comp_complete);
891NX842DEV_COUNTER_ATTR_RO(comp_failed);
892NX842DEV_COUNTER_ATTR_RO(decomp_complete);
893NX842DEV_COUNTER_ATTR_RO(decomp_failed);
894NX842DEV_COUNTER_ATTR_RO(swdecomp);
895
896static ssize_t nx842_timehist_show(struct device *,
897 struct device_attribute *, char *);
898
899static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
900 nx842_timehist_show, NULL);
901static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
902 0444, nx842_timehist_show, NULL);
903
904static ssize_t nx842_timehist_show(struct device *dev,
905 struct device_attribute *attr, char *buf) {
906 char *p = buf;
907 struct nx842_devdata *local_devdata;
908 atomic64_t *times;
909 int bytes_remain = PAGE_SIZE;
910 int bytes;
911 int i;
912
913 rcu_read_lock();
914 local_devdata = rcu_dereference(devdata);
915 if (!local_devdata) {
916 rcu_read_unlock();
917 return 0;
918 }
919
920 if (attr == &dev_attr_comp_times)
921 times = local_devdata->counters->comp_times;
922 else if (attr == &dev_attr_decomp_times)
923 times = local_devdata->counters->decomp_times;
924 else {
925 rcu_read_unlock();
926 return 0;
927 }
928
929 for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
930 bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n",
931 i ? (2<<(i-1)) : 0, (2<<i)-1,
932 atomic64_read(&times[i]));
933 bytes_remain -= bytes;
934 p += bytes;
935 }
936 /* The last bucket holds everything over
937 * 2<<(NX842_HIST_SLOTS - 2) us */
938 bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n",
939 2<<(NX842_HIST_SLOTS - 2),
940 atomic64_read(&times[(NX842_HIST_SLOTS - 1)]));
941 p += bytes;
942
943 rcu_read_unlock();
944 return p - buf;
945}
946
947static struct attribute *nx842_sysfs_entries[] = {
948 &dev_attr_comp_complete.attr,
949 &dev_attr_comp_failed.attr,
950 &dev_attr_decomp_complete.attr,
951 &dev_attr_decomp_failed.attr,
952 &dev_attr_swdecomp.attr,
953 &dev_attr_comp_times.attr,
954 &dev_attr_decomp_times.attr,
955 NULL,
956};
957
958static struct attribute_group nx842_attribute_group = {
959 .name = NULL, /* put in device directory */
960 .attrs = nx842_sysfs_entries,
961};
962
Dan Streetman7011a122015-05-07 13:49:17 -0400963static struct nx842_driver nx842_pseries_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -0400964 .name = KBUILD_MODNAME,
Dan Streetman7011a122015-05-07 13:49:17 -0400965 .owner = THIS_MODULE,
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400966 .workmem_size = sizeof(struct nx842_workmem),
Dan Streetman959e6652015-05-07 13:49:18 -0400967 .constraints = &nx842_pseries_constraints,
Dan Streetman7011a122015-05-07 13:49:17 -0400968 .compress = nx842_pseries_compress,
969 .decompress = nx842_pseries_decompress,
970};
971
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500972static int __init nx842_probe(struct vio_dev *viodev,
973 const struct vio_device_id *id)
974{
975 struct nx842_devdata *old_devdata, *new_devdata = NULL;
976 unsigned long flags;
977 int ret = 0;
978
979 spin_lock_irqsave(&devdata_mutex, flags);
980 old_devdata = rcu_dereference_check(devdata,
981 lockdep_is_held(&devdata_mutex));
982
983 if (old_devdata && old_devdata->vdev != NULL) {
984 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
985 ret = -1;
986 goto error_unlock;
987 }
988
989 dev_set_drvdata(&viodev->dev, NULL);
990
991 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
992 if (!new_devdata) {
993 dev_err(&viodev->dev, "%s: Could not allocate memory for device data\n", __func__);
994 ret = -ENOMEM;
995 goto error_unlock;
996 }
997
998 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
999 GFP_NOFS);
1000 if (!new_devdata->counters) {
1001 dev_err(&viodev->dev, "%s: Could not allocate memory for performance counters\n", __func__);
1002 ret = -ENOMEM;
1003 goto error_unlock;
1004 }
1005
1006 new_devdata->vdev = viodev;
1007 new_devdata->dev = &viodev->dev;
1008 nx842_OF_set_defaults(new_devdata);
1009
1010 rcu_assign_pointer(devdata, new_devdata);
1011 spin_unlock_irqrestore(&devdata_mutex, flags);
1012 synchronize_rcu();
1013 kfree(old_devdata);
1014
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001015 of_reconfig_notifier_register(&nx842_of_nb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001016
1017 ret = nx842_OF_upd(NULL);
1018 if (ret && ret != -ENODEV) {
1019 dev_err(&viodev->dev, "could not parse device tree. %d\n", ret);
1020 ret = -1;
1021 goto error;
1022 }
1023
1024 rcu_read_lock();
Jean Delvarecda43572014-05-28 14:02:24 +02001025 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001026 rcu_read_unlock();
1027
1028 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1029 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1030 ret = -1;
1031 goto error;
1032 }
1033
1034 return 0;
1035
1036error_unlock:
1037 spin_unlock_irqrestore(&devdata_mutex, flags);
1038 if (new_devdata)
1039 kfree(new_devdata->counters);
1040 kfree(new_devdata);
1041error:
1042 return ret;
1043}
1044
1045static int __exit nx842_remove(struct vio_dev *viodev)
1046{
1047 struct nx842_devdata *old_devdata;
1048 unsigned long flags;
1049
1050 pr_info("Removing IBM Power 842 compression device\n");
1051 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1052
1053 spin_lock_irqsave(&devdata_mutex, flags);
1054 old_devdata = rcu_dereference_check(devdata,
1055 lockdep_is_held(&devdata_mutex));
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001056 of_reconfig_notifier_unregister(&nx842_of_nb);
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301057 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001058 spin_unlock_irqrestore(&devdata_mutex, flags);
1059 synchronize_rcu();
1060 dev_set_drvdata(&viodev->dev, NULL);
1061 if (old_devdata)
1062 kfree(old_devdata->counters);
1063 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001064
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001065 return 0;
1066}
1067
Dan Streetmanb8e041872015-05-07 13:49:20 -04001068static struct vio_device_id nx842_vio_driver_ids[] = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001069 {"ibm,compression-v1", "ibm,compression"},
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001070 {"", ""},
1071};
1072
Dan Streetmanb8e041872015-05-07 13:49:20 -04001073static struct vio_driver nx842_vio_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001074 .name = KBUILD_MODNAME,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001075 .probe = nx842_probe,
Jean Delvare13269ec2014-05-29 09:54:35 +02001076 .remove = __exit_p(nx842_remove),
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001077 .get_desired_dma = nx842_get_desired_dma,
Dan Streetmanb8e041872015-05-07 13:49:20 -04001078 .id_table = nx842_vio_driver_ids,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001079};
1080
1081static int __init nx842_init(void)
1082{
1083 struct nx842_devdata *new_devdata;
Dan Streetman3e648cb2015-05-28 16:21:31 -04001084 int ret;
1085
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001086 pr_info("Registering IBM Power 842 compression driver\n");
1087
Dan Streetman3e648cb2015-05-28 16:21:31 -04001088 if (!of_find_compatible_node(NULL, NULL, "ibm,compression"))
1089 return -ENODEV;
1090
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001091 RCU_INIT_POINTER(devdata, NULL);
1092 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1093 if (!new_devdata) {
1094 pr_err("Could not allocate memory for device data\n");
1095 return -ENOMEM;
1096 }
1097 new_devdata->status = UNAVAILABLE;
1098 RCU_INIT_POINTER(devdata, new_devdata);
1099
Dan Streetman3e648cb2015-05-28 16:21:31 -04001100 ret = vio_register_driver(&nx842_vio_driver);
1101 if (ret) {
1102 pr_err("Could not register VIO driver %d\n", ret);
1103
1104 kfree(new_devdata);
1105 return ret;
1106 }
1107
1108 if (!nx842_platform_driver_set(&nx842_pseries_driver)) {
1109 vio_unregister_driver(&nx842_vio_driver);
1110 kfree(new_devdata);
1111 return -EEXIST;
1112 }
1113
1114 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001115}
1116
1117module_init(nx842_init);
1118
1119static void __exit nx842_exit(void)
1120{
1121 struct nx842_devdata *old_devdata;
1122 unsigned long flags;
1123
1124 pr_info("Exiting IBM Power 842 compression driver\n");
Dan Streetman3e648cb2015-05-28 16:21:31 -04001125 nx842_platform_driver_unset(&nx842_pseries_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001126 spin_lock_irqsave(&devdata_mutex, flags);
1127 old_devdata = rcu_dereference_check(devdata,
1128 lockdep_is_held(&devdata_mutex));
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301129 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001130 spin_unlock_irqrestore(&devdata_mutex, flags);
1131 synchronize_rcu();
Dan Streetmanb8e041872015-05-07 13:49:20 -04001132 if (old_devdata && old_devdata->dev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001133 dev_set_drvdata(old_devdata->dev, NULL);
1134 kfree(old_devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -04001135 vio_unregister_driver(&nx842_vio_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001136}
1137
1138module_exit(nx842_exit);
1139