file.c 17 KB

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