blob: b3758779dc3ed1449c38371966bd8630fb25ca25 [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
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500102struct ibm_nx842_counters {
103 atomic64_t comp_complete;
104 atomic64_t comp_failed;
105 atomic64_t decomp_complete;
106 atomic64_t decomp_failed;
107 atomic64_t swdecomp;
108 atomic64_t comp_times[32];
109 atomic64_t decomp_times[32];
110};
111
112static struct nx842_devdata {
113 struct vio_dev *vdev;
114 struct device *dev;
115 struct ibm_nx842_counters *counters;
116 unsigned int max_sg_len;
117 unsigned int max_sync_size;
118 unsigned int max_sync_sg;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500119} __rcu *devdata;
120static DEFINE_SPINLOCK(devdata_mutex);
121
122#define NX842_COUNTER_INC(_x) \
123static inline void nx842_inc_##_x( \
124 const struct nx842_devdata *dev) { \
125 if (dev) \
126 atomic64_inc(&dev->counters->_x); \
127}
128NX842_COUNTER_INC(comp_complete);
129NX842_COUNTER_INC(comp_failed);
130NX842_COUNTER_INC(decomp_complete);
131NX842_COUNTER_INC(decomp_failed);
132NX842_COUNTER_INC(swdecomp);
133
134#define NX842_HIST_SLOTS 16
135
136static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
137{
138 int bucket = fls(time);
139
140 if (bucket)
141 bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
142
143 atomic64_inc(&times[bucket]);
144}
145
146/* NX unit operation flags */
147#define NX842_OP_COMPRESS 0x0
148#define NX842_OP_CRC 0x1
149#define NX842_OP_DECOMPRESS 0x2
150#define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC)
151#define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC)
152#define NX842_OP_ASYNC (1<<23)
153#define NX842_OP_NOTIFY (1<<22)
154#define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8)
155
156static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
157{
158 /* No use of DMA mappings within the driver. */
159 return 0;
160}
161
162struct nx842_slentry {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400163 __be64 ptr; /* Real address (use __pa()) */
164 __be64 len;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500165};
166
167/* pHyp scatterlist entry */
168struct nx842_scatterlist {
169 int entry_nr; /* number of slentries */
170 struct nx842_slentry *entries; /* ptr to array of slentries */
171};
172
173/* Does not include sizeof(entry_nr) in the size */
174static inline unsigned long nx842_get_scatterlist_size(
175 struct nx842_scatterlist *sl)
176{
177 return sl->entry_nr * sizeof(struct nx842_slentry);
178}
179
180static int nx842_build_scatterlist(unsigned long buf, int len,
181 struct nx842_scatterlist *sl)
182{
Dan Streetmanc47d6302015-06-18 12:05:30 -0400183 unsigned long entrylen;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500184 struct nx842_slentry *entry;
185
186 sl->entry_nr = 0;
187
188 entry = sl->entries;
189 while (len) {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400190 entry->ptr = cpu_to_be64(nx842_get_pa((void *)buf));
191 entrylen = min_t(int, len,
192 LEN_ON_SIZE(buf, NX842_HW_PAGE_SIZE));
193 entry->len = cpu_to_be64(entrylen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500194
Dan Streetmanc47d6302015-06-18 12:05:30 -0400195 len -= entrylen;
196 buf += entrylen;
197
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500198 sl->entry_nr++;
199 entry++;
200 }
201
202 return 0;
203}
204
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500205static int nx842_validate_result(struct device *dev,
206 struct cop_status_block *csb)
207{
208 /* The csb must be valid after returning from vio_h_cop_sync */
209 if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
210 dev_err(dev, "%s: cspcbp not valid upon completion.\n",
211 __func__);
212 dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
213 csb->valid,
214 csb->crb_seq_number,
215 csb->completion_code,
216 csb->completion_extension);
217 dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
Dan Streetmanc47d6302015-06-18 12:05:30 -0400218 be32_to_cpu(csb->processed_byte_count),
219 (unsigned long)be64_to_cpu(csb->address));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500220 return -EIO;
221 }
222
223 /* Check return values from the hardware in the CSB */
224 switch (csb->completion_code) {
225 case 0: /* Completed without error */
226 break;
227 case 64: /* Target bytes > Source bytes during compression */
228 case 13: /* Output buffer too small */
229 dev_dbg(dev, "%s: Compression output larger than input\n",
230 __func__);
231 return -ENOSPC;
232 case 66: /* Input data contains an illegal template field */
233 case 67: /* Template indicates data past the end of the input stream */
234 dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
235 __func__, csb->completion_code);
236 return -EINVAL;
237 default:
238 dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
239 __func__, csb->completion_code);
240 return -EIO;
241 }
242
243 /* Hardware sanity check */
244 if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
245 dev_err(dev, "%s: No error returned by hardware, but "
246 "data returned is unusable, contact support.\n"
247 "(Additional info: csbcbp->processed bytes "
248 "does not specify processed bytes for the "
249 "target buffer.)\n", __func__);
250 return -EIO;
251 }
252
253 return 0;
254}
255
256/**
Dan Streetman7011a122015-05-07 13:49:17 -0400257 * nx842_pseries_compress - Compress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500258 *
259 * Compression provide by the NX842 coprocessor on IBM Power systems.
260 * The input buffer is compressed and the result is stored in the
261 * provided output buffer.
262 *
263 * Upon return from this function @outlen contains the length of the
264 * compressed data. If there is an error then @outlen will be 0 and an
265 * error will be specified by the return code from this function.
266 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400267 * @in: Pointer to input buffer
268 * @inlen: Length of input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500269 * @out: Pointer to output buffer
270 * @outlen: Length of output buffer
271 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400272 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500273 *
274 * Returns:
275 * 0 Success, output of length @outlen stored in the buffer at @out
276 * -ENOMEM Unable to allocate internal buffers
277 * -ENOSPC Output buffer is to small
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500278 * -EIO Internal error
279 * -ENODEV Hardware unavailable
280 */
Dan Streetman7011a122015-05-07 13:49:17 -0400281static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
282 unsigned char *out, unsigned int *outlen,
283 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500284{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500285 struct nx842_devdata *local_devdata;
286 struct device *dev = NULL;
287 struct nx842_workmem *workmem;
288 struct nx842_scatterlist slin, slout;
289 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400290 int ret = 0, max_sync_size;
291 unsigned long inbuf, outbuf;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500292 struct vio_pfo_op op = {
293 .done = NULL,
294 .handle = 0,
295 .timeout = 0,
296 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400297 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500298
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500299 inbuf = (unsigned long)in;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400300 if (check_constraints(inbuf, &inlen, true))
301 return -EINVAL;
302
303 outbuf = (unsigned long)out;
304 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500305 return -EINVAL;
306
307 rcu_read_lock();
308 local_devdata = rcu_dereference(devdata);
309 if (!local_devdata || !local_devdata->dev) {
310 rcu_read_unlock();
311 return -ENODEV;
312 }
313 max_sync_size = local_devdata->max_sync_size;
314 dev = local_devdata->dev;
315
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500316 /* Init scatterlist */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400317 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500318 slin.entries = (struct nx842_slentry *)workmem->slin;
319 slout.entries = (struct nx842_slentry *)workmem->slout;
320
321 /* Init operation */
322 op.flags = NX842_OP_COMPRESS;
323 csbcpb = &workmem->csbcpb;
324 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600325 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500326
Dan Streetmanb8e041872015-05-07 13:49:20 -0400327 if ((inbuf & NX842_HW_PAGE_MASK) ==
328 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
329 /* Create direct DDE */
330 op.in = nx842_get_pa((void *)inbuf);
331 op.inlen = inlen;
332 } else {
333 /* Create indirect DDE (scatterlist) */
334 nx842_build_scatterlist(inbuf, inlen, &slin);
335 op.in = nx842_get_pa(slin.entries);
336 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500337 }
338
Dan Streetmanb8e041872015-05-07 13:49:20 -0400339 if ((outbuf & NX842_HW_PAGE_MASK) ==
340 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
341 /* Create direct DDE */
342 op.out = nx842_get_pa((void *)outbuf);
343 op.outlen = *outlen;
344 } else {
345 /* Create indirect DDE (scatterlist) */
346 nx842_build_scatterlist(outbuf, *outlen, &slout);
347 op.out = nx842_get_pa(slout.entries);
348 op.outlen = -nx842_get_scatterlist_size(&slout);
349 }
350
Dan Streetmanc47d6302015-06-18 12:05:30 -0400351 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
352 __func__, (unsigned long)op.in, (long)op.inlen,
353 (unsigned long)op.out, (long)op.outlen);
354
Dan Streetmanb8e041872015-05-07 13:49:20 -0400355 /* Send request to pHyp */
356 ret = vio_h_cop_sync(local_devdata->vdev, &op);
357
358 /* Check for pHyp error */
359 if (ret) {
360 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
361 __func__, ret, op.hcall_err);
362 ret = -EIO;
363 goto unlock;
364 }
365
366 /* Check for hardware error */
367 ret = nx842_validate_result(dev, &csbcpb->csb);
368 if (ret)
369 goto unlock;
370
Dan Streetmanc47d6302015-06-18 12:05:30 -0400371 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400372 dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500373
374unlock:
375 if (ret)
376 nx842_inc_comp_failed(local_devdata);
377 else {
378 nx842_inc_comp_complete(local_devdata);
379 ibm_nx842_incr_hist(local_devdata->counters->comp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400380 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500381 }
382 rcu_read_unlock();
383 return ret;
384}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500385
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500386/**
Dan Streetman7011a122015-05-07 13:49:17 -0400387 * nx842_pseries_decompress - Decompress data using the 842 algorithm
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500388 *
389 * Decompression provide by the NX842 coprocessor on IBM Power systems.
390 * The input buffer is decompressed and the result is stored in the
391 * provided output buffer. The size allocated to the output buffer is
392 * provided by the caller of this function in @outlen. Upon return from
393 * this function @outlen contains the length of the decompressed data.
394 * If there is an error then @outlen will be 0 and an error will be
395 * specified by the return code from this function.
396 *
Dan Streetmanb8e041872015-05-07 13:49:20 -0400397 * @in: Pointer to input buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500398 * @inlen: Length of input buffer
Dan Streetmanb8e041872015-05-07 13:49:20 -0400399 * @out: Pointer to output buffer
400 * @outlen: Length of output buffer
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500401 * @wrkmem: ptr to buffer for working memory, size determined by
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400402 * nx842_pseries_driver.workmem_size
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500403 *
404 * Returns:
405 * 0 Success, output of length @outlen stored in the buffer at @out
406 * -ENODEV Hardware decompression device is unavailable
407 * -ENOMEM Unable to allocate internal buffers
408 * -ENOSPC Output buffer is to small
409 * -EINVAL Bad input data encountered when attempting decompress
410 * -EIO Internal error
411 */
Dan Streetman7011a122015-05-07 13:49:17 -0400412static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
413 unsigned char *out, unsigned int *outlen,
414 void *wmem)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500415{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500416 struct nx842_devdata *local_devdata;
417 struct device *dev = NULL;
418 struct nx842_workmem *workmem;
419 struct nx842_scatterlist slin, slout;
420 struct nx_csbcpb *csbcpb;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400421 int ret = 0, max_sync_size;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500422 unsigned long inbuf, outbuf;
423 struct vio_pfo_op op = {
424 .done = NULL,
425 .handle = 0,
426 .timeout = 0,
427 };
Dan Streetmanb8e041872015-05-07 13:49:20 -0400428 unsigned long start = get_tb();
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500429
430 /* Ensure page alignment and size */
Dan Streetmanb8e041872015-05-07 13:49:20 -0400431 inbuf = (unsigned long)in;
432 if (check_constraints(inbuf, &inlen, true))
433 return -EINVAL;
434
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500435 outbuf = (unsigned long)out;
Dan Streetmanb8e041872015-05-07 13:49:20 -0400436 if (check_constraints(outbuf, outlen, false))
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500437 return -EINVAL;
438
439 rcu_read_lock();
440 local_devdata = rcu_dereference(devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -0400441 if (!local_devdata || !local_devdata->dev) {
442 rcu_read_unlock();
443 return -ENODEV;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500444 }
Dan Streetmanb8e041872015-05-07 13:49:20 -0400445 max_sync_size = local_devdata->max_sync_size;
446 dev = local_devdata->dev;
447
448 workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500449
450 /* Init scatterlist */
451 slin.entries = (struct nx842_slentry *)workmem->slin;
452 slout.entries = (struct nx842_slentry *)workmem->slout;
453
454 /* Init operation */
455 op.flags = NX842_OP_DECOMPRESS;
456 csbcpb = &workmem->csbcpb;
457 memset(csbcpb, 0, sizeof(*csbcpb));
Nathan Fontenot0ba3e102014-01-29 10:34:56 -0600458 op.csbcpb = nx842_get_pa(csbcpb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500459
Dan Streetmanb8e041872015-05-07 13:49:20 -0400460 if ((inbuf & NX842_HW_PAGE_MASK) ==
461 ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) {
462 /* Create direct DDE */
463 op.in = nx842_get_pa((void *)inbuf);
464 op.inlen = inlen;
465 } else {
466 /* Create indirect DDE (scatterlist) */
467 nx842_build_scatterlist(inbuf, inlen, &slin);
468 op.in = nx842_get_pa(slin.entries);
469 op.inlen = -nx842_get_scatterlist_size(&slin);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500470 }
471
Dan Streetmanb8e041872015-05-07 13:49:20 -0400472 if ((outbuf & NX842_HW_PAGE_MASK) ==
473 ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) {
474 /* Create direct DDE */
475 op.out = nx842_get_pa((void *)outbuf);
476 op.outlen = *outlen;
477 } else {
478 /* Create indirect DDE (scatterlist) */
479 nx842_build_scatterlist(outbuf, *outlen, &slout);
480 op.out = nx842_get_pa(slout.entries);
481 op.outlen = -nx842_get_scatterlist_size(&slout);
482 }
483
Dan Streetmanc47d6302015-06-18 12:05:30 -0400484 dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n",
485 __func__, (unsigned long)op.in, (long)op.inlen,
486 (unsigned long)op.out, (long)op.outlen);
487
Dan Streetmanb8e041872015-05-07 13:49:20 -0400488 /* Send request to pHyp */
489 ret = vio_h_cop_sync(local_devdata->vdev, &op);
490
491 /* Check for pHyp error */
492 if (ret) {
493 dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
494 __func__, ret, op.hcall_err);
495 goto unlock;
496 }
497
498 /* Check for hardware error */
499 ret = nx842_validate_result(dev, &csbcpb->csb);
500 if (ret)
501 goto unlock;
502
Dan Streetmanc47d6302015-06-18 12:05:30 -0400503 *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500504
505unlock:
506 if (ret)
507 /* decompress fail */
508 nx842_inc_decomp_failed(local_devdata);
509 else {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500510 nx842_inc_decomp_complete(local_devdata);
511 ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400512 (get_tb() - start) / tb_ticks_per_usec);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500513 }
514
515 rcu_read_unlock();
516 return ret;
517}
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500518
519/**
520 * nx842_OF_set_defaults -- Set default (disabled) values for devdata
521 *
522 * @devdata - struct nx842_devdata to update
523 *
524 * Returns:
525 * 0 on success
526 * -ENOENT if @devdata ptr is NULL
527 */
528static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
529{
530 if (devdata) {
531 devdata->max_sync_size = 0;
532 devdata->max_sync_sg = 0;
533 devdata->max_sg_len = 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500534 return 0;
535 } else
536 return -ENOENT;
537}
538
539/**
Dan Streetman90fd73f2015-07-22 14:26:32 -0400540 * nx842_OF_upd_status -- Check the device info from OF status prop
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500541 *
542 * The status property indicates if the accelerator is enabled. If the
543 * device is in the OF tree it indicates that the hardware is present.
544 * The status field indicates if the device is enabled when the status
545 * is 'okay'. Otherwise the device driver will be disabled.
546 *
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500547 * @prop - struct property point containing the maxsyncop for the update
548 *
549 * Returns:
550 * 0 - Device is available
Nishanth Aravamudanfa9a9a02015-07-02 15:38:48 -0700551 * -ENODEV - Device is not available
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500552 */
Dan Streetman90fd73f2015-07-22 14:26:32 -0400553static int nx842_OF_upd_status(struct property *prop)
554{
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500555 const char *status = (const char *)prop->value;
556
Dan Streetman90fd73f2015-07-22 14:26:32 -0400557 if (!strncmp(status, "okay", (size_t)prop->length))
558 return 0;
559 if (!strncmp(status, "disabled", (size_t)prop->length))
560 return -ENODEV;
561 dev_info(devdata->dev, "%s: unknown status '%s'\n", __func__, status);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500562
Dan Streetman90fd73f2015-07-22 14:26:32 -0400563 return -EINVAL;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500564}
565
566/**
567 * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
568 *
569 * Definition of the 'ibm,max-sg-len' OF property:
570 * This field indicates the maximum byte length of a scatter list
571 * for the platform facility. It is a single cell encoded as with encode-int.
572 *
573 * Example:
574 * # od -x ibm,max-sg-len
575 * 0000000 0000 0ff0
576 *
577 * In this example, the maximum byte length of a scatter list is
578 * 0x0ff0 (4,080).
579 *
580 * @devdata - struct nx842_devdata to update
581 * @prop - struct property point containing the maxsyncop for the update
582 *
583 * Returns:
584 * 0 on success
585 * -EINVAL on failure
586 */
587static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
588 struct property *prop) {
589 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400590 const unsigned int maxsglen = of_read_number(prop->value, 1);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500591
Dan Streetmanc47d6302015-06-18 12:05:30 -0400592 if (prop->length != sizeof(maxsglen)) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500593 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
594 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 -0400595 prop->length, sizeof(maxsglen));
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500596 ret = -EINVAL;
597 } else {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400598 devdata->max_sg_len = min_t(unsigned int,
599 maxsglen, NX842_HW_PAGE_SIZE);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500600 }
601
602 return ret;
603}
604
605/**
606 * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
607 *
608 * Definition of the 'ibm,max-sync-cop' OF property:
609 * Two series of cells. The first series of cells represents the maximums
610 * that can be synchronously compressed. The second series of cells
611 * represents the maximums that can be synchronously decompressed.
612 * 1. The first cell in each series contains the count of the number of
613 * data length, scatter list elements pairs that follow – each being
614 * of the form
615 * a. One cell data byte length
616 * b. One cell total number of scatter list elements
617 *
618 * Example:
619 * # od -x ibm,max-sync-cop
620 * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
621 * 0000020 0000 1000 0000 01fe
622 *
623 * In this example, compression supports 0x1000 (4,096) data byte length
624 * and 0x1fe (510) total scatter list elements. Decompression supports
625 * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
626 * elements.
627 *
628 * @devdata - struct nx842_devdata to update
629 * @prop - struct property point containing the maxsyncop for the update
630 *
631 * Returns:
632 * 0 on success
633 * -EINVAL on failure
634 */
635static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
636 struct property *prop) {
637 int ret = 0;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400638 unsigned int comp_data_limit, decomp_data_limit;
639 unsigned int comp_sg_limit, decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500640 const struct maxsynccop_t {
Dan Streetmanc47d6302015-06-18 12:05:30 -0400641 __be32 comp_elements;
642 __be32 comp_data_limit;
643 __be32 comp_sg_limit;
644 __be32 decomp_elements;
645 __be32 decomp_data_limit;
646 __be32 decomp_sg_limit;
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500647 } *maxsynccop;
648
649 if (prop->length != sizeof(*maxsynccop)) {
650 dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
651 dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
652 sizeof(*maxsynccop));
653 ret = -EINVAL;
654 goto out;
655 }
656
657 maxsynccop = (const struct maxsynccop_t *)prop->value;
Dan Streetmanc47d6302015-06-18 12:05:30 -0400658 comp_data_limit = be32_to_cpu(maxsynccop->comp_data_limit);
659 comp_sg_limit = be32_to_cpu(maxsynccop->comp_sg_limit);
660 decomp_data_limit = be32_to_cpu(maxsynccop->decomp_data_limit);
661 decomp_sg_limit = be32_to_cpu(maxsynccop->decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500662
663 /* Use one limit rather than separate limits for compression and
664 * decompression. Set a maximum for this so as not to exceed the
665 * size that the header can support and round the value down to
666 * the hardware page size (4K) */
Dan Streetmanc47d6302015-06-18 12:05:30 -0400667 devdata->max_sync_size = min(comp_data_limit, decomp_data_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500668
669 devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
Dan Streetmanb8e041872015-05-07 13:49:20 -0400670 65536);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500671
Dan Streetmanb8e041872015-05-07 13:49:20 -0400672 if (devdata->max_sync_size < 4096) {
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500673 dev_err(devdata->dev, "%s: hardware max data size (%u) is "
674 "less than the driver minimum, unable to use "
675 "the hardware device\n",
676 __func__, devdata->max_sync_size);
677 ret = -EINVAL;
678 goto out;
679 }
680
Dan Streetman959e6652015-05-07 13:49:18 -0400681 nx842_pseries_constraints.maximum = devdata->max_sync_size;
682
Dan Streetmanc47d6302015-06-18 12:05:30 -0400683 devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500684 if (devdata->max_sync_sg < 1) {
685 dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
686 "less than the driver minimum, unable to use "
687 "the hardware device\n",
688 __func__, devdata->max_sync_sg);
689 ret = -EINVAL;
690 goto out;
691 }
692
693out:
694 return ret;
695}
696
697/**
698 *
699 * nx842_OF_upd -- Handle OF properties updates for the device.
700 *
701 * Set all properties from the OF tree. Optionally, a new property
702 * can be provided by the @new_prop pointer to overwrite an existing value.
703 * The device will remain disabled until all values are valid, this function
704 * will return an error for updates unless all values are valid.
705 *
706 * @new_prop: If not NULL, this property is being updated. If NULL, update
707 * all properties from the current values in the OF tree.
708 *
709 * Returns:
710 * 0 - Success
711 * -ENOMEM - Could not allocate memory for new devdata structure
712 * -EINVAL - property value not found, new_prop is not a recognized
713 * property for the device or property value is not valid.
714 * -ENODEV - Device is not available
715 */
716static int nx842_OF_upd(struct property *new_prop)
717{
718 struct nx842_devdata *old_devdata = NULL;
719 struct nx842_devdata *new_devdata = NULL;
720 struct device_node *of_node = NULL;
721 struct property *status = NULL;
722 struct property *maxsglen = NULL;
723 struct property *maxsyncop = NULL;
724 int ret = 0;
725 unsigned long flags;
726
727 spin_lock_irqsave(&devdata_mutex, flags);
728 old_devdata = rcu_dereference_check(devdata,
729 lockdep_is_held(&devdata_mutex));
730 if (old_devdata)
731 of_node = old_devdata->dev->of_node;
732
733 if (!old_devdata || !of_node) {
734 pr_err("%s: device is not available\n", __func__);
735 spin_unlock_irqrestore(&devdata_mutex, flags);
736 return -ENODEV;
737 }
738
739 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
740 if (!new_devdata) {
741 dev_err(old_devdata->dev, "%s: Could not allocate memory for device data\n", __func__);
742 ret = -ENOMEM;
743 goto error_out;
744 }
745
746 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 Streetman039af962015-07-22 14:26:31 -0400962static int nx842_probe(struct vio_dev *viodev,
963 const struct vio_device_id *id)
Seth Jennings0e16aaf2012-07-19 09:42:40 -0500964{
965 struct nx842_devdata *old_devdata, *new_devdata = NULL;
966 unsigned long flags;
967 int ret = 0;
968
969 spin_lock_irqsave(&devdata_mutex, flags);
970 old_devdata = rcu_dereference_check(devdata,
971 lockdep_is_held(&devdata_mutex));
972
973 if (old_devdata && old_devdata->vdev != NULL) {
974 dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
975 ret = -1;
976 goto error_unlock;
977 }
978
979 dev_set_drvdata(&viodev->dev, NULL);
980
981 new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
982 if (!new_devdata) {
983 dev_err(&viodev->dev, "%s: Could not allocate memory for device data\n", __func__);
984 ret = -ENOMEM;
985 goto error_unlock;
986 }
987
988 new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
989 GFP_NOFS);
990 if (!new_devdata->counters) {
991 dev_err(&viodev->dev, "%s: Could not allocate memory for performance counters\n", __func__);
992 ret = -ENOMEM;
993 goto error_unlock;
994 }
995
996 new_devdata->vdev = viodev;
997 new_devdata->dev = &viodev->dev;
998 nx842_OF_set_defaults(new_devdata);
999
1000 rcu_assign_pointer(devdata, new_devdata);
1001 spin_unlock_irqrestore(&devdata_mutex, flags);
1002 synchronize_rcu();
1003 kfree(old_devdata);
1004
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001005 of_reconfig_notifier_register(&nx842_of_nb);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001006
1007 ret = nx842_OF_upd(NULL);
1008 if (ret && ret != -ENODEV) {
1009 dev_err(&viodev->dev, "could not parse device tree. %d\n", ret);
1010 ret = -1;
1011 goto error;
1012 }
1013
1014 rcu_read_lock();
Jean Delvarecda43572014-05-28 14:02:24 +02001015 dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001016 rcu_read_unlock();
1017
1018 if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
1019 dev_err(&viodev->dev, "could not create sysfs device attributes\n");
1020 ret = -1;
1021 goto error;
1022 }
1023
1024 return 0;
1025
1026error_unlock:
1027 spin_unlock_irqrestore(&devdata_mutex, flags);
1028 if (new_devdata)
1029 kfree(new_devdata->counters);
1030 kfree(new_devdata);
1031error:
1032 return ret;
1033}
1034
Dan Streetman039af962015-07-22 14:26:31 -04001035static int nx842_remove(struct vio_dev *viodev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001036{
1037 struct nx842_devdata *old_devdata;
1038 unsigned long flags;
1039
1040 pr_info("Removing IBM Power 842 compression device\n");
1041 sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
1042
1043 spin_lock_irqsave(&devdata_mutex, flags);
1044 old_devdata = rcu_dereference_check(devdata,
1045 lockdep_is_held(&devdata_mutex));
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +00001046 of_reconfig_notifier_unregister(&nx842_of_nb);
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301047 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001048 spin_unlock_irqrestore(&devdata_mutex, flags);
1049 synchronize_rcu();
1050 dev_set_drvdata(&viodev->dev, NULL);
1051 if (old_devdata)
1052 kfree(old_devdata->counters);
1053 kfree(old_devdata);
Dan Streetman7011a122015-05-07 13:49:17 -04001054
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001055 return 0;
1056}
1057
Dan Streetmanb8e041872015-05-07 13:49:20 -04001058static struct vio_device_id nx842_vio_driver_ids[] = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001059 {"ibm,compression-v1", "ibm,compression"},
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001060 {"", ""},
1061};
1062
Dan Streetmanb8e041872015-05-07 13:49:20 -04001063static struct vio_driver nx842_vio_driver = {
Dan Streetman3e648cb2015-05-28 16:21:31 -04001064 .name = KBUILD_MODNAME,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001065 .probe = nx842_probe,
Dan Streetman039af962015-07-22 14:26:31 -04001066 .remove = nx842_remove,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001067 .get_desired_dma = nx842_get_desired_dma,
Dan Streetmanb8e041872015-05-07 13:49:20 -04001068 .id_table = nx842_vio_driver_ids,
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001069};
1070
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001071static int __init nx842_pseries_init(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001072{
1073 struct nx842_devdata *new_devdata;
Dan Streetman3e648cb2015-05-28 16:21:31 -04001074 int ret;
1075
Dan Streetman3e648cb2015-05-28 16:21:31 -04001076 if (!of_find_compatible_node(NULL, NULL, "ibm,compression"))
1077 return -ENODEV;
1078
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001079 RCU_INIT_POINTER(devdata, NULL);
1080 new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
1081 if (!new_devdata) {
1082 pr_err("Could not allocate memory for device data\n");
1083 return -ENOMEM;
1084 }
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001085 RCU_INIT_POINTER(devdata, new_devdata);
1086
Dan Streetman3e648cb2015-05-28 16:21:31 -04001087 ret = vio_register_driver(&nx842_vio_driver);
1088 if (ret) {
1089 pr_err("Could not register VIO driver %d\n", ret);
1090
1091 kfree(new_devdata);
1092 return ret;
1093 }
1094
1095 if (!nx842_platform_driver_set(&nx842_pseries_driver)) {
1096 vio_unregister_driver(&nx842_vio_driver);
1097 kfree(new_devdata);
1098 return -EEXIST;
1099 }
1100
1101 return 0;
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001102}
1103
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001104module_init(nx842_pseries_init);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001105
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001106static void __exit nx842_pseries_exit(void)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001107{
1108 struct nx842_devdata *old_devdata;
1109 unsigned long flags;
1110
Dan Streetman3e648cb2015-05-28 16:21:31 -04001111 nx842_platform_driver_unset(&nx842_pseries_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001112 spin_lock_irqsave(&devdata_mutex, flags);
1113 old_devdata = rcu_dereference_check(devdata,
1114 lockdep_is_held(&devdata_mutex));
Monam Agarwal7ded6e32014-03-24 01:02:42 +05301115 RCU_INIT_POINTER(devdata, NULL);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001116 spin_unlock_irqrestore(&devdata_mutex, flags);
1117 synchronize_rcu();
Dan Streetmanb8e041872015-05-07 13:49:20 -04001118 if (old_devdata && old_devdata->dev)
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001119 dev_set_drvdata(old_devdata->dev, NULL);
1120 kfree(old_devdata);
Dan Streetmanb8e041872015-05-07 13:49:20 -04001121 vio_unregister_driver(&nx842_vio_driver);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001122}
1123
Nishanth Aravamudanec13bcb2015-07-02 15:39:21 -07001124module_exit(nx842_pseries_exit);
Seth Jennings0e16aaf2012-07-19 09:42:40 -05001125