nx-842.c 44 KB

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