blob: e17f4d2e96e08810233638ad27e397223a7c592a [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 {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400169 __be64 ptr; /* Real address (use __pa()) */
170 __be64 len;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500171};
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{
Dan Streetmanc47d6302015-06-18 12:05:30 -0400189 unsigned long entrylen;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500190 struct nx842_slentry *entry;
191
192 sl->entry_nr = 0;
193
194 entry = sl->entries;
195 while (len) {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400196 entry->ptr = cpu_to_be64(nx842_get_pa((void *)buf));
197 entrylen = min_t(int, len,
198 LEN_ON_SIZE(buf, NX842_HW_PAGE_SIZE));
199 entry->len = cpu_to_be64(entrylen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500200
Dan Streetmanc47d6302015-06-18 12:05:30 -0400201 len -= entrylen;
202 buf += entrylen;
203
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500204 sl->entry_nr++;
205 entry++;
206 }
207
208 return 0;
209}
210
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500211static int nx842_validate_result(struct device *dev,
212 struct cop_status_block *csb)
213{
214 /* The csb must be valid after returning from vio_h_cop_sync */
215 if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
216 dev_err(dev, "%s: cspcbp not valid upon completion.\n",
217 __func__);
218 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
219 csb->valid,
220 csb->crb_seq_number,
221 csb->completion_code,
222 csb->completion_extension);
223 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
Dan Streetmanc47d6302015-06-18 12:05:30 -0400224 be32_to_cpu(csb->processed_byte_count),
225 (unsigned long)be64_to_cpu(csb->address));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500226 return -EIO;
227 }
228
229 /* Check return values from the hardware in the CSB */
230 switch (csb->completion_code) {
231 case 0: /* Completed without error */
232 break;
233 case 64: /* Target bytes > Source bytes during compression */
234 case 13: /* Output buffer too small */
235 dev_dbg(dev, "%s: Compression output larger than input\n",
236 __func__);
237 return -ENOSPC;
238 case 66: /* Input data contains an illegal template field */
239 case 67: /* Template indicates data past the end of the input stream */
240 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
241 __func__, csb->completion_code);
242 return -EINVAL;
243 default:
244 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
245 __func__, csb->completion_code);
246 return -EIO;
247 }
248
249 /* Hardware sanity check */
250 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
251 dev_err(dev, "%s: No error returned by hardware, but "
252 "data returned is unusable, contact support.\n"
253 "(Additional info: csbcbp->processed bytes "
254 "does not specify processed bytes for the "
255 "target buffer.)\n", __func__);
256 return -EIO;
257 }
258
259 return 0;
260}
261
262/**
Dan Streetman7011a122015-05-07 13:49:17 -0400263 * nx842_pseries_compress - Compress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500264 *
265 * Compression provide by the NX842 coprocessor on IBM Power systems.
266 * The input buffer is compressed and the result is stored in the
267 * provided output buffer.
268 *
269 * Upon return from this function @outlen contains the length of the
270 * compressed data. If there is an error then @outlen will be 0 and an
271 * error will be specified by the return code from this function.
272 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400273 * @in: Pointer to input buffer
274 * @inlen: Length of input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500275 * @out: Pointer to output buffer
276 * @outlen: Length of output buffer
277 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400278 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500279 *
280 * Returns:
281 * 0 Success, output of length @outlen stored in the buffer at @out
282 * -ENOMEM Unable to allocate internal buffers
283 * -ENOSPC Output buffer is to small
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500284 * -EIO Internal error
285 * -ENODEV Hardware unavailable
286 */
Dan Streetman7011a122015-05-07 13:49:17 -0400287static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
288 unsigned char *out, unsigned int *outlen,
289 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500290{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500291 struct nx842_devdata *local_devdata;
292 struct device *dev = NULL;
293 struct nx842_workmem *workmem;
294 struct nx842_scatterlist slin, slout;
295 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400296 int ret = 0, max_sync_size;
297 unsigned long inbuf, outbuf;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500298 struct vio_pfo_op op = {
299 .done = NULL,
300 .handle = 0,
301 .timeout = 0,
302 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400303 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500304
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500305 inbuf = (unsigned long)in;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400306 if (check_constraints(inbuf, &inlen, true))
307 return -EINVAL;
308
309 outbuf = (unsigned long)out;
310 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500311 return -EINVAL;
312
313 rcu_read_lock();
314 local_devdata = rcu_dereference(devdata);
315 if (!local_devdata || !local_devdata->dev) {
316 rcu_read_unlock();
317 return -ENODEV;
318 }
319 max_sync_size = local_devdata->max_sync_size;
320 dev = local_devdata->dev;
321
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500322 /* Init scatterlist */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400323 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500324 slin.entries = (struct nx842_slentry *)workmem->slin;
325 slout.entries = (struct nx842_slentry *)workmem->slout;
326
327 /* Init operation */
328 op.flags = NX842_OP_COMPRESS;
329 csbcpb = &workmem->csbcpb;
330 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600331 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500332
Dan Streetmanb8e041872015-05-07 13:49:20 -0400333 if ((inbuf & NX842_HW_PAGE_MASK) ==
334 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
335 /* Create direct DDE */
336 op.in = nx842_get_pa((void *)inbuf);
337 op.inlen = inlen;
338 } else {
339 /* Create indirect DDE (scatterlist) */
340 nx842_build_scatterlist(inbuf, inlen, &slin);
341 op.in = nx842_get_pa(slin.entries);
342 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500343 }
344
Dan Streetmanb8e041872015-05-07 13:49:20 -0400345 if ((outbuf & NX842_HW_PAGE_MASK) ==
346 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
347 /* Create direct DDE */
348 op.out = nx842_get_pa((void *)outbuf);
349 op.outlen = *outlen;
350 } else {
351 /* Create indirect DDE (scatterlist) */
352 nx842_build_scatterlist(outbuf, *outlen, &slout);
353 op.out = nx842_get_pa(slout.entries);
354 op.outlen = -nx842_get_scatterlist_size(&slout);
355 }
356
Dan Streetmanc47d6302015-06-18 12:05:30 -0400357 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
358 __func__, (unsigned long)op.in, (long)op.inlen,
359 (unsigned long)op.out, (long)op.outlen);
360
Dan Streetmanb8e041872015-05-07 13:49:20 -0400361 /* Send request to pHyp */
362 ret = vio_h_cop_sync(local_devdata->vdev, &op);
363
364 /* Check for pHyp error */
365 if (ret) {
366 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
367 __func__, ret, op.hcall_err);
368 ret = -EIO;
369 goto unlock;
370 }
371
372 /* Check for hardware error */
373 ret = nx842_validate_result(dev, &csbcpb->csb);
374 if (ret)
375 goto unlock;
376
Dan Streetmanc47d6302015-06-18 12:05:30 -0400377 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400378 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500379
380unlock:
381 if (ret)
382 nx842_inc_comp_failed(local_devdata);
383 else {
384 nx842_inc_comp_complete(local_devdata);
385 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400386 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500387 }
388 rcu_read_unlock();
389 return ret;
390}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500391
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500392/**
Dan Streetman7011a122015-05-07 13:49:17 -0400393 * nx842_pseries_decompress - Decompress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500394 *
395 * Decompression provide by the NX842 coprocessor on IBM Power systems.
396 * The input buffer is decompressed and the result is stored in the
397 * provided output buffer. The size allocated to the output buffer is
398 * provided by the caller of this function in @outlen. Upon return from
399 * this function @outlen contains the length of the decompressed data.
400 * If there is an error then @outlen will be 0 and an error will be
401 * specified by the return code from this function.
402 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400403 * @in: Pointer to input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500404 * @inlen: Length of input buffer
Dan Streetmanb8e041872015-05-07 13:49:20 -0400405 * @out: Pointer to output buffer
406 * @outlen: Length of output buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500407 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400408 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500409 *
410 * Returns:
411 * 0 Success, output of length @outlen stored in the buffer at @out
412 * -ENODEV Hardware decompression device is unavailable
413 * -ENOMEM Unable to allocate internal buffers
414 * -ENOSPC Output buffer is to small
415 * -EINVAL Bad input data encountered when attempting decompress
416 * -EIO Internal error
417 */
Dan Streetman7011a122015-05-07 13:49:17 -0400418static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
419 unsigned char *out, unsigned int *outlen,
420 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500421{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500422 struct nx842_devdata *local_devdata;
423 struct device *dev = NULL;
424 struct nx842_workmem *workmem;
425 struct nx842_scatterlist slin, slout;
426 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400427 int ret = 0, max_sync_size;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500428 unsigned long inbuf, outbuf;
429 struct vio_pfo_op op = {
430 .done = NULL,
431 .handle = 0,
432 .timeout = 0,
433 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400434 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500435
436 /* Ensure page alignment and size */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400437 inbuf = (unsigned long)in;
438 if (check_constraints(inbuf, &inlen, true))
439 return -EINVAL;
440
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500441 outbuf = (unsigned long)out;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400442 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500443 return -EINVAL;
444
445 rcu_read_lock();
446 local_devdata = rcu_dereference(devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400447 if (!local_devdata || !local_devdata->dev) {
448 rcu_read_unlock();
449 return -ENODEV;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500450 }
Dan Streetmanb8e041872015-05-07 13:49:20 -0400451 max_sync_size = local_devdata->max_sync_size;
452 dev = local_devdata->dev;
453
454 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500455
456 /* Init scatterlist */
457 slin.entries = (struct nx842_slentry *)workmem->slin;
458 slout.entries = (struct nx842_slentry *)workmem->slout;
459
460 /* Init operation */
461 op.flags = NX842_OP_DECOMPRESS;
462 csbcpb = &workmem->csbcpb;
463 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600464 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500465
Dan Streetmanb8e041872015-05-07 13:49:20 -0400466 if ((inbuf & NX842_HW_PAGE_MASK) ==
467 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
468 /* Create direct DDE */
469 op.in = nx842_get_pa((void *)inbuf);
470 op.inlen = inlen;
471 } else {
472 /* Create indirect DDE (scatterlist) */
473 nx842_build_scatterlist(inbuf, inlen, &slin);
474 op.in = nx842_get_pa(slin.entries);
475 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500476 }
477
Dan Streetmanb8e041872015-05-07 13:49:20 -0400478 if ((outbuf & NX842_HW_PAGE_MASK) ==
479 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
480 /* Create direct DDE */
481 op.out = nx842_get_pa((void *)outbuf);
482 op.outlen = *outlen;
483 } else {
484 /* Create indirect DDE (scatterlist) */
485 nx842_build_scatterlist(outbuf, *outlen, &slout);
486 op.out = nx842_get_pa(slout.entries);
487 op.outlen = -nx842_get_scatterlist_size(&slout);
488 }
489
Dan Streetmanc47d6302015-06-18 12:05:30 -0400490 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
491 __func__, (unsigned long)op.in, (long)op.inlen,
492 (unsigned long)op.out, (long)op.outlen);
493
Dan Streetmanb8e041872015-05-07 13:49:20 -0400494 /* Send request to pHyp */
495 ret = vio_h_cop_sync(local_devdata->vdev, &op);
496
497 /* Check for pHyp error */
498 if (ret) {
499 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
500 __func__, ret, op.hcall_err);
501 goto unlock;
502 }
503
504 /* Check for hardware error */
505 ret = nx842_validate_result(dev, &csbcpb->csb);
506 if (ret)
507 goto unlock;
508
Dan Streetmanc47d6302015-06-18 12:05:30 -0400509 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500510
511unlock:
512 if (ret)
513 /* decompress fail */
514 nx842_inc_decomp_failed(local_devdata);
515 else {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500516 nx842_inc_decomp_complete(local_devdata);
517 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400518 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500519 }
520
521 rcu_read_unlock();
522 return ret;
523}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500524
525/**
526 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
527 *
528 * @devdata - struct nx842_devdata to update
529 *
530 * Returns:
531 * 0 on success
532 * -ENOENT if @devdata ptr is NULL
533 */
534static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
535{
536 if (devdata) {
537 devdata->max_sync_size = 0;
538 devdata->max_sync_sg = 0;
539 devdata->max_sg_len = 0;
540 devdata->status = UNAVAILABLE;
541 return 0;
542 } else
543 return -ENOENT;
544}
545
546/**
547 * nx842_OF_upd_status -- Update the device info from OF status prop
548 *
549 * The status property indicates if the accelerator is enabled. If the
550 * device is in the OF tree it indicates that the hardware is present.
551 * The status field indicates if the device is enabled when the status
552 * is 'okay'. Otherwise the device driver will be disabled.
553 *
554 * @devdata - struct nx842_devdata to update
555 * @prop - struct property point containing the maxsyncop for the update
556 *
557 * Returns:
558 * 0 - Device is available
Nishanth Aravamudanfa9a9a02015-07-02 15:38:48 -0700559 * -ENODEV - Device is not available
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500560 */
561static int nx842_OF_upd_status(struct nx842_devdata *devdata,
562 struct property *prop) {
563 int ret = 0;
564 const char *status = (const char *)prop->value;
565
566 if (!strncmp(status, "okay", (size_t)prop->length)) {
567 devdata->status = AVAILABLE;
568 } else {
569 dev_info(devdata->dev, "%s: status '%s' is not 'okay'\n",
570 __func__, status);
571 devdata->status = UNAVAILABLE;
Nishanth Aravamudanfa9a9a02015-07-02 15:38:48 -0700572 ret = -ENODEV;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500573 }
574
575 return ret;
576}
577
578/**
579 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
580 *
581 * Definition of the 'ibm,max-sg-len' OF property:
582 * This field indicates the maximum byte length of a scatter list
583 * for the platform facility. It is a single cell encoded as with encode-int.
584 *
585 * Example:
586 * # od -x ibm,max-sg-len
587 * 0000000 0000 0ff0
588 *
589 * In this example, the maximum byte length of a scatter list is
590 * 0x0ff0 (4,080).
591 *
592 * @devdata - struct nx842_devdata to update
593 * @prop - struct property point containing the maxsyncop for the update
594 *
595 * Returns:
596 * 0 on success
597 * -EINVAL on failure
598 */
599static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
600 struct property *prop) {
601 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400602 const unsigned int maxsglen = of_read_number(prop->value, 1);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500603
Dan Streetmanc47d6302015-06-18 12:05:30 -0400604 if (prop->length != sizeof(maxsglen)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500605 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
606 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 -0400607 prop->length, sizeof(maxsglen));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500608 ret = -EINVAL;
609 } else {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400610 devdata->max_sg_len = min_t(unsigned int,
611 maxsglen, NX842_HW_PAGE_SIZE);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500612 }
613
614 return ret;
615}
616
617/**
618 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
619 *
620 * Definition of the 'ibm,max-sync-cop' OF property:
621 * Two series of cells. The first series of cells represents the maximums
622 * that can be synchronously compressed. The second series of cells
623 * represents the maximums that can be synchronously decompressed.
624 * 1. The first cell in each series contains the count of the number of
625 * data length, scatter list elements pairs that follow – each being
626 * of the form
627 * a. One cell data byte length
628 * b. One cell total number of scatter list elements
629 *
630 * Example:
631 * # od -x ibm,max-sync-cop
632 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
633 * 0000020 0000 1000 0000 01fe
634 *
635 * In this example, compression supports 0x1000 (4,096) data byte length
636 * and 0x1fe (510) total scatter list elements. Decompression supports
637 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
638 * elements.
639 *
640 * @devdata - struct nx842_devdata to update
641 * @prop - struct property point containing the maxsyncop for the update
642 *
643 * Returns:
644 * 0 on success
645 * -EINVAL on failure
646 */
647static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
648 struct property *prop) {
649 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400650 unsigned int comp_data_limit, decomp_data_limit;
651 unsigned int comp_sg_limit, decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500652 const struct maxsynccop_t {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400653 __be32 comp_elements;
654 __be32 comp_data_limit;
655 __be32 comp_sg_limit;
656 __be32 decomp_elements;
657 __be32 decomp_data_limit;
658 __be32 decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500659 } *maxsynccop;
660
661 if (prop->length != sizeof(*maxsynccop)) {
662 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
663 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
664 sizeof(*maxsynccop));
665 ret = -EINVAL;
666 goto out;
667 }
668
669 maxsynccop = (const struct maxsynccop_t *)prop->value;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400670 comp_data_limit = be32_to_cpu(maxsynccop->comp_data_limit);
671 comp_sg_limit = be32_to_cpu(maxsynccop->comp_sg_limit);
672 decomp_data_limit = be32_to_cpu(maxsynccop->decomp_data_limit);
673 decomp_sg_limit = be32_to_cpu(maxsynccop->decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500674
675 /* Use one limit rather than separate limits for compression and
676 * decompression. Set a maximum for this so as not to exceed the
677 * size that the header can support and round the value down to
678 * the hardware page size (4K) */
Dan Streetmanc47d6302015-06-18 12:05:30 -0400679 devdata->max_sync_size = min(comp_data_limit, decomp_data_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500680
681 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400682 65536);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500683
Dan Streetmanb8e041872015-05-07 13:49:20 -0400684 if (devdata->max_sync_size < 4096) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500685 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
686 "less than the driver minimum, unable to use "
687 "the hardware device\n",
688 __func__, devdata->max_sync_size);
689 ret = -EINVAL;
690 goto out;
691 }
692
Dan Streetman959e6652015-05-07 13:49:18 -0400693 nx842_pseries_constraints.maximum = devdata->max_sync_size;
694
Dan Streetmanc47d6302015-06-18 12:05:30 -0400695 devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500696 if (devdata->max_sync_sg < 1) {
697 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
698 "less than the driver minimum, unable to use "
699 "the hardware device\n",
700 __func__, devdata->max_sync_sg);
701 ret = -EINVAL;
702 goto out;
703 }
704
705out:
706 return ret;
707}
708
709/**
710 *
711 * nx842_OF_upd -- Handle OF properties updates for the device.
712 *
713 * Set all properties from the OF tree. Optionally, a new property
714 * can be provided by the @new_prop pointer to overwrite an existing value.
715 * The device will remain disabled until all values are valid, this function
716 * will return an error for updates unless all values are valid.
717 *
718 * @new_prop: If not NULL, this property is being updated. If NULL, update
719 * all properties from the current values in the OF tree.
720 *
721 * Returns:
722 * 0 - Success
723 * -ENOMEM - Could not allocate memory for new devdata structure
724 * -EINVAL - property value not found, new_prop is not a recognized
725 * property for the device or property value is not valid.
726 * -ENODEV - Device is not available
727 */
728static int nx842_OF_upd(struct property *new_prop)
729{
730 struct nx842_devdata *old_devdata = NULL;
731 struct nx842_devdata *new_devdata = NULL;
732 struct device_node *of_node = NULL;
733 struct property *status = NULL;
734 struct property *maxsglen = NULL;
735 struct property *maxsyncop = NULL;
736 int ret = 0;
737 unsigned long flags;
738
739 spin_lock_irqsave(&devdata_mutex, flags);
740 old_devdata = rcu_dereference_check(devdata,
741 lockdep_is_held(&devdata_mutex));
742 if (old_devdata)
743 of_node = old_devdata->dev->of_node;
744
745 if (!old_devdata || !of_node) {
746 pr_err("%s: device is not available\n", __func__);
747 spin_unlock_irqrestore(&devdata_mutex, flags);
748 return -ENODEV;
749 }
750
751 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
752 if (!new_devdata) {
753 dev_err(old_devdata->dev, "%s: Could not allocate memory for device data\n", __func__);
754 ret = -ENOMEM;
755 goto error_out;
756 }
757
758 memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
759 new_devdata->counters = old_devdata->counters;
760
761 /* Set ptrs for existing properties */
762 status = of_find_property(of_node, "status", NULL);
763 maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
764 maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
765 if (!status || !maxsglen || !maxsyncop) {
766 dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
767 ret = -EINVAL;
768 goto error_out;
769 }
770
Grant Likely259092a2014-07-16 12:48:23 -0600771 /*
772 * If this is a property update, there are only certain properties that
773 * we care about. Bail if it isn't in the below list
774 */
775 if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
776 strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
777 strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
778 goto out;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500779
780 /* Perform property updates */
781 ret = nx842_OF_upd_status(new_devdata, status);
782 if (ret)
783 goto error_out;
784
785 ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
786 if (ret)
787 goto error_out;
788
789 ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
790 if (ret)
791 goto error_out;
792
793out:
794 dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
795 __func__, new_devdata->max_sync_size,
796 old_devdata->max_sync_size);
797 dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
798 __func__, new_devdata->max_sync_sg,
799 old_devdata->max_sync_sg);
800 dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
801 __func__, new_devdata->max_sg_len,
802 old_devdata->max_sg_len);
803
804 rcu_assign_pointer(devdata, new_devdata);
805 spin_unlock_irqrestore(&devdata_mutex, flags);
806 synchronize_rcu();
807 dev_set_drvdata(new_devdata->dev, new_devdata);
808 kfree(old_devdata);
809 return 0;
810
811error_out:
812 if (new_devdata) {
813 dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
814 nx842_OF_set_defaults(new_devdata);
815 rcu_assign_pointer(devdata, new_devdata);
816 spin_unlock_irqrestore(&devdata_mutex, flags);
817 synchronize_rcu();
818 dev_set_drvdata(new_devdata->dev, new_devdata);
819 kfree(old_devdata);
820 } else {
821 dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
822 spin_unlock_irqrestore(&devdata_mutex, flags);
823 }
824
825 if (!ret)
826 ret = -EINVAL;
827 return ret;
828}
829
830/**
831 * nx842_OF_notifier - Process updates to OF properties for the device
832 *
833 * @np: notifier block
834 * @action: notifier action
835 * @update: struct pSeries_reconfig_prop_update pointer if action is
836 * PSERIES_UPDATE_PROPERTY
837 *
838 * Returns:
839 * NOTIFY_OK on success
840 * NOTIFY_BAD encoded with error number on failure, use
841 * notifier_to_errno() to decode this value
842 */
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000843static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
Grant Likelyf5242e52014-11-24 17:58:01 +0000844 void *data)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500845{
Grant Likelyf5242e52014-11-24 17:58:01 +0000846 struct of_reconfig_data *upd = data;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500847 struct nx842_devdata *local_devdata;
848 struct device_node *node = NULL;
849
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500850 rcu_read_lock();
851 local_devdata = rcu_dereference(devdata);
852 if (local_devdata)
853 node = local_devdata->dev->of_node;
854
855 if (local_devdata &&
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000856 action == OF_RECONFIG_UPDATE_PROPERTY &&
857 !strcmp(upd->dn->name, node->name)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500858 rcu_read_unlock();
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000859 nx842_OF_upd(upd->prop);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500860 } else
861 rcu_read_unlock();
862
863 return NOTIFY_OK;
864}
865
866static struct notifier_block nx842_of_nb = {
867 .notifier_call = nx842_OF_notifier,
868};
869
870#define nx842_counter_read(_name) \
871static ssize_t nx842_##_name##_show(struct device *dev, \
872 struct device_attribute *attr, \
873 char *buf) { \
874 struct nx842_devdata *local_devdata; \
875 int p = 0; \
876 rcu_read_lock(); \
877 local_devdata = rcu_dereference(devdata); \
878 if (local_devdata) \
879 p = snprintf(buf, PAGE_SIZE, "%ld\n", \
880 atomic64_read(&local_devdata->counters->_name)); \
881 rcu_read_unlock(); \
882 return p; \
883}
884
885#define NX842DEV_COUNTER_ATTR_RO(_name) \
886 nx842_counter_read(_name); \
887 static struct device_attribute dev_attr_##_name = __ATTR(_name, \
888 0444, \
889 nx842_##_name##_show,\
890 NULL);
891
892NX842DEV_COUNTER_ATTR_RO(comp_complete);
893NX842DEV_COUNTER_ATTR_RO(comp_failed);
894NX842DEV_COUNTER_ATTR_RO(decomp_complete);
895NX842DEV_COUNTER_ATTR_RO(decomp_failed);
896NX842DEV_COUNTER_ATTR_RO(swdecomp);
897
898static ssize_t nx842_timehist_show(struct device *,
899 struct device_attribute *, char *);
900
901static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
902 nx842_timehist_show, NULL);
903static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
904 0444, nx842_timehist_show, NULL);
905
906static ssize_t nx842_timehist_show(struct device *dev,
907 struct device_attribute *attr, char *buf) {
908 char *p = buf;
909 struct nx842_devdata *local_devdata;
910 atomic64_t *times;
911 int bytes_remain = PAGE_SIZE;
912 int bytes;
913 int i;
914
915 rcu_read_lock();
916 local_devdata = rcu_dereference(devdata);
917 if (!local_devdata) {
918 rcu_read_unlock();
919 return 0;
920 }
921
922 if (attr == &dev_attr_comp_times)
923 times = local_devdata->counters->comp_times;
924 else if (attr == &dev_attr_decomp_times)
925 times = local_devdata->counters->decomp_times;
926 else {
927 rcu_read_unlock();
928 return 0;
929 }
930
931 for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
932 bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n",
933 i ? (2<<(i-1)) : 0, (2<<i)-1,
934 atomic64_read(&times[i]));
935 bytes_remain -= bytes;
936 p += bytes;
937 }
938 /* The last bucket holds everything over
939 * 2<<(NX842_HIST_SLOTS - 2) us */
940 bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n",
941 2<<(NX842_HIST_SLOTS - 2),
942 atomic64_read(&times[(NX842_HIST_SLOTS - 1)]));
943 p += bytes;
944
945 rcu_read_unlock();
946 return p - buf;
947}
948
949static struct attribute *nx842_sysfs_entries[] = {
950 &dev_attr_comp_complete.attr,
951 &dev_attr_comp_failed.attr,
952 &dev_attr_decomp_complete.attr,
953 &dev_attr_decomp_failed.attr,
954 &dev_attr_swdecomp.attr,
955 &dev_attr_comp_times.attr,
956 &dev_attr_decomp_times.attr,
957 NULL,
958};
959
960static struct attribute_group nx842_attribute_group = {
961 .name = NULL, /* put in device directory */
962 .attrs = nx842_sysfs_entries,
963};
964
Dan Streetman7011a122015-05-07 13:49:17 -0400965static struct nx842_driver nx842_pseries_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -0400966 .name = KBUILD_MODNAME,
Dan Streetman7011a122015-05-07 13:49:17 -0400967 .owner = THIS_MODULE,
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400968 .workmem_size = sizeof(struct nx842_workmem),
Dan Streetman959e6652015-05-07 13:49:18 -0400969 .constraints = &nx842_pseries_constraints,
Dan Streetman7011a122015-05-07 13:49:17 -0400970 .compress = nx842_pseries_compress,
971 .decompress = nx842_pseries_decompress,
972};
973
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500974static int __init nx842_probe(struct vio_dev *viodev,
975 const struct vio_device_id *id)
976{
977 struct nx842_devdata *old_devdata, *new_devdata = NULL;
978 unsigned long flags;
979 int ret = 0;
980
981 spin_lock_irqsave(&devdata_mutex, flags);
982 old_devdata = rcu_dereference_check(devdata,
983 lockdep_is_held(&devdata_mutex));
984
985 if (old_devdata && old_devdata->vdev != NULL) {
986 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
987 ret = -1;
988 goto error_unlock;
989 }
990
991 dev_set_drvdata(&viodev->dev, NULL);
992
993 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
994 if (!new_devdata) {
995 dev_err(&viodev->dev, "%s: Could not allocate memory for device data\n", __func__);
996 ret = -ENOMEM;
997 goto error_unlock;
998 }
999
1000 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
1001 GFP_NOFS);
1002 if (!new_devdata->counters) {
1003 dev_err(&viodev->dev, "%s: Could not allocate memory for performance counters\n", __func__);
1004 ret = -ENOMEM;
1005 goto error_unlock;
1006 }
1007
1008 new_devdata->vdev = viodev;
1009 new_devdata->dev = &viodev->dev;
1010 nx842_OF_set_defaults(new_devdata);
1011
1012 rcu_assign_pointer(devdata, new_devdata);
1013 spin_unlock_irqrestore(&devdata_mutex, flags);
1014 synchronize_rcu();
1015 kfree(old_devdata);
1016
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001017 of_reconfig_notifier_register(&nx842_of_nb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001018
1019 ret = nx842_OF_upd(NULL);
1020 if (ret && ret != -ENODEV) {
1021 dev_err(&viodev->dev, "could not parse device tree. %d\n", ret);
1022 ret = -1;
1023 goto error;
1024 }
1025
1026 rcu_read_lock();
Jean Delvarecda43572014-05-28 14:02:24 +02001027 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001028 rcu_read_unlock();
1029
1030 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1031 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1032 ret = -1;
1033 goto error;
1034 }
1035
1036 return 0;
1037
1038error_unlock:
1039 spin_unlock_irqrestore(&devdata_mutex, flags);
1040 if (new_devdata)
1041 kfree(new_devdata->counters);
1042 kfree(new_devdata);
1043error:
1044 return ret;
1045}
1046
1047static int __exit nx842_remove(struct vio_dev *viodev)
1048{
1049 struct nx842_devdata *old_devdata;
1050 unsigned long flags;
1051
1052 pr_info("Removing IBM Power 842 compression device\n");
1053 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1054
1055 spin_lock_irqsave(&devdata_mutex, flags);
1056 old_devdata = rcu_dereference_check(devdata,
1057 lockdep_is_held(&devdata_mutex));
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001058 of_reconfig_notifier_unregister(&nx842_of_nb);
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301059 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001060 spin_unlock_irqrestore(&devdata_mutex, flags);
1061 synchronize_rcu();
1062 dev_set_drvdata(&viodev->dev, NULL);
1063 if (old_devdata)
1064 kfree(old_devdata->counters);
1065 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001066
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001067 return 0;
1068}
1069
Dan Streetmanb8e041872015-05-07 13:49:20 -04001070static struct vio_device_id nx842_vio_driver_ids[] = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001071 {"ibm,compression-v1", "ibm,compression"},
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001072 {"", ""},
1073};
1074
Dan Streetmanb8e041872015-05-07 13:49:20 -04001075static struct vio_driver nx842_vio_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001076 .name = KBUILD_MODNAME,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001077 .probe = nx842_probe,
Jean Delvare13269ec2014-05-29 09:54:35 +02001078 .remove = __exit_p(nx842_remove),
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001079 .get_desired_dma = nx842_get_desired_dma,
Dan Streetmanb8e041872015-05-07 13:49:20 -04001080 .id_table = nx842_vio_driver_ids,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001081};
1082
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001083static int __init nx842_pseries_init(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001084{
1085 struct nx842_devdata *new_devdata;
Dan Streetman3e648cb2015-05-28 16:21:31 -04001086 int ret;
1087
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001088 pr_info("Registering IBM Power 842 compression driver\n");
1089
Dan Streetman3e648cb2015-05-28 16:21:31 -04001090 if (!of_find_compatible_node(NULL, NULL, "ibm,compression"))
1091 return -ENODEV;
1092
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001093 RCU_INIT_POINTER(devdata, NULL);
1094 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1095 if (!new_devdata) {
1096 pr_err("Could not allocate memory for device data\n");
1097 return -ENOMEM;
1098 }
1099 new_devdata->status = UNAVAILABLE;
1100 RCU_INIT_POINTER(devdata, new_devdata);
1101
Dan Streetman3e648cb2015-05-28 16:21:31 -04001102 ret = vio_register_driver(&nx842_vio_driver);
1103 if (ret) {
1104 pr_err("Could not register VIO driver %d\n", ret);
1105
1106 kfree(new_devdata);
1107 return ret;
1108 }
1109
1110 if (!nx842_platform_driver_set(&nx842_pseries_driver)) {
1111 vio_unregister_driver(&nx842_vio_driver);
1112 kfree(new_devdata);
1113 return -EEXIST;
1114 }
1115
1116 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001117}
1118
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001119module_init(nx842_pseries_init);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001120
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001121static void __exit nx842_pseries_exit(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001122{
1123 struct nx842_devdata *old_devdata;
1124 unsigned long flags;
1125
1126 pr_info("Exiting IBM Power 842 compression driver\n");
Dan Streetman3e648cb2015-05-28 16:21:31 -04001127 nx842_platform_driver_unset(&nx842_pseries_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001128 spin_lock_irqsave(&devdata_mutex, flags);
1129 old_devdata = rcu_dereference_check(devdata,
1130 lockdep_is_held(&devdata_mutex));
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301131 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001132 spin_unlock_irqrestore(&devdata_mutex, flags);
1133 synchronize_rcu();
Dan Streetmanb8e041872015-05-07 13:49:20 -04001134 if (old_devdata && old_devdata->dev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001135 dev_set_drvdata(old_devdata->dev, NULL);
1136 kfree(old_devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -04001137 vio_unregister_driver(&nx842_vio_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001138}
1139
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001140module_exit(nx842_pseries_exit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001141