ioctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /**
  2. *
  3. * @file ioctl.c
  4. * @author Guillermo Marcus
  5. * @date 2009-04-05
  6. * @brief Contains the functions handling the different ioctl calls.
  7. *
  8. */
  9. #include <linux/version.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/pci.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/fs.h>
  19. #include <linux/cdev.h>
  20. #include <linux/sysfs.h>
  21. #include <asm/atomic.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/list.h>
  25. #include <asm/scatterlist.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/stat.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/wait.h>
  30. #include <linux/sched.h>
  31. #include <linux/iommu.h>
  32. #include "../pcilib/version.h"
  33. #include "config.h" /* Configuration for the driver */
  34. #include "compat.h" /* Compatibility functions/definitions */
  35. #include "pciDriver.h" /* External interface for the driver */
  36. #include "common.h" /* Internal definitions for all parts */
  37. #include "kmem.h" /* Internal definitions for kernel memory */
  38. #include "umem.h" /* Internal definitions for user space memory */
  39. #include "ioctl.h" /* Internal definitions for the ioctl part */
  40. /** Declares a variable of the given type with the given name and copies it from userspace */
  41. #define READ_FROM_USER(type, name) \
  42. type name; \
  43. if ((ret = copy_from_user(&name, (type*)arg, sizeof(name))) != 0) \
  44. return -EFAULT;
  45. /** Writes back the given variable with the given type to userspace */
  46. #define WRITE_TO_USER(type, name) \
  47. if ((ret = copy_to_user((type*)arg, &name, sizeof(name))) != 0) \
  48. return -EFAULT;
  49. /**
  50. *
  51. * Sets the mmap mode for following mmap() calls.
  52. *
  53. * @param arg Not a pointer, but either PCIDRIVER_MMAP_PCI or PCIDRIVER_MMAP_KMEM
  54. *
  55. */
  56. static int ioctl_mmap_mode(pcidriver_privdata_t *privdata, unsigned long arg)
  57. {
  58. if ((arg != PCIDRIVER_MMAP_PCI) && (arg != PCIDRIVER_MMAP_KMEM))
  59. return -EINVAL;
  60. /* change the mode */
  61. privdata->mmap_mode = arg;
  62. return 0;
  63. }
  64. /**
  65. *
  66. * Sets the mmap area (BAR) for following mmap() calls.
  67. *
  68. */
  69. static int ioctl_mmap_area(pcidriver_privdata_t *privdata, unsigned long arg)
  70. {
  71. /* validate input */
  72. if ((arg < PCIDRIVER_BAR0) || (arg > PCIDRIVER_BAR5))
  73. return -EINVAL;
  74. /* change the PCI area to mmap */
  75. privdata->mmap_area = arg;
  76. return 0;
  77. }
  78. /**
  79. *
  80. * Reads/writes a byte/word/dword of the device's PCI config.
  81. *
  82. * @see pcidriver_pci_read
  83. * @see pcidriver_pci_write
  84. *
  85. */
  86. static int ioctl_pci_config_read_write(pcidriver_privdata_t *privdata, unsigned int cmd, unsigned long arg)
  87. {
  88. #ifdef PCIDRIVER_DUMMY_DEVICE
  89. return -ENXIO;
  90. #else /* PCIDRIVER_DUMMY_DEVICE */
  91. int ret;
  92. READ_FROM_USER(pci_cfg_cmd, pci_cmd);
  93. if (cmd == PCIDRIVER_IOC_PCI_CFG_RD) {
  94. switch (pci_cmd.size) {
  95. case PCIDRIVER_PCI_CFG_SZ_BYTE:
  96. ret = pci_read_config_byte( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.byte) );
  97. break;
  98. case PCIDRIVER_PCI_CFG_SZ_WORD:
  99. ret = pci_read_config_word( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.word) );
  100. break;
  101. case PCIDRIVER_PCI_CFG_SZ_DWORD:
  102. ret = pci_read_config_dword( privdata->pdev, pci_cmd.addr, &(pci_cmd.val.dword) );
  103. break;
  104. default:
  105. return -EINVAL; /* Wrong size setting */
  106. }
  107. } else {
  108. switch (pci_cmd.size) {
  109. case PCIDRIVER_PCI_CFG_SZ_BYTE:
  110. ret = pci_write_config_byte( privdata->pdev, pci_cmd.addr, pci_cmd.val.byte );
  111. break;
  112. case PCIDRIVER_PCI_CFG_SZ_WORD:
  113. ret = pci_write_config_word( privdata->pdev, pci_cmd.addr, pci_cmd.val.word );
  114. break;
  115. case PCIDRIVER_PCI_CFG_SZ_DWORD:
  116. ret = pci_write_config_dword( privdata->pdev, pci_cmd.addr, pci_cmd.val.dword );
  117. break;
  118. default:
  119. return -EINVAL; /* Wrong size setting */
  120. break;
  121. }
  122. }
  123. WRITE_TO_USER(pci_cfg_cmd, pci_cmd);
  124. return 0;
  125. #endif /* PCIDRIVER_DUMMY_DEVICE */
  126. }
  127. /**
  128. *
  129. * Gets the PCI information for the device.
  130. *
  131. * @see pcidriver_pci_info
  132. *
  133. */
  134. static int ioctl_pci_info(pcidriver_privdata_t *privdata, unsigned long arg)
  135. {
  136. int ret;
  137. #ifdef PCIDRIVER_DUMMY_DEVICE
  138. READ_FROM_USER(pcilib_board_info_t, pci_info);
  139. memset(&pci_info, 0, sizeof(pci_info));
  140. WRITE_TO_USER(pcilib_board_info_t, pci_info);
  141. #else /* PCIDRIVER_DUMMY_DEVICE */
  142. int bar;
  143. READ_FROM_USER(pcilib_board_info_t, pci_info);
  144. pci_info.vendor_id = privdata->pdev->vendor;
  145. pci_info.device_id = privdata->pdev->device;
  146. pci_info.bus = privdata->pdev->bus->number;
  147. pci_info.slot = PCI_SLOT(privdata->pdev->devfn);
  148. pci_info.devfn = privdata->pdev->devfn;
  149. pci_info.func = PCI_FUNC(privdata->pdev->devfn);
  150. if ((ret = pci_read_config_byte(privdata->pdev, PCI_INTERRUPT_PIN, &(pci_info.interrupt_pin))) != 0)
  151. return ret;
  152. if ((ret = pci_read_config_byte(privdata->pdev, PCI_INTERRUPT_LINE, &(pci_info.interrupt_line))) != 0)
  153. return ret;
  154. for (bar = 0; bar < 6; bar++) {
  155. pci_info.bar_start[bar] = pci_resource_start(privdata->pdev, bar);
  156. pci_info.bar_length[bar] = pci_resource_len(privdata->pdev, bar);
  157. pci_info.bar_flags[bar] = pci_resource_flags(privdata->pdev, bar);
  158. }
  159. WRITE_TO_USER(pcilib_board_info_t, pci_info);
  160. #endif /* PCIDRIVER_DUMMY_DEVICE */
  161. return 0;
  162. }
  163. /**
  164. *
  165. * Allocates kernel memory.
  166. *
  167. * @see pcidriver_kmem_alloc
  168. *
  169. */
  170. static int ioctl_kmem_alloc(pcidriver_privdata_t *privdata, unsigned long arg)
  171. {
  172. int err, ret;
  173. READ_FROM_USER(kmem_handle_t, khandle);
  174. err = pcidriver_kmem_alloc(privdata, &khandle);
  175. WRITE_TO_USER(kmem_handle_t, khandle);
  176. return err;
  177. }
  178. /**
  179. *
  180. * Frees kernel memory.
  181. *
  182. * @see pcidriver_kmem_free
  183. *
  184. */
  185. static int ioctl_kmem_free(pcidriver_privdata_t *privdata, unsigned long arg)
  186. {
  187. int ret;
  188. READ_FROM_USER(kmem_handle_t, khandle);
  189. if ((ret = pcidriver_kmem_free(privdata, &khandle)) != 0)
  190. return ret;
  191. return 0;
  192. }
  193. /**
  194. *
  195. * Syncs kernel memory.
  196. *
  197. * @see pcidriver_kmem_sync
  198. *
  199. */
  200. static int ioctl_kmem_sync(pcidriver_privdata_t *privdata, unsigned long arg)
  201. {
  202. int ret;
  203. READ_FROM_USER(kmem_sync_t, ksync);
  204. if ((ret = pcidriver_kmem_sync(privdata, &ksync)) != 0)
  205. return ret;
  206. WRITE_TO_USER(kmem_sync_t, ksync);
  207. return 0;
  208. }
  209. /*
  210. *
  211. * Maps the given scatter/gather list from memory to PCI bus addresses.
  212. *
  213. * @see pcidriver_umem_sgmap
  214. *
  215. */
  216. static int ioctl_umem_sgmap(pcidriver_privdata_t *privdata, unsigned long arg)
  217. {
  218. int ret;
  219. READ_FROM_USER(umem_handle_t, uhandle);
  220. if ((ret = pcidriver_umem_sgmap(privdata, &uhandle)) != 0)
  221. return ret;
  222. WRITE_TO_USER(umem_handle_t, uhandle);
  223. return 0;
  224. }
  225. /**
  226. *
  227. * Unmaps the given scatter/gather list.
  228. *
  229. * @see pcidriver_umem_sgunmap
  230. *
  231. */
  232. static int ioctl_umem_sgunmap(pcidriver_privdata_t *privdata, unsigned long arg)
  233. {
  234. int ret;
  235. pcidriver_umem_entry_t *umem_entry;
  236. READ_FROM_USER(umem_handle_t, uhandle);
  237. /* Find the associated umem_entry for this buffer,
  238. * return -EINVAL if the specified handle id is invalid */
  239. if ((umem_entry = pcidriver_umem_find_entry_id(privdata, uhandle.handle_id)) == NULL)
  240. return -EINVAL;
  241. if ((ret = pcidriver_umem_sgunmap(privdata, umem_entry)) != 0)
  242. return ret;
  243. return 0;
  244. }
  245. /**
  246. *
  247. * Copies the scatter/gather list from kernelspace to userspace.
  248. *
  249. * @see pcidriver_umem_sgget
  250. *
  251. */
  252. static int ioctl_umem_sgget(pcidriver_privdata_t *privdata, unsigned long arg)
  253. {
  254. int ret;
  255. READ_FROM_USER(umem_sglist_t, usglist);
  256. /* The umem_sglist_t has a pointer to the scatter/gather list itself which
  257. * needs to be copied separately. The number of elements is stored in ->nents.
  258. * As the list can get very big, we need to use vmalloc. */
  259. if ((usglist.sg = vmalloc(usglist.nents * sizeof(umem_sgentry_t))) == NULL)
  260. return -ENOMEM;
  261. /* copy array to kernel structure */
  262. ret = copy_from_user(usglist.sg, ((umem_sglist_t *)arg)->sg, (usglist.nents)*sizeof(umem_sgentry_t));
  263. if (ret) return -EFAULT;
  264. if ((ret = pcidriver_umem_sgget(privdata, &usglist)) != 0)
  265. return ret;
  266. /* write data to user space */
  267. ret = copy_to_user(((umem_sglist_t *)arg)->sg, usglist.sg, (usglist.nents)*sizeof(umem_sgentry_t));
  268. if (ret) return -EFAULT;
  269. /* free array memory */
  270. vfree(usglist.sg);
  271. /* restore sg pointer to vma address in user space before copying */
  272. usglist.sg = ((umem_sglist_t *)arg)->sg;
  273. WRITE_TO_USER(umem_sglist_t, usglist);
  274. return 0;
  275. }
  276. /**
  277. *
  278. * Syncs user memory.
  279. *
  280. * @see pcidriver_umem_sync
  281. *
  282. */
  283. static int ioctl_umem_sync(pcidriver_privdata_t *privdata, unsigned long arg)
  284. {
  285. int ret;
  286. READ_FROM_USER(umem_handle_t, uhandle);
  287. return pcidriver_umem_sync( privdata, &uhandle );
  288. }
  289. /**
  290. *
  291. * Waits for an interrupt
  292. *
  293. * @param arg Not a pointer, but the irq source to wait for (unsigned int)
  294. *
  295. */
  296. static int ioctl_wait_interrupt(pcidriver_privdata_t *privdata, unsigned long arg)
  297. {
  298. #ifdef ENABLE_IRQ
  299. int ret;
  300. unsigned long timeout;
  301. unsigned int irq_source;
  302. unsigned long temp = 0;
  303. READ_FROM_USER(interrupt_wait_t, irq_handle);
  304. irq_source = irq_handle.source;
  305. if (irq_source >= PCIDRIVER_INT_MAXSOURCES)
  306. return -EFAULT; /* User tried to overrun the IRQ_SOURCES array */
  307. timeout = jiffies + (irq_handle.timeout * HZ / 1000000);
  308. /* Thanks to Joern for the correction and tips! */
  309. /* done this way to avoid wrong behaviour (endless loop) of the compiler in AMD platforms */
  310. do {
  311. /* We wait here with an interruptible timeout. This will be interrupted
  312. * by int.c:check_acknowledge_channel() as soon as in interrupt for
  313. * the specified source arrives. */
  314. wait_event_interruptible_timeout( (privdata->irq_queues[irq_source]), (atomic_read(&(privdata->irq_outstanding[irq_source])) > 0), (10*HZ/1000) );
  315. if (atomic_add_negative( -1, &(privdata->irq_outstanding[irq_source])) )
  316. atomic_inc( &(privdata->irq_outstanding[irq_source]) );
  317. else
  318. temp = 1;
  319. } while ((!temp)&&(jiffies < timeout));
  320. if ((temp)&&(irq_handle.count)) {
  321. while (!atomic_add_negative( -1, &(privdata->irq_outstanding[irq_source]))) temp++;
  322. atomic_inc( &(privdata->irq_outstanding[irq_source]) );
  323. }
  324. irq_handle.count = temp;
  325. WRITE_TO_USER(interrupt_wait_t, irq_handle);
  326. return 0;
  327. #else
  328. mod_info("Asked to wait for interrupt but interrupts are not enabled in the driver\n");
  329. return -EFAULT;
  330. #endif
  331. }
  332. /**
  333. *
  334. * Clears the interrupt wait queue.
  335. *
  336. * @param arg Not a pointer, but the irq source (unsigned int)
  337. * @returns -EFAULT if the user specified an irq source out of range
  338. *
  339. */
  340. static int ioctl_clear_ioq(pcidriver_privdata_t *privdata, unsigned long arg)
  341. {
  342. #ifdef ENABLE_IRQ
  343. unsigned int irq_source;
  344. if (arg >= PCIDRIVER_INT_MAXSOURCES)
  345. return -EFAULT;
  346. irq_source = arg;
  347. atomic_set(&(privdata->irq_outstanding[irq_source]), 0);
  348. return 0;
  349. #else
  350. mod_info("Asked to wait for interrupt but interrupts are not enabled in the driver\n");
  351. return -EFAULT;
  352. #endif
  353. }
  354. /**
  355. *
  356. * Gets the device and API versions.
  357. *
  358. * @see pcilib_driver_version_t
  359. *
  360. */
  361. static int ioctl_version(pcidriver_privdata_t *privdata, unsigned long arg)
  362. {
  363. int ret;
  364. pcilib_driver_version_t info;
  365. info = (pcilib_driver_version_t) {
  366. .version = PCILIB_VERSION,
  367. .interface = PCIDRIVER_INTERFACE_VERSION,
  368. .ioctls = PCIDRIVER_IOC_MAX + 1
  369. };
  370. WRITE_TO_USER(pcilib_driver_version_t, info);
  371. return 0;
  372. }
  373. /**
  374. *
  375. * Gets current device and driver configuration
  376. *
  377. * @see pcilib_device_state_t
  378. *
  379. */
  380. static int ioctl_device_state(pcidriver_privdata_t *privdata, unsigned long arg)
  381. {
  382. int ret;
  383. pcilib_device_state_t info;
  384. #ifdef PCIDRIVER_DUMMY_DEVICE
  385. memset(&info, 0, sizeof(info));
  386. #else /* PCIDRIVER_DUMMY_DEVICE */
  387. info = (pcilib_device_state_t) {
  388. .iommu = iommu_present(privdata->pdev->dev.bus),
  389. .mps = pcidriver_pcie_get_mps(privdata->pdev),
  390. .readrq = pcie_get_readrq(privdata->pdev),
  391. .dma_mask = privdata->pdev->dma_mask
  392. };
  393. #endif /* PCIDRIVER_DUMMY_DEVICE */
  394. WRITE_TO_USER(pcilib_device_state_t, info);
  395. return 0;
  396. }
  397. /**
  398. *
  399. * Sets DMA mask for the following DMA mappings.
  400. *
  401. * @param arg Not a pointer, but a number of bits
  402. *
  403. */
  404. static int ioctl_set_dma_mask(pcidriver_privdata_t *privdata, unsigned long arg)
  405. {
  406. #ifndef PCIDRIVER_DUMMY_DEVICE
  407. int err;
  408. if ((arg < 24) || (arg > 64))
  409. return -EINVAL;
  410. err = pci_set_dma_mask(privdata->pdev, DMA_BIT_MASK(arg));
  411. if (err < 0) {
  412. printk(KERN_ERR "pci_set_dma_mask(%lu) failed\n", arg);
  413. return err;
  414. }
  415. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  416. return 0;
  417. }
  418. /**
  419. *
  420. * Sets Max Payload Size.
  421. *
  422. * @param arg Not a pointer, but payload size in bits
  423. *
  424. */
  425. static int ioctl_set_mps(pcidriver_privdata_t *privdata, unsigned long arg)
  426. {
  427. #ifndef PCIDRIVER_DUMMY_DEVICE
  428. int err;
  429. if ((arg != 128) && (arg != 256) && (arg != 512))
  430. return -EINVAL;
  431. err = pcidriver_pcie_set_mps(privdata->pdev, arg);
  432. if (err < 0) {
  433. printk(KERN_ERR "pcie_set_mps(%lu) failed\n", arg);
  434. return err;
  435. }
  436. #endif /* ! PCIDRIVER_DUMMY_DEVICE */
  437. return 0;
  438. }
  439. /**
  440. *
  441. * This function handles all ioctl file operations.
  442. * Generally, the data of the ioctl is copied from userspace to kernelspace, a separate
  443. * function is called to handle the ioctl itself, then the data is copied back to userspace.
  444. *
  445. * @returns -EFAULT when an invalid memory pointer is passed
  446. *
  447. */
  448. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
  449. int pcidriver_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  450. #else
  451. long pcidriver_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  452. #endif
  453. {
  454. pcidriver_privdata_t *privdata = filp->private_data;
  455. /* Select the appropiate command */
  456. switch (cmd) {
  457. case PCIDRIVER_IOC_MMAP_MODE:
  458. return ioctl_mmap_mode(privdata, arg);
  459. case PCIDRIVER_IOC_MMAP_AREA:
  460. return ioctl_mmap_area(privdata, arg);
  461. case PCIDRIVER_IOC_PCI_CFG_RD:
  462. case PCIDRIVER_IOC_PCI_CFG_WR:
  463. return ioctl_pci_config_read_write(privdata, cmd, arg);
  464. case PCIDRIVER_IOC_PCI_INFO:
  465. return ioctl_pci_info(privdata, arg);
  466. case PCIDRIVER_IOC_KMEM_ALLOC:
  467. return ioctl_kmem_alloc(privdata, arg);
  468. case PCIDRIVER_IOC_KMEM_FREE:
  469. return ioctl_kmem_free(privdata, arg);
  470. case PCIDRIVER_IOC_KMEM_SYNC:
  471. return ioctl_kmem_sync(privdata, arg);
  472. case PCIDRIVER_IOC_UMEM_SGMAP:
  473. return ioctl_umem_sgmap(privdata, arg);
  474. case PCIDRIVER_IOC_UMEM_SGUNMAP:
  475. return ioctl_umem_sgunmap(privdata, arg);
  476. case PCIDRIVER_IOC_UMEM_SGGET:
  477. return ioctl_umem_sgget(privdata, arg);
  478. case PCIDRIVER_IOC_UMEM_SYNC:
  479. return ioctl_umem_sync(privdata, arg);
  480. case PCIDRIVER_IOC_WAITI:
  481. return ioctl_wait_interrupt(privdata, arg);
  482. case PCIDRIVER_IOC_CLEAR_IOQ:
  483. return ioctl_clear_ioq(privdata, arg);
  484. case PCIDRIVER_IOC_VERSION:
  485. return ioctl_version(privdata, arg);
  486. case PCIDRIVER_IOC_DEVICE_STATE:
  487. return ioctl_device_state(privdata, arg);
  488. case PCIDRIVER_IOC_DMA_MASK:
  489. return ioctl_set_dma_mask(privdata, arg);
  490. case PCIDRIVER_IOC_MPS:
  491. return ioctl_set_mps(privdata, arg);
  492. default:
  493. return -EINVAL;
  494. }
  495. }