ocxl_hw.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CXL Flash Device Driver
  4. *
  5. * Written by: Matthew R. Ochs <[email protected]>, IBM Corporation
  6. * Uma Krishnan <[email protected]>, IBM Corporation
  7. *
  8. * Copyright (C) 2018 IBM Corporation
  9. */
  10. #include <linux/file.h>
  11. #include <linux/idr.h>
  12. #include <linux/module.h>
  13. #include <linux/mount.h>
  14. #include <linux/pseudo_fs.h>
  15. #include <linux/poll.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irqdomain.h>
  19. #include <asm/xive.h>
  20. #include <misc/ocxl.h>
  21. #include <uapi/misc/cxl.h>
  22. #include "backend.h"
  23. #include "ocxl_hw.h"
  24. /*
  25. * Pseudo-filesystem to allocate inodes.
  26. */
  27. #define OCXLFLASH_FS_MAGIC 0x1697698f
  28. static int ocxlflash_fs_cnt;
  29. static struct vfsmount *ocxlflash_vfs_mount;
  30. static int ocxlflash_fs_init_fs_context(struct fs_context *fc)
  31. {
  32. return init_pseudo(fc, OCXLFLASH_FS_MAGIC) ? 0 : -ENOMEM;
  33. }
  34. static struct file_system_type ocxlflash_fs_type = {
  35. .name = "ocxlflash",
  36. .owner = THIS_MODULE,
  37. .init_fs_context = ocxlflash_fs_init_fs_context,
  38. .kill_sb = kill_anon_super,
  39. };
  40. /*
  41. * ocxlflash_release_mapping() - release the memory mapping
  42. * @ctx: Context whose mapping is to be released.
  43. */
  44. static void ocxlflash_release_mapping(struct ocxlflash_context *ctx)
  45. {
  46. if (ctx->mapping)
  47. simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
  48. ctx->mapping = NULL;
  49. }
  50. /*
  51. * ocxlflash_getfile() - allocate pseudo filesystem, inode, and the file
  52. * @dev: Generic device of the host.
  53. * @name: Name of the pseudo filesystem.
  54. * @fops: File operations.
  55. * @priv: Private data.
  56. * @flags: Flags for the file.
  57. *
  58. * Return: pointer to the file on success, ERR_PTR on failure
  59. */
  60. static struct file *ocxlflash_getfile(struct device *dev, const char *name,
  61. const struct file_operations *fops,
  62. void *priv, int flags)
  63. {
  64. struct file *file;
  65. struct inode *inode;
  66. int rc;
  67. if (fops->owner && !try_module_get(fops->owner)) {
  68. dev_err(dev, "%s: Owner does not exist\n", __func__);
  69. rc = -ENOENT;
  70. goto err1;
  71. }
  72. rc = simple_pin_fs(&ocxlflash_fs_type, &ocxlflash_vfs_mount,
  73. &ocxlflash_fs_cnt);
  74. if (unlikely(rc < 0)) {
  75. dev_err(dev, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
  76. __func__, rc);
  77. goto err2;
  78. }
  79. inode = alloc_anon_inode(ocxlflash_vfs_mount->mnt_sb);
  80. if (IS_ERR(inode)) {
  81. rc = PTR_ERR(inode);
  82. dev_err(dev, "%s: alloc_anon_inode failed rc=%d\n",
  83. __func__, rc);
  84. goto err3;
  85. }
  86. file = alloc_file_pseudo(inode, ocxlflash_vfs_mount, name,
  87. flags & (O_ACCMODE | O_NONBLOCK), fops);
  88. if (IS_ERR(file)) {
  89. rc = PTR_ERR(file);
  90. dev_err(dev, "%s: alloc_file failed rc=%d\n",
  91. __func__, rc);
  92. goto err4;
  93. }
  94. file->private_data = priv;
  95. out:
  96. return file;
  97. err4:
  98. iput(inode);
  99. err3:
  100. simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
  101. err2:
  102. module_put(fops->owner);
  103. err1:
  104. file = ERR_PTR(rc);
  105. goto out;
  106. }
  107. /**
  108. * ocxlflash_psa_map() - map the process specific MMIO space
  109. * @ctx_cookie: Adapter context for which the mapping needs to be done.
  110. *
  111. * Return: MMIO pointer of the mapped region
  112. */
  113. static void __iomem *ocxlflash_psa_map(void *ctx_cookie)
  114. {
  115. struct ocxlflash_context *ctx = ctx_cookie;
  116. struct device *dev = ctx->hw_afu->dev;
  117. mutex_lock(&ctx->state_mutex);
  118. if (ctx->state != STARTED) {
  119. dev_err(dev, "%s: Context not started, state=%d\n", __func__,
  120. ctx->state);
  121. mutex_unlock(&ctx->state_mutex);
  122. return NULL;
  123. }
  124. mutex_unlock(&ctx->state_mutex);
  125. return ioremap(ctx->psn_phys, ctx->psn_size);
  126. }
  127. /**
  128. * ocxlflash_psa_unmap() - unmap the process specific MMIO space
  129. * @addr: MMIO pointer to unmap.
  130. */
  131. static void ocxlflash_psa_unmap(void __iomem *addr)
  132. {
  133. iounmap(addr);
  134. }
  135. /**
  136. * ocxlflash_process_element() - get process element of the adapter context
  137. * @ctx_cookie: Adapter context associated with the process element.
  138. *
  139. * Return: process element of the adapter context
  140. */
  141. static int ocxlflash_process_element(void *ctx_cookie)
  142. {
  143. struct ocxlflash_context *ctx = ctx_cookie;
  144. return ctx->pe;
  145. }
  146. /**
  147. * afu_map_irq() - map the interrupt of the adapter context
  148. * @flags: Flags.
  149. * @ctx: Adapter context.
  150. * @num: Per-context AFU interrupt number.
  151. * @handler: Interrupt handler to register.
  152. * @cookie: Interrupt handler private data.
  153. * @name: Name of the interrupt.
  154. *
  155. * Return: 0 on success, -errno on failure
  156. */
  157. static int afu_map_irq(u64 flags, struct ocxlflash_context *ctx, int num,
  158. irq_handler_t handler, void *cookie, char *name)
  159. {
  160. struct ocxl_hw_afu *afu = ctx->hw_afu;
  161. struct device *dev = afu->dev;
  162. struct ocxlflash_irqs *irq;
  163. struct xive_irq_data *xd;
  164. u32 virq;
  165. int rc = 0;
  166. if (num < 0 || num >= ctx->num_irqs) {
  167. dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
  168. rc = -ENOENT;
  169. goto out;
  170. }
  171. irq = &ctx->irqs[num];
  172. virq = irq_create_mapping(NULL, irq->hwirq);
  173. if (unlikely(!virq)) {
  174. dev_err(dev, "%s: irq_create_mapping failed\n", __func__);
  175. rc = -ENOMEM;
  176. goto out;
  177. }
  178. rc = request_irq(virq, handler, 0, name, cookie);
  179. if (unlikely(rc)) {
  180. dev_err(dev, "%s: request_irq failed rc=%d\n", __func__, rc);
  181. goto err1;
  182. }
  183. xd = irq_get_handler_data(virq);
  184. if (unlikely(!xd)) {
  185. dev_err(dev, "%s: Can't get interrupt data\n", __func__);
  186. rc = -ENXIO;
  187. goto err2;
  188. }
  189. irq->virq = virq;
  190. irq->vtrig = xd->trig_mmio;
  191. out:
  192. return rc;
  193. err2:
  194. free_irq(virq, cookie);
  195. err1:
  196. irq_dispose_mapping(virq);
  197. goto out;
  198. }
  199. /**
  200. * ocxlflash_map_afu_irq() - map the interrupt of the adapter context
  201. * @ctx_cookie: Adapter context.
  202. * @num: Per-context AFU interrupt number.
  203. * @handler: Interrupt handler to register.
  204. * @cookie: Interrupt handler private data.
  205. * @name: Name of the interrupt.
  206. *
  207. * Return: 0 on success, -errno on failure
  208. */
  209. static int ocxlflash_map_afu_irq(void *ctx_cookie, int num,
  210. irq_handler_t handler, void *cookie,
  211. char *name)
  212. {
  213. return afu_map_irq(0, ctx_cookie, num, handler, cookie, name);
  214. }
  215. /**
  216. * afu_unmap_irq() - unmap the interrupt
  217. * @flags: Flags.
  218. * @ctx: Adapter context.
  219. * @num: Per-context AFU interrupt number.
  220. * @cookie: Interrupt handler private data.
  221. */
  222. static void afu_unmap_irq(u64 flags, struct ocxlflash_context *ctx, int num,
  223. void *cookie)
  224. {
  225. struct ocxl_hw_afu *afu = ctx->hw_afu;
  226. struct device *dev = afu->dev;
  227. struct ocxlflash_irqs *irq;
  228. if (num < 0 || num >= ctx->num_irqs) {
  229. dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
  230. return;
  231. }
  232. irq = &ctx->irqs[num];
  233. if (irq_find_mapping(NULL, irq->hwirq)) {
  234. free_irq(irq->virq, cookie);
  235. irq_dispose_mapping(irq->virq);
  236. }
  237. memset(irq, 0, sizeof(*irq));
  238. }
  239. /**
  240. * ocxlflash_unmap_afu_irq() - unmap the interrupt
  241. * @ctx_cookie: Adapter context.
  242. * @num: Per-context AFU interrupt number.
  243. * @cookie: Interrupt handler private data.
  244. */
  245. static void ocxlflash_unmap_afu_irq(void *ctx_cookie, int num, void *cookie)
  246. {
  247. return afu_unmap_irq(0, ctx_cookie, num, cookie);
  248. }
  249. /**
  250. * ocxlflash_get_irq_objhndl() - get the object handle for an interrupt
  251. * @ctx_cookie: Context associated with the interrupt.
  252. * @irq: Interrupt number.
  253. *
  254. * Return: effective address of the mapped region
  255. */
  256. static u64 ocxlflash_get_irq_objhndl(void *ctx_cookie, int irq)
  257. {
  258. struct ocxlflash_context *ctx = ctx_cookie;
  259. if (irq < 0 || irq >= ctx->num_irqs)
  260. return 0;
  261. return (__force u64)ctx->irqs[irq].vtrig;
  262. }
  263. /**
  264. * ocxlflash_xsl_fault() - callback when translation error is triggered
  265. * @data: Private data provided at callback registration, the context.
  266. * @addr: Address that triggered the error.
  267. * @dsisr: Value of dsisr register.
  268. */
  269. static void ocxlflash_xsl_fault(void *data, u64 addr, u64 dsisr)
  270. {
  271. struct ocxlflash_context *ctx = data;
  272. spin_lock(&ctx->slock);
  273. ctx->fault_addr = addr;
  274. ctx->fault_dsisr = dsisr;
  275. ctx->pending_fault = true;
  276. spin_unlock(&ctx->slock);
  277. wake_up_all(&ctx->wq);
  278. }
  279. /**
  280. * start_context() - local routine to start a context
  281. * @ctx: Adapter context to be started.
  282. *
  283. * Assign the context specific MMIO space, add and enable the PE.
  284. *
  285. * Return: 0 on success, -errno on failure
  286. */
  287. static int start_context(struct ocxlflash_context *ctx)
  288. {
  289. struct ocxl_hw_afu *afu = ctx->hw_afu;
  290. struct ocxl_afu_config *acfg = &afu->acfg;
  291. void *link_token = afu->link_token;
  292. struct pci_dev *pdev = afu->pdev;
  293. struct device *dev = afu->dev;
  294. bool master = ctx->master;
  295. struct mm_struct *mm;
  296. int rc = 0;
  297. u32 pid;
  298. mutex_lock(&ctx->state_mutex);
  299. if (ctx->state != OPENED) {
  300. dev_err(dev, "%s: Context state invalid, state=%d\n",
  301. __func__, ctx->state);
  302. rc = -EINVAL;
  303. goto out;
  304. }
  305. if (master) {
  306. ctx->psn_size = acfg->global_mmio_size;
  307. ctx->psn_phys = afu->gmmio_phys;
  308. } else {
  309. ctx->psn_size = acfg->pp_mmio_stride;
  310. ctx->psn_phys = afu->ppmmio_phys + (ctx->pe * ctx->psn_size);
  311. }
  312. /* pid and mm not set for master contexts */
  313. if (master) {
  314. pid = 0;
  315. mm = NULL;
  316. } else {
  317. pid = current->mm->context.id;
  318. mm = current->mm;
  319. }
  320. rc = ocxl_link_add_pe(link_token, ctx->pe, pid, 0, 0,
  321. pci_dev_id(pdev), mm, ocxlflash_xsl_fault,
  322. ctx);
  323. if (unlikely(rc)) {
  324. dev_err(dev, "%s: ocxl_link_add_pe failed rc=%d\n",
  325. __func__, rc);
  326. goto out;
  327. }
  328. ctx->state = STARTED;
  329. out:
  330. mutex_unlock(&ctx->state_mutex);
  331. return rc;
  332. }
  333. /**
  334. * ocxlflash_start_context() - start a kernel context
  335. * @ctx_cookie: Adapter context to be started.
  336. *
  337. * Return: 0 on success, -errno on failure
  338. */
  339. static int ocxlflash_start_context(void *ctx_cookie)
  340. {
  341. struct ocxlflash_context *ctx = ctx_cookie;
  342. return start_context(ctx);
  343. }
  344. /**
  345. * ocxlflash_stop_context() - stop a context
  346. * @ctx_cookie: Adapter context to be stopped.
  347. *
  348. * Return: 0 on success, -errno on failure
  349. */
  350. static int ocxlflash_stop_context(void *ctx_cookie)
  351. {
  352. struct ocxlflash_context *ctx = ctx_cookie;
  353. struct ocxl_hw_afu *afu = ctx->hw_afu;
  354. struct ocxl_afu_config *acfg = &afu->acfg;
  355. struct pci_dev *pdev = afu->pdev;
  356. struct device *dev = afu->dev;
  357. enum ocxlflash_ctx_state state;
  358. int rc = 0;
  359. mutex_lock(&ctx->state_mutex);
  360. state = ctx->state;
  361. ctx->state = CLOSED;
  362. mutex_unlock(&ctx->state_mutex);
  363. if (state != STARTED)
  364. goto out;
  365. rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
  366. ctx->pe);
  367. if (unlikely(rc)) {
  368. dev_err(dev, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
  369. __func__, rc);
  370. /* If EBUSY, PE could be referenced in future by the AFU */
  371. if (rc == -EBUSY)
  372. goto out;
  373. }
  374. rc = ocxl_link_remove_pe(afu->link_token, ctx->pe);
  375. if (unlikely(rc)) {
  376. dev_err(dev, "%s: ocxl_link_remove_pe failed rc=%d\n",
  377. __func__, rc);
  378. goto out;
  379. }
  380. out:
  381. return rc;
  382. }
  383. /**
  384. * ocxlflash_afu_reset() - reset the AFU
  385. * @ctx_cookie: Adapter context.
  386. */
  387. static int ocxlflash_afu_reset(void *ctx_cookie)
  388. {
  389. struct ocxlflash_context *ctx = ctx_cookie;
  390. struct device *dev = ctx->hw_afu->dev;
  391. /* Pending implementation from OCXL transport services */
  392. dev_err_once(dev, "%s: afu_reset() fop not supported\n", __func__);
  393. /* Silently return success until it is implemented */
  394. return 0;
  395. }
  396. /**
  397. * ocxlflash_set_master() - sets the context as master
  398. * @ctx_cookie: Adapter context to set as master.
  399. */
  400. static void ocxlflash_set_master(void *ctx_cookie)
  401. {
  402. struct ocxlflash_context *ctx = ctx_cookie;
  403. ctx->master = true;
  404. }
  405. /**
  406. * ocxlflash_get_context() - obtains the context associated with the host
  407. * @pdev: PCI device associated with the host.
  408. * @afu_cookie: Hardware AFU associated with the host.
  409. *
  410. * Return: returns the pointer to host adapter context
  411. */
  412. static void *ocxlflash_get_context(struct pci_dev *pdev, void *afu_cookie)
  413. {
  414. struct ocxl_hw_afu *afu = afu_cookie;
  415. return afu->ocxl_ctx;
  416. }
  417. /**
  418. * ocxlflash_dev_context_init() - allocate and initialize an adapter context
  419. * @pdev: PCI device associated with the host.
  420. * @afu_cookie: Hardware AFU associated with the host.
  421. *
  422. * Return: returns the adapter context on success, ERR_PTR on failure
  423. */
  424. static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
  425. {
  426. struct ocxl_hw_afu *afu = afu_cookie;
  427. struct device *dev = afu->dev;
  428. struct ocxlflash_context *ctx;
  429. int rc;
  430. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  431. if (unlikely(!ctx)) {
  432. dev_err(dev, "%s: Context allocation failed\n", __func__);
  433. rc = -ENOMEM;
  434. goto err1;
  435. }
  436. idr_preload(GFP_KERNEL);
  437. rc = idr_alloc(&afu->idr, ctx, 0, afu->max_pasid, GFP_NOWAIT);
  438. idr_preload_end();
  439. if (unlikely(rc < 0)) {
  440. dev_err(dev, "%s: idr_alloc failed rc=%d\n", __func__, rc);
  441. goto err2;
  442. }
  443. spin_lock_init(&ctx->slock);
  444. init_waitqueue_head(&ctx->wq);
  445. mutex_init(&ctx->state_mutex);
  446. ctx->state = OPENED;
  447. ctx->pe = rc;
  448. ctx->master = false;
  449. ctx->mapping = NULL;
  450. ctx->hw_afu = afu;
  451. ctx->irq_bitmap = 0;
  452. ctx->pending_irq = false;
  453. ctx->pending_fault = false;
  454. out:
  455. return ctx;
  456. err2:
  457. kfree(ctx);
  458. err1:
  459. ctx = ERR_PTR(rc);
  460. goto out;
  461. }
  462. /**
  463. * ocxlflash_release_context() - releases an adapter context
  464. * @ctx_cookie: Adapter context to be released.
  465. *
  466. * Return: 0 on success, -errno on failure
  467. */
  468. static int ocxlflash_release_context(void *ctx_cookie)
  469. {
  470. struct ocxlflash_context *ctx = ctx_cookie;
  471. struct device *dev;
  472. int rc = 0;
  473. if (!ctx)
  474. goto out;
  475. dev = ctx->hw_afu->dev;
  476. mutex_lock(&ctx->state_mutex);
  477. if (ctx->state >= STARTED) {
  478. dev_err(dev, "%s: Context in use, state=%d\n", __func__,
  479. ctx->state);
  480. mutex_unlock(&ctx->state_mutex);
  481. rc = -EBUSY;
  482. goto out;
  483. }
  484. mutex_unlock(&ctx->state_mutex);
  485. idr_remove(&ctx->hw_afu->idr, ctx->pe);
  486. ocxlflash_release_mapping(ctx);
  487. kfree(ctx);
  488. out:
  489. return rc;
  490. }
  491. /**
  492. * ocxlflash_perst_reloads_same_image() - sets the image reload policy
  493. * @afu_cookie: Hardware AFU associated with the host.
  494. * @image: Whether to load the same image on PERST.
  495. */
  496. static void ocxlflash_perst_reloads_same_image(void *afu_cookie, bool image)
  497. {
  498. struct ocxl_hw_afu *afu = afu_cookie;
  499. afu->perst_same_image = image;
  500. }
  501. /**
  502. * ocxlflash_read_adapter_vpd() - reads the adapter VPD
  503. * @pdev: PCI device associated with the host.
  504. * @buf: Buffer to get the VPD data.
  505. * @count: Size of buffer (maximum bytes that can be read).
  506. *
  507. * Return: size of VPD on success, -errno on failure
  508. */
  509. static ssize_t ocxlflash_read_adapter_vpd(struct pci_dev *pdev, void *buf,
  510. size_t count)
  511. {
  512. return pci_read_vpd(pdev, 0, count, buf);
  513. }
  514. /**
  515. * free_afu_irqs() - internal service to free interrupts
  516. * @ctx: Adapter context.
  517. */
  518. static void free_afu_irqs(struct ocxlflash_context *ctx)
  519. {
  520. struct ocxl_hw_afu *afu = ctx->hw_afu;
  521. struct device *dev = afu->dev;
  522. int i;
  523. if (!ctx->irqs) {
  524. dev_err(dev, "%s: Interrupts not allocated\n", __func__);
  525. return;
  526. }
  527. for (i = ctx->num_irqs; i >= 0; i--)
  528. ocxl_link_free_irq(afu->link_token, ctx->irqs[i].hwirq);
  529. kfree(ctx->irqs);
  530. ctx->irqs = NULL;
  531. }
  532. /**
  533. * alloc_afu_irqs() - internal service to allocate interrupts
  534. * @ctx: Context associated with the request.
  535. * @num: Number of interrupts requested.
  536. *
  537. * Return: 0 on success, -errno on failure
  538. */
  539. static int alloc_afu_irqs(struct ocxlflash_context *ctx, int num)
  540. {
  541. struct ocxl_hw_afu *afu = ctx->hw_afu;
  542. struct device *dev = afu->dev;
  543. struct ocxlflash_irqs *irqs;
  544. int rc = 0;
  545. int hwirq;
  546. int i;
  547. if (ctx->irqs) {
  548. dev_err(dev, "%s: Interrupts already allocated\n", __func__);
  549. rc = -EEXIST;
  550. goto out;
  551. }
  552. if (num > OCXL_MAX_IRQS) {
  553. dev_err(dev, "%s: Too many interrupts num=%d\n", __func__, num);
  554. rc = -EINVAL;
  555. goto out;
  556. }
  557. irqs = kcalloc(num, sizeof(*irqs), GFP_KERNEL);
  558. if (unlikely(!irqs)) {
  559. dev_err(dev, "%s: Context irqs allocation failed\n", __func__);
  560. rc = -ENOMEM;
  561. goto out;
  562. }
  563. for (i = 0; i < num; i++) {
  564. rc = ocxl_link_irq_alloc(afu->link_token, &hwirq);
  565. if (unlikely(rc)) {
  566. dev_err(dev, "%s: ocxl_link_irq_alloc failed rc=%d\n",
  567. __func__, rc);
  568. goto err;
  569. }
  570. irqs[i].hwirq = hwirq;
  571. }
  572. ctx->irqs = irqs;
  573. ctx->num_irqs = num;
  574. out:
  575. return rc;
  576. err:
  577. for (i = i-1; i >= 0; i--)
  578. ocxl_link_free_irq(afu->link_token, irqs[i].hwirq);
  579. kfree(irqs);
  580. goto out;
  581. }
  582. /**
  583. * ocxlflash_allocate_afu_irqs() - allocates the requested number of interrupts
  584. * @ctx_cookie: Context associated with the request.
  585. * @num: Number of interrupts requested.
  586. *
  587. * Return: 0 on success, -errno on failure
  588. */
  589. static int ocxlflash_allocate_afu_irqs(void *ctx_cookie, int num)
  590. {
  591. return alloc_afu_irqs(ctx_cookie, num);
  592. }
  593. /**
  594. * ocxlflash_free_afu_irqs() - frees the interrupts of an adapter context
  595. * @ctx_cookie: Adapter context.
  596. */
  597. static void ocxlflash_free_afu_irqs(void *ctx_cookie)
  598. {
  599. free_afu_irqs(ctx_cookie);
  600. }
  601. /**
  602. * ocxlflash_unconfig_afu() - unconfigure the AFU
  603. * @afu: AFU associated with the host.
  604. */
  605. static void ocxlflash_unconfig_afu(struct ocxl_hw_afu *afu)
  606. {
  607. if (afu->gmmio_virt) {
  608. iounmap(afu->gmmio_virt);
  609. afu->gmmio_virt = NULL;
  610. }
  611. }
  612. /**
  613. * ocxlflash_destroy_afu() - destroy the AFU structure
  614. * @afu_cookie: AFU to be freed.
  615. */
  616. static void ocxlflash_destroy_afu(void *afu_cookie)
  617. {
  618. struct ocxl_hw_afu *afu = afu_cookie;
  619. int pos;
  620. if (!afu)
  621. return;
  622. ocxlflash_release_context(afu->ocxl_ctx);
  623. idr_destroy(&afu->idr);
  624. /* Disable the AFU */
  625. pos = afu->acfg.dvsec_afu_control_pos;
  626. ocxl_config_set_afu_state(afu->pdev, pos, 0);
  627. ocxlflash_unconfig_afu(afu);
  628. kfree(afu);
  629. }
  630. /**
  631. * ocxlflash_config_fn() - configure the host function
  632. * @pdev: PCI device associated with the host.
  633. * @afu: AFU associated with the host.
  634. *
  635. * Return: 0 on success, -errno on failure
  636. */
  637. static int ocxlflash_config_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
  638. {
  639. struct ocxl_fn_config *fcfg = &afu->fcfg;
  640. struct device *dev = &pdev->dev;
  641. u16 base, enabled, supported;
  642. int rc = 0;
  643. /* Read DVSEC config of the function */
  644. rc = ocxl_config_read_function(pdev, fcfg);
  645. if (unlikely(rc)) {
  646. dev_err(dev, "%s: ocxl_config_read_function failed rc=%d\n",
  647. __func__, rc);
  648. goto out;
  649. }
  650. /* Check if function has AFUs defined, only 1 per function supported */
  651. if (fcfg->max_afu_index >= 0) {
  652. afu->is_present = true;
  653. if (fcfg->max_afu_index != 0)
  654. dev_warn(dev, "%s: Unexpected AFU index value %d\n",
  655. __func__, fcfg->max_afu_index);
  656. }
  657. rc = ocxl_config_get_actag_info(pdev, &base, &enabled, &supported);
  658. if (unlikely(rc)) {
  659. dev_err(dev, "%s: ocxl_config_get_actag_info failed rc=%d\n",
  660. __func__, rc);
  661. goto out;
  662. }
  663. afu->fn_actag_base = base;
  664. afu->fn_actag_enabled = enabled;
  665. ocxl_config_set_actag(pdev, fcfg->dvsec_function_pos, base, enabled);
  666. dev_dbg(dev, "%s: Function acTag range base=%u enabled=%u\n",
  667. __func__, base, enabled);
  668. rc = ocxl_link_setup(pdev, 0, &afu->link_token);
  669. if (unlikely(rc)) {
  670. dev_err(dev, "%s: ocxl_link_setup failed rc=%d\n",
  671. __func__, rc);
  672. goto out;
  673. }
  674. rc = ocxl_config_set_TL(pdev, fcfg->dvsec_tl_pos);
  675. if (unlikely(rc)) {
  676. dev_err(dev, "%s: ocxl_config_set_TL failed rc=%d\n",
  677. __func__, rc);
  678. goto err;
  679. }
  680. out:
  681. return rc;
  682. err:
  683. ocxl_link_release(pdev, afu->link_token);
  684. goto out;
  685. }
  686. /**
  687. * ocxlflash_unconfig_fn() - unconfigure the host function
  688. * @pdev: PCI device associated with the host.
  689. * @afu: AFU associated with the host.
  690. */
  691. static void ocxlflash_unconfig_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
  692. {
  693. ocxl_link_release(pdev, afu->link_token);
  694. }
  695. /**
  696. * ocxlflash_map_mmio() - map the AFU MMIO space
  697. * @afu: AFU associated with the host.
  698. *
  699. * Return: 0 on success, -errno on failure
  700. */
  701. static int ocxlflash_map_mmio(struct ocxl_hw_afu *afu)
  702. {
  703. struct ocxl_afu_config *acfg = &afu->acfg;
  704. struct pci_dev *pdev = afu->pdev;
  705. struct device *dev = afu->dev;
  706. phys_addr_t gmmio, ppmmio;
  707. int rc = 0;
  708. rc = pci_request_region(pdev, acfg->global_mmio_bar, "ocxlflash");
  709. if (unlikely(rc)) {
  710. dev_err(dev, "%s: pci_request_region for global failed rc=%d\n",
  711. __func__, rc);
  712. goto out;
  713. }
  714. gmmio = pci_resource_start(pdev, acfg->global_mmio_bar);
  715. gmmio += acfg->global_mmio_offset;
  716. rc = pci_request_region(pdev, acfg->pp_mmio_bar, "ocxlflash");
  717. if (unlikely(rc)) {
  718. dev_err(dev, "%s: pci_request_region for pp bar failed rc=%d\n",
  719. __func__, rc);
  720. goto err1;
  721. }
  722. ppmmio = pci_resource_start(pdev, acfg->pp_mmio_bar);
  723. ppmmio += acfg->pp_mmio_offset;
  724. afu->gmmio_virt = ioremap(gmmio, acfg->global_mmio_size);
  725. if (unlikely(!afu->gmmio_virt)) {
  726. dev_err(dev, "%s: MMIO mapping failed\n", __func__);
  727. rc = -ENOMEM;
  728. goto err2;
  729. }
  730. afu->gmmio_phys = gmmio;
  731. afu->ppmmio_phys = ppmmio;
  732. out:
  733. return rc;
  734. err2:
  735. pci_release_region(pdev, acfg->pp_mmio_bar);
  736. err1:
  737. pci_release_region(pdev, acfg->global_mmio_bar);
  738. goto out;
  739. }
  740. /**
  741. * ocxlflash_config_afu() - configure the host AFU
  742. * @pdev: PCI device associated with the host.
  743. * @afu: AFU associated with the host.
  744. *
  745. * Must be called _after_ host function configuration.
  746. *
  747. * Return: 0 on success, -errno on failure
  748. */
  749. static int ocxlflash_config_afu(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
  750. {
  751. struct ocxl_afu_config *acfg = &afu->acfg;
  752. struct ocxl_fn_config *fcfg = &afu->fcfg;
  753. struct device *dev = &pdev->dev;
  754. int count;
  755. int base;
  756. int pos;
  757. int rc = 0;
  758. /* This HW AFU function does not have any AFUs defined */
  759. if (!afu->is_present)
  760. goto out;
  761. /* Read AFU config at index 0 */
  762. rc = ocxl_config_read_afu(pdev, fcfg, acfg, 0);
  763. if (unlikely(rc)) {
  764. dev_err(dev, "%s: ocxl_config_read_afu failed rc=%d\n",
  765. __func__, rc);
  766. goto out;
  767. }
  768. /* Only one AFU per function is supported, so actag_base is same */
  769. base = afu->fn_actag_base;
  770. count = min_t(int, acfg->actag_supported, afu->fn_actag_enabled);
  771. pos = acfg->dvsec_afu_control_pos;
  772. ocxl_config_set_afu_actag(pdev, pos, base, count);
  773. dev_dbg(dev, "%s: acTag base=%d enabled=%d\n", __func__, base, count);
  774. afu->afu_actag_base = base;
  775. afu->afu_actag_enabled = count;
  776. afu->max_pasid = 1 << acfg->pasid_supported_log;
  777. ocxl_config_set_afu_pasid(pdev, pos, 0, acfg->pasid_supported_log);
  778. rc = ocxlflash_map_mmio(afu);
  779. if (unlikely(rc)) {
  780. dev_err(dev, "%s: ocxlflash_map_mmio failed rc=%d\n",
  781. __func__, rc);
  782. goto out;
  783. }
  784. /* Enable the AFU */
  785. ocxl_config_set_afu_state(pdev, acfg->dvsec_afu_control_pos, 1);
  786. out:
  787. return rc;
  788. }
  789. /**
  790. * ocxlflash_create_afu() - create the AFU for OCXL
  791. * @pdev: PCI device associated with the host.
  792. *
  793. * Return: AFU on success, NULL on failure
  794. */
  795. static void *ocxlflash_create_afu(struct pci_dev *pdev)
  796. {
  797. struct device *dev = &pdev->dev;
  798. struct ocxlflash_context *ctx;
  799. struct ocxl_hw_afu *afu;
  800. int rc;
  801. afu = kzalloc(sizeof(*afu), GFP_KERNEL);
  802. if (unlikely(!afu)) {
  803. dev_err(dev, "%s: HW AFU allocation failed\n", __func__);
  804. goto out;
  805. }
  806. afu->pdev = pdev;
  807. afu->dev = dev;
  808. idr_init(&afu->idr);
  809. rc = ocxlflash_config_fn(pdev, afu);
  810. if (unlikely(rc)) {
  811. dev_err(dev, "%s: Function configuration failed rc=%d\n",
  812. __func__, rc);
  813. goto err1;
  814. }
  815. rc = ocxlflash_config_afu(pdev, afu);
  816. if (unlikely(rc)) {
  817. dev_err(dev, "%s: AFU configuration failed rc=%d\n",
  818. __func__, rc);
  819. goto err2;
  820. }
  821. ctx = ocxlflash_dev_context_init(pdev, afu);
  822. if (IS_ERR(ctx)) {
  823. rc = PTR_ERR(ctx);
  824. dev_err(dev, "%s: ocxlflash_dev_context_init failed rc=%d\n",
  825. __func__, rc);
  826. goto err3;
  827. }
  828. afu->ocxl_ctx = ctx;
  829. out:
  830. return afu;
  831. err3:
  832. ocxlflash_unconfig_afu(afu);
  833. err2:
  834. ocxlflash_unconfig_fn(pdev, afu);
  835. err1:
  836. idr_destroy(&afu->idr);
  837. kfree(afu);
  838. afu = NULL;
  839. goto out;
  840. }
  841. /**
  842. * ctx_event_pending() - check for any event pending on the context
  843. * @ctx: Context to be checked.
  844. *
  845. * Return: true if there is an event pending, false if none pending
  846. */
  847. static inline bool ctx_event_pending(struct ocxlflash_context *ctx)
  848. {
  849. if (ctx->pending_irq || ctx->pending_fault)
  850. return true;
  851. return false;
  852. }
  853. /**
  854. * afu_poll() - poll the AFU for events on the context
  855. * @file: File associated with the adapter context.
  856. * @poll: Poll structure from the user.
  857. *
  858. * Return: poll mask
  859. */
  860. static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
  861. {
  862. struct ocxlflash_context *ctx = file->private_data;
  863. struct device *dev = ctx->hw_afu->dev;
  864. ulong lock_flags;
  865. int mask = 0;
  866. poll_wait(file, &ctx->wq, poll);
  867. spin_lock_irqsave(&ctx->slock, lock_flags);
  868. if (ctx_event_pending(ctx))
  869. mask |= POLLIN | POLLRDNORM;
  870. else if (ctx->state == CLOSED)
  871. mask |= POLLERR;
  872. spin_unlock_irqrestore(&ctx->slock, lock_flags);
  873. dev_dbg(dev, "%s: Poll wait completed for pe %i mask %i\n",
  874. __func__, ctx->pe, mask);
  875. return mask;
  876. }
  877. /**
  878. * afu_read() - perform a read on the context for any event
  879. * @file: File associated with the adapter context.
  880. * @buf: Buffer to receive the data.
  881. * @count: Size of buffer (maximum bytes that can be read).
  882. * @off: Offset.
  883. *
  884. * Return: size of the data read on success, -errno on failure
  885. */
  886. static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
  887. loff_t *off)
  888. {
  889. struct ocxlflash_context *ctx = file->private_data;
  890. struct device *dev = ctx->hw_afu->dev;
  891. struct cxl_event event;
  892. ulong lock_flags;
  893. ssize_t esize;
  894. ssize_t rc;
  895. int bit;
  896. DEFINE_WAIT(event_wait);
  897. if (*off != 0) {
  898. dev_err(dev, "%s: Non-zero offset not supported, off=%lld\n",
  899. __func__, *off);
  900. rc = -EINVAL;
  901. goto out;
  902. }
  903. spin_lock_irqsave(&ctx->slock, lock_flags);
  904. for (;;) {
  905. prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
  906. if (ctx_event_pending(ctx) || (ctx->state == CLOSED))
  907. break;
  908. if (file->f_flags & O_NONBLOCK) {
  909. dev_err(dev, "%s: File cannot be blocked on I/O\n",
  910. __func__);
  911. rc = -EAGAIN;
  912. goto err;
  913. }
  914. if (signal_pending(current)) {
  915. dev_err(dev, "%s: Signal pending on the process\n",
  916. __func__);
  917. rc = -ERESTARTSYS;
  918. goto err;
  919. }
  920. spin_unlock_irqrestore(&ctx->slock, lock_flags);
  921. schedule();
  922. spin_lock_irqsave(&ctx->slock, lock_flags);
  923. }
  924. finish_wait(&ctx->wq, &event_wait);
  925. memset(&event, 0, sizeof(event));
  926. event.header.process_element = ctx->pe;
  927. event.header.size = sizeof(struct cxl_event_header);
  928. if (ctx->pending_irq) {
  929. esize = sizeof(struct cxl_event_afu_interrupt);
  930. event.header.size += esize;
  931. event.header.type = CXL_EVENT_AFU_INTERRUPT;
  932. bit = find_first_bit(&ctx->irq_bitmap, ctx->num_irqs);
  933. clear_bit(bit, &ctx->irq_bitmap);
  934. event.irq.irq = bit + 1;
  935. if (bitmap_empty(&ctx->irq_bitmap, ctx->num_irqs))
  936. ctx->pending_irq = false;
  937. } else if (ctx->pending_fault) {
  938. event.header.size += sizeof(struct cxl_event_data_storage);
  939. event.header.type = CXL_EVENT_DATA_STORAGE;
  940. event.fault.addr = ctx->fault_addr;
  941. event.fault.dsisr = ctx->fault_dsisr;
  942. ctx->pending_fault = false;
  943. }
  944. spin_unlock_irqrestore(&ctx->slock, lock_flags);
  945. if (copy_to_user(buf, &event, event.header.size)) {
  946. dev_err(dev, "%s: copy_to_user failed\n", __func__);
  947. rc = -EFAULT;
  948. goto out;
  949. }
  950. rc = event.header.size;
  951. out:
  952. return rc;
  953. err:
  954. finish_wait(&ctx->wq, &event_wait);
  955. spin_unlock_irqrestore(&ctx->slock, lock_flags);
  956. goto out;
  957. }
  958. /**
  959. * afu_release() - release and free the context
  960. * @inode: File inode pointer.
  961. * @file: File associated with the context.
  962. *
  963. * Return: 0 on success, -errno on failure
  964. */
  965. static int afu_release(struct inode *inode, struct file *file)
  966. {
  967. struct ocxlflash_context *ctx = file->private_data;
  968. int i;
  969. /* Unmap and free the interrupts associated with the context */
  970. for (i = ctx->num_irqs; i >= 0; i--)
  971. afu_unmap_irq(0, ctx, i, ctx);
  972. free_afu_irqs(ctx);
  973. return ocxlflash_release_context(ctx);
  974. }
  975. /**
  976. * ocxlflash_mmap_fault() - mmap fault handler
  977. * @vmf: VM fault associated with current fault.
  978. *
  979. * Return: 0 on success, -errno on failure
  980. */
  981. static vm_fault_t ocxlflash_mmap_fault(struct vm_fault *vmf)
  982. {
  983. struct vm_area_struct *vma = vmf->vma;
  984. struct ocxlflash_context *ctx = vma->vm_file->private_data;
  985. struct device *dev = ctx->hw_afu->dev;
  986. u64 mmio_area, offset;
  987. offset = vmf->pgoff << PAGE_SHIFT;
  988. if (offset >= ctx->psn_size)
  989. return VM_FAULT_SIGBUS;
  990. mutex_lock(&ctx->state_mutex);
  991. if (ctx->state != STARTED) {
  992. dev_err(dev, "%s: Context not started, state=%d\n",
  993. __func__, ctx->state);
  994. mutex_unlock(&ctx->state_mutex);
  995. return VM_FAULT_SIGBUS;
  996. }
  997. mutex_unlock(&ctx->state_mutex);
  998. mmio_area = ctx->psn_phys;
  999. mmio_area += offset;
  1000. return vmf_insert_pfn(vma, vmf->address, mmio_area >> PAGE_SHIFT);
  1001. }
  1002. static const struct vm_operations_struct ocxlflash_vmops = {
  1003. .fault = ocxlflash_mmap_fault,
  1004. };
  1005. /**
  1006. * afu_mmap() - map the fault handler operations
  1007. * @file: File associated with the context.
  1008. * @vma: VM area associated with mapping.
  1009. *
  1010. * Return: 0 on success, -errno on failure
  1011. */
  1012. static int afu_mmap(struct file *file, struct vm_area_struct *vma)
  1013. {
  1014. struct ocxlflash_context *ctx = file->private_data;
  1015. if ((vma_pages(vma) + vma->vm_pgoff) >
  1016. (ctx->psn_size >> PAGE_SHIFT))
  1017. return -EINVAL;
  1018. vm_flags_set(vma, VM_IO | VM_PFNMAP);
  1019. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1020. vma->vm_ops = &ocxlflash_vmops;
  1021. return 0;
  1022. }
  1023. static const struct file_operations ocxl_afu_fops = {
  1024. .owner = THIS_MODULE,
  1025. .poll = afu_poll,
  1026. .read = afu_read,
  1027. .release = afu_release,
  1028. .mmap = afu_mmap,
  1029. };
  1030. #define PATCH_FOPS(NAME) \
  1031. do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
  1032. /**
  1033. * ocxlflash_get_fd() - get file descriptor for an adapter context
  1034. * @ctx_cookie: Adapter context.
  1035. * @fops: File operations to be associated.
  1036. * @fd: File descriptor to be returned back.
  1037. *
  1038. * Return: pointer to the file on success, ERR_PTR on failure
  1039. */
  1040. static struct file *ocxlflash_get_fd(void *ctx_cookie,
  1041. struct file_operations *fops, int *fd)
  1042. {
  1043. struct ocxlflash_context *ctx = ctx_cookie;
  1044. struct device *dev = ctx->hw_afu->dev;
  1045. struct file *file;
  1046. int flags, fdtmp;
  1047. int rc = 0;
  1048. char *name = NULL;
  1049. /* Only allow one fd per context */
  1050. if (ctx->mapping) {
  1051. dev_err(dev, "%s: Context is already mapped to an fd\n",
  1052. __func__);
  1053. rc = -EEXIST;
  1054. goto err1;
  1055. }
  1056. flags = O_RDWR | O_CLOEXEC;
  1057. /* This code is similar to anon_inode_getfd() */
  1058. rc = get_unused_fd_flags(flags);
  1059. if (unlikely(rc < 0)) {
  1060. dev_err(dev, "%s: get_unused_fd_flags failed rc=%d\n",
  1061. __func__, rc);
  1062. goto err1;
  1063. }
  1064. fdtmp = rc;
  1065. /* Patch the file ops that are not defined */
  1066. if (fops) {
  1067. PATCH_FOPS(poll);
  1068. PATCH_FOPS(read);
  1069. PATCH_FOPS(release);
  1070. PATCH_FOPS(mmap);
  1071. } else /* Use default ops */
  1072. fops = (struct file_operations *)&ocxl_afu_fops;
  1073. name = kasprintf(GFP_KERNEL, "ocxlflash:%d", ctx->pe);
  1074. file = ocxlflash_getfile(dev, name, fops, ctx, flags);
  1075. kfree(name);
  1076. if (IS_ERR(file)) {
  1077. rc = PTR_ERR(file);
  1078. dev_err(dev, "%s: ocxlflash_getfile failed rc=%d\n",
  1079. __func__, rc);
  1080. goto err2;
  1081. }
  1082. ctx->mapping = file->f_mapping;
  1083. *fd = fdtmp;
  1084. out:
  1085. return file;
  1086. err2:
  1087. put_unused_fd(fdtmp);
  1088. err1:
  1089. file = ERR_PTR(rc);
  1090. goto out;
  1091. }
  1092. /**
  1093. * ocxlflash_fops_get_context() - get the context associated with the file
  1094. * @file: File associated with the adapter context.
  1095. *
  1096. * Return: pointer to the context
  1097. */
  1098. static void *ocxlflash_fops_get_context(struct file *file)
  1099. {
  1100. return file->private_data;
  1101. }
  1102. /**
  1103. * ocxlflash_afu_irq() - interrupt handler for user contexts
  1104. * @irq: Interrupt number.
  1105. * @data: Private data provided at interrupt registration, the context.
  1106. *
  1107. * Return: Always return IRQ_HANDLED.
  1108. */
  1109. static irqreturn_t ocxlflash_afu_irq(int irq, void *data)
  1110. {
  1111. struct ocxlflash_context *ctx = data;
  1112. struct device *dev = ctx->hw_afu->dev;
  1113. int i;
  1114. dev_dbg(dev, "%s: Interrupt raised for pe %i virq %i\n",
  1115. __func__, ctx->pe, irq);
  1116. for (i = 0; i < ctx->num_irqs; i++) {
  1117. if (ctx->irqs[i].virq == irq)
  1118. break;
  1119. }
  1120. if (unlikely(i >= ctx->num_irqs)) {
  1121. dev_err(dev, "%s: Received AFU IRQ out of range\n", __func__);
  1122. goto out;
  1123. }
  1124. spin_lock(&ctx->slock);
  1125. set_bit(i - 1, &ctx->irq_bitmap);
  1126. ctx->pending_irq = true;
  1127. spin_unlock(&ctx->slock);
  1128. wake_up_all(&ctx->wq);
  1129. out:
  1130. return IRQ_HANDLED;
  1131. }
  1132. /**
  1133. * ocxlflash_start_work() - start a user context
  1134. * @ctx_cookie: Context to be started.
  1135. * @num_irqs: Number of interrupts requested.
  1136. *
  1137. * Return: 0 on success, -errno on failure
  1138. */
  1139. static int ocxlflash_start_work(void *ctx_cookie, u64 num_irqs)
  1140. {
  1141. struct ocxlflash_context *ctx = ctx_cookie;
  1142. struct ocxl_hw_afu *afu = ctx->hw_afu;
  1143. struct device *dev = afu->dev;
  1144. char *name;
  1145. int rc = 0;
  1146. int i;
  1147. rc = alloc_afu_irqs(ctx, num_irqs);
  1148. if (unlikely(rc < 0)) {
  1149. dev_err(dev, "%s: alloc_afu_irqs failed rc=%d\n", __func__, rc);
  1150. goto out;
  1151. }
  1152. for (i = 0; i < num_irqs; i++) {
  1153. name = kasprintf(GFP_KERNEL, "ocxlflash-%s-pe%i-%i",
  1154. dev_name(dev), ctx->pe, i);
  1155. rc = afu_map_irq(0, ctx, i, ocxlflash_afu_irq, ctx, name);
  1156. kfree(name);
  1157. if (unlikely(rc < 0)) {
  1158. dev_err(dev, "%s: afu_map_irq failed rc=%d\n",
  1159. __func__, rc);
  1160. goto err;
  1161. }
  1162. }
  1163. rc = start_context(ctx);
  1164. if (unlikely(rc)) {
  1165. dev_err(dev, "%s: start_context failed rc=%d\n", __func__, rc);
  1166. goto err;
  1167. }
  1168. out:
  1169. return rc;
  1170. err:
  1171. for (i = i-1; i >= 0; i--)
  1172. afu_unmap_irq(0, ctx, i, ctx);
  1173. free_afu_irqs(ctx);
  1174. goto out;
  1175. };
  1176. /**
  1177. * ocxlflash_fd_mmap() - mmap handler for adapter file descriptor
  1178. * @file: File installed with adapter file descriptor.
  1179. * @vma: VM area associated with mapping.
  1180. *
  1181. * Return: 0 on success, -errno on failure
  1182. */
  1183. static int ocxlflash_fd_mmap(struct file *file, struct vm_area_struct *vma)
  1184. {
  1185. return afu_mmap(file, vma);
  1186. }
  1187. /**
  1188. * ocxlflash_fd_release() - release the context associated with the file
  1189. * @inode: File inode pointer.
  1190. * @file: File associated with the adapter context.
  1191. *
  1192. * Return: 0 on success, -errno on failure
  1193. */
  1194. static int ocxlflash_fd_release(struct inode *inode, struct file *file)
  1195. {
  1196. return afu_release(inode, file);
  1197. }
  1198. /* Backend ops to ocxlflash services */
  1199. const struct cxlflash_backend_ops cxlflash_ocxl_ops = {
  1200. .module = THIS_MODULE,
  1201. .psa_map = ocxlflash_psa_map,
  1202. .psa_unmap = ocxlflash_psa_unmap,
  1203. .process_element = ocxlflash_process_element,
  1204. .map_afu_irq = ocxlflash_map_afu_irq,
  1205. .unmap_afu_irq = ocxlflash_unmap_afu_irq,
  1206. .get_irq_objhndl = ocxlflash_get_irq_objhndl,
  1207. .start_context = ocxlflash_start_context,
  1208. .stop_context = ocxlflash_stop_context,
  1209. .afu_reset = ocxlflash_afu_reset,
  1210. .set_master = ocxlflash_set_master,
  1211. .get_context = ocxlflash_get_context,
  1212. .dev_context_init = ocxlflash_dev_context_init,
  1213. .release_context = ocxlflash_release_context,
  1214. .perst_reloads_same_image = ocxlflash_perst_reloads_same_image,
  1215. .read_adapter_vpd = ocxlflash_read_adapter_vpd,
  1216. .allocate_afu_irqs = ocxlflash_allocate_afu_irqs,
  1217. .free_afu_irqs = ocxlflash_free_afu_irqs,
  1218. .create_afu = ocxlflash_create_afu,
  1219. .destroy_afu = ocxlflash_destroy_afu,
  1220. .get_fd = ocxlflash_get_fd,
  1221. .fops_get_context = ocxlflash_fops_get_context,
  1222. .start_work = ocxlflash_start_work,
  1223. .fd_mmap = ocxlflash_fd_mmap,
  1224. .fd_release = ocxlflash_fd_release,
  1225. };