file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 IBM Corp.
  4. */
  5. #include <linux/spinlock.h>
  6. #include <linux/module.h>
  7. #include <linux/export.h>
  8. #include <linux/kernel.h>
  9. #include <linux/bitmap.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/poll.h>
  12. #include <linux/pid.h>
  13. #include <linux/fs.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched/mm.h>
  17. #include <linux/mmu_context.h>
  18. #include <asm/cputable.h>
  19. #include <asm/current.h>
  20. #include <asm/copro.h>
  21. #include "cxl.h"
  22. #include "trace.h"
  23. #define CXL_NUM_MINORS 256 /* Total to reserve */
  24. #define CXL_AFU_MINOR_D(afu) (CXL_CARD_MINOR(afu->adapter) + 1 + (3 * afu->slice))
  25. #define CXL_AFU_MINOR_M(afu) (CXL_AFU_MINOR_D(afu) + 1)
  26. #define CXL_AFU_MINOR_S(afu) (CXL_AFU_MINOR_D(afu) + 2)
  27. #define CXL_AFU_MKDEV_D(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_D(afu))
  28. #define CXL_AFU_MKDEV_M(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_M(afu))
  29. #define CXL_AFU_MKDEV_S(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_S(afu))
  30. #define CXL_DEVT_AFU(dev) ((MINOR(dev) % CXL_DEV_MINORS - 1) / 3)
  31. #define CXL_DEVT_IS_CARD(dev) (MINOR(dev) % CXL_DEV_MINORS == 0)
  32. static dev_t cxl_dev;
  33. static int __afu_open(struct inode *inode, struct file *file, bool master)
  34. {
  35. struct cxl *adapter;
  36. struct cxl_afu *afu;
  37. struct cxl_context *ctx;
  38. int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
  39. int slice = CXL_DEVT_AFU(inode->i_rdev);
  40. int rc = -ENODEV;
  41. pr_devel("afu_open afu%i.%i\n", slice, adapter_num);
  42. if (!(adapter = get_cxl_adapter(adapter_num)))
  43. return -ENODEV;
  44. if (slice > adapter->slices)
  45. goto err_put_adapter;
  46. spin_lock(&adapter->afu_list_lock);
  47. if (!(afu = adapter->afu[slice])) {
  48. spin_unlock(&adapter->afu_list_lock);
  49. goto err_put_adapter;
  50. }
  51. /*
  52. * taking a ref to the afu so that it doesn't go away
  53. * for rest of the function. This ref is released before
  54. * we return.
  55. */
  56. cxl_afu_get(afu);
  57. spin_unlock(&adapter->afu_list_lock);
  58. if (!afu->current_mode)
  59. goto err_put_afu;
  60. if (!cxl_ops->link_ok(adapter, afu)) {
  61. rc = -EIO;
  62. goto err_put_afu;
  63. }
  64. if (!(ctx = cxl_context_alloc())) {
  65. rc = -ENOMEM;
  66. goto err_put_afu;
  67. }
  68. rc = cxl_context_init(ctx, afu, master);
  69. if (rc)
  70. goto err_put_afu;
  71. cxl_context_set_mapping(ctx, inode->i_mapping);
  72. pr_devel("afu_open pe: %i\n", ctx->pe);
  73. file->private_data = ctx;
  74. /* indicate success */
  75. rc = 0;
  76. err_put_afu:
  77. /* release the ref taken earlier */
  78. cxl_afu_put(afu);
  79. err_put_adapter:
  80. put_device(&adapter->dev);
  81. return rc;
  82. }
  83. int afu_open(struct inode *inode, struct file *file)
  84. {
  85. return __afu_open(inode, file, false);
  86. }
  87. static int afu_master_open(struct inode *inode, struct file *file)
  88. {
  89. return __afu_open(inode, file, true);
  90. }
  91. int afu_release(struct inode *inode, struct file *file)
  92. {
  93. struct cxl_context *ctx = file->private_data;
  94. pr_devel("%s: closing cxl file descriptor. pe: %i\n",
  95. __func__, ctx->pe);
  96. cxl_context_detach(ctx);
  97. /*
  98. * Delete the context's mapping pointer, unless it's created by the
  99. * kernel API, in which case leave it so it can be freed by reclaim_ctx()
  100. */
  101. if (!ctx->kernelapi) {
  102. mutex_lock(&ctx->mapping_lock);
  103. ctx->mapping = NULL;
  104. mutex_unlock(&ctx->mapping_lock);
  105. }
  106. /*
  107. * At this this point all bottom halfs have finished and we should be
  108. * getting no more IRQs from the hardware for this context. Once it's
  109. * removed from the IDR (and RCU synchronised) it's safe to free the
  110. * sstp and context.
  111. */
  112. cxl_context_free(ctx);
  113. return 0;
  114. }
  115. static long afu_ioctl_start_work(struct cxl_context *ctx,
  116. struct cxl_ioctl_start_work __user *uwork)
  117. {
  118. struct cxl_ioctl_start_work work;
  119. u64 amr = 0;
  120. int rc;
  121. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  122. /* Do this outside the status_mutex to avoid a circular dependency with
  123. * the locking in cxl_mmap_fault() */
  124. if (copy_from_user(&work, uwork, sizeof(work)))
  125. return -EFAULT;
  126. mutex_lock(&ctx->status_mutex);
  127. if (ctx->status != OPENED) {
  128. rc = -EIO;
  129. goto out;
  130. }
  131. /*
  132. * if any of the reserved fields are set or any of the unused
  133. * flags are set it's invalid
  134. */
  135. if (work.reserved1 || work.reserved2 || work.reserved3 ||
  136. work.reserved4 || work.reserved5 ||
  137. (work.flags & ~CXL_START_WORK_ALL)) {
  138. rc = -EINVAL;
  139. goto out;
  140. }
  141. if (!(work.flags & CXL_START_WORK_NUM_IRQS))
  142. work.num_interrupts = ctx->afu->pp_irqs;
  143. else if ((work.num_interrupts < ctx->afu->pp_irqs) ||
  144. (work.num_interrupts > ctx->afu->irqs_max)) {
  145. rc = -EINVAL;
  146. goto out;
  147. }
  148. if ((rc = afu_register_irqs(ctx, work.num_interrupts)))
  149. goto out;
  150. if (work.flags & CXL_START_WORK_AMR)
  151. amr = work.amr & mfspr(SPRN_UAMOR);
  152. if (work.flags & CXL_START_WORK_TID)
  153. ctx->assign_tidr = true;
  154. ctx->mmio_err_ff = !!(work.flags & CXL_START_WORK_ERR_FF);
  155. /*
  156. * Increment the mapped context count for adapter. This also checks
  157. * if adapter_context_lock is taken.
  158. */
  159. rc = cxl_adapter_context_get(ctx->afu->adapter);
  160. if (rc) {
  161. afu_release_irqs(ctx, ctx);
  162. goto out;
  163. }
  164. /*
  165. * We grab the PID here and not in the file open to allow for the case
  166. * where a process (master, some daemon, etc) has opened the chardev on
  167. * behalf of another process, so the AFU's mm gets bound to the process
  168. * that performs this ioctl and not the process that opened the file.
  169. * Also we grab the PID of the group leader so that if the task that
  170. * has performed the attach operation exits the mm context of the
  171. * process is still accessible.
  172. */
  173. ctx->pid = get_task_pid(current, PIDTYPE_PID);
  174. /* acquire a reference to the task's mm */
  175. ctx->mm = get_task_mm(current);
  176. /* ensure this mm_struct can't be freed */
  177. cxl_context_mm_count_get(ctx);
  178. if (ctx->mm) {
  179. /* decrement the use count from above */
  180. mmput(ctx->mm);
  181. /* make TLBIs for this context global */
  182. mm_context_add_copro(ctx->mm);
  183. }
  184. /*
  185. * Increment driver use count. Enables global TLBIs for hash
  186. * and callbacks to handle the segment table
  187. */
  188. cxl_ctx_get();
  189. /*
  190. * A barrier is needed to make sure all TLBIs are global
  191. * before we attach and the context starts being used by the
  192. * adapter.
  193. *
  194. * Needed after mm_context_add_copro() for radix and
  195. * cxl_ctx_get() for hash/p8.
  196. *
  197. * The barrier should really be mb(), since it involves a
  198. * device. However, it's only useful when we have local
  199. * vs. global TLBIs, i.e SMP=y. So keep smp_mb().
  200. */
  201. smp_mb();
  202. trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
  203. if ((rc = cxl_ops->attach_process(ctx, false, work.work_element_descriptor,
  204. amr))) {
  205. afu_release_irqs(ctx, ctx);
  206. cxl_adapter_context_put(ctx->afu->adapter);
  207. put_pid(ctx->pid);
  208. ctx->pid = NULL;
  209. cxl_ctx_put();
  210. cxl_context_mm_count_put(ctx);
  211. if (ctx->mm)
  212. mm_context_remove_copro(ctx->mm);
  213. goto out;
  214. }
  215. rc = 0;
  216. if (work.flags & CXL_START_WORK_TID) {
  217. work.tid = ctx->tidr;
  218. if (copy_to_user(uwork, &work, sizeof(work)))
  219. rc = -EFAULT;
  220. }
  221. ctx->status = STARTED;
  222. out:
  223. mutex_unlock(&ctx->status_mutex);
  224. return rc;
  225. }
  226. static long afu_ioctl_process_element(struct cxl_context *ctx,
  227. int __user *upe)
  228. {
  229. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  230. if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
  231. return -EFAULT;
  232. return 0;
  233. }
  234. static long afu_ioctl_get_afu_id(struct cxl_context *ctx,
  235. struct cxl_afu_id __user *upafuid)
  236. {
  237. struct cxl_afu_id afuid = { 0 };
  238. afuid.card_id = ctx->afu->adapter->adapter_num;
  239. afuid.afu_offset = ctx->afu->slice;
  240. afuid.afu_mode = ctx->afu->current_mode;
  241. /* set the flag bit in case the afu is a slave */
  242. if (ctx->afu->current_mode == CXL_MODE_DIRECTED && !ctx->master)
  243. afuid.flags |= CXL_AFUID_FLAG_SLAVE;
  244. if (copy_to_user(upafuid, &afuid, sizeof(afuid)))
  245. return -EFAULT;
  246. return 0;
  247. }
  248. long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  249. {
  250. struct cxl_context *ctx = file->private_data;
  251. if (ctx->status == CLOSED)
  252. return -EIO;
  253. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  254. return -EIO;
  255. pr_devel("afu_ioctl\n");
  256. switch (cmd) {
  257. case CXL_IOCTL_START_WORK:
  258. return afu_ioctl_start_work(ctx, (struct cxl_ioctl_start_work __user *)arg);
  259. case CXL_IOCTL_GET_PROCESS_ELEMENT:
  260. return afu_ioctl_process_element(ctx, (__u32 __user *)arg);
  261. case CXL_IOCTL_GET_AFU_ID:
  262. return afu_ioctl_get_afu_id(ctx, (struct cxl_afu_id __user *)
  263. arg);
  264. }
  265. return -EINVAL;
  266. }
  267. static long afu_compat_ioctl(struct file *file, unsigned int cmd,
  268. unsigned long arg)
  269. {
  270. return afu_ioctl(file, cmd, arg);
  271. }
  272. int afu_mmap(struct file *file, struct vm_area_struct *vm)
  273. {
  274. struct cxl_context *ctx = file->private_data;
  275. /* AFU must be started before we can MMIO */
  276. if (ctx->status != STARTED)
  277. return -EIO;
  278. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  279. return -EIO;
  280. return cxl_context_iomap(ctx, vm);
  281. }
  282. static inline bool ctx_event_pending(struct cxl_context *ctx)
  283. {
  284. if (ctx->pending_irq || ctx->pending_fault || ctx->pending_afu_err)
  285. return true;
  286. if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events))
  287. return true;
  288. return false;
  289. }
  290. __poll_t afu_poll(struct file *file, struct poll_table_struct *poll)
  291. {
  292. struct cxl_context *ctx = file->private_data;
  293. __poll_t mask = 0;
  294. unsigned long flags;
  295. poll_wait(file, &ctx->wq, poll);
  296. pr_devel("afu_poll wait done pe: %i\n", ctx->pe);
  297. spin_lock_irqsave(&ctx->lock, flags);
  298. if (ctx_event_pending(ctx))
  299. mask |= EPOLLIN | EPOLLRDNORM;
  300. else if (ctx->status == CLOSED)
  301. /* Only error on closed when there are no futher events pending
  302. */
  303. mask |= EPOLLERR;
  304. spin_unlock_irqrestore(&ctx->lock, flags);
  305. pr_devel("afu_poll pe: %i returning %#x\n", ctx->pe, mask);
  306. return mask;
  307. }
  308. static ssize_t afu_driver_event_copy(struct cxl_context *ctx,
  309. char __user *buf,
  310. struct cxl_event *event,
  311. struct cxl_event_afu_driver_reserved *pl)
  312. {
  313. /* Check event */
  314. if (!pl) {
  315. ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
  316. return -EFAULT;
  317. }
  318. /* Check event size */
  319. event->header.size += pl->data_size;
  320. if (event->header.size > CXL_READ_MIN_SIZE) {
  321. ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
  322. return -EFAULT;
  323. }
  324. /* Copy event header */
  325. if (copy_to_user(buf, event, sizeof(struct cxl_event_header))) {
  326. ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
  327. return -EFAULT;
  328. }
  329. /* Copy event data */
  330. buf += sizeof(struct cxl_event_header);
  331. if (copy_to_user(buf, &pl->data, pl->data_size)) {
  332. ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
  333. return -EFAULT;
  334. }
  335. ctx->afu_driver_ops->event_delivered(ctx, pl, 0); /* Success */
  336. return event->header.size;
  337. }
  338. ssize_t afu_read(struct file *file, char __user *buf, size_t count,
  339. loff_t *off)
  340. {
  341. struct cxl_context *ctx = file->private_data;
  342. struct cxl_event_afu_driver_reserved *pl = NULL;
  343. struct cxl_event event;
  344. unsigned long flags;
  345. int rc;
  346. DEFINE_WAIT(wait);
  347. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
  348. return -EIO;
  349. if (count < CXL_READ_MIN_SIZE)
  350. return -EINVAL;
  351. spin_lock_irqsave(&ctx->lock, flags);
  352. for (;;) {
  353. prepare_to_wait(&ctx->wq, &wait, TASK_INTERRUPTIBLE);
  354. if (ctx_event_pending(ctx) || (ctx->status == CLOSED))
  355. break;
  356. if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
  357. rc = -EIO;
  358. goto out;
  359. }
  360. if (file->f_flags & O_NONBLOCK) {
  361. rc = -EAGAIN;
  362. goto out;
  363. }
  364. if (signal_pending(current)) {
  365. rc = -ERESTARTSYS;
  366. goto out;
  367. }
  368. spin_unlock_irqrestore(&ctx->lock, flags);
  369. pr_devel("afu_read going to sleep...\n");
  370. schedule();
  371. pr_devel("afu_read woken up\n");
  372. spin_lock_irqsave(&ctx->lock, flags);
  373. }
  374. finish_wait(&ctx->wq, &wait);
  375. memset(&event, 0, sizeof(event));
  376. event.header.process_element = ctx->pe;
  377. event.header.size = sizeof(struct cxl_event_header);
  378. if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events)) {
  379. pr_devel("afu_read delivering AFU driver specific event\n");
  380. pl = ctx->afu_driver_ops->fetch_event(ctx);
  381. atomic_dec(&ctx->afu_driver_events);
  382. event.header.type = CXL_EVENT_AFU_DRIVER;
  383. } else if (ctx->pending_irq) {
  384. pr_devel("afu_read delivering AFU interrupt\n");
  385. event.header.size += sizeof(struct cxl_event_afu_interrupt);
  386. event.header.type = CXL_EVENT_AFU_INTERRUPT;
  387. event.irq.irq = find_first_bit(ctx->irq_bitmap, ctx->irq_count) + 1;
  388. clear_bit(event.irq.irq - 1, ctx->irq_bitmap);
  389. if (bitmap_empty(ctx->irq_bitmap, ctx->irq_count))
  390. ctx->pending_irq = false;
  391. } else if (ctx->pending_fault) {
  392. pr_devel("afu_read delivering data storage fault\n");
  393. event.header.size += sizeof(struct cxl_event_data_storage);
  394. event.header.type = CXL_EVENT_DATA_STORAGE;
  395. event.fault.addr = ctx->fault_addr;
  396. event.fault.dsisr = ctx->fault_dsisr;
  397. ctx->pending_fault = false;
  398. } else if (ctx->pending_afu_err) {
  399. pr_devel("afu_read delivering afu error\n");
  400. event.header.size += sizeof(struct cxl_event_afu_error);
  401. event.header.type = CXL_EVENT_AFU_ERROR;
  402. event.afu_error.error = ctx->afu_err;
  403. ctx->pending_afu_err = false;
  404. } else if (ctx->status == CLOSED) {
  405. pr_devel("afu_read fatal error\n");
  406. spin_unlock_irqrestore(&ctx->lock, flags);
  407. return -EIO;
  408. } else
  409. WARN(1, "afu_read must be buggy\n");
  410. spin_unlock_irqrestore(&ctx->lock, flags);
  411. if (event.header.type == CXL_EVENT_AFU_DRIVER)
  412. return afu_driver_event_copy(ctx, buf, &event, pl);
  413. if (copy_to_user(buf, &event, event.header.size))
  414. return -EFAULT;
  415. return event.header.size;
  416. out:
  417. finish_wait(&ctx->wq, &wait);
  418. spin_unlock_irqrestore(&ctx->lock, flags);
  419. return rc;
  420. }
  421. /*
  422. * Note: if this is updated, we need to update api.c to patch the new ones in
  423. * too
  424. */
  425. const struct file_operations afu_fops = {
  426. .owner = THIS_MODULE,
  427. .open = afu_open,
  428. .poll = afu_poll,
  429. .read = afu_read,
  430. .release = afu_release,
  431. .unlocked_ioctl = afu_ioctl,
  432. .compat_ioctl = afu_compat_ioctl,
  433. .mmap = afu_mmap,
  434. };
  435. static const struct file_operations afu_master_fops = {
  436. .owner = THIS_MODULE,
  437. .open = afu_master_open,
  438. .poll = afu_poll,
  439. .read = afu_read,
  440. .release = afu_release,
  441. .unlocked_ioctl = afu_ioctl,
  442. .compat_ioctl = afu_compat_ioctl,
  443. .mmap = afu_mmap,
  444. };
  445. static char *cxl_devnode(const struct device *dev, umode_t *mode)
  446. {
  447. if (cpu_has_feature(CPU_FTR_HVMODE) &&
  448. CXL_DEVT_IS_CARD(dev->devt)) {
  449. /*
  450. * These minor numbers will eventually be used to program the
  451. * PSL and AFUs once we have dynamic reprogramming support
  452. */
  453. return NULL;
  454. }
  455. return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev));
  456. }
  457. static const struct class cxl_class = {
  458. .name = "cxl",
  459. .devnode = cxl_devnode,
  460. };
  461. static int cxl_add_chardev(struct cxl_afu *afu, dev_t devt, struct cdev *cdev,
  462. struct device **chardev, char *postfix, char *desc,
  463. const struct file_operations *fops)
  464. {
  465. struct device *dev;
  466. int rc;
  467. cdev_init(cdev, fops);
  468. rc = cdev_add(cdev, devt, 1);
  469. if (rc) {
  470. dev_err(&afu->dev, "Unable to add %s chardev: %i\n", desc, rc);
  471. return rc;
  472. }
  473. dev = device_create(&cxl_class, &afu->dev, devt, afu,
  474. "afu%i.%i%s", afu->adapter->adapter_num, afu->slice, postfix);
  475. if (IS_ERR(dev)) {
  476. rc = PTR_ERR(dev);
  477. dev_err(&afu->dev, "Unable to create %s chardev in sysfs: %i\n", desc, rc);
  478. goto err;
  479. }
  480. *chardev = dev;
  481. return 0;
  482. err:
  483. cdev_del(cdev);
  484. return rc;
  485. }
  486. int cxl_chardev_d_afu_add(struct cxl_afu *afu)
  487. {
  488. return cxl_add_chardev(afu, CXL_AFU_MKDEV_D(afu), &afu->afu_cdev_d,
  489. &afu->chardev_d, "d", "dedicated",
  490. &afu_master_fops); /* Uses master fops */
  491. }
  492. int cxl_chardev_m_afu_add(struct cxl_afu *afu)
  493. {
  494. return cxl_add_chardev(afu, CXL_AFU_MKDEV_M(afu), &afu->afu_cdev_m,
  495. &afu->chardev_m, "m", "master",
  496. &afu_master_fops);
  497. }
  498. int cxl_chardev_s_afu_add(struct cxl_afu *afu)
  499. {
  500. return cxl_add_chardev(afu, CXL_AFU_MKDEV_S(afu), &afu->afu_cdev_s,
  501. &afu->chardev_s, "s", "shared",
  502. &afu_fops);
  503. }
  504. void cxl_chardev_afu_remove(struct cxl_afu *afu)
  505. {
  506. if (afu->chardev_d) {
  507. cdev_del(&afu->afu_cdev_d);
  508. device_unregister(afu->chardev_d);
  509. afu->chardev_d = NULL;
  510. }
  511. if (afu->chardev_m) {
  512. cdev_del(&afu->afu_cdev_m);
  513. device_unregister(afu->chardev_m);
  514. afu->chardev_m = NULL;
  515. }
  516. if (afu->chardev_s) {
  517. cdev_del(&afu->afu_cdev_s);
  518. device_unregister(afu->chardev_s);
  519. afu->chardev_s = NULL;
  520. }
  521. }
  522. int cxl_register_afu(struct cxl_afu *afu)
  523. {
  524. afu->dev.class = &cxl_class;
  525. return device_register(&afu->dev);
  526. }
  527. int cxl_register_adapter(struct cxl *adapter)
  528. {
  529. adapter->dev.class = &cxl_class;
  530. /*
  531. * Future: When we support dynamically reprogramming the PSL & AFU we
  532. * will expose the interface to do that via a chardev:
  533. * adapter->dev.devt = CXL_CARD_MKDEV(adapter);
  534. */
  535. return device_register(&adapter->dev);
  536. }
  537. dev_t cxl_get_dev(void)
  538. {
  539. return cxl_dev;
  540. }
  541. int __init cxl_file_init(void)
  542. {
  543. int rc;
  544. /*
  545. * If these change we really need to update API. Either change some
  546. * flags or update API version number CXL_API_VERSION.
  547. */
  548. BUILD_BUG_ON(CXL_API_VERSION != 3);
  549. BUILD_BUG_ON(sizeof(struct cxl_ioctl_start_work) != 64);
  550. BUILD_BUG_ON(sizeof(struct cxl_event_header) != 8);
  551. BUILD_BUG_ON(sizeof(struct cxl_event_afu_interrupt) != 8);
  552. BUILD_BUG_ON(sizeof(struct cxl_event_data_storage) != 32);
  553. BUILD_BUG_ON(sizeof(struct cxl_event_afu_error) != 16);
  554. if ((rc = alloc_chrdev_region(&cxl_dev, 0, CXL_NUM_MINORS, "cxl"))) {
  555. pr_err("Unable to allocate CXL major number: %i\n", rc);
  556. return rc;
  557. }
  558. pr_devel("CXL device allocated, MAJOR %i\n", MAJOR(cxl_dev));
  559. rc = class_register(&cxl_class);
  560. if (rc) {
  561. pr_err("Unable to create CXL class\n");
  562. goto err;
  563. }
  564. return 0;
  565. err:
  566. unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
  567. return rc;
  568. }
  569. void cxl_file_exit(void)
  570. {
  571. unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
  572. class_unregister(&cxl_class);
  573. }