nx-842-pseries.c 32 KB

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