file.c 18 KB

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