nx-common-pseries.c 35 KB

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