nx-842.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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 <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/nx842.h>
  26. #include <linux/of.h>
  27. #include <linux/slab.h>
  28. #include <asm/page.h>
  29. #include <asm/vio.h>
  30. #include "nx_csbcpb.h" /* struct nx_csbcpb */
  31. #define MODULE_NAME "nx-compress"
  32. MODULE_LICENSE("GPL");
  33. MODULE_AUTHOR("Robert Jennings <rcj@linux.vnet.ibm.com>");
  34. MODULE_DESCRIPTION("842 H/W Compression driver for IBM Power processors");
  35. #define SHIFT_4K 12
  36. #define SHIFT_64K 16
  37. #define SIZE_4K (1UL << SHIFT_4K)
  38. #define SIZE_64K (1UL << SHIFT_64K)
  39. /* IO buffer must be 128 byte aligned */
  40. #define IO_BUFFER_ALIGN 128
  41. struct nx842_header {
  42. int blocks_nr; /* number of compressed blocks */
  43. int offset; /* offset of the first block (from beginning of header) */
  44. int sizes[0]; /* size of compressed blocks */
  45. };
  46. static inline int nx842_header_size(const struct nx842_header *hdr)
  47. {
  48. return sizeof(struct nx842_header) +
  49. hdr->blocks_nr * sizeof(hdr->sizes[0]);
  50. }
  51. /* Macros for fields within nx_csbcpb */
  52. /* Check the valid bit within the csbcpb valid field */
  53. #define NX842_CSBCBP_VALID_CHK(x) (x & BIT_MASK(7))
  54. /* CE macros operate on the completion_extension field bits in the csbcpb.
  55. * CE0 0=full completion, 1=partial completion
  56. * CE1 0=CE0 indicates completion, 1=termination (output may be modified)
  57. * CE2 0=processed_bytes is source bytes, 1=processed_bytes is target bytes */
  58. #define NX842_CSBCPB_CE0(x) (x & BIT_MASK(7))
  59. #define NX842_CSBCPB_CE1(x) (x & BIT_MASK(6))
  60. #define NX842_CSBCPB_CE2(x) (x & BIT_MASK(5))
  61. /* The NX unit accepts data only on 4K page boundaries */
  62. #define NX842_HW_PAGE_SHIFT SHIFT_4K
  63. #define NX842_HW_PAGE_SIZE (ASM_CONST(1) << NX842_HW_PAGE_SHIFT)
  64. #define NX842_HW_PAGE_MASK (~(NX842_HW_PAGE_SIZE-1))
  65. enum nx842_status {
  66. UNAVAILABLE,
  67. AVAILABLE
  68. };
  69. struct ibm_nx842_counters {
  70. atomic64_t comp_complete;
  71. atomic64_t comp_failed;
  72. atomic64_t decomp_complete;
  73. atomic64_t decomp_failed;
  74. atomic64_t swdecomp;
  75. atomic64_t comp_times[32];
  76. atomic64_t decomp_times[32];
  77. };
  78. static struct nx842_devdata {
  79. struct vio_dev *vdev;
  80. struct device *dev;
  81. struct ibm_nx842_counters *counters;
  82. unsigned int max_sg_len;
  83. unsigned int max_sync_size;
  84. unsigned int max_sync_sg;
  85. enum nx842_status status;
  86. } __rcu *devdata;
  87. static DEFINE_SPINLOCK(devdata_mutex);
  88. #define NX842_COUNTER_INC(_x) \
  89. static inline void nx842_inc_##_x( \
  90. const struct nx842_devdata *dev) { \
  91. if (dev) \
  92. atomic64_inc(&dev->counters->_x); \
  93. }
  94. NX842_COUNTER_INC(comp_complete);
  95. NX842_COUNTER_INC(comp_failed);
  96. NX842_COUNTER_INC(decomp_complete);
  97. NX842_COUNTER_INC(decomp_failed);
  98. NX842_COUNTER_INC(swdecomp);
  99. #define NX842_HIST_SLOTS 16
  100. static void ibm_nx842_incr_hist(atomic64_t *times, unsigned int time)
  101. {
  102. int bucket = fls(time);
  103. if (bucket)
  104. bucket = min((NX842_HIST_SLOTS - 1), bucket - 1);
  105. atomic64_inc(&times[bucket]);
  106. }
  107. /* NX unit operation flags */
  108. #define NX842_OP_COMPRESS 0x0
  109. #define NX842_OP_CRC 0x1
  110. #define NX842_OP_DECOMPRESS 0x2
  111. #define NX842_OP_COMPRESS_CRC (NX842_OP_COMPRESS | NX842_OP_CRC)
  112. #define NX842_OP_DECOMPRESS_CRC (NX842_OP_DECOMPRESS | NX842_OP_CRC)
  113. #define NX842_OP_ASYNC (1<<23)
  114. #define NX842_OP_NOTIFY (1<<22)
  115. #define NX842_OP_NOTIFY_INT(x) ((x & 0xff)<<8)
  116. static unsigned long nx842_get_desired_dma(struct vio_dev *viodev)
  117. {
  118. /* No use of DMA mappings within the driver. */
  119. return 0;
  120. }
  121. struct nx842_slentry {
  122. unsigned long ptr; /* Real address (use __pa()) */
  123. unsigned long len;
  124. };
  125. /* pHyp scatterlist entry */
  126. struct nx842_scatterlist {
  127. int entry_nr; /* number of slentries */
  128. struct nx842_slentry *entries; /* ptr to array of slentries */
  129. };
  130. /* Does not include sizeof(entry_nr) in the size */
  131. static inline unsigned long nx842_get_scatterlist_size(
  132. struct nx842_scatterlist *sl)
  133. {
  134. return sl->entry_nr * sizeof(struct nx842_slentry);
  135. }
  136. static inline unsigned long nx842_get_pa(void *addr)
  137. {
  138. if (is_vmalloc_addr(addr))
  139. return page_to_phys(vmalloc_to_page(addr))
  140. + offset_in_page(addr);
  141. else
  142. return __pa(addr);
  143. }
  144. static int nx842_build_scatterlist(unsigned long buf, int len,
  145. struct nx842_scatterlist *sl)
  146. {
  147. unsigned long nextpage;
  148. struct nx842_slentry *entry;
  149. sl->entry_nr = 0;
  150. entry = sl->entries;
  151. while (len) {
  152. entry->ptr = nx842_get_pa((void *)buf);
  153. nextpage = ALIGN(buf + 1, NX842_HW_PAGE_SIZE);
  154. if (nextpage < buf + len) {
  155. /* we aren't at the end yet */
  156. if (IS_ALIGNED(buf, NX842_HW_PAGE_SIZE))
  157. /* we are in the middle (or beginning) */
  158. entry->len = NX842_HW_PAGE_SIZE;
  159. else
  160. /* we are at the beginning */
  161. entry->len = nextpage - buf;
  162. } else {
  163. /* at the end */
  164. entry->len = len;
  165. }
  166. len -= entry->len;
  167. buf += entry->len;
  168. sl->entry_nr++;
  169. entry++;
  170. }
  171. return 0;
  172. }
  173. /*
  174. * Working memory for software decompression
  175. */
  176. struct sw842_fifo {
  177. union {
  178. char f8[256][8];
  179. char f4[512][4];
  180. };
  181. char f2[256][2];
  182. unsigned char f84_full;
  183. unsigned char f2_full;
  184. unsigned char f8_count;
  185. unsigned char f2_count;
  186. unsigned int f4_count;
  187. };
  188. /*
  189. * Working memory for crypto API
  190. */
  191. struct nx842_workmem {
  192. char bounce[PAGE_SIZE]; /* bounce buffer for decompression input */
  193. union {
  194. /* hardware working memory */
  195. struct {
  196. /* scatterlist */
  197. char slin[SIZE_4K];
  198. char slout[SIZE_4K];
  199. /* coprocessor status/parameter block */
  200. struct nx_csbcpb csbcpb;
  201. };
  202. /* software working memory */
  203. struct sw842_fifo swfifo; /* software decompression fifo */
  204. };
  205. };
  206. int nx842_get_workmem_size(void)
  207. {
  208. return sizeof(struct nx842_workmem) + NX842_HW_PAGE_SIZE;
  209. }
  210. EXPORT_SYMBOL_GPL(nx842_get_workmem_size);
  211. int nx842_get_workmem_size_aligned(void)
  212. {
  213. return sizeof(struct nx842_workmem);
  214. }
  215. EXPORT_SYMBOL_GPL(nx842_get_workmem_size_aligned);
  216. static int nx842_validate_result(struct device *dev,
  217. struct cop_status_block *csb)
  218. {
  219. /* The csb must be valid after returning from vio_h_cop_sync */
  220. if (!NX842_CSBCBP_VALID_CHK(csb->valid)) {
  221. dev_err(dev, "%s: cspcbp not valid upon completion.\n",
  222. __func__);
  223. dev_dbg(dev, "valid:0x%02x cs:0x%02x cc:0x%02x ce:0x%02x\n",
  224. csb->valid,
  225. csb->crb_seq_number,
  226. csb->completion_code,
  227. csb->completion_extension);
  228. dev_dbg(dev, "processed_bytes:%d address:0x%016lx\n",
  229. csb->processed_byte_count,
  230. (unsigned long)csb->address);
  231. return -EIO;
  232. }
  233. /* Check return values from the hardware in the CSB */
  234. switch (csb->completion_code) {
  235. case 0: /* Completed without error */
  236. break;
  237. case 64: /* Target bytes > Source bytes during compression */
  238. case 13: /* Output buffer too small */
  239. dev_dbg(dev, "%s: Compression output larger than input\n",
  240. __func__);
  241. return -ENOSPC;
  242. case 66: /* Input data contains an illegal template field */
  243. case 67: /* Template indicates data past the end of the input stream */
  244. dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
  245. __func__, csb->completion_code);
  246. return -EINVAL;
  247. default:
  248. dev_dbg(dev, "%s: Unspecified error (code:%d)\n",
  249. __func__, csb->completion_code);
  250. return -EIO;
  251. }
  252. /* Hardware sanity check */
  253. if (!NX842_CSBCPB_CE2(csb->completion_extension)) {
  254. dev_err(dev, "%s: No error returned by hardware, but "
  255. "data returned is unusable, contact support.\n"
  256. "(Additional info: csbcbp->processed bytes "
  257. "does not specify processed bytes for the "
  258. "target buffer.)\n", __func__);
  259. return -EIO;
  260. }
  261. return 0;
  262. }
  263. /**
  264. * nx842_compress - Compress data using the 842 algorithm
  265. *
  266. * Compression provide by the NX842 coprocessor on IBM Power systems.
  267. * The input buffer is compressed and the result is stored in the
  268. * provided output buffer.
  269. *
  270. * Upon return from this function @outlen contains the length of the
  271. * compressed data. If there is an error then @outlen will be 0 and an
  272. * error will be specified by the return code from this function.
  273. *
  274. * @in: Pointer to input buffer, must be page aligned
  275. * @inlen: Length of input buffer, must be PAGE_SIZE
  276. * @out: Pointer to output buffer
  277. * @outlen: Length of output buffer
  278. * @wrkmem: ptr to buffer for working memory, size determined by
  279. * nx842_get_workmem_size()
  280. *
  281. * Returns:
  282. * 0 Success, output of length @outlen stored in the buffer at @out
  283. * -ENOMEM Unable to allocate internal buffers
  284. * -ENOSPC Output buffer is to small
  285. * -EMSGSIZE XXX Difficult to describe this limitation
  286. * -EIO Internal error
  287. * -ENODEV Hardware unavailable
  288. */
  289. int nx842_compress(const unsigned char *in, unsigned int inlen,
  290. unsigned char *out, unsigned int *outlen, void *wmem)
  291. {
  292. struct nx842_header *hdr;
  293. struct nx842_devdata *local_devdata;
  294. struct device *dev = NULL;
  295. struct nx842_workmem *workmem;
  296. struct nx842_scatterlist slin, slout;
  297. struct nx_csbcpb *csbcpb;
  298. int ret = 0, max_sync_size, i, bytesleft, size, hdrsize;
  299. unsigned long inbuf, outbuf, padding;
  300. struct vio_pfo_op op = {
  301. .done = NULL,
  302. .handle = 0,
  303. .timeout = 0,
  304. };
  305. unsigned long start_time = get_tb();
  306. /*
  307. * Make sure input buffer is 64k page aligned. This is assumed since
  308. * this driver is designed for page compression only (for now). This
  309. * is very nice since we can now use direct DDE(s) for the input and
  310. * the alignment is guaranteed.
  311. */
  312. inbuf = (unsigned long)in;
  313. if (!IS_ALIGNED(inbuf, PAGE_SIZE) || inlen != PAGE_SIZE)
  314. return -EINVAL;
  315. rcu_read_lock();
  316. local_devdata = rcu_dereference(devdata);
  317. if (!local_devdata || !local_devdata->dev) {
  318. rcu_read_unlock();
  319. return -ENODEV;
  320. }
  321. max_sync_size = local_devdata->max_sync_size;
  322. dev = local_devdata->dev;
  323. /* Create the header */
  324. hdr = (struct nx842_header *)out;
  325. hdr->blocks_nr = PAGE_SIZE / max_sync_size;
  326. hdrsize = nx842_header_size(hdr);
  327. outbuf = (unsigned long)out + hdrsize;
  328. bytesleft = *outlen - hdrsize;
  329. /* Init scatterlist */
  330. workmem = (struct nx842_workmem *)ALIGN((unsigned long)wmem,
  331. NX842_HW_PAGE_SIZE);
  332. slin.entries = (struct nx842_slentry *)workmem->slin;
  333. slout.entries = (struct nx842_slentry *)workmem->slout;
  334. /* Init operation */
  335. op.flags = NX842_OP_COMPRESS;
  336. csbcpb = &workmem->csbcpb;
  337. memset(csbcpb, 0, sizeof(*csbcpb));
  338. op.csbcpb = nx842_get_pa(csbcpb);
  339. op.out = nx842_get_pa(slout.entries);
  340. for (i = 0; i < hdr->blocks_nr; i++) {
  341. /*
  342. * Aligning the output blocks to 128 bytes does waste space,
  343. * but it prevents the need for bounce buffers and memory
  344. * copies. It also simplifies the code a lot. In the worst
  345. * case (64k page, 4k max_sync_size), you lose up to
  346. * (128*16)/64k = ~3% the compression factor. For 64k
  347. * max_sync_size, the loss would be at most 128/64k = ~0.2%.
  348. */
  349. padding = ALIGN(outbuf, IO_BUFFER_ALIGN) - outbuf;
  350. outbuf += padding;
  351. bytesleft -= padding;
  352. if (i == 0)
  353. /* save offset into first block in header */
  354. hdr->offset = padding + hdrsize;
  355. if (bytesleft <= 0) {
  356. ret = -ENOSPC;
  357. goto unlock;
  358. }
  359. /*
  360. * NOTE: If the default max_sync_size is changed from 4k
  361. * to 64k, remove the "likely" case below, since a
  362. * scatterlist will always be needed.
  363. */
  364. if (likely(max_sync_size == NX842_HW_PAGE_SIZE)) {
  365. /* Create direct DDE */
  366. op.in = nx842_get_pa((void *)inbuf);
  367. op.inlen = max_sync_size;
  368. } else {
  369. /* Create indirect DDE (scatterlist) */
  370. nx842_build_scatterlist(inbuf, max_sync_size, &slin);
  371. op.in = nx842_get_pa(slin.entries);
  372. op.inlen = -nx842_get_scatterlist_size(&slin);
  373. }
  374. /*
  375. * If max_sync_size != NX842_HW_PAGE_SIZE, an indirect
  376. * DDE is required for the outbuf.
  377. * If max_sync_size == NX842_HW_PAGE_SIZE, outbuf must
  378. * also be page aligned (1 in 128/4k=32 chance) in order
  379. * to use a direct DDE.
  380. * This is unlikely, just use an indirect DDE always.
  381. */
  382. nx842_build_scatterlist(outbuf,
  383. min(bytesleft, max_sync_size), &slout);
  384. /* op.out set before loop */
  385. op.outlen = -nx842_get_scatterlist_size(&slout);
  386. /* Send request to pHyp */
  387. ret = vio_h_cop_sync(local_devdata->vdev, &op);
  388. /* Check for pHyp error */
  389. if (ret) {
  390. dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
  391. __func__, ret, op.hcall_err);
  392. ret = -EIO;
  393. goto unlock;
  394. }
  395. /* Check for hardware error */
  396. ret = nx842_validate_result(dev, &csbcpb->csb);
  397. if (ret && ret != -ENOSPC)
  398. goto unlock;
  399. /* Handle incompressible data */
  400. if (unlikely(ret == -ENOSPC)) {
  401. if (bytesleft < max_sync_size) {
  402. /*
  403. * Not enough space left in the output buffer
  404. * to store uncompressed block
  405. */
  406. goto unlock;
  407. } else {
  408. /* Store incompressible block */
  409. memcpy((void *)outbuf, (void *)inbuf,
  410. max_sync_size);
  411. hdr->sizes[i] = -max_sync_size;
  412. outbuf += max_sync_size;
  413. bytesleft -= max_sync_size;
  414. /* Reset ret, incompressible data handled */
  415. ret = 0;
  416. }
  417. } else {
  418. /* Normal case, compression was successful */
  419. size = csbcpb->csb.processed_byte_count;
  420. dev_dbg(dev, "%s: processed_bytes=%d\n",
  421. __func__, size);
  422. hdr->sizes[i] = size;
  423. outbuf += size;
  424. bytesleft -= size;
  425. }
  426. inbuf += max_sync_size;
  427. }
  428. *outlen = (unsigned int)(outbuf - (unsigned long)out);
  429. unlock:
  430. if (ret)
  431. nx842_inc_comp_failed(local_devdata);
  432. else {
  433. nx842_inc_comp_complete(local_devdata);
  434. ibm_nx842_incr_hist(local_devdata->counters->comp_times,
  435. (get_tb() - start_time) / tb_ticks_per_usec);
  436. }
  437. rcu_read_unlock();
  438. return ret;
  439. }
  440. EXPORT_SYMBOL_GPL(nx842_compress);
  441. static int sw842_decompress(const unsigned char *, int, unsigned char *, int *,
  442. const void *);
  443. /**
  444. * nx842_decompress - Decompress data using the 842 algorithm
  445. *
  446. * Decompression provide by the NX842 coprocessor on IBM Power systems.
  447. * The input buffer is decompressed and the result is stored in the
  448. * provided output buffer. The size allocated to the output buffer is
  449. * provided by the caller of this function in @outlen. Upon return from
  450. * this function @outlen contains the length of the decompressed data.
  451. * If there is an error then @outlen will be 0 and an error will be
  452. * specified by the return code from this function.
  453. *
  454. * @in: Pointer to input buffer, will use bounce buffer if not 128 byte
  455. * aligned
  456. * @inlen: Length of input buffer
  457. * @out: Pointer to output buffer, must be page aligned
  458. * @outlen: Length of output buffer, must be PAGE_SIZE
  459. * @wrkmem: ptr to buffer for working memory, size determined by
  460. * nx842_get_workmem_size()
  461. *
  462. * Returns:
  463. * 0 Success, output of length @outlen stored in the buffer at @out
  464. * -ENODEV Hardware decompression device is unavailable
  465. * -ENOMEM Unable to allocate internal buffers
  466. * -ENOSPC Output buffer is to small
  467. * -EINVAL Bad input data encountered when attempting decompress
  468. * -EIO Internal error
  469. */
  470. int nx842_decompress(const unsigned char *in, unsigned int inlen,
  471. unsigned char *out, unsigned int *outlen, void *wmem)
  472. {
  473. struct nx842_header *hdr;
  474. struct nx842_devdata *local_devdata;
  475. struct device *dev = NULL;
  476. struct nx842_workmem *workmem;
  477. struct nx842_scatterlist slin, slout;
  478. struct nx_csbcpb *csbcpb;
  479. int ret = 0, i, size, max_sync_size;
  480. unsigned long inbuf, outbuf;
  481. struct vio_pfo_op op = {
  482. .done = NULL,
  483. .handle = 0,
  484. .timeout = 0,
  485. };
  486. unsigned long start_time = get_tb();
  487. /* Ensure page alignment and size */
  488. outbuf = (unsigned long)out;
  489. if (!IS_ALIGNED(outbuf, PAGE_SIZE) || *outlen != PAGE_SIZE)
  490. return -EINVAL;
  491. rcu_read_lock();
  492. local_devdata = rcu_dereference(devdata);
  493. if (local_devdata)
  494. dev = local_devdata->dev;
  495. /* Get header */
  496. hdr = (struct nx842_header *)in;
  497. workmem = (struct nx842_workmem *)ALIGN((unsigned long)wmem,
  498. NX842_HW_PAGE_SIZE);
  499. inbuf = (unsigned long)in + hdr->offset;
  500. if (likely(!IS_ALIGNED(inbuf, IO_BUFFER_ALIGN))) {
  501. /* Copy block(s) into bounce buffer for alignment */
  502. memcpy(workmem->bounce, in + hdr->offset, inlen - hdr->offset);
  503. inbuf = (unsigned long)workmem->bounce;
  504. }
  505. /* Init scatterlist */
  506. slin.entries = (struct nx842_slentry *)workmem->slin;
  507. slout.entries = (struct nx842_slentry *)workmem->slout;
  508. /* Init operation */
  509. op.flags = NX842_OP_DECOMPRESS;
  510. csbcpb = &workmem->csbcpb;
  511. memset(csbcpb, 0, sizeof(*csbcpb));
  512. op.csbcpb = nx842_get_pa(csbcpb);
  513. /*
  514. * max_sync_size may have changed since compression,
  515. * so we can't read it from the device info. We need
  516. * to derive it from hdr->blocks_nr.
  517. */
  518. max_sync_size = PAGE_SIZE / hdr->blocks_nr;
  519. for (i = 0; i < hdr->blocks_nr; i++) {
  520. /* Skip padding */
  521. inbuf = ALIGN(inbuf, IO_BUFFER_ALIGN);
  522. if (hdr->sizes[i] < 0) {
  523. /* Negative sizes indicate uncompressed data blocks */
  524. size = abs(hdr->sizes[i]);
  525. memcpy((void *)outbuf, (void *)inbuf, size);
  526. outbuf += size;
  527. inbuf += size;
  528. continue;
  529. }
  530. if (!dev)
  531. goto sw;
  532. /*
  533. * The better the compression, the more likely the "likely"
  534. * case becomes.
  535. */
  536. if (likely((inbuf & NX842_HW_PAGE_MASK) ==
  537. ((inbuf + hdr->sizes[i] - 1) & NX842_HW_PAGE_MASK))) {
  538. /* Create direct DDE */
  539. op.in = nx842_get_pa((void *)inbuf);
  540. op.inlen = hdr->sizes[i];
  541. } else {
  542. /* Create indirect DDE (scatterlist) */
  543. nx842_build_scatterlist(inbuf, hdr->sizes[i] , &slin);
  544. op.in = nx842_get_pa(slin.entries);
  545. op.inlen = -nx842_get_scatterlist_size(&slin);
  546. }
  547. /*
  548. * NOTE: If the default max_sync_size is changed from 4k
  549. * to 64k, remove the "likely" case below, since a
  550. * scatterlist will always be needed.
  551. */
  552. if (likely(max_sync_size == NX842_HW_PAGE_SIZE)) {
  553. /* Create direct DDE */
  554. op.out = nx842_get_pa((void *)outbuf);
  555. op.outlen = max_sync_size;
  556. } else {
  557. /* Create indirect DDE (scatterlist) */
  558. nx842_build_scatterlist(outbuf, max_sync_size, &slout);
  559. op.out = nx842_get_pa(slout.entries);
  560. op.outlen = -nx842_get_scatterlist_size(&slout);
  561. }
  562. /* Send request to pHyp */
  563. ret = vio_h_cop_sync(local_devdata->vdev, &op);
  564. /* Check for pHyp error */
  565. if (ret) {
  566. dev_dbg(dev, "%s: vio_h_cop_sync error (ret=%d, hret=%ld)\n",
  567. __func__, ret, op.hcall_err);
  568. dev = NULL;
  569. goto sw;
  570. }
  571. /* Check for hardware error */
  572. ret = nx842_validate_result(dev, &csbcpb->csb);
  573. if (ret) {
  574. dev = NULL;
  575. goto sw;
  576. }
  577. /* HW decompression success */
  578. inbuf += hdr->sizes[i];
  579. outbuf += csbcpb->csb.processed_byte_count;
  580. continue;
  581. sw:
  582. /* software decompression */
  583. size = max_sync_size;
  584. ret = sw842_decompress(
  585. (unsigned char *)inbuf, hdr->sizes[i],
  586. (unsigned char *)outbuf, &size, wmem);
  587. if (ret)
  588. pr_debug("%s: sw842_decompress failed with %d\n",
  589. __func__, ret);
  590. if (ret) {
  591. if (ret != -ENOSPC && ret != -EINVAL &&
  592. ret != -EMSGSIZE)
  593. ret = -EIO;
  594. goto unlock;
  595. }
  596. /* SW decompression success */
  597. inbuf += hdr->sizes[i];
  598. outbuf += size;
  599. }
  600. *outlen = (unsigned int)(outbuf - (unsigned long)out);
  601. unlock:
  602. if (ret)
  603. /* decompress fail */
  604. nx842_inc_decomp_failed(local_devdata);
  605. else {
  606. if (!dev)
  607. /* software decompress */
  608. nx842_inc_swdecomp(local_devdata);
  609. nx842_inc_decomp_complete(local_devdata);
  610. ibm_nx842_incr_hist(local_devdata->counters->decomp_times,
  611. (get_tb() - start_time) / tb_ticks_per_usec);
  612. }
  613. rcu_read_unlock();
  614. return ret;
  615. }
  616. EXPORT_SYMBOL_GPL(nx842_decompress);
  617. /**
  618. * nx842_OF_set_defaults -- Set default (disabled) values for devdata
  619. *
  620. * @devdata - struct nx842_devdata to update
  621. *
  622. * Returns:
  623. * 0 on success
  624. * -ENOENT if @devdata ptr is NULL
  625. */
  626. static int nx842_OF_set_defaults(struct nx842_devdata *devdata)
  627. {
  628. if (devdata) {
  629. devdata->max_sync_size = 0;
  630. devdata->max_sync_sg = 0;
  631. devdata->max_sg_len = 0;
  632. devdata->status = UNAVAILABLE;
  633. return 0;
  634. } else
  635. return -ENOENT;
  636. }
  637. /**
  638. * nx842_OF_upd_status -- Update the device info from OF status prop
  639. *
  640. * The status property indicates if the accelerator is enabled. If the
  641. * device is in the OF tree it indicates that the hardware is present.
  642. * The status field indicates if the device is enabled when the status
  643. * is 'okay'. Otherwise the device driver will be disabled.
  644. *
  645. * @devdata - struct nx842_devdata to update
  646. * @prop - struct property point containing the maxsyncop for the update
  647. *
  648. * Returns:
  649. * 0 - Device is available
  650. * -EINVAL - Device is not available
  651. */
  652. static int nx842_OF_upd_status(struct nx842_devdata *devdata,
  653. struct property *prop) {
  654. int ret = 0;
  655. const char *status = (const char *)prop->value;
  656. if (!strncmp(status, "okay", (size_t)prop->length)) {
  657. devdata->status = AVAILABLE;
  658. } else {
  659. dev_info(devdata->dev, "%s: status '%s' is not 'okay'\n",
  660. __func__, status);
  661. devdata->status = UNAVAILABLE;
  662. }
  663. return ret;
  664. }
  665. /**
  666. * nx842_OF_upd_maxsglen -- Update the device info from OF maxsglen prop
  667. *
  668. * Definition of the 'ibm,max-sg-len' OF property:
  669. * This field indicates the maximum byte length of a scatter list
  670. * for the platform facility. It is a single cell encoded as with encode-int.
  671. *
  672. * Example:
  673. * # od -x ibm,max-sg-len
  674. * 0000000 0000 0ff0
  675. *
  676. * In this example, the maximum byte length of a scatter list is
  677. * 0x0ff0 (4,080).
  678. *
  679. * @devdata - struct nx842_devdata to update
  680. * @prop - struct property point containing the maxsyncop for the update
  681. *
  682. * Returns:
  683. * 0 on success
  684. * -EINVAL on failure
  685. */
  686. static int nx842_OF_upd_maxsglen(struct nx842_devdata *devdata,
  687. struct property *prop) {
  688. int ret = 0;
  689. const int *maxsglen = prop->value;
  690. if (prop->length != sizeof(*maxsglen)) {
  691. dev_err(devdata->dev, "%s: unexpected format for ibm,max-sg-len property\n", __func__);
  692. dev_dbg(devdata->dev, "%s: ibm,max-sg-len is %d bytes long, expected %lu bytes\n", __func__,
  693. prop->length, sizeof(*maxsglen));
  694. ret = -EINVAL;
  695. } else {
  696. devdata->max_sg_len = (unsigned int)min(*maxsglen,
  697. (int)NX842_HW_PAGE_SIZE);
  698. }
  699. return ret;
  700. }
  701. /**
  702. * nx842_OF_upd_maxsyncop -- Update the device info from OF maxsyncop prop
  703. *
  704. * Definition of the 'ibm,max-sync-cop' OF property:
  705. * Two series of cells. The first series of cells represents the maximums
  706. * that can be synchronously compressed. The second series of cells
  707. * represents the maximums that can be synchronously decompressed.
  708. * 1. The first cell in each series contains the count of the number of
  709. * data length, scatter list elements pairs that follow – each being
  710. * of the form
  711. * a. One cell data byte length
  712. * b. One cell total number of scatter list elements
  713. *
  714. * Example:
  715. * # od -x ibm,max-sync-cop
  716. * 0000000 0000 0001 0000 1000 0000 01fe 0000 0001
  717. * 0000020 0000 1000 0000 01fe
  718. *
  719. * In this example, compression supports 0x1000 (4,096) data byte length
  720. * and 0x1fe (510) total scatter list elements. Decompression supports
  721. * 0x1000 (4,096) data byte length and 0x1f3 (510) total scatter list
  722. * elements.
  723. *
  724. * @devdata - struct nx842_devdata to update
  725. * @prop - struct property point containing the maxsyncop for the update
  726. *
  727. * Returns:
  728. * 0 on success
  729. * -EINVAL on failure
  730. */
  731. static int nx842_OF_upd_maxsyncop(struct nx842_devdata *devdata,
  732. struct property *prop) {
  733. int ret = 0;
  734. const struct maxsynccop_t {
  735. int comp_elements;
  736. int comp_data_limit;
  737. int comp_sg_limit;
  738. int decomp_elements;
  739. int decomp_data_limit;
  740. int decomp_sg_limit;
  741. } *maxsynccop;
  742. if (prop->length != sizeof(*maxsynccop)) {
  743. dev_err(devdata->dev, "%s: unexpected format for ibm,max-sync-cop property\n", __func__);
  744. dev_dbg(devdata->dev, "%s: ibm,max-sync-cop is %d bytes long, expected %lu bytes\n", __func__, prop->length,
  745. sizeof(*maxsynccop));
  746. ret = -EINVAL;
  747. goto out;
  748. }
  749. maxsynccop = (const struct maxsynccop_t *)prop->value;
  750. /* Use one limit rather than separate limits for compression and
  751. * decompression. Set a maximum for this so as not to exceed the
  752. * size that the header can support and round the value down to
  753. * the hardware page size (4K) */
  754. devdata->max_sync_size =
  755. (unsigned int)min(maxsynccop->comp_data_limit,
  756. maxsynccop->decomp_data_limit);
  757. devdata->max_sync_size = min_t(unsigned int, devdata->max_sync_size,
  758. SIZE_64K);
  759. if (devdata->max_sync_size < SIZE_4K) {
  760. dev_err(devdata->dev, "%s: hardware max data size (%u) is "
  761. "less than the driver minimum, unable to use "
  762. "the hardware device\n",
  763. __func__, devdata->max_sync_size);
  764. ret = -EINVAL;
  765. goto out;
  766. }
  767. devdata->max_sync_sg = (unsigned int)min(maxsynccop->comp_sg_limit,
  768. maxsynccop->decomp_sg_limit);
  769. if (devdata->max_sync_sg < 1) {
  770. dev_err(devdata->dev, "%s: hardware max sg size (%u) is "
  771. "less than the driver minimum, unable to use "
  772. "the hardware device\n",
  773. __func__, devdata->max_sync_sg);
  774. ret = -EINVAL;
  775. goto out;
  776. }
  777. out:
  778. return ret;
  779. }
  780. /**
  781. *
  782. * nx842_OF_upd -- Handle OF properties updates for the device.
  783. *
  784. * Set all properties from the OF tree. Optionally, a new property
  785. * can be provided by the @new_prop pointer to overwrite an existing value.
  786. * The device will remain disabled until all values are valid, this function
  787. * will return an error for updates unless all values are valid.
  788. *
  789. * @new_prop: If not NULL, this property is being updated. If NULL, update
  790. * all properties from the current values in the OF tree.
  791. *
  792. * Returns:
  793. * 0 - Success
  794. * -ENOMEM - Could not allocate memory for new devdata structure
  795. * -EINVAL - property value not found, new_prop is not a recognized
  796. * property for the device or property value is not valid.
  797. * -ENODEV - Device is not available
  798. */
  799. static int nx842_OF_upd(struct property *new_prop)
  800. {
  801. struct nx842_devdata *old_devdata = NULL;
  802. struct nx842_devdata *new_devdata = NULL;
  803. struct device_node *of_node = NULL;
  804. struct property *status = NULL;
  805. struct property *maxsglen = NULL;
  806. struct property *maxsyncop = NULL;
  807. int ret = 0;
  808. unsigned long flags;
  809. spin_lock_irqsave(&devdata_mutex, flags);
  810. old_devdata = rcu_dereference_check(devdata,
  811. lockdep_is_held(&devdata_mutex));
  812. if (old_devdata)
  813. of_node = old_devdata->dev->of_node;
  814. if (!old_devdata || !of_node) {
  815. pr_err("%s: device is not available\n", __func__);
  816. spin_unlock_irqrestore(&devdata_mutex, flags);
  817. return -ENODEV;
  818. }
  819. new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
  820. if (!new_devdata) {
  821. dev_err(old_devdata->dev, "%s: Could not allocate memory for device data\n", __func__);
  822. ret = -ENOMEM;
  823. goto error_out;
  824. }
  825. memcpy(new_devdata, old_devdata, sizeof(*old_devdata));
  826. new_devdata->counters = old_devdata->counters;
  827. /* Set ptrs for existing properties */
  828. status = of_find_property(of_node, "status", NULL);
  829. maxsglen = of_find_property(of_node, "ibm,max-sg-len", NULL);
  830. maxsyncop = of_find_property(of_node, "ibm,max-sync-cop", NULL);
  831. if (!status || !maxsglen || !maxsyncop) {
  832. dev_err(old_devdata->dev, "%s: Could not locate device properties\n", __func__);
  833. ret = -EINVAL;
  834. goto error_out;
  835. }
  836. /*
  837. * If this is a property update, there are only certain properties that
  838. * we care about. Bail if it isn't in the below list
  839. */
  840. if (new_prop && (strncmp(new_prop->name, "status", new_prop->length) ||
  841. strncmp(new_prop->name, "ibm,max-sg-len", new_prop->length) ||
  842. strncmp(new_prop->name, "ibm,max-sync-cop", new_prop->length)))
  843. goto out;
  844. /* Perform property updates */
  845. ret = nx842_OF_upd_status(new_devdata, status);
  846. if (ret)
  847. goto error_out;
  848. ret = nx842_OF_upd_maxsglen(new_devdata, maxsglen);
  849. if (ret)
  850. goto error_out;
  851. ret = nx842_OF_upd_maxsyncop(new_devdata, maxsyncop);
  852. if (ret)
  853. goto error_out;
  854. out:
  855. dev_info(old_devdata->dev, "%s: max_sync_size new:%u old:%u\n",
  856. __func__, new_devdata->max_sync_size,
  857. old_devdata->max_sync_size);
  858. dev_info(old_devdata->dev, "%s: max_sync_sg new:%u old:%u\n",
  859. __func__, new_devdata->max_sync_sg,
  860. old_devdata->max_sync_sg);
  861. dev_info(old_devdata->dev, "%s: max_sg_len new:%u old:%u\n",
  862. __func__, new_devdata->max_sg_len,
  863. old_devdata->max_sg_len);
  864. rcu_assign_pointer(devdata, new_devdata);
  865. spin_unlock_irqrestore(&devdata_mutex, flags);
  866. synchronize_rcu();
  867. dev_set_drvdata(new_devdata->dev, new_devdata);
  868. kfree(old_devdata);
  869. return 0;
  870. error_out:
  871. if (new_devdata) {
  872. dev_info(old_devdata->dev, "%s: device disabled\n", __func__);
  873. nx842_OF_set_defaults(new_devdata);
  874. rcu_assign_pointer(devdata, new_devdata);
  875. spin_unlock_irqrestore(&devdata_mutex, flags);
  876. synchronize_rcu();
  877. dev_set_drvdata(new_devdata->dev, new_devdata);
  878. kfree(old_devdata);
  879. } else {
  880. dev_err(old_devdata->dev, "%s: could not update driver from hardware\n", __func__);
  881. spin_unlock_irqrestore(&devdata_mutex, flags);
  882. }
  883. if (!ret)
  884. ret = -EINVAL;
  885. return ret;
  886. }
  887. /**
  888. * nx842_OF_notifier - Process updates to OF properties for the device
  889. *
  890. * @np: notifier block
  891. * @action: notifier action
  892. * @update: struct pSeries_reconfig_prop_update pointer if action is
  893. * PSERIES_UPDATE_PROPERTY
  894. *
  895. * Returns:
  896. * NOTIFY_OK on success
  897. * NOTIFY_BAD encoded with error number on failure, use
  898. * notifier_to_errno() to decode this value
  899. */
  900. static int nx842_OF_notifier(struct notifier_block *np, unsigned long action,
  901. void *data)
  902. {
  903. struct of_reconfig_data *upd = data;
  904. struct nx842_devdata *local_devdata;
  905. struct device_node *node = NULL;
  906. rcu_read_lock();
  907. local_devdata = rcu_dereference(devdata);
  908. if (local_devdata)
  909. node = local_devdata->dev->of_node;
  910. if (local_devdata &&
  911. action == OF_RECONFIG_UPDATE_PROPERTY &&
  912. !strcmp(upd->dn->name, node->name)) {
  913. rcu_read_unlock();
  914. nx842_OF_upd(upd->prop);
  915. } else
  916. rcu_read_unlock();
  917. return NOTIFY_OK;
  918. }
  919. static struct notifier_block nx842_of_nb = {
  920. .notifier_call = nx842_OF_notifier,
  921. };
  922. #define nx842_counter_read(_name) \
  923. static ssize_t nx842_##_name##_show(struct device *dev, \
  924. struct device_attribute *attr, \
  925. char *buf) { \
  926. struct nx842_devdata *local_devdata; \
  927. int p = 0; \
  928. rcu_read_lock(); \
  929. local_devdata = rcu_dereference(devdata); \
  930. if (local_devdata) \
  931. p = snprintf(buf, PAGE_SIZE, "%ld\n", \
  932. atomic64_read(&local_devdata->counters->_name)); \
  933. rcu_read_unlock(); \
  934. return p; \
  935. }
  936. #define NX842DEV_COUNTER_ATTR_RO(_name) \
  937. nx842_counter_read(_name); \
  938. static struct device_attribute dev_attr_##_name = __ATTR(_name, \
  939. 0444, \
  940. nx842_##_name##_show,\
  941. NULL);
  942. NX842DEV_COUNTER_ATTR_RO(comp_complete);
  943. NX842DEV_COUNTER_ATTR_RO(comp_failed);
  944. NX842DEV_COUNTER_ATTR_RO(decomp_complete);
  945. NX842DEV_COUNTER_ATTR_RO(decomp_failed);
  946. NX842DEV_COUNTER_ATTR_RO(swdecomp);
  947. static ssize_t nx842_timehist_show(struct device *,
  948. struct device_attribute *, char *);
  949. static struct device_attribute dev_attr_comp_times = __ATTR(comp_times, 0444,
  950. nx842_timehist_show, NULL);
  951. static struct device_attribute dev_attr_decomp_times = __ATTR(decomp_times,
  952. 0444, nx842_timehist_show, NULL);
  953. static ssize_t nx842_timehist_show(struct device *dev,
  954. struct device_attribute *attr, char *buf) {
  955. char *p = buf;
  956. struct nx842_devdata *local_devdata;
  957. atomic64_t *times;
  958. int bytes_remain = PAGE_SIZE;
  959. int bytes;
  960. int i;
  961. rcu_read_lock();
  962. local_devdata = rcu_dereference(devdata);
  963. if (!local_devdata) {
  964. rcu_read_unlock();
  965. return 0;
  966. }
  967. if (attr == &dev_attr_comp_times)
  968. times = local_devdata->counters->comp_times;
  969. else if (attr == &dev_attr_decomp_times)
  970. times = local_devdata->counters->decomp_times;
  971. else {
  972. rcu_read_unlock();
  973. return 0;
  974. }
  975. for (i = 0; i < (NX842_HIST_SLOTS - 2); i++) {
  976. bytes = snprintf(p, bytes_remain, "%u-%uus:\t%ld\n",
  977. i ? (2<<(i-1)) : 0, (2<<i)-1,
  978. atomic64_read(&times[i]));
  979. bytes_remain -= bytes;
  980. p += bytes;
  981. }
  982. /* The last bucket holds everything over
  983. * 2<<(NX842_HIST_SLOTS - 2) us */
  984. bytes = snprintf(p, bytes_remain, "%uus - :\t%ld\n",
  985. 2<<(NX842_HIST_SLOTS - 2),
  986. atomic64_read(&times[(NX842_HIST_SLOTS - 1)]));
  987. p += bytes;
  988. rcu_read_unlock();
  989. return p - buf;
  990. }
  991. static struct attribute *nx842_sysfs_entries[] = {
  992. &dev_attr_comp_complete.attr,
  993. &dev_attr_comp_failed.attr,
  994. &dev_attr_decomp_complete.attr,
  995. &dev_attr_decomp_failed.attr,
  996. &dev_attr_swdecomp.attr,
  997. &dev_attr_comp_times.attr,
  998. &dev_attr_decomp_times.attr,
  999. NULL,
  1000. };
  1001. static struct attribute_group nx842_attribute_group = {
  1002. .name = NULL, /* put in device directory */
  1003. .attrs = nx842_sysfs_entries,
  1004. };
  1005. static int __init nx842_probe(struct vio_dev *viodev,
  1006. const struct vio_device_id *id)
  1007. {
  1008. struct nx842_devdata *old_devdata, *new_devdata = NULL;
  1009. unsigned long flags;
  1010. int ret = 0;
  1011. spin_lock_irqsave(&devdata_mutex, flags);
  1012. old_devdata = rcu_dereference_check(devdata,
  1013. lockdep_is_held(&devdata_mutex));
  1014. if (old_devdata && old_devdata->vdev != NULL) {
  1015. dev_err(&viodev->dev, "%s: Attempt to register more than one instance of the hardware\n", __func__);
  1016. ret = -1;
  1017. goto error_unlock;
  1018. }
  1019. dev_set_drvdata(&viodev->dev, NULL);
  1020. new_devdata = kzalloc(sizeof(*new_devdata), GFP_NOFS);
  1021. if (!new_devdata) {
  1022. dev_err(&viodev->dev, "%s: Could not allocate memory for device data\n", __func__);
  1023. ret = -ENOMEM;
  1024. goto error_unlock;
  1025. }
  1026. new_devdata->counters = kzalloc(sizeof(*new_devdata->counters),
  1027. GFP_NOFS);
  1028. if (!new_devdata->counters) {
  1029. dev_err(&viodev->dev, "%s: Could not allocate memory for performance counters\n", __func__);
  1030. ret = -ENOMEM;
  1031. goto error_unlock;
  1032. }
  1033. new_devdata->vdev = viodev;
  1034. new_devdata->dev = &viodev->dev;
  1035. nx842_OF_set_defaults(new_devdata);
  1036. rcu_assign_pointer(devdata, new_devdata);
  1037. spin_unlock_irqrestore(&devdata_mutex, flags);
  1038. synchronize_rcu();
  1039. kfree(old_devdata);
  1040. of_reconfig_notifier_register(&nx842_of_nb);
  1041. ret = nx842_OF_upd(NULL);
  1042. if (ret && ret != -ENODEV) {
  1043. dev_err(&viodev->dev, "could not parse device tree. %d\n", ret);
  1044. ret = -1;
  1045. goto error;
  1046. }
  1047. rcu_read_lock();
  1048. dev_set_drvdata(&viodev->dev, rcu_dereference(devdata));
  1049. rcu_read_unlock();
  1050. if (sysfs_create_group(&viodev->dev.kobj, &nx842_attribute_group)) {
  1051. dev_err(&viodev->dev, "could not create sysfs device attributes\n");
  1052. ret = -1;
  1053. goto error;
  1054. }
  1055. return 0;
  1056. error_unlock:
  1057. spin_unlock_irqrestore(&devdata_mutex, flags);
  1058. if (new_devdata)
  1059. kfree(new_devdata->counters);
  1060. kfree(new_devdata);
  1061. error:
  1062. return ret;
  1063. }
  1064. static int __exit nx842_remove(struct vio_dev *viodev)
  1065. {
  1066. struct nx842_devdata *old_devdata;
  1067. unsigned long flags;
  1068. pr_info("Removing IBM Power 842 compression device\n");
  1069. sysfs_remove_group(&viodev->dev.kobj, &nx842_attribute_group);
  1070. spin_lock_irqsave(&devdata_mutex, flags);
  1071. old_devdata = rcu_dereference_check(devdata,
  1072. lockdep_is_held(&devdata_mutex));
  1073. of_reconfig_notifier_unregister(&nx842_of_nb);
  1074. RCU_INIT_POINTER(devdata, NULL);
  1075. spin_unlock_irqrestore(&devdata_mutex, flags);
  1076. synchronize_rcu();
  1077. dev_set_drvdata(&viodev->dev, NULL);
  1078. if (old_devdata)
  1079. kfree(old_devdata->counters);
  1080. kfree(old_devdata);
  1081. return 0;
  1082. }
  1083. static struct vio_device_id nx842_driver_ids[] = {
  1084. {"ibm,compression-v1", "ibm,compression"},
  1085. {"", ""},
  1086. };
  1087. static struct vio_driver nx842_driver = {
  1088. .name = MODULE_NAME,
  1089. .probe = nx842_probe,
  1090. .remove = __exit_p(nx842_remove),
  1091. .get_desired_dma = nx842_get_desired_dma,
  1092. .id_table = nx842_driver_ids,
  1093. };
  1094. static int __init nx842_init(void)
  1095. {
  1096. struct nx842_devdata *new_devdata;
  1097. pr_info("Registering IBM Power 842 compression driver\n");
  1098. RCU_INIT_POINTER(devdata, NULL);
  1099. new_devdata = kzalloc(sizeof(*new_devdata), GFP_KERNEL);
  1100. if (!new_devdata) {
  1101. pr_err("Could not allocate memory for device data\n");
  1102. return -ENOMEM;
  1103. }
  1104. new_devdata->status = UNAVAILABLE;
  1105. RCU_INIT_POINTER(devdata, new_devdata);
  1106. return vio_register_driver(&nx842_driver);
  1107. }
  1108. module_init(nx842_init);
  1109. static void __exit nx842_exit(void)
  1110. {
  1111. struct nx842_devdata *old_devdata;
  1112. unsigned long flags;
  1113. pr_info("Exiting IBM Power 842 compression driver\n");
  1114. spin_lock_irqsave(&devdata_mutex, flags);
  1115. old_devdata = rcu_dereference_check(devdata,
  1116. lockdep_is_held(&devdata_mutex));
  1117. RCU_INIT_POINTER(devdata, NULL);
  1118. spin_unlock_irqrestore(&devdata_mutex, flags);
  1119. synchronize_rcu();
  1120. if (old_devdata)
  1121. dev_set_drvdata(old_devdata->dev, NULL);
  1122. kfree(old_devdata);
  1123. vio_unregister_driver(&nx842_driver);
  1124. }
  1125. module_exit(nx842_exit);
  1126. /*********************************
  1127. * 842 software decompressor
  1128. *********************************/
  1129. typedef int (*sw842_template_op)(const char **, int *, unsigned char **,
  1130. struct sw842_fifo *);
  1131. static int sw842_data8(const char **, int *, unsigned char **,
  1132. struct sw842_fifo *);
  1133. static int sw842_data4(const char **, int *, unsigned char **,
  1134. struct sw842_fifo *);
  1135. static int sw842_data2(const char **, int *, unsigned char **,
  1136. struct sw842_fifo *);
  1137. static int sw842_ptr8(const char **, int *, unsigned char **,
  1138. struct sw842_fifo *);
  1139. static int sw842_ptr4(const char **, int *, unsigned char **,
  1140. struct sw842_fifo *);
  1141. static int sw842_ptr2(const char **, int *, unsigned char **,
  1142. struct sw842_fifo *);
  1143. /* special templates */
  1144. #define SW842_TMPL_REPEAT 0x1B
  1145. #define SW842_TMPL_ZEROS 0x1C
  1146. #define SW842_TMPL_EOF 0x1E
  1147. static sw842_template_op sw842_tmpl_ops[26][4] = {
  1148. { sw842_data8, NULL}, /* 0 (00000) */
  1149. { sw842_data4, sw842_data2, sw842_ptr2, NULL},
  1150. { sw842_data4, sw842_ptr2, sw842_data2, NULL},
  1151. { sw842_data4, sw842_ptr2, sw842_ptr2, NULL},
  1152. { sw842_data4, sw842_ptr4, NULL},
  1153. { sw842_data2, sw842_ptr2, sw842_data4, NULL},
  1154. { sw842_data2, sw842_ptr2, sw842_data2, sw842_ptr2},
  1155. { sw842_data2, sw842_ptr2, sw842_ptr2, sw842_data2},
  1156. { sw842_data2, sw842_ptr2, sw842_ptr2, sw842_ptr2,},
  1157. { sw842_data2, sw842_ptr2, sw842_ptr4, NULL},
  1158. { sw842_ptr2, sw842_data2, sw842_data4, NULL}, /* 10 (01010) */
  1159. { sw842_ptr2, sw842_data4, sw842_ptr2, NULL},
  1160. { sw842_ptr2, sw842_data2, sw842_ptr2, sw842_data2},
  1161. { sw842_ptr2, sw842_data2, sw842_ptr2, sw842_ptr2},
  1162. { sw842_ptr2, sw842_data2, sw842_ptr4, NULL},
  1163. { sw842_ptr2, sw842_ptr2, sw842_data4, NULL},
  1164. { sw842_ptr2, sw842_ptr2, sw842_data2, sw842_ptr2},
  1165. { sw842_ptr2, sw842_ptr2, sw842_ptr2, sw842_data2},
  1166. { sw842_ptr2, sw842_ptr2, sw842_ptr2, sw842_ptr2},
  1167. { sw842_ptr2, sw842_ptr2, sw842_ptr4, NULL},
  1168. { sw842_ptr4, sw842_data4, NULL}, /* 20 (10100) */
  1169. { sw842_ptr4, sw842_data2, sw842_ptr2, NULL},
  1170. { sw842_ptr4, sw842_ptr2, sw842_data2, NULL},
  1171. { sw842_ptr4, sw842_ptr2, sw842_ptr2, NULL},
  1172. { sw842_ptr4, sw842_ptr4, NULL},
  1173. { sw842_ptr8, NULL}
  1174. };
  1175. /* Software decompress helpers */
  1176. static uint8_t sw842_get_byte(const char *buf, int bit)
  1177. {
  1178. uint8_t tmpl;
  1179. uint16_t tmp;
  1180. tmp = htons(*(uint16_t *)(buf));
  1181. tmp = (uint16_t)(tmp << bit);
  1182. tmp = ntohs(tmp);
  1183. memcpy(&tmpl, &tmp, 1);
  1184. return tmpl;
  1185. }
  1186. static uint8_t sw842_get_template(const char **buf, int *bit)
  1187. {
  1188. uint8_t byte;
  1189. byte = sw842_get_byte(*buf, *bit);
  1190. byte = byte >> 3;
  1191. byte &= 0x1F;
  1192. *buf += (*bit + 5) / 8;
  1193. *bit = (*bit + 5) % 8;
  1194. return byte;
  1195. }
  1196. /* repeat_count happens to be 5-bit too (like the template) */
  1197. static uint8_t sw842_get_repeat_count(const char **buf, int *bit)
  1198. {
  1199. uint8_t byte;
  1200. byte = sw842_get_byte(*buf, *bit);
  1201. byte = byte >> 2;
  1202. byte &= 0x3F;
  1203. *buf += (*bit + 6) / 8;
  1204. *bit = (*bit + 6) % 8;
  1205. return byte;
  1206. }
  1207. static uint8_t sw842_get_ptr2(const char **buf, int *bit)
  1208. {
  1209. uint8_t ptr;
  1210. ptr = sw842_get_byte(*buf, *bit);
  1211. (*buf)++;
  1212. return ptr;
  1213. }
  1214. static uint16_t sw842_get_ptr4(const char **buf, int *bit,
  1215. struct sw842_fifo *fifo)
  1216. {
  1217. uint16_t ptr;
  1218. ptr = htons(*(uint16_t *)(*buf));
  1219. ptr = (uint16_t)(ptr << *bit);
  1220. ptr = ptr >> 7;
  1221. ptr &= 0x01FF;
  1222. *buf += (*bit + 9) / 8;
  1223. *bit = (*bit + 9) % 8;
  1224. return ptr;
  1225. }
  1226. static uint8_t sw842_get_ptr8(const char **buf, int *bit,
  1227. struct sw842_fifo *fifo)
  1228. {
  1229. return sw842_get_ptr2(buf, bit);
  1230. }
  1231. /* Software decompress template ops */
  1232. static int sw842_data8(const char **inbuf, int *inbit,
  1233. unsigned char **outbuf, struct sw842_fifo *fifo)
  1234. {
  1235. int ret;
  1236. ret = sw842_data4(inbuf, inbit, outbuf, fifo);
  1237. if (ret)
  1238. return ret;
  1239. ret = sw842_data4(inbuf, inbit, outbuf, fifo);
  1240. return ret;
  1241. }
  1242. static int sw842_data4(const char **inbuf, int *inbit,
  1243. unsigned char **outbuf, struct sw842_fifo *fifo)
  1244. {
  1245. int ret;
  1246. ret = sw842_data2(inbuf, inbit, outbuf, fifo);
  1247. if (ret)
  1248. return ret;
  1249. ret = sw842_data2(inbuf, inbit, outbuf, fifo);
  1250. return ret;
  1251. }
  1252. static int sw842_data2(const char **inbuf, int *inbit,
  1253. unsigned char **outbuf, struct sw842_fifo *fifo)
  1254. {
  1255. **outbuf = sw842_get_byte(*inbuf, *inbit);
  1256. (*inbuf)++;
  1257. (*outbuf)++;
  1258. **outbuf = sw842_get_byte(*inbuf, *inbit);
  1259. (*inbuf)++;
  1260. (*outbuf)++;
  1261. return 0;
  1262. }
  1263. static int sw842_ptr8(const char **inbuf, int *inbit,
  1264. unsigned char **outbuf, struct sw842_fifo *fifo)
  1265. {
  1266. uint8_t ptr;
  1267. ptr = sw842_get_ptr8(inbuf, inbit, fifo);
  1268. if (!fifo->f84_full && (ptr >= fifo->f8_count))
  1269. return 1;
  1270. memcpy(*outbuf, fifo->f8[ptr], 8);
  1271. *outbuf += 8;
  1272. return 0;
  1273. }
  1274. static int sw842_ptr4(const char **inbuf, int *inbit,
  1275. unsigned char **outbuf, struct sw842_fifo *fifo)
  1276. {
  1277. uint16_t ptr;
  1278. ptr = sw842_get_ptr4(inbuf, inbit, fifo);
  1279. if (!fifo->f84_full && (ptr >= fifo->f4_count))
  1280. return 1;
  1281. memcpy(*outbuf, fifo->f4[ptr], 4);
  1282. *outbuf += 4;
  1283. return 0;
  1284. }
  1285. static int sw842_ptr2(const char **inbuf, int *inbit,
  1286. unsigned char **outbuf, struct sw842_fifo *fifo)
  1287. {
  1288. uint8_t ptr;
  1289. ptr = sw842_get_ptr2(inbuf, inbit);
  1290. if (!fifo->f2_full && (ptr >= fifo->f2_count))
  1291. return 1;
  1292. memcpy(*outbuf, fifo->f2[ptr], 2);
  1293. *outbuf += 2;
  1294. return 0;
  1295. }
  1296. static void sw842_copy_to_fifo(const char *buf, struct sw842_fifo *fifo)
  1297. {
  1298. unsigned char initial_f2count = fifo->f2_count;
  1299. memcpy(fifo->f8[fifo->f8_count], buf, 8);
  1300. fifo->f4_count += 2;
  1301. fifo->f8_count += 1;
  1302. if (!fifo->f84_full && fifo->f4_count >= 512) {
  1303. fifo->f84_full = 1;
  1304. fifo->f4_count /= 512;
  1305. }
  1306. memcpy(fifo->f2[fifo->f2_count++], buf, 2);
  1307. memcpy(fifo->f2[fifo->f2_count++], buf + 2, 2);
  1308. memcpy(fifo->f2[fifo->f2_count++], buf + 4, 2);
  1309. memcpy(fifo->f2[fifo->f2_count++], buf + 6, 2);
  1310. if (fifo->f2_count < initial_f2count)
  1311. fifo->f2_full = 1;
  1312. }
  1313. static int sw842_decompress(const unsigned char *src, int srclen,
  1314. unsigned char *dst, int *destlen,
  1315. const void *wrkmem)
  1316. {
  1317. uint8_t tmpl;
  1318. const char *inbuf;
  1319. int inbit = 0;
  1320. unsigned char *outbuf, *outbuf_end, *origbuf, *prevbuf;
  1321. const char *inbuf_end;
  1322. sw842_template_op op;
  1323. int opindex;
  1324. int i, repeat_count;
  1325. struct sw842_fifo *fifo;
  1326. int ret = 0;
  1327. fifo = &((struct nx842_workmem *)(wrkmem))->swfifo;
  1328. memset(fifo, 0, sizeof(*fifo));
  1329. origbuf = NULL;
  1330. inbuf = src;
  1331. inbuf_end = src + srclen;
  1332. outbuf = dst;
  1333. outbuf_end = dst + *destlen;
  1334. while ((tmpl = sw842_get_template(&inbuf, &inbit)) != SW842_TMPL_EOF) {
  1335. if (inbuf >= inbuf_end) {
  1336. ret = -EINVAL;
  1337. goto out;
  1338. }
  1339. opindex = 0;
  1340. prevbuf = origbuf;
  1341. origbuf = outbuf;
  1342. switch (tmpl) {
  1343. case SW842_TMPL_REPEAT:
  1344. if (prevbuf == NULL) {
  1345. ret = -EINVAL;
  1346. goto out;
  1347. }
  1348. repeat_count = sw842_get_repeat_count(&inbuf,
  1349. &inbit) + 1;
  1350. /* Did the repeat count advance past the end of input */
  1351. if (inbuf > inbuf_end) {
  1352. ret = -EINVAL;
  1353. goto out;
  1354. }
  1355. for (i = 0; i < repeat_count; i++) {
  1356. /* Would this overflow the output buffer */
  1357. if ((outbuf + 8) > outbuf_end) {
  1358. ret = -ENOSPC;
  1359. goto out;
  1360. }
  1361. memcpy(outbuf, prevbuf, 8);
  1362. sw842_copy_to_fifo(outbuf, fifo);
  1363. outbuf += 8;
  1364. }
  1365. break;
  1366. case SW842_TMPL_ZEROS:
  1367. /* Would this overflow the output buffer */
  1368. if ((outbuf + 8) > outbuf_end) {
  1369. ret = -ENOSPC;
  1370. goto out;
  1371. }
  1372. memset(outbuf, 0, 8);
  1373. sw842_copy_to_fifo(outbuf, fifo);
  1374. outbuf += 8;
  1375. break;
  1376. default:
  1377. if (tmpl > 25) {
  1378. ret = -EINVAL;
  1379. goto out;
  1380. }
  1381. /* Does this go past the end of the input buffer */
  1382. if ((inbuf + 2) > inbuf_end) {
  1383. ret = -EINVAL;
  1384. goto out;
  1385. }
  1386. /* Would this overflow the output buffer */
  1387. if ((outbuf + 8) > outbuf_end) {
  1388. ret = -ENOSPC;
  1389. goto out;
  1390. }
  1391. while (opindex < 4 &&
  1392. (op = sw842_tmpl_ops[tmpl][opindex++])
  1393. != NULL) {
  1394. ret = (*op)(&inbuf, &inbit, &outbuf, fifo);
  1395. if (ret) {
  1396. ret = -EINVAL;
  1397. goto out;
  1398. }
  1399. sw842_copy_to_fifo(origbuf, fifo);
  1400. }
  1401. }
  1402. }
  1403. out:
  1404. if (!ret)
  1405. *destlen = (unsigned int)(outbuf - dst);
  1406. else
  1407. *destlen = 0;
  1408. return ret;
  1409. }