]>
Commit | Line | Data |
---|---|---|
0e16aafb SJ |
1 | /* |
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 | ||
33b58b01 | 24 | #include <asm/vio.h> |
0e16aafb | 25 | |
7011a122 | 26 | #include "nx-842.h" |
0e16aafb SJ |
27 | #include "nx_csbcpb.h" /* struct nx_csbcpb */ |
28 | ||
0e16aafb SJ |
29 | MODULE_LICENSE("GPL"); |
30 | MODULE_AUTHOR("Robert Jennings <rcj@linux.vnet.ibm.com>"); | |
31 | MODULE_DESCRIPTION("842 H/W Compression driver for IBM Power processors"); | |
03952d98 DS |
32 | MODULE_ALIAS_CRYPTO("842"); |
33 | MODULE_ALIAS_CRYPTO("842-nx"); | |
0e16aafb | 34 | |
959e6659 | 35 | static struct nx842_constraints nx842_pseries_constraints = { |
3154de71 | 36 | .alignment = DDE_BUFFER_ALIGN, |
959e6659 | 37 | .multiple = DDE_BUFFER_LAST_MULT, |
3154de71 | 38 | .minimum = DDE_BUFFER_LAST_MULT, |
959e6659 DS |
39 | .maximum = PAGE_SIZE, /* dynamic, max_sync_size */ |
40 | }; | |
41 | ||
b8e04187 | 42 | static int check_constraints(unsigned long buf, unsigned int *len, bool in) |
0e16aafb | 43 | { |
b8e04187 DS |
44 | if (!IS_ALIGNED(buf, nx842_pseries_constraints.alignment)) { |
45 | pr_debug("%s buffer 0x%lx not aligned to 0x%x\n", | |
46 | in ? "input" : "output", buf, | |
47 | nx842_pseries_constraints.alignment); | |
48 | return -EINVAL; | |
49 | } | |
50 | if (*len % nx842_pseries_constraints.multiple) { | |
51 | pr_debug("%s buffer len 0x%x not multiple of 0x%x\n", | |
52 | in ? "input" : "output", *len, | |
53 | nx842_pseries_constraints.multiple); | |
54 | if (in) | |
55 | return -EINVAL; | |
56 | *len = round_down(*len, nx842_pseries_constraints.multiple); | |
57 | } | |
58 | if (*len < nx842_pseries_constraints.minimum) { | |
59 | pr_debug("%s buffer len 0x%x under minimum 0x%x\n", | |
60 | in ? "input" : "output", *len, | |
61 | nx842_pseries_constraints.minimum); | |
62 | return -EINVAL; | |
63 | } | |
64 | if (*len > nx842_pseries_constraints.maximum) { | |
65 | pr_debug("%s buffer len 0x%x over maximum 0x%x\n", | |
66 | in ? "input" : "output", *len, | |
67 | nx842_pseries_constraints.maximum); | |
68 | if (in) | |
69 | return -EINVAL; | |
70 | *len = nx842_pseries_constraints.maximum; | |
71 | } | |
72 | return 0; | |
0e16aafb SJ |
73 | } |
74 | ||
b8e04187 DS |
75 | /* I assume we need to align the CSB? */ |
76 | #define WORKMEM_ALIGN (256) | |
77 | ||
78 | struct nx842_workmem { | |
79 | /* scatterlist */ | |
80 | char slin[4096]; | |
81 | char slout[4096]; | |
82 | /* coprocessor status/parameter block */ | |
83 | struct nx_csbcpb csbcpb; | |
84 | ||
85 | char padding[WORKMEM_ALIGN]; | |
86 | } __aligned(WORKMEM_ALIGN); | |
87 | ||
0e16aafb SJ |
88 | /* Macros for fields within nx_csbcpb */ |
89 | /* Check the valid bit within the csbcpb valid field */ | |
90 | #define NX842_CSBCBP_VALID_CHK(x) (x & BIT_MASK(7)) | |
91 | ||
92 | /* CE macros operate on the completion_extension field bits in the csbcpb. | |
93 | * CE0 0=full completion, 1=partial completion | |
94 | * CE1 0=CE0 indicates completion, 1=termination (output may be modified) | |
95 | * CE2 0=processed_bytes is source bytes, 1=processed_bytes is target bytes */ | |
96 | #define NX842_CSBCPB_CE0(x) (x & BIT_MASK(7)) | |
97 | #define NX842_CSBCPB_CE1(x) (x & BIT_MASK(6)) | |
98 | #define NX842_CSBCPB_CE2(x) (x & BIT_MASK(5)) | |
99 | ||
100 | /* The NX unit accepts data only on 4K page boundaries */ | |
b8e04187 | 101 | #define NX842_HW_PAGE_SIZE (4096) |
0e16aafb SJ |
102 | #define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1)) |
103 | ||
0e16aafb SJ |
104 | struct ibm_nx842_counters { |
105 | atomic64_t comp_complete; | |
106 | atomic64_t comp_failed; | |
107 | atomic64_t decomp_complete; | |
108 | atomic64_t decomp_failed; | |
109 | atomic64_t swdecomp; | |
110 | atomic64_t comp_times[32]; | |
111 | atomic64_t decomp_times[32]; | |
112 | }; | |
113 | ||
114 | static struct nx842_devdata { | |
115 | struct vio_dev *vdev; | |
116 | struct device *dev; | |
117 | struct ibm_nx842_counters *counters; | |
118 | unsigned int max_sg_len; | |
119 | unsigned int max_sync_size; | |
120 | unsigned int max_sync_sg; | |
0e16aafb SJ |
121 | } __rcu *devdata; |
122 | static DEFINE_SPINLOCK(devdata_mutex); | |
123 | ||
124 | #define NX842_COUNTER_INC(_x) \ | |
125 | static inline void nx842_inc_##_x( \ | |
126 | const struct nx842_devdata *dev) { \ | |
127 | if (dev) \ | |
128 | atomic64_inc(&dev->counters->_x); \ | |
129 | } | |
130 | NX842_COUNTER_INC(comp_complete); | |
131 | NX842_COUNTER_INC(comp_failed); | |
132 | NX842_COUNTER_INC(decomp_complete); | |
133 | NX842_COUNTER_INC(decomp_failed); | |
134 | NX842_COUNTER_INC(swdecomp); | |
135 | ||
136 | #define NX842_HIST_SLOTS 16 | |
137 | ||
138 | static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time) | |
139 | { | |
140 | int bucket = fls(time); | |
141 | ||
142 | if (bucket) | |
143 | bucket = min((NX842_HIST_SLOTS - 1), bucket - 1); | |
144 | ||
145 | atomic64_inc(×[bucket]); | |
146 | } | |
147 | ||
148 | /* NX unit operation flags */ | |
149 | #define NX842_OP_COMPRESS 0x0 | |
150 | #define NX842_OP_CRC 0x1 | |
151 | #define NX842_OP_DECOMPRESS 0x2 | |
152 | #define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC) | |
153 | #define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC) | |
154 | #define NX842_OP_ASYNC (1<<23) | |
155 | #define NX842_OP_NOTIFY (1<<22) | |
156 | #define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8) | |
157 | ||
158 | static unsigned long nx842_get_desired_dma(struct vio_dev *viodev) | |
159 | { | |
160 | /* No use of DMA mappings within the driver. */ | |
161 | return 0; | |
162 | } | |
163 | ||
164 | struct nx842_slentry { | |
c47d6302 DS |
165 | __be64 ptr; /* Real address (use __pa()) */ |
166 | __be64 len; | |
0e16aafb SJ |
167 | }; |
168 | ||
169 | /* pHyp scatterlist entry */ | |
170 | struct nx842_scatterlist { | |
171 | int entry_nr; /* number of slentries */ | |
172 | struct nx842_slentry *entries; /* ptr to array of slentries */ | |
173 | }; | |
174 | ||
175 | /* Does not include sizeof(entry_nr) in the size */ | |
176 | static inline unsigned long nx842_get_scatterlist_size( | |
177 | struct nx842_scatterlist *sl) | |
178 | { | |
179 | return sl->entry_nr * sizeof(struct nx842_slentry); | |
180 | } | |
181 | ||
182 | static int nx842_build_scatterlist(unsigned long buf, int len, | |
183 | struct nx842_scatterlist *sl) | |
184 | { | |
c47d6302 | 185 | unsigned long entrylen; |
0e16aafb SJ |
186 | struct nx842_slentry *entry; |
187 | ||
188 | sl->entry_nr = 0; | |
189 | ||
190 | entry = sl->entries; | |
191 | while (len) { | |
c47d6302 DS |
192 | entry->ptr = cpu_to_be64(nx842_get_pa((void *)buf)); |
193 | entrylen = min_t(int, len, | |
194 | LEN_ON_SIZE(buf, NX842_HW_PAGE_SIZE)); | |
195 | entry->len = cpu_to_be64(entrylen); | |
196 | ||
197 | len -= entrylen; | |
198 | buf += entrylen; | |
199 | ||
0e16aafb SJ |
200 | sl->entry_nr++; |
201 | entry++; | |
202 | } | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
0e16aafb SJ |
207 | static int nx842_validate_result(struct device *dev, |
208 | struct cop_status_block *csb) | |
209 | { | |
210 | /* The csb must be valid after returning from vio_h_cop_sync */ | |
211 | if (!NX842_CSBCBP_VALID_CHK(csb->valid)) { | |
212 | dev_err(dev, "%s: cspcbp not valid upon completion.\n", | |
213 | __func__); | |
214 | dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n", | |
215 | csb->valid, | |
216 | csb->crb_seq_number, | |
217 | csb->completion_code, | |
218 | csb->completion_extension); | |
219 | dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n", | |
c47d6302 DS |
220 | be32_to_cpu(csb->processed_byte_count), |
221 | (unsigned long)be64_to_cpu(csb->address)); | |
0e16aafb SJ |
222 | return -EIO; |
223 | } | |
224 | ||
225 | /* Check return values from the hardware in the CSB */ | |
226 | switch (csb->completion_code) { | |
227 | case 0: /* Completed without error */ | |
228 | break; | |
7371c0a5 DS |
229 | case 64: /* Compression ok, but output larger than input */ |
230 | dev_dbg(dev, "%s: output size larger than input size\n", | |
231 | __func__); | |
232 | break; | |
0e16aafb | 233 | case 13: /* Output buffer too small */ |
7371c0a5 | 234 | dev_dbg(dev, "%s: Out of space in output buffer\n", |
0e16aafb SJ |
235 | __func__); |
236 | return -ENOSPC; | |
237 | case 66: /* Input data contains an illegal template field */ | |
238 | case 67: /* Template indicates data past the end of the input stream */ | |
239 | dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n", | |
240 | __func__, csb->completion_code); | |
241 | return -EINVAL; | |
242 | default: | |
243 | dev_dbg(dev, "%s: Unspecified error (code:%d)\n", | |
244 | __func__, csb->completion_code); | |
245 | return -EIO; | |
246 | } | |
247 | ||
248 | /* Hardware sanity check */ | |
249 | if (!NX842_CSBCPB_CE2(csb->completion_extension)) { | |
250 | dev_err(dev, "%s: No error returned by hardware, but " | |
251 | "data returned is unusable, contact support.\n" | |
252 | "(Additional info: csbcbp->processed bytes " | |
253 | "does not specify processed bytes for the " | |
254 | "target buffer.)\n", __func__); | |
255 | return -EIO; | |
256 | } | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | /** | |
7011a122 | 262 | * nx842_pseries_compress - Compress data using the 842 algorithm |
0e16aafb SJ |
263 | * |
264 | * Compression provide by the NX842 coprocessor on IBM Power systems. | |
265 | * The input buffer is compressed and the result is stored in the | |
266 | * provided output buffer. | |
267 | * | |
268 | * Upon return from this function @outlen contains the length of the | |
269 | * compressed data. If there is an error then @outlen will be 0 and an | |
270 | * error will be specified by the return code from this function. | |
271 | * | |
b8e04187 DS |
272 | * @in: Pointer to input buffer |
273 | * @inlen: Length of input buffer | |
0e16aafb SJ |
274 | * @out: Pointer to output buffer |
275 | * @outlen: Length of output buffer | |
276 | * @wrkmem: ptr to buffer for working memory, size determined by | |
2c6f6eab | 277 | * nx842_pseries_driver.workmem_size |
0e16aafb SJ |
278 | * |
279 | * Returns: | |
280 | * 0 Success, output of length @outlen stored in the buffer at @out | |
281 | * -ENOMEM Unable to allocate internal buffers | |
282 | * -ENOSPC Output buffer is to small | |
0e16aafb SJ |
283 | * -EIO Internal error |
284 | * -ENODEV Hardware unavailable | |
285 | */ | |
7011a122 DS |
286 | static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen, |
287 | unsigned char *out, unsigned int *outlen, | |
288 | void *wmem) | |
0e16aafb | 289 | { |
0e16aafb SJ |
290 | struct nx842_devdata *local_devdata; |
291 | struct device *dev = NULL; | |
292 | struct nx842_workmem *workmem; | |
293 | struct nx842_scatterlist slin, slout; | |
294 | struct nx_csbcpb *csbcpb; | |
b8e04187 DS |
295 | int ret = 0, max_sync_size; |
296 | unsigned long inbuf, outbuf; | |
0e16aafb SJ |
297 | struct vio_pfo_op op = { |
298 | .done = NULL, | |
299 | .handle = 0, | |
300 | .timeout = 0, | |
301 | }; | |
b8e04187 | 302 | unsigned long start = get_tb(); |
0e16aafb | 303 | |
0e16aafb | 304 | inbuf = (unsigned long)in; |
b8e04187 DS |
305 | if (check_constraints(inbuf, &inlen, true)) |
306 | return -EINVAL; | |
307 | ||
308 | outbuf = (unsigned long)out; | |
309 | if (check_constraints(outbuf, outlen, false)) | |
0e16aafb SJ |
310 | return -EINVAL; |
311 | ||
312 | rcu_read_lock(); | |
313 | local_devdata = rcu_dereference(devdata); | |
314 | if (!local_devdata || !local_devdata->dev) { | |
315 | rcu_read_unlock(); | |
316 | return -ENODEV; | |
317 | } | |
318 | max_sync_size = local_devdata->max_sync_size; | |
319 | dev = local_devdata->dev; | |
320 | ||
0e16aafb | 321 | /* Init scatterlist */ |
b8e04187 | 322 | workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN); |
0e16aafb SJ |
323 | slin.entries = (struct nx842_slentry *)workmem->slin; |
324 | slout.entries = (struct nx842_slentry *)workmem->slout; | |
325 | ||
326 | /* Init operation */ | |
327 | op.flags = NX842_OP_COMPRESS; | |
328 | csbcpb = &workmem->csbcpb; | |
329 | memset(csbcpb, 0, sizeof(*csbcpb)); | |
0ba3e101 | 330 | op.csbcpb = nx842_get_pa(csbcpb); |
0e16aafb | 331 | |
b8e04187 DS |
332 | if ((inbuf & NX842_HW_PAGE_MASK) == |
333 | ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) { | |
334 | /* Create direct DDE */ | |
335 | op.in = nx842_get_pa((void *)inbuf); | |
336 | op.inlen = inlen; | |
337 | } else { | |
338 | /* Create indirect DDE (scatterlist) */ | |
339 | nx842_build_scatterlist(inbuf, inlen, &slin); | |
340 | op.in = nx842_get_pa(slin.entries); | |
341 | op.inlen = -nx842_get_scatterlist_size(&slin); | |
342 | } | |
0e16aafb | 343 | |
b8e04187 DS |
344 | if ((outbuf & NX842_HW_PAGE_MASK) == |
345 | ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) { | |
346 | /* Create direct DDE */ | |
347 | op.out = nx842_get_pa((void *)outbuf); | |
348 | op.outlen = *outlen; | |
349 | } else { | |
350 | /* Create indirect DDE (scatterlist) */ | |
351 | nx842_build_scatterlist(outbuf, *outlen, &slout); | |
352 | op.out = nx842_get_pa(slout.entries); | |
0e16aafb | 353 | op.outlen = -nx842_get_scatterlist_size(&slout); |
b8e04187 | 354 | } |
0e16aafb | 355 | |
c47d6302 DS |
356 | dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n", |
357 | __func__, (unsigned long)op.in, (long)op.inlen, | |
358 | (unsigned long)op.out, (long)op.outlen); | |
359 | ||
b8e04187 DS |
360 | /* Send request to pHyp */ |
361 | ret = vio_h_cop_sync(local_devdata->vdev, &op); | |
0e16aafb | 362 | |
b8e04187 DS |
363 | /* Check for pHyp error */ |
364 | if (ret) { | |
365 | dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n", | |
366 | __func__, ret, op.hcall_err); | |
367 | ret = -EIO; | |
368 | goto unlock; | |
0e16aafb SJ |
369 | } |
370 | ||
b8e04187 DS |
371 | /* Check for hardware error */ |
372 | ret = nx842_validate_result(dev, &csbcpb->csb); | |
373 | if (ret) | |
374 | goto unlock; | |
375 | ||
c47d6302 | 376 | *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count); |
b8e04187 | 377 | dev_dbg(dev, "%s: processed_bytes=%d\n", __func__, *outlen); |
0e16aafb SJ |
378 | |
379 | unlock: | |
380 | if (ret) | |
381 | nx842_inc_comp_failed(local_devdata); | |
382 | else { | |
383 | nx842_inc_comp_complete(local_devdata); | |
384 | ibm_nx842_incr_hist(local_devdata->counters->comp_times, | |
b8e04187 | 385 | (get_tb() - start) / tb_ticks_per_usec); |
0e16aafb SJ |
386 | } |
387 | rcu_read_unlock(); | |
388 | return ret; | |
389 | } | |
0e16aafb | 390 | |
0e16aafb | 391 | /** |
7011a122 | 392 | * nx842_pseries_decompress - Decompress data using the 842 algorithm |
0e16aafb SJ |
393 | * |
394 | * Decompression provide by the NX842 coprocessor on IBM Power systems. | |
395 | * The input buffer is decompressed and the result is stored in the | |
396 | * provided output buffer. The size allocated to the output buffer is | |
397 | * provided by the caller of this function in @outlen. Upon return from | |
398 | * this function @outlen contains the length of the decompressed data. | |
399 | * If there is an error then @outlen will be 0 and an error will be | |
400 | * specified by the return code from this function. | |
401 | * | |
b8e04187 | 402 | * @in: Pointer to input buffer |
0e16aafb | 403 | * @inlen: Length of input buffer |
b8e04187 DS |
404 | * @out: Pointer to output buffer |
405 | * @outlen: Length of output buffer | |
0e16aafb | 406 | * @wrkmem: ptr to buffer for working memory, size determined by |
2c6f6eab | 407 | * nx842_pseries_driver.workmem_size |
0e16aafb SJ |
408 | * |
409 | * Returns: | |
410 | * 0 Success, output of length @outlen stored in the buffer at @out | |
411 | * -ENODEV Hardware decompression device is unavailable | |
412 | * -ENOMEM Unable to allocate internal buffers | |
413 | * -ENOSPC Output buffer is to small | |
414 | * -EINVAL Bad input data encountered when attempting decompress | |
415 | * -EIO Internal error | |
416 | */ | |
7011a122 DS |
417 | static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen, |
418 | unsigned char *out, unsigned int *outlen, | |
419 | void *wmem) | |
0e16aafb | 420 | { |
0e16aafb SJ |
421 | struct nx842_devdata *local_devdata; |
422 | struct device *dev = NULL; | |
423 | struct nx842_workmem *workmem; | |
424 | struct nx842_scatterlist slin, slout; | |
425 | struct nx_csbcpb *csbcpb; | |
b8e04187 | 426 | int ret = 0, max_sync_size; |
0e16aafb SJ |
427 | unsigned long inbuf, outbuf; |
428 | struct vio_pfo_op op = { | |
429 | .done = NULL, | |
430 | .handle = 0, | |
431 | .timeout = 0, | |
432 | }; | |
b8e04187 | 433 | unsigned long start = get_tb(); |
0e16aafb SJ |
434 | |
435 | /* Ensure page alignment and size */ | |
b8e04187 DS |
436 | inbuf = (unsigned long)in; |
437 | if (check_constraints(inbuf, &inlen, true)) | |
438 | return -EINVAL; | |
439 | ||
0e16aafb | 440 | outbuf = (unsigned long)out; |
b8e04187 | 441 | if (check_constraints(outbuf, outlen, false)) |
0e16aafb SJ |
442 | return -EINVAL; |
443 | ||
444 | rcu_read_lock(); | |
445 | local_devdata = rcu_dereference(devdata); | |
b8e04187 DS |
446 | if (!local_devdata || !local_devdata->dev) { |
447 | rcu_read_unlock(); | |
448 | return -ENODEV; | |
0e16aafb | 449 | } |
b8e04187 DS |
450 | max_sync_size = local_devdata->max_sync_size; |
451 | dev = local_devdata->dev; | |
452 | ||
453 | workmem = PTR_ALIGN(wmem, WORKMEM_ALIGN); | |
0e16aafb SJ |
454 | |
455 | /* Init scatterlist */ | |
456 | slin.entries = (struct nx842_slentry *)workmem->slin; | |
457 | slout.entries = (struct nx842_slentry *)workmem->slout; | |
458 | ||
459 | /* Init operation */ | |
460 | op.flags = NX842_OP_DECOMPRESS; | |
461 | csbcpb = &workmem->csbcpb; | |
462 | memset(csbcpb, 0, sizeof(*csbcpb)); | |
0ba3e101 | 463 | op.csbcpb = nx842_get_pa(csbcpb); |
0e16aafb | 464 | |
b8e04187 DS |
465 | if ((inbuf & NX842_HW_PAGE_MASK) == |
466 | ((inbuf + inlen - 1) & NX842_HW_PAGE_MASK)) { | |
467 | /* Create direct DDE */ | |
468 | op.in = nx842_get_pa((void *)inbuf); | |
469 | op.inlen = inlen; | |
470 | } else { | |
471 | /* Create indirect DDE (scatterlist) */ | |
472 | nx842_build_scatterlist(inbuf, inlen, &slin); | |
473 | op.in = nx842_get_pa(slin.entries); | |
474 | op.inlen = -nx842_get_scatterlist_size(&slin); | |
475 | } | |
0e16aafb | 476 | |
b8e04187 DS |
477 | if ((outbuf & NX842_HW_PAGE_MASK) == |
478 | ((outbuf + *outlen - 1) & NX842_HW_PAGE_MASK)) { | |
479 | /* Create direct DDE */ | |
480 | op.out = nx842_get_pa((void *)outbuf); | |
481 | op.outlen = *outlen; | |
482 | } else { | |
483 | /* Create indirect DDE (scatterlist) */ | |
484 | nx842_build_scatterlist(outbuf, *outlen, &slout); | |
485 | op.out = nx842_get_pa(slout.entries); | |
486 | op.outlen = -nx842_get_scatterlist_size(&slout); | |
487 | } | |
0e16aafb | 488 | |
c47d6302 DS |
489 | dev_dbg(dev, "%s: op.in %lx op.inlen %ld op.out %lx op.outlen %ld\n", |
490 | __func__, (unsigned long)op.in, (long)op.inlen, | |
491 | (unsigned long)op.out, (long)op.outlen); | |
492 | ||
b8e04187 DS |
493 | /* Send request to pHyp */ |
494 | ret = vio_h_cop_sync(local_devdata->vdev, &op); | |
0e16aafb | 495 | |
b8e04187 DS |
496 | /* Check for pHyp error */ |
497 | if (ret) { | |
498 | dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n", | |
499 | __func__, ret, op.hcall_err); | |
500 | goto unlock; | |
0e16aafb SJ |
501 | } |
502 | ||
b8e04187 DS |
503 | /* Check for hardware error */ |
504 | ret = nx842_validate_result(dev, &csbcpb->csb); | |
505 | if (ret) | |
506 | goto unlock; | |
507 | ||
c47d6302 | 508 | *outlen = be32_to_cpu(csbcpb->csb.processed_byte_count); |
0e16aafb SJ |
509 | |
510 | unlock: | |
511 | if (ret) | |
512 | /* decompress fail */ | |
513 | nx842_inc_decomp_failed(local_devdata); | |
514 | else { | |
0e16aafb SJ |
515 | nx842_inc_decomp_complete(local_devdata); |
516 | ibm_nx842_incr_hist(local_devdata->counters->decomp_times, | |
b8e04187 | 517 | (get_tb() - start) / tb_ticks_per_usec); |
0e16aafb SJ |
518 | } |
519 | ||
520 | rcu_read_unlock(); | |
521 | return ret; | |
522 | } | |
0e16aafb SJ |
523 | |
524 | /** | |
525 | * nx842_OF_set_defaults -- Set default (disabled) values for devdata | |
526 | * | |
527 | * @devdata - struct nx842_devdata to update | |
528 | * | |
529 | * Returns: | |
530 | * 0 on success | |
531 | * -ENOENT if @devdata ptr is NULL | |
532 | */ | |
533 | static int nx842_OF_set_defaults(struct nx842_devdata *devdata) | |
534 | { | |
535 | if (devdata) { | |
536 | devdata->max_sync_size = 0; | |
537 | devdata->max_sync_sg = 0; | |
538 | devdata->max_sg_len = 0; | |
0e16aafb SJ |
539 | return 0; |
540 | } else | |
541 | return -ENOENT; | |
542 | } | |
543 | ||
544 | /** | |
90fd73f9 | 545 | * nx842_OF_upd_status -- Check the device info from OF status prop |
0e16aafb SJ |
546 | * |
547 | * The status property indicates if the accelerator is enabled. If the | |
548 | * device is in the OF tree it indicates that the hardware is present. | |
549 | * The status field indicates if the device is enabled when the status | |
550 | * is 'okay'. Otherwise the device driver will be disabled. | |
551 | * | |
0e16aafb SJ |
552 | * @prop - struct property point containing the maxsyncop for the update |
553 | * | |
554 | * Returns: | |
555 | * 0 - Device is available | |
fa9a9a08 | 556 | * -ENODEV - Device is not available |
0e16aafb | 557 | */ |
90fd73f9 DS |
558 | static int nx842_OF_upd_status(struct property *prop) |
559 | { | |
0e16aafb SJ |
560 | const char *status = (const char *)prop->value; |
561 | ||
90fd73f9 DS |
562 | if (!strncmp(status, "okay", (size_t)prop->length)) |
563 | return 0; | |
564 | if (!strncmp(status, "disabled", (size_t)prop->length)) | |
565 | return -ENODEV; | |
566 | dev_info(devdata->dev, "%s: unknown status '%s'\n", __func__, status); | |
0e16aafb | 567 | |
90fd73f9 | 568 | return -EINVAL; |
0e16aafb SJ |
569 | } |
570 | ||
571 | /** | |
572 | * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop | |
573 | * | |
574 | * Definition of the 'ibm,max-sg-len' OF property: | |
575 | * This field indicates the maximum byte length of a scatter list | |
576 | * for the platform facility. It is a single cell encoded as with encode-int. | |
577 | * | |
578 | * Example: | |
579 | * # od -x ibm,max-sg-len | |
580 | * 0000000 0000 0ff0 | |
581 | * | |
582 | * In this example, the maximum byte length of a scatter list is | |
583 | * 0x0ff0 (4,080). | |
584 | * | |
585 | * @devdata - struct nx842_devdata to update | |
586 | * @prop - struct property point containing the maxsyncop for the update | |
587 | * | |
588 | * Returns: | |
589 | * 0 on success | |
590 | * -EINVAL on failure | |
591 | */ | |
592 | static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata, | |
593 | struct property *prop) { | |
594 | int ret = 0; | |
c47d6302 | 595 | const unsigned int maxsglen = of_read_number(prop->value, 1); |
0e16aafb | 596 | |
c47d6302 | 597 | if (prop->length != sizeof(maxsglen)) { |
0e16aafb SJ |
598 | dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__); |
599 | dev_dbg(devdata->dev, "%s: ibm,max-sg-len is %d bytes long, expected %lu bytes\n", __func__, | |
c47d6302 | 600 | prop->length, sizeof(maxsglen)); |
0e16aafb SJ |
601 | ret = -EINVAL; |
602 | } else { | |
c47d6302 DS |
603 | devdata->max_sg_len = min_t(unsigned int, |
604 | maxsglen, NX842_HW_PAGE_SIZE); | |
0e16aafb SJ |
605 | } |
606 | ||
607 | return ret; | |
608 | } | |
609 | ||
610 | /** | |
611 | * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop | |
612 | * | |
613 | * Definition of the 'ibm,max-sync-cop' OF property: | |
614 | * Two series of cells. The first series of cells represents the maximums | |
615 | * that can be synchronously compressed. The second series of cells | |
616 | * represents the maximums that can be synchronously decompressed. | |
617 | * 1. The first cell in each series contains the count of the number of | |
618 | * data length, scatter list elements pairs that follow – each being | |
619 | * of the form | |
620 | * a. One cell data byte length | |
621 | * b. One cell total number of scatter list elements | |
622 | * | |
623 | * Example: | |
624 | * # od -x ibm,max-sync-cop | |
625 | * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001 | |
626 | * 0000020 0000 1000 0000 01fe | |
627 | * | |
628 | * In this example, compression supports 0x1000 (4,096) data byte length | |
629 | * and 0x1fe (510) total scatter list elements. Decompression supports | |
630 | * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list | |
631 | * elements. | |
632 | * | |
633 | * @devdata - struct nx842_devdata to update | |
634 | * @prop - struct property point containing the maxsyncop for the update | |
635 | * | |
636 | * Returns: | |
637 | * 0 on success | |
638 | * -EINVAL on failure | |
639 | */ | |
640 | static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata, | |
641 | struct property *prop) { | |
642 | int ret = 0; | |
c47d6302 DS |
643 | unsigned int comp_data_limit, decomp_data_limit; |
644 | unsigned int comp_sg_limit, decomp_sg_limit; | |
0e16aafb | 645 | const struct maxsynccop_t { |
c47d6302 DS |
646 | __be32 comp_elements; |
647 | __be32 comp_data_limit; | |
648 | __be32 comp_sg_limit; | |
649 | __be32 decomp_elements; | |
650 | __be32 decomp_data_limit; | |
651 | __be32 decomp_sg_limit; | |
0e16aafb SJ |
652 | } *maxsynccop; |
653 | ||
654 | if (prop->length != sizeof(*maxsynccop)) { | |
655 | dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__); | |
656 | dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length, | |
657 | sizeof(*maxsynccop)); | |
658 | ret = -EINVAL; | |
659 | goto out; | |
660 | } | |
661 | ||
662 | maxsynccop = (const struct maxsynccop_t *)prop->value; | |
c47d6302 DS |
663 | comp_data_limit = be32_to_cpu(maxsynccop->comp_data_limit); |
664 | comp_sg_limit = be32_to_cpu(maxsynccop->comp_sg_limit); | |
665 | decomp_data_limit = be32_to_cpu(maxsynccop->decomp_data_limit); | |
666 | decomp_sg_limit = be32_to_cpu(maxsynccop->decomp_sg_limit); | |
0e16aafb SJ |
667 | |
668 | /* Use one limit rather than separate limits for compression and | |
669 | * decompression. Set a maximum for this so as not to exceed the | |
670 | * size that the header can support and round the value down to | |
671 | * the hardware page size (4K) */ | |
c47d6302 | 672 | devdata->max_sync_size = min(comp_data_limit, decomp_data_limit); |
0e16aafb SJ |
673 | |
674 | devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size, | |
b8e04187 | 675 | 65536); |
0e16aafb | 676 | |
b8e04187 | 677 | if (devdata->max_sync_size < 4096) { |
0e16aafb SJ |
678 | dev_err(devdata->dev, "%s: hardware max data size (%u) is " |
679 | "less than the driver minimum, unable to use " | |
680 | "the hardware device\n", | |
681 | __func__, devdata->max_sync_size); | |
682 | ret = -EINVAL; | |
683 | goto out; | |
684 | } | |
685 | ||
959e6659 DS |
686 | nx842_pseries_constraints.maximum = devdata->max_sync_size; |
687 | ||
c47d6302 | 688 | devdata->max_sync_sg = min(comp_sg_limit, decomp_sg_limit); |
0e16aafb SJ |
689 | if (devdata->max_sync_sg < 1) { |
690 | dev_err(devdata->dev, "%s: hardware max sg size (%u) is " | |
691 | "less than the driver minimum, unable to use " | |
692 | "the hardware device\n", | |
693 | __func__, devdata->max_sync_sg); | |
694 | ret = -EINVAL; | |
695 | goto out; | |
696 | } | |
697 | ||
698 | out: | |
699 | return ret; | |
700 | } | |
701 | ||
702 | /** | |
703 | * | |
704 | * nx842_OF_upd -- Handle OF properties updates for the device. | |
705 | * | |
706 | * Set all properties from the OF tree. Optionally, a new property | |
707 | * can be provided by the @new_prop pointer to overwrite an existing value. | |
708 | * The device will remain disabled until all values are valid, this function | |
709 | * will return an error for updates unless all values are valid. | |
710 | * | |
711 | * @new_prop: If not NULL, this property is being updated. If NULL, update | |
712 | * all properties from the current values in the OF tree. | |
713 | * | |
714 | * Returns: | |
715 | * 0 - Success | |
716 | * -ENOMEM - Could not allocate memory for new devdata structure | |
717 | * -EINVAL - property value not found, new_prop is not a recognized | |
718 | * property for the device or property value is not valid. | |
719 | * -ENODEV - Device is not available | |
720 | */ | |
721 | static int nx842_OF_upd(struct property *new_prop) | |
722 | { | |
723 | struct nx842_devdata *old_devdata = NULL; | |
724 | struct nx842_devdata *new_devdata = NULL; | |
725 | struct device_node *of_node = NULL; | |
726 | struct property *status = NULL; | |
727 | struct property *maxsglen = NULL; | |
728 | struct property *maxsyncop = NULL; | |
729 | int ret = 0; | |
730 | unsigned long flags; | |
731 | ||
7f6e3aad DS |
732 | new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS); |
733 | if (!new_devdata) | |
734 | return -ENOMEM; | |
735 | ||
0e16aafb SJ |
736 | spin_lock_irqsave(&devdata_mutex, flags); |
737 | old_devdata = rcu_dereference_check(devdata, | |
738 | lockdep_is_held(&devdata_mutex)); | |
739 | if (old_devdata) | |
740 | of_node = old_devdata->dev->of_node; | |
741 | ||
742 | if (!old_devdata || !of_node) { | |
743 | pr_err("%s: device is not available\n", __func__); | |
744 | spin_unlock_irqrestore(&devdata_mutex, flags); | |
7f6e3aad | 745 | kfree(new_devdata); |
0e16aafb SJ |
746 | return -ENODEV; |
747 | } | |
748 | ||
0e16aafb SJ |
749 | memcpy(new_devdata, old_devdata, sizeof(*old_devdata)); |
750 | new_devdata->counters = old_devdata->counters; | |
751 | ||
752 | /* Set ptrs for existing properties */ | |
753 | status = of_find_property(of_node, "status", NULL); | |
754 | maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL); | |
755 | maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL); | |
756 | if (!status || !maxsglen || !maxsyncop) { | |
757 | dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__); | |
758 | ret = -EINVAL; | |
759 | goto error_out; | |
760 | } | |
761 | ||
259092a3 GL |
762 | /* |
763 | * If this is a property update, there are only certain properties that | |
764 | * we care about. Bail if it isn't in the below list | |
765 | */ | |
766 | if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) || | |
767 | strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) || | |
768 | strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length))) | |
769 | goto out; | |
0e16aafb SJ |
770 | |
771 | /* Perform property updates */ | |
90fd73f9 | 772 | ret = nx842_OF_upd_status(status); |
0e16aafb SJ |
773 | if (ret) |
774 | goto error_out; | |
775 | ||
776 | ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen); | |
777 | if (ret) | |
778 | goto error_out; | |
779 | ||
780 | ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop); | |
781 | if (ret) | |
782 | goto error_out; | |
783 | ||
784 | out: | |
785 | dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n", | |
786 | __func__, new_devdata->max_sync_size, | |
787 | old_devdata->max_sync_size); | |
788 | dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n", | |
789 | __func__, new_devdata->max_sync_sg, | |
790 | old_devdata->max_sync_sg); | |
791 | dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n", | |
792 | __func__, new_devdata->max_sg_len, | |
793 | old_devdata->max_sg_len); | |
794 | ||
795 | rcu_assign_pointer(devdata, new_devdata); | |
796 | spin_unlock_irqrestore(&devdata_mutex, flags); | |
797 | synchronize_rcu(); | |
798 | dev_set_drvdata(new_devdata->dev, new_devdata); | |
799 | kfree(old_devdata); | |
800 | return 0; | |
801 | ||
802 | error_out: | |
803 | if (new_devdata) { | |
804 | dev_info(old_devdata->dev, "%s: device disabled\n", __func__); | |
805 | nx842_OF_set_defaults(new_devdata); | |
806 | rcu_assign_pointer(devdata, new_devdata); | |
807 | spin_unlock_irqrestore(&devdata_mutex, flags); | |
808 | synchronize_rcu(); | |
809 | dev_set_drvdata(new_devdata->dev, new_devdata); | |
810 | kfree(old_devdata); | |
811 | } else { | |
812 | dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__); | |
813 | spin_unlock_irqrestore(&devdata_mutex, flags); | |
814 | } | |
815 | ||
816 | if (!ret) | |
817 | ret = -EINVAL; | |
818 | return ret; | |
819 | } | |
820 | ||
821 | /** | |
822 | * nx842_OF_notifier - Process updates to OF properties for the device | |
823 | * | |
824 | * @np: notifier block | |
825 | * @action: notifier action | |
826 | * @update: struct pSeries_reconfig_prop_update pointer if action is | |
827 | * PSERIES_UPDATE_PROPERTY | |
828 | * | |
829 | * Returns: | |
830 | * NOTIFY_OK on success | |
831 | * NOTIFY_BAD encoded with error number on failure, use | |
832 | * notifier_to_errno() to decode this value | |
833 | */ | |
1cf3d8b3 | 834 | static int nx842_OF_notifier(struct notifier_block *np, unsigned long action, |
f5242e5a | 835 | void *data) |
0e16aafb | 836 | { |
f5242e5a | 837 | struct of_reconfig_data *upd = data; |
0e16aafb SJ |
838 | struct nx842_devdata *local_devdata; |
839 | struct device_node *node = NULL; | |
840 | ||
0e16aafb SJ |
841 | rcu_read_lock(); |
842 | local_devdata = rcu_dereference(devdata); | |
843 | if (local_devdata) | |
844 | node = local_devdata->dev->of_node; | |
845 | ||
846 | if (local_devdata && | |
1cf3d8b3 NF |
847 | action == OF_RECONFIG_UPDATE_PROPERTY && |
848 | !strcmp(upd->dn->name, node->name)) { | |
0e16aafb | 849 | rcu_read_unlock(); |
1cf3d8b3 | 850 | nx842_OF_upd(upd->prop); |
0e16aafb SJ |
851 | } else |
852 | rcu_read_unlock(); | |
853 | ||
854 | return NOTIFY_OK; | |
855 | } | |
856 | ||
857 | static struct notifier_block nx842_of_nb = { | |
858 | .notifier_call = nx842_OF_notifier, | |
859 | }; | |
860 | ||
861 | #define nx842_counter_read(_name) \ | |
862 | static ssize_t nx842_##_name##_show(struct device *dev, \ | |
863 | struct device_attribute *attr, \ | |
864 | char *buf) { \ | |
865 | struct nx842_devdata *local_devdata; \ | |
866 | int p = 0; \ | |
867 | rcu_read_lock(); \ | |
868 | local_devdata = rcu_dereference(devdata); \ | |
869 | if (local_devdata) \ | |
870 | p = snprintf(buf, PAGE_SIZE, "%ld\n", \ | |
871 | atomic64_read(&local_devdata->counters->_name)); \ | |
872 | rcu_read_unlock(); \ | |
873 | return p; \ | |
874 | } | |
875 | ||
876 | #define NX842DEV_COUNTER_ATTR_RO(_name) \ | |
877 | nx842_counter_read(_name); \ | |
878 | static struct device_attribute dev_attr_##_name = __ATTR(_name, \ | |
879 | 0444, \ | |
880 | nx842_##_name##_show,\ | |
881 | NULL); | |
882 | ||
883 | NX842DEV_COUNTER_ATTR_RO(comp_complete); | |
884 | NX842DEV_COUNTER_ATTR_RO(comp_failed); | |
885 | NX842DEV_COUNTER_ATTR_RO(decomp_complete); | |
886 | NX842DEV_COUNTER_ATTR_RO(decomp_failed); | |
887 | NX842DEV_COUNTER_ATTR_RO(swdecomp); | |
888 | ||
889 | static ssize_t nx842_timehist_show(struct device *, | |
890 | struct device_attribute *, char *); | |
891 | ||
892 | static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444, | |
893 | nx842_timehist_show, NULL); | |
894 | static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times, | |
895 | 0444, nx842_timehist_show, NULL); | |
896 | ||
897 | static ssize_t nx842_timehist_show(struct device *dev, | |
898 | struct device_attribute *attr, char *buf) { | |
899 | char *p = buf; | |
900 | struct nx842_devdata *local_devdata; | |
901 | atomic64_t *times; | |
902 | int bytes_remain = PAGE_SIZE; | |
903 | int bytes; | |
904 | int i; | |
905 | ||
906 | rcu_read_lock(); | |
907 | local_devdata = rcu_dereference(devdata); | |
908 | if (!local_devdata) { | |
909 | rcu_read_unlock(); | |
910 | return 0; | |
911 | } | |
912 | ||
913 | if (attr == &dev_attr_comp_times) | |
914 | times = local_devdata->counters->comp_times; | |
915 | else if (attr == &dev_attr_decomp_times) | |
916 | times = local_devdata->counters->decomp_times; | |
917 | else { | |
918 | rcu_read_unlock(); | |
919 | return 0; | |
920 | } | |
921 | ||
922 | for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) { | |
923 | bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n", | |
924 | i ? (2<<(i-1)) : 0, (2<<i)-1, | |
925 | atomic64_read(×[i])); | |
926 | bytes_remain -= bytes; | |
927 | p += bytes; | |
928 | } | |
929 | /* The last bucket holds everything over | |
930 | * 2<<(NX842_HIST_SLOTS - 2) us */ | |
931 | bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n", | |
932 | 2<<(NX842_HIST_SLOTS - 2), | |
933 | atomic64_read(×[(NX842_HIST_SLOTS - 1)])); | |
934 | p += bytes; | |
935 | ||
936 | rcu_read_unlock(); | |
937 | return p - buf; | |
938 | } | |
939 | ||
940 | static struct attribute *nx842_sysfs_entries[] = { | |
941 | &dev_attr_comp_complete.attr, | |
942 | &dev_attr_comp_failed.attr, | |
943 | &dev_attr_decomp_complete.attr, | |
944 | &dev_attr_decomp_failed.attr, | |
945 | &dev_attr_swdecomp.attr, | |
946 | &dev_attr_comp_times.attr, | |
947 | &dev_attr_decomp_times.attr, | |
948 | NULL, | |
949 | }; | |
950 | ||
951 | static struct attribute_group nx842_attribute_group = { | |
952 | .name = NULL, /* put in device directory */ | |
953 | .attrs = nx842_sysfs_entries, | |
954 | }; | |
955 | ||
7011a122 | 956 | static struct nx842_driver nx842_pseries_driver = { |
3e648cbe | 957 | .name = KBUILD_MODNAME, |
7011a122 | 958 | .owner = THIS_MODULE, |
2c6f6eab | 959 | .workmem_size = sizeof(struct nx842_workmem), |
959e6659 | 960 | .constraints = &nx842_pseries_constraints, |
7011a122 DS |
961 | .compress = nx842_pseries_compress, |
962 | .decompress = nx842_pseries_decompress, | |
963 | }; | |
964 | ||
03952d98 DS |
965 | static int nx842_pseries_crypto_init(struct crypto_tfm *tfm) |
966 | { | |
967 | return nx842_crypto_init(tfm, &nx842_pseries_driver); | |
968 | } | |
969 | ||
970 | static struct crypto_alg nx842_pseries_alg = { | |
971 | .cra_name = "842", | |
972 | .cra_driver_name = "842-nx", | |
973 | .cra_priority = 300, | |
974 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, | |
975 | .cra_ctxsize = sizeof(struct nx842_crypto_ctx), | |
976 | .cra_module = THIS_MODULE, | |
977 | .cra_init = nx842_pseries_crypto_init, | |
978 | .cra_exit = nx842_crypto_exit, | |
979 | .cra_u = { .compress = { | |
980 | .coa_compress = nx842_crypto_compress, | |
981 | .coa_decompress = nx842_crypto_decompress } } | |
982 | }; | |
983 | ||
039af967 DS |
984 | static int nx842_probe(struct vio_dev *viodev, |
985 | const struct vio_device_id *id) | |
0e16aafb SJ |
986 | { |
987 | struct nx842_devdata *old_devdata, *new_devdata = NULL; | |
988 | unsigned long flags; | |
989 | int ret = 0; | |
990 | ||
7f6e3aad DS |
991 | new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS); |
992 | if (!new_devdata) | |
993 | return -ENOMEM; | |
994 | ||
995 | new_devdata->counters = kzalloc(sizeof(*new_devdata->counters), | |
996 | GFP_NOFS); | |
997 | if (!new_devdata->counters) { | |
998 | kfree(new_devdata); | |
999 | return -ENOMEM; | |
1000 | } | |
1001 | ||
0e16aafb SJ |
1002 | spin_lock_irqsave(&devdata_mutex, flags); |
1003 | old_devdata = rcu_dereference_check(devdata, | |
1004 | lockdep_is_held(&devdata_mutex)); | |
1005 | ||
1006 | if (old_devdata && old_devdata->vdev != NULL) { | |
1007 | dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__); | |
1008 | ret = -1; | |
1009 | goto error_unlock; | |
1010 | } | |
1011 | ||
1012 | dev_set_drvdata(&viodev->dev, NULL); | |
1013 | ||
0e16aafb SJ |
1014 | new_devdata->vdev = viodev; |
1015 | new_devdata->dev = &viodev->dev; | |
1016 | nx842_OF_set_defaults(new_devdata); | |
1017 | ||
1018 | rcu_assign_pointer(devdata, new_devdata); | |
1019 | spin_unlock_irqrestore(&devdata_mutex, flags); | |
1020 | synchronize_rcu(); | |
1021 | kfree(old_devdata); | |
1022 | ||
1cf3d8b3 | 1023 | of_reconfig_notifier_register(&nx842_of_nb); |
0e16aafb SJ |
1024 | |
1025 | ret = nx842_OF_upd(NULL); | |
ee781b7f | 1026 | if (ret) |
0e16aafb | 1027 | goto error; |
0e16aafb | 1028 | |
03952d98 DS |
1029 | ret = crypto_register_alg(&nx842_pseries_alg); |
1030 | if (ret) { | |
1031 | dev_err(&viodev->dev, "could not register comp alg: %d\n", ret); | |
1032 | goto error; | |
1033 | } | |
1034 | ||
0e16aafb | 1035 | rcu_read_lock(); |
cda43576 | 1036 | dev_set_drvdata(&viodev->dev, rcu_dereference(devdata)); |
0e16aafb SJ |
1037 | rcu_read_unlock(); |
1038 | ||
1039 | if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) { | |
1040 | dev_err(&viodev->dev, "could not create sysfs device attributes\n"); | |
1041 | ret = -1; | |
1042 | goto error; | |
1043 | } | |
1044 | ||
1045 | return 0; | |
1046 | ||
1047 | error_unlock: | |
1048 | spin_unlock_irqrestore(&devdata_mutex, flags); | |
1049 | if (new_devdata) | |
1050 | kfree(new_devdata->counters); | |
1051 | kfree(new_devdata); | |
1052 | error: | |
1053 | return ret; | |
1054 | } | |
1055 | ||
039af967 | 1056 | static int nx842_remove(struct vio_dev *viodev) |
0e16aafb SJ |
1057 | { |
1058 | struct nx842_devdata *old_devdata; | |
1059 | unsigned long flags; | |
1060 | ||
1061 | pr_info("Removing IBM Power 842 compression device\n"); | |
1062 | sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group); | |
1063 | ||
03952d98 DS |
1064 | crypto_unregister_alg(&nx842_pseries_alg); |
1065 | ||
0e16aafb SJ |
1066 | spin_lock_irqsave(&devdata_mutex, flags); |
1067 | old_devdata = rcu_dereference_check(devdata, | |
1068 | lockdep_is_held(&devdata_mutex)); | |
1cf3d8b3 | 1069 | of_reconfig_notifier_unregister(&nx842_of_nb); |
7ded6e3d | 1070 | RCU_INIT_POINTER(devdata, NULL); |
0e16aafb SJ |
1071 | spin_unlock_irqrestore(&devdata_mutex, flags); |
1072 | synchronize_rcu(); | |
1073 | dev_set_drvdata(&viodev->dev, NULL); | |
1074 | if (old_devdata) | |
1075 | kfree(old_devdata->counters); | |
1076 | kfree(old_devdata); | |
7011a122 | 1077 | |
0e16aafb SJ |
1078 | return 0; |
1079 | } | |
1080 | ||
b8e04187 | 1081 | static struct vio_device_id nx842_vio_driver_ids[] = { |
3e648cbe | 1082 | {"ibm,compression-v1", "ibm,compression"}, |
0e16aafb SJ |
1083 | {"", ""}, |
1084 | }; | |
1085 | ||
b8e04187 | 1086 | static struct vio_driver nx842_vio_driver = { |
3e648cbe | 1087 | .name = KBUILD_MODNAME, |
0e16aafb | 1088 | .probe = nx842_probe, |
039af967 | 1089 | .remove = nx842_remove, |
0e16aafb | 1090 | .get_desired_dma = nx842_get_desired_dma, |
b8e04187 | 1091 | .id_table = nx842_vio_driver_ids, |
0e16aafb SJ |
1092 | }; |
1093 | ||
ec13bcbe | 1094 | static int __init nx842_pseries_init(void) |
0e16aafb SJ |
1095 | { |
1096 | struct nx842_devdata *new_devdata; | |
3e648cbe DS |
1097 | int ret; |
1098 | ||
3e648cbe DS |
1099 | if (!of_find_compatible_node(NULL, NULL, "ibm,compression")) |
1100 | return -ENODEV; | |
1101 | ||
0e16aafb SJ |
1102 | RCU_INIT_POINTER(devdata, NULL); |
1103 | new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL); | |
1104 | if (!new_devdata) { | |
1105 | pr_err("Could not allocate memory for device data\n"); | |
1106 | return -ENOMEM; | |
1107 | } | |
0e16aafb SJ |
1108 | RCU_INIT_POINTER(devdata, new_devdata); |
1109 | ||
3e648cbe DS |
1110 | ret = vio_register_driver(&nx842_vio_driver); |
1111 | if (ret) { | |
1112 | pr_err("Could not register VIO driver %d\n", ret); | |
1113 | ||
1114 | kfree(new_devdata); | |
1115 | return ret; | |
1116 | } | |
1117 | ||
3e648cbe | 1118 | return 0; |
0e16aafb SJ |
1119 | } |
1120 | ||
ec13bcbe | 1121 | module_init(nx842_pseries_init); |
0e16aafb | 1122 | |
ec13bcbe | 1123 | static void __exit nx842_pseries_exit(void) |
0e16aafb SJ |
1124 | { |
1125 | struct nx842_devdata *old_devdata; | |
1126 | unsigned long flags; | |
1127 | ||
03952d98 DS |
1128 | crypto_unregister_alg(&nx842_pseries_alg); |
1129 | ||
0e16aafb SJ |
1130 | spin_lock_irqsave(&devdata_mutex, flags); |
1131 | old_devdata = rcu_dereference_check(devdata, | |
1132 | lockdep_is_held(&devdata_mutex)); | |
7ded6e3d | 1133 | RCU_INIT_POINTER(devdata, NULL); |
0e16aafb SJ |
1134 | spin_unlock_irqrestore(&devdata_mutex, flags); |
1135 | synchronize_rcu(); | |
b8e04187 | 1136 | if (old_devdata && old_devdata->dev) |
0e16aafb SJ |
1137 | dev_set_drvdata(old_devdata->dev, NULL); |
1138 | kfree(old_devdata); | |
b8e04187 | 1139 | vio_unregister_driver(&nx842_vio_driver); |
0e16aafb SJ |
1140 | } |
1141 | ||
ec13bcbe | 1142 | module_exit(nx842_pseries_exit); |
0e16aafb | 1143 |