nx-842.c 43 KB

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