file.c 17 KB

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