card_dev.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /**
  2. * IBM Accelerator Family 'GenWQE'
  3. *
  4. * (C) Copyright IBM Corp. 2013
  5. *
  6. * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
  7. * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
  8. * Author: Michael Jung <mijung@gmx.net>
  9. * Author: Michael Ruettger <michael@ibmra.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. /*
  21. * Character device representation of the GenWQE device. This allows
  22. * user-space applications to communicate with the card.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/string.h>
  29. #include <linux/fs.h>
  30. #include <linux/sched.h>
  31. #include <linux/wait.h>
  32. #include <linux/delay.h>
  33. #include <linux/atomic.h>
  34. #include "card_base.h"
  35. #include "card_ddcb.h"
  36. static int genwqe_open_files(struct genwqe_dev *cd)
  37. {
  38. int rc;
  39. unsigned long flags;
  40. spin_lock_irqsave(&cd->file_lock, flags);
  41. rc = list_empty(&cd->file_list);
  42. spin_unlock_irqrestore(&cd->file_lock, flags);
  43. return !rc;
  44. }
  45. static void genwqe_add_file(struct genwqe_dev *cd, struct genwqe_file *cfile)
  46. {
  47. unsigned long flags;
  48. cfile->owner = current;
  49. spin_lock_irqsave(&cd->file_lock, flags);
  50. list_add(&cfile->list, &cd->file_list);
  51. spin_unlock_irqrestore(&cd->file_lock, flags);
  52. }
  53. static int genwqe_del_file(struct genwqe_dev *cd, struct genwqe_file *cfile)
  54. {
  55. unsigned long flags;
  56. spin_lock_irqsave(&cd->file_lock, flags);
  57. list_del(&cfile->list);
  58. spin_unlock_irqrestore(&cd->file_lock, flags);
  59. return 0;
  60. }
  61. static void genwqe_add_pin(struct genwqe_file *cfile, struct dma_mapping *m)
  62. {
  63. unsigned long flags;
  64. spin_lock_irqsave(&cfile->pin_lock, flags);
  65. list_add(&m->pin_list, &cfile->pin_list);
  66. spin_unlock_irqrestore(&cfile->pin_lock, flags);
  67. }
  68. static int genwqe_del_pin(struct genwqe_file *cfile, struct dma_mapping *m)
  69. {
  70. unsigned long flags;
  71. spin_lock_irqsave(&cfile->pin_lock, flags);
  72. list_del(&m->pin_list);
  73. spin_unlock_irqrestore(&cfile->pin_lock, flags);
  74. return 0;
  75. }
  76. /**
  77. * genwqe_search_pin() - Search for the mapping for a userspace address
  78. * @cfile: Descriptor of opened file
  79. * @u_addr: User virtual address
  80. * @size: Size of buffer
  81. * @dma_addr: DMA address to be updated
  82. *
  83. * Return: Pointer to the corresponding mapping NULL if not found
  84. */
  85. static struct dma_mapping *genwqe_search_pin(struct genwqe_file *cfile,
  86. unsigned long u_addr,
  87. unsigned int size,
  88. void **virt_addr)
  89. {
  90. unsigned long flags;
  91. struct dma_mapping *m;
  92. spin_lock_irqsave(&cfile->pin_lock, flags);
  93. list_for_each_entry(m, &cfile->pin_list, pin_list) {
  94. if ((((u64)m->u_vaddr) <= (u_addr)) &&
  95. (((u64)m->u_vaddr + m->size) >= (u_addr + size))) {
  96. if (virt_addr)
  97. *virt_addr = m->k_vaddr +
  98. (u_addr - (u64)m->u_vaddr);
  99. spin_unlock_irqrestore(&cfile->pin_lock, flags);
  100. return m;
  101. }
  102. }
  103. spin_unlock_irqrestore(&cfile->pin_lock, flags);
  104. return NULL;
  105. }
  106. static void __genwqe_add_mapping(struct genwqe_file *cfile,
  107. struct dma_mapping *dma_map)
  108. {
  109. unsigned long flags;
  110. spin_lock_irqsave(&cfile->map_lock, flags);
  111. list_add(&dma_map->card_list, &cfile->map_list);
  112. spin_unlock_irqrestore(&cfile->map_lock, flags);
  113. }
  114. static void __genwqe_del_mapping(struct genwqe_file *cfile,
  115. struct dma_mapping *dma_map)
  116. {
  117. unsigned long flags;
  118. spin_lock_irqsave(&cfile->map_lock, flags);
  119. list_del(&dma_map->card_list);
  120. spin_unlock_irqrestore(&cfile->map_lock, flags);
  121. }
  122. /**
  123. * __genwqe_search_mapping() - Search for the mapping for a userspace address
  124. * @cfile: descriptor of opened file
  125. * @u_addr: user virtual address
  126. * @size: size of buffer
  127. * @dma_addr: DMA address to be updated
  128. * Return: Pointer to the corresponding mapping NULL if not found
  129. */
  130. static struct dma_mapping *__genwqe_search_mapping(struct genwqe_file *cfile,
  131. unsigned long u_addr,
  132. unsigned int size,
  133. dma_addr_t *dma_addr,
  134. void **virt_addr)
  135. {
  136. unsigned long flags;
  137. struct dma_mapping *m;
  138. struct pci_dev *pci_dev = cfile->cd->pci_dev;
  139. spin_lock_irqsave(&cfile->map_lock, flags);
  140. list_for_each_entry(m, &cfile->map_list, card_list) {
  141. if ((((u64)m->u_vaddr) <= (u_addr)) &&
  142. (((u64)m->u_vaddr + m->size) >= (u_addr + size))) {
  143. /* match found: current is as expected and
  144. addr is in range */
  145. if (dma_addr)
  146. *dma_addr = m->dma_addr +
  147. (u_addr - (u64)m->u_vaddr);
  148. if (virt_addr)
  149. *virt_addr = m->k_vaddr +
  150. (u_addr - (u64)m->u_vaddr);
  151. spin_unlock_irqrestore(&cfile->map_lock, flags);
  152. return m;
  153. }
  154. }
  155. spin_unlock_irqrestore(&cfile->map_lock, flags);
  156. dev_err(&pci_dev->dev,
  157. "[%s] Entry not found: u_addr=%lx, size=%x\n",
  158. __func__, u_addr, size);
  159. return NULL;
  160. }
  161. static void genwqe_remove_mappings(struct genwqe_file *cfile)
  162. {
  163. int i = 0;
  164. struct list_head *node, *next;
  165. struct dma_mapping *dma_map;
  166. struct genwqe_dev *cd = cfile->cd;
  167. struct pci_dev *pci_dev = cfile->cd->pci_dev;
  168. list_for_each_safe(node, next, &cfile->map_list) {
  169. dma_map = list_entry(node, struct dma_mapping, card_list);
  170. list_del_init(&dma_map->card_list);
  171. /*
  172. * This is really a bug, because those things should
  173. * have been already tidied up.
  174. *
  175. * GENWQE_MAPPING_RAW should have been removed via mmunmap().
  176. * GENWQE_MAPPING_SGL_TEMP should be removed by tidy up code.
  177. */
  178. dev_err(&pci_dev->dev,
  179. "[%s] %d. cleanup mapping: u_vaddr=%p u_kaddr=%016lx dma_addr=%lx\n",
  180. __func__, i++, dma_map->u_vaddr,
  181. (unsigned long)dma_map->k_vaddr,
  182. (unsigned long)dma_map->dma_addr);
  183. if (dma_map->type == GENWQE_MAPPING_RAW) {
  184. /* we allocated this dynamically */
  185. __genwqe_free_consistent(cd, dma_map->size,
  186. dma_map->k_vaddr,
  187. dma_map->dma_addr);
  188. kfree(dma_map);
  189. } else if (dma_map->type == GENWQE_MAPPING_SGL_TEMP) {
  190. /* we use dma_map statically from the request */
  191. genwqe_user_vunmap(cd, dma_map, NULL);
  192. }
  193. }
  194. }
  195. static void genwqe_remove_pinnings(struct genwqe_file *cfile)
  196. {
  197. struct list_head *node, *next;
  198. struct dma_mapping *dma_map;
  199. struct genwqe_dev *cd = cfile->cd;
  200. list_for_each_safe(node, next, &cfile->pin_list) {
  201. dma_map = list_entry(node, struct dma_mapping, pin_list);
  202. /*
  203. * This is not a bug, because a killed processed might
  204. * not call the unpin ioctl, which is supposed to free
  205. * the resources.
  206. *
  207. * Pinnings are dymically allocated and need to be
  208. * deleted.
  209. */
  210. list_del_init(&dma_map->pin_list);
  211. genwqe_user_vunmap(cd, dma_map, NULL);
  212. kfree(dma_map);
  213. }
  214. }
  215. /**
  216. * genwqe_kill_fasync() - Send signal to all processes with open GenWQE files
  217. *
  218. * E.g. genwqe_send_signal(cd, SIGIO);
  219. */
  220. static int genwqe_kill_fasync(struct genwqe_dev *cd, int sig)
  221. {
  222. unsigned int files = 0;
  223. unsigned long flags;
  224. struct genwqe_file *cfile;
  225. spin_lock_irqsave(&cd->file_lock, flags);
  226. list_for_each_entry(cfile, &cd->file_list, list) {
  227. if (cfile->async_queue)
  228. kill_fasync(&cfile->async_queue, sig, POLL_HUP);
  229. files++;
  230. }
  231. spin_unlock_irqrestore(&cd->file_lock, flags);
  232. return files;
  233. }
  234. static int genwqe_force_sig(struct genwqe_dev *cd, int sig)
  235. {
  236. unsigned int files = 0;
  237. unsigned long flags;
  238. struct genwqe_file *cfile;
  239. spin_lock_irqsave(&cd->file_lock, flags);
  240. list_for_each_entry(cfile, &cd->file_list, list) {
  241. force_sig(sig, cfile->owner);
  242. files++;
  243. }
  244. spin_unlock_irqrestore(&cd->file_lock, flags);
  245. return files;
  246. }
  247. /**
  248. * genwqe_open() - file open
  249. * @inode: file system information
  250. * @filp: file handle
  251. *
  252. * This function is executed whenever an application calls
  253. * open("/dev/genwqe",..).
  254. *
  255. * Return: 0 if successful or <0 if errors
  256. */
  257. static int genwqe_open(struct inode *inode, struct file *filp)
  258. {
  259. struct genwqe_dev *cd;
  260. struct genwqe_file *cfile;
  261. struct pci_dev *pci_dev;
  262. cfile = kzalloc(sizeof(*cfile), GFP_KERNEL);
  263. if (cfile == NULL)
  264. return -ENOMEM;
  265. cd = container_of(inode->i_cdev, struct genwqe_dev, cdev_genwqe);
  266. pci_dev = cd->pci_dev;
  267. cfile->cd = cd;
  268. cfile->filp = filp;
  269. cfile->client = NULL;
  270. spin_lock_init(&cfile->map_lock); /* list of raw memory allocations */
  271. INIT_LIST_HEAD(&cfile->map_list);
  272. spin_lock_init(&cfile->pin_lock); /* list of user pinned memory */
  273. INIT_LIST_HEAD(&cfile->pin_list);
  274. filp->private_data = cfile;
  275. genwqe_add_file(cd, cfile);
  276. return 0;
  277. }
  278. /**
  279. * genwqe_fasync() - Setup process to receive SIGIO.
  280. * @fd: file descriptor
  281. * @filp: file handle
  282. * @mode: file mode
  283. *
  284. * Sending a signal is working as following:
  285. *
  286. * if (cdev->async_queue)
  287. * kill_fasync(&cdev->async_queue, SIGIO, POLL_IN);
  288. *
  289. * Some devices also implement asynchronous notification to indicate
  290. * when the device can be written; in this case, of course,
  291. * kill_fasync must be called with a mode of POLL_OUT.
  292. */
  293. static int genwqe_fasync(int fd, struct file *filp, int mode)
  294. {
  295. struct genwqe_file *cdev = (struct genwqe_file *)filp->private_data;
  296. return fasync_helper(fd, filp, mode, &cdev->async_queue);
  297. }
  298. /**
  299. * genwqe_release() - file close
  300. * @inode: file system information
  301. * @filp: file handle
  302. *
  303. * This function is executed whenever an application calls 'close(fd_genwqe)'
  304. *
  305. * Return: always 0
  306. */
  307. static int genwqe_release(struct inode *inode, struct file *filp)
  308. {
  309. struct genwqe_file *cfile = (struct genwqe_file *)filp->private_data;
  310. struct genwqe_dev *cd = cfile->cd;
  311. /* there must be no entries in these lists! */
  312. genwqe_remove_mappings(cfile);
  313. genwqe_remove_pinnings(cfile);
  314. /* remove this filp from the asynchronously notified filp's */
  315. genwqe_fasync(-1, filp, 0);
  316. /*
  317. * For this to work we must not release cd when this cfile is
  318. * not yet released, otherwise the list entry is invalid,
  319. * because the list itself gets reinstantiated!
  320. */
  321. genwqe_del_file(cd, cfile);
  322. kfree(cfile);
  323. return 0;
  324. }
  325. static void genwqe_vma_open(struct vm_area_struct *vma)
  326. {
  327. /* nothing ... */
  328. }
  329. /**
  330. * genwqe_vma_close() - Called each time when vma is unmapped
  331. *
  332. * Free memory which got allocated by GenWQE mmap().
  333. */
  334. static void genwqe_vma_close(struct vm_area_struct *vma)
  335. {
  336. unsigned long vsize = vma->vm_end - vma->vm_start;
  337. struct inode *inode = file_inode(vma->vm_file);
  338. struct dma_mapping *dma_map;
  339. struct genwqe_dev *cd = container_of(inode->i_cdev, struct genwqe_dev,
  340. cdev_genwqe);
  341. struct pci_dev *pci_dev = cd->pci_dev;
  342. dma_addr_t d_addr = 0;
  343. struct genwqe_file *cfile = vma->vm_private_data;
  344. dma_map = __genwqe_search_mapping(cfile, vma->vm_start, vsize,
  345. &d_addr, NULL);
  346. if (dma_map == NULL) {
  347. dev_err(&pci_dev->dev,
  348. " [%s] err: mapping not found: v=%lx, p=%lx s=%lx\n",
  349. __func__, vma->vm_start, vma->vm_pgoff << PAGE_SHIFT,
  350. vsize);
  351. return;
  352. }
  353. __genwqe_del_mapping(cfile, dma_map);
  354. __genwqe_free_consistent(cd, dma_map->size, dma_map->k_vaddr,
  355. dma_map->dma_addr);
  356. kfree(dma_map);
  357. }
  358. static const struct vm_operations_struct genwqe_vma_ops = {
  359. .open = genwqe_vma_open,
  360. .close = genwqe_vma_close,
  361. };
  362. /**
  363. * genwqe_mmap() - Provide contignous buffers to userspace
  364. *
  365. * We use mmap() to allocate contignous buffers used for DMA
  366. * transfers. After the buffer is allocated we remap it to user-space
  367. * and remember a reference to our dma_mapping data structure, where
  368. * we store the associated DMA address and allocated size.
  369. *
  370. * When we receive a DDCB execution request with the ATS bits set to
  371. * plain buffer, we lookup our dma_mapping list to find the
  372. * corresponding DMA address for the associated user-space address.
  373. */
  374. static int genwqe_mmap(struct file *filp, struct vm_area_struct *vma)
  375. {
  376. int rc;
  377. unsigned long pfn, vsize = vma->vm_end - vma->vm_start;
  378. struct genwqe_file *cfile = (struct genwqe_file *)filp->private_data;
  379. struct genwqe_dev *cd = cfile->cd;
  380. struct dma_mapping *dma_map;
  381. if (vsize == 0)
  382. return -EINVAL;
  383. if (get_order(vsize) > MAX_ORDER)
  384. return -ENOMEM;
  385. dma_map = kzalloc(sizeof(struct dma_mapping), GFP_KERNEL);
  386. if (dma_map == NULL)
  387. return -ENOMEM;
  388. genwqe_mapping_init(dma_map, GENWQE_MAPPING_RAW);
  389. dma_map->u_vaddr = (void *)vma->vm_start;
  390. dma_map->size = vsize;
  391. dma_map->nr_pages = DIV_ROUND_UP(vsize, PAGE_SIZE);
  392. dma_map->k_vaddr = __genwqe_alloc_consistent(cd, vsize,
  393. &dma_map->dma_addr);
  394. if (dma_map->k_vaddr == NULL) {
  395. rc = -ENOMEM;
  396. goto free_dma_map;
  397. }
  398. if (capable(CAP_SYS_ADMIN) && (vsize > sizeof(dma_addr_t)))
  399. *(dma_addr_t *)dma_map->k_vaddr = dma_map->dma_addr;
  400. pfn = virt_to_phys(dma_map->k_vaddr) >> PAGE_SHIFT;
  401. rc = remap_pfn_range(vma,
  402. vma->vm_start,
  403. pfn,
  404. vsize,
  405. vma->vm_page_prot);
  406. if (rc != 0) {
  407. rc = -EFAULT;
  408. goto free_dma_mem;
  409. }
  410. vma->vm_private_data = cfile;
  411. vma->vm_ops = &genwqe_vma_ops;
  412. __genwqe_add_mapping(cfile, dma_map);
  413. return 0;
  414. free_dma_mem:
  415. __genwqe_free_consistent(cd, dma_map->size,
  416. dma_map->k_vaddr,
  417. dma_map->dma_addr);
  418. free_dma_map:
  419. kfree(dma_map);
  420. return rc;
  421. }
  422. /**
  423. * do_flash_update() - Excute flash update (write image or CVPD)
  424. * @cd: genwqe device
  425. * @load: details about image load
  426. *
  427. * Return: 0 if successful
  428. */
  429. #define FLASH_BLOCK 0x40000 /* we use 256k blocks */
  430. static int do_flash_update(struct genwqe_file *cfile,
  431. struct genwqe_bitstream *load)
  432. {
  433. int rc = 0;
  434. int blocks_to_flash;
  435. dma_addr_t dma_addr;
  436. u64 flash = 0;
  437. size_t tocopy = 0;
  438. u8 __user *buf;
  439. u8 *xbuf;
  440. u32 crc;
  441. u8 cmdopts;
  442. struct genwqe_dev *cd = cfile->cd;
  443. struct file *filp = cfile->filp;
  444. struct pci_dev *pci_dev = cd->pci_dev;
  445. if ((load->size & 0x3) != 0)
  446. return -EINVAL;
  447. if (((unsigned long)(load->data_addr) & ~PAGE_MASK) != 0)
  448. return -EINVAL;
  449. /* FIXME Bits have changed for new service layer! */
  450. switch ((char)load->partition) {
  451. case '0':
  452. cmdopts = 0x14;
  453. break; /* download/erase_first/part_0 */
  454. case '1':
  455. cmdopts = 0x1C;
  456. break; /* download/erase_first/part_1 */
  457. case 'v':
  458. cmdopts = 0x0C;
  459. break; /* download/erase_first/vpd */
  460. default:
  461. return -EINVAL;
  462. }
  463. buf = (u8 __user *)load->data_addr;
  464. xbuf = __genwqe_alloc_consistent(cd, FLASH_BLOCK, &dma_addr);
  465. if (xbuf == NULL)
  466. return -ENOMEM;
  467. blocks_to_flash = load->size / FLASH_BLOCK;
  468. while (load->size) {
  469. struct genwqe_ddcb_cmd *req;
  470. /*
  471. * We must be 4 byte aligned. Buffer must be 0 appened
  472. * to have defined values when calculating CRC.
  473. */
  474. tocopy = min_t(size_t, load->size, FLASH_BLOCK);
  475. rc = copy_from_user(xbuf, buf, tocopy);
  476. if (rc) {
  477. rc = -EFAULT;
  478. goto free_buffer;
  479. }
  480. crc = genwqe_crc32(xbuf, tocopy, 0xffffffff);
  481. dev_dbg(&pci_dev->dev,
  482. "[%s] DMA: %lx CRC: %08x SZ: %ld %d\n",
  483. __func__, (unsigned long)dma_addr, crc, tocopy,
  484. blocks_to_flash);
  485. /* prepare DDCB for SLU process */
  486. req = ddcb_requ_alloc();
  487. if (req == NULL) {
  488. rc = -ENOMEM;
  489. goto free_buffer;
  490. }
  491. req->cmd = SLCMD_MOVE_FLASH;
  492. req->cmdopts = cmdopts;
  493. /* prepare invariant values */
  494. if (genwqe_get_slu_id(cd) <= 0x2) {
  495. *(__be64 *)&req->__asiv[0] = cpu_to_be64(dma_addr);
  496. *(__be64 *)&req->__asiv[8] = cpu_to_be64(tocopy);
  497. *(__be64 *)&req->__asiv[16] = cpu_to_be64(flash);
  498. *(__be32 *)&req->__asiv[24] = cpu_to_be32(0);
  499. req->__asiv[24] = load->uid;
  500. *(__be32 *)&req->__asiv[28] = cpu_to_be32(crc);
  501. /* for simulation only */
  502. *(__be64 *)&req->__asiv[88] = cpu_to_be64(load->slu_id);
  503. *(__be64 *)&req->__asiv[96] = cpu_to_be64(load->app_id);
  504. req->asiv_length = 32; /* bytes included in crc calc */
  505. } else { /* setup DDCB for ATS architecture */
  506. *(__be64 *)&req->asiv[0] = cpu_to_be64(dma_addr);
  507. *(__be32 *)&req->asiv[8] = cpu_to_be32(tocopy);
  508. *(__be32 *)&req->asiv[12] = cpu_to_be32(0); /* resvd */
  509. *(__be64 *)&req->asiv[16] = cpu_to_be64(flash);
  510. *(__be32 *)&req->asiv[24] = cpu_to_be32(load->uid<<24);
  511. *(__be32 *)&req->asiv[28] = cpu_to_be32(crc);
  512. /* for simulation only */
  513. *(__be64 *)&req->asiv[80] = cpu_to_be64(load->slu_id);
  514. *(__be64 *)&req->asiv[88] = cpu_to_be64(load->app_id);
  515. /* Rd only */
  516. req->ats = 0x4ULL << 44;
  517. req->asiv_length = 40; /* bytes included in crc calc */
  518. }
  519. req->asv_length = 8;
  520. /* For Genwqe5 we get back the calculated CRC */
  521. *(u64 *)&req->asv[0] = 0ULL; /* 0x80 */
  522. rc = __genwqe_execute_raw_ddcb(cd, req, filp->f_flags);
  523. load->retc = req->retc;
  524. load->attn = req->attn;
  525. load->progress = req->progress;
  526. if (rc < 0) {
  527. ddcb_requ_free(req);
  528. goto free_buffer;
  529. }
  530. if (req->retc != DDCB_RETC_COMPLETE) {
  531. rc = -EIO;
  532. ddcb_requ_free(req);
  533. goto free_buffer;
  534. }
  535. load->size -= tocopy;
  536. flash += tocopy;
  537. buf += tocopy;
  538. blocks_to_flash--;
  539. ddcb_requ_free(req);
  540. }
  541. free_buffer:
  542. __genwqe_free_consistent(cd, FLASH_BLOCK, xbuf, dma_addr);
  543. return rc;
  544. }
  545. static int do_flash_read(struct genwqe_file *cfile,
  546. struct genwqe_bitstream *load)
  547. {
  548. int rc, blocks_to_flash;
  549. dma_addr_t dma_addr;
  550. u64 flash = 0;
  551. size_t tocopy = 0;
  552. u8 __user *buf;
  553. u8 *xbuf;
  554. u8 cmdopts;
  555. struct genwqe_dev *cd = cfile->cd;
  556. struct file *filp = cfile->filp;
  557. struct pci_dev *pci_dev = cd->pci_dev;
  558. struct genwqe_ddcb_cmd *cmd;
  559. if ((load->size & 0x3) != 0)
  560. return -EINVAL;
  561. if (((unsigned long)(load->data_addr) & ~PAGE_MASK) != 0)
  562. return -EINVAL;
  563. /* FIXME Bits have changed for new service layer! */
  564. switch ((char)load->partition) {
  565. case '0':
  566. cmdopts = 0x12;
  567. break; /* upload/part_0 */
  568. case '1':
  569. cmdopts = 0x1A;
  570. break; /* upload/part_1 */
  571. case 'v':
  572. cmdopts = 0x0A;
  573. break; /* upload/vpd */
  574. default:
  575. return -EINVAL;
  576. }
  577. buf = (u8 __user *)load->data_addr;
  578. xbuf = __genwqe_alloc_consistent(cd, FLASH_BLOCK, &dma_addr);
  579. if (xbuf == NULL)
  580. return -ENOMEM;
  581. blocks_to_flash = load->size / FLASH_BLOCK;
  582. while (load->size) {
  583. /*
  584. * We must be 4 byte aligned. Buffer must be 0 appened
  585. * to have defined values when calculating CRC.
  586. */
  587. tocopy = min_t(size_t, load->size, FLASH_BLOCK);
  588. dev_dbg(&pci_dev->dev,
  589. "[%s] DMA: %lx SZ: %ld %d\n",
  590. __func__, (unsigned long)dma_addr, tocopy,
  591. blocks_to_flash);
  592. /* prepare DDCB for SLU process */
  593. cmd = ddcb_requ_alloc();
  594. if (cmd == NULL) {
  595. rc = -ENOMEM;
  596. goto free_buffer;
  597. }
  598. cmd->cmd = SLCMD_MOVE_FLASH;
  599. cmd->cmdopts = cmdopts;
  600. /* prepare invariant values */
  601. if (genwqe_get_slu_id(cd) <= 0x2) {
  602. *(__be64 *)&cmd->__asiv[0] = cpu_to_be64(dma_addr);
  603. *(__be64 *)&cmd->__asiv[8] = cpu_to_be64(tocopy);
  604. *(__be64 *)&cmd->__asiv[16] = cpu_to_be64(flash);
  605. *(__be32 *)&cmd->__asiv[24] = cpu_to_be32(0);
  606. cmd->__asiv[24] = load->uid;
  607. *(__be32 *)&cmd->__asiv[28] = cpu_to_be32(0) /* CRC */;
  608. cmd->asiv_length = 32; /* bytes included in crc calc */
  609. } else { /* setup DDCB for ATS architecture */
  610. *(__be64 *)&cmd->asiv[0] = cpu_to_be64(dma_addr);
  611. *(__be32 *)&cmd->asiv[8] = cpu_to_be32(tocopy);
  612. *(__be32 *)&cmd->asiv[12] = cpu_to_be32(0); /* resvd */
  613. *(__be64 *)&cmd->asiv[16] = cpu_to_be64(flash);
  614. *(__be32 *)&cmd->asiv[24] = cpu_to_be32(load->uid<<24);
  615. *(__be32 *)&cmd->asiv[28] = cpu_to_be32(0); /* CRC */
  616. /* rd/wr */
  617. cmd->ats = 0x5ULL << 44;
  618. cmd->asiv_length = 40; /* bytes included in crc calc */
  619. }
  620. cmd->asv_length = 8;
  621. /* we only get back the calculated CRC */
  622. *(u64 *)&cmd->asv[0] = 0ULL; /* 0x80 */
  623. rc = __genwqe_execute_raw_ddcb(cd, cmd, filp->f_flags);
  624. load->retc = cmd->retc;
  625. load->attn = cmd->attn;
  626. load->progress = cmd->progress;
  627. if ((rc < 0) && (rc != -EBADMSG)) {
  628. ddcb_requ_free(cmd);
  629. goto free_buffer;
  630. }
  631. rc = copy_to_user(buf, xbuf, tocopy);
  632. if (rc) {
  633. rc = -EFAULT;
  634. ddcb_requ_free(cmd);
  635. goto free_buffer;
  636. }
  637. /* We know that we can get retc 0x104 with CRC err */
  638. if (((cmd->retc == DDCB_RETC_FAULT) &&
  639. (cmd->attn != 0x02)) || /* Normally ignore CRC error */
  640. ((cmd->retc == DDCB_RETC_COMPLETE) &&
  641. (cmd->attn != 0x00))) { /* Everything was fine */
  642. rc = -EIO;
  643. ddcb_requ_free(cmd);
  644. goto free_buffer;
  645. }
  646. load->size -= tocopy;
  647. flash += tocopy;
  648. buf += tocopy;
  649. blocks_to_flash--;
  650. ddcb_requ_free(cmd);
  651. }
  652. rc = 0;
  653. free_buffer:
  654. __genwqe_free_consistent(cd, FLASH_BLOCK, xbuf, dma_addr);
  655. return rc;
  656. }
  657. static int genwqe_pin_mem(struct genwqe_file *cfile, struct genwqe_mem *m)
  658. {
  659. int rc;
  660. struct genwqe_dev *cd = cfile->cd;
  661. struct pci_dev *pci_dev = cfile->cd->pci_dev;
  662. struct dma_mapping *dma_map;
  663. unsigned long map_addr;
  664. unsigned long map_size;
  665. if ((m->addr == 0x0) || (m->size == 0))
  666. return -EINVAL;
  667. map_addr = (m->addr & PAGE_MASK);
  668. map_size = round_up(m->size + (m->addr & ~PAGE_MASK), PAGE_SIZE);
  669. dma_map = kzalloc(sizeof(struct dma_mapping), GFP_KERNEL);
  670. if (dma_map == NULL)
  671. return -ENOMEM;
  672. genwqe_mapping_init(dma_map, GENWQE_MAPPING_SGL_PINNED);
  673. rc = genwqe_user_vmap(cd, dma_map, (void *)map_addr, map_size, NULL);
  674. if (rc != 0) {
  675. dev_err(&pci_dev->dev,
  676. "[%s] genwqe_user_vmap rc=%d\n", __func__, rc);
  677. kfree(dma_map);
  678. return rc;
  679. }
  680. genwqe_add_pin(cfile, dma_map);
  681. return 0;
  682. }
  683. static int genwqe_unpin_mem(struct genwqe_file *cfile, struct genwqe_mem *m)
  684. {
  685. struct genwqe_dev *cd = cfile->cd;
  686. struct dma_mapping *dma_map;
  687. unsigned long map_addr;
  688. unsigned long map_size;
  689. if (m->addr == 0x0)
  690. return -EINVAL;
  691. map_addr = (m->addr & PAGE_MASK);
  692. map_size = round_up(m->size + (m->addr & ~PAGE_MASK), PAGE_SIZE);
  693. dma_map = genwqe_search_pin(cfile, map_addr, map_size, NULL);
  694. if (dma_map == NULL)
  695. return -ENOENT;
  696. genwqe_del_pin(cfile, dma_map);
  697. genwqe_user_vunmap(cd, dma_map, NULL);
  698. kfree(dma_map);
  699. return 0;
  700. }
  701. /**
  702. * ddcb_cmd_cleanup() - Remove dynamically created fixup entries
  703. *
  704. * Only if there are any. Pinnings are not removed.
  705. */
  706. static int ddcb_cmd_cleanup(struct genwqe_file *cfile, struct ddcb_requ *req)
  707. {
  708. unsigned int i;
  709. struct dma_mapping *dma_map;
  710. struct genwqe_dev *cd = cfile->cd;
  711. for (i = 0; i < DDCB_FIXUPS; i++) {
  712. dma_map = &req->dma_mappings[i];
  713. if (dma_mapping_used(dma_map)) {
  714. __genwqe_del_mapping(cfile, dma_map);
  715. genwqe_user_vunmap(cd, dma_map, req);
  716. }
  717. if (req->sgls[i].sgl != NULL)
  718. genwqe_free_sync_sgl(cd, &req->sgls[i]);
  719. }
  720. return 0;
  721. }
  722. /**
  723. * ddcb_cmd_fixups() - Establish DMA fixups/sglists for user memory references
  724. *
  725. * Before the DDCB gets executed we need to handle the fixups. We
  726. * replace the user-space addresses with DMA addresses or do
  727. * additional setup work e.g. generating a scatter-gather list which
  728. * is used to describe the memory referred to in the fixup.
  729. */
  730. static int ddcb_cmd_fixups(struct genwqe_file *cfile, struct ddcb_requ *req)
  731. {
  732. int rc;
  733. unsigned int asiv_offs, i;
  734. struct genwqe_dev *cd = cfile->cd;
  735. struct genwqe_ddcb_cmd *cmd = &req->cmd;
  736. struct dma_mapping *m;
  737. const char *type = "UNKNOWN";
  738. for (i = 0, asiv_offs = 0x00; asiv_offs <= 0x58;
  739. i++, asiv_offs += 0x08) {
  740. u64 u_addr;
  741. dma_addr_t d_addr;
  742. u32 u_size = 0;
  743. u64 ats_flags;
  744. ats_flags = ATS_GET_FLAGS(cmd->ats, asiv_offs);
  745. switch (ats_flags) {
  746. case ATS_TYPE_DATA:
  747. break; /* nothing to do here */
  748. case ATS_TYPE_FLAT_RDWR:
  749. case ATS_TYPE_FLAT_RD: {
  750. u_addr = be64_to_cpu(*((__be64 *)&cmd->
  751. asiv[asiv_offs]));
  752. u_size = be32_to_cpu(*((__be32 *)&cmd->
  753. asiv[asiv_offs + 0x08]));
  754. /*
  755. * No data available. Ignore u_addr in this
  756. * case and set addr to 0. Hardware must not
  757. * fetch the buffer.
  758. */
  759. if (u_size == 0x0) {
  760. *((__be64 *)&cmd->asiv[asiv_offs]) =
  761. cpu_to_be64(0x0);
  762. break;
  763. }
  764. m = __genwqe_search_mapping(cfile, u_addr, u_size,
  765. &d_addr, NULL);
  766. if (m == NULL) {
  767. rc = -EFAULT;
  768. goto err_out;
  769. }
  770. *((__be64 *)&cmd->asiv[asiv_offs]) =
  771. cpu_to_be64(d_addr);
  772. break;
  773. }
  774. case ATS_TYPE_SGL_RDWR:
  775. case ATS_TYPE_SGL_RD: {
  776. int page_offs;
  777. u_addr = be64_to_cpu(*((__be64 *)
  778. &cmd->asiv[asiv_offs]));
  779. u_size = be32_to_cpu(*((__be32 *)
  780. &cmd->asiv[asiv_offs + 0x08]));
  781. /*
  782. * No data available. Ignore u_addr in this
  783. * case and set addr to 0. Hardware must not
  784. * fetch the empty sgl.
  785. */
  786. if (u_size == 0x0) {
  787. *((__be64 *)&cmd->asiv[asiv_offs]) =
  788. cpu_to_be64(0x0);
  789. break;
  790. }
  791. m = genwqe_search_pin(cfile, u_addr, u_size, NULL);
  792. if (m != NULL) {
  793. type = "PINNING";
  794. page_offs = (u_addr -
  795. (u64)m->u_vaddr)/PAGE_SIZE;
  796. } else {
  797. type = "MAPPING";
  798. m = &req->dma_mappings[i];
  799. genwqe_mapping_init(m,
  800. GENWQE_MAPPING_SGL_TEMP);
  801. rc = genwqe_user_vmap(cd, m, (void *)u_addr,
  802. u_size, req);
  803. if (rc != 0)
  804. goto err_out;
  805. __genwqe_add_mapping(cfile, m);
  806. page_offs = 0;
  807. }
  808. /* create genwqe style scatter gather list */
  809. rc = genwqe_alloc_sync_sgl(cd, &req->sgls[i],
  810. (void __user *)u_addr,
  811. u_size);
  812. if (rc != 0)
  813. goto err_out;
  814. genwqe_setup_sgl(cd, &req->sgls[i],
  815. &m->dma_list[page_offs]);
  816. *((__be64 *)&cmd->asiv[asiv_offs]) =
  817. cpu_to_be64(req->sgls[i].sgl_dma_addr);
  818. break;
  819. }
  820. default:
  821. rc = -EINVAL;
  822. goto err_out;
  823. }
  824. }
  825. return 0;
  826. err_out:
  827. ddcb_cmd_cleanup(cfile, req);
  828. return rc;
  829. }
  830. /**
  831. * genwqe_execute_ddcb() - Execute DDCB using userspace address fixups
  832. *
  833. * The code will build up the translation tables or lookup the
  834. * contignous memory allocation table to find the right translations
  835. * and DMA addresses.
  836. */
  837. static int genwqe_execute_ddcb(struct genwqe_file *cfile,
  838. struct genwqe_ddcb_cmd *cmd)
  839. {
  840. int rc;
  841. struct genwqe_dev *cd = cfile->cd;
  842. struct file *filp = cfile->filp;
  843. struct ddcb_requ *req = container_of(cmd, struct ddcb_requ, cmd);
  844. rc = ddcb_cmd_fixups(cfile, req);
  845. if (rc != 0)
  846. return rc;
  847. rc = __genwqe_execute_raw_ddcb(cd, cmd, filp->f_flags);
  848. ddcb_cmd_cleanup(cfile, req);
  849. return rc;
  850. }
  851. static int do_execute_ddcb(struct genwqe_file *cfile,
  852. unsigned long arg, int raw)
  853. {
  854. int rc;
  855. struct genwqe_ddcb_cmd *cmd;
  856. struct ddcb_requ *req;
  857. struct genwqe_dev *cd = cfile->cd;
  858. struct file *filp = cfile->filp;
  859. cmd = ddcb_requ_alloc();
  860. if (cmd == NULL)
  861. return -ENOMEM;
  862. req = container_of(cmd, struct ddcb_requ, cmd);
  863. if (copy_from_user(cmd, (void __user *)arg, sizeof(*cmd))) {
  864. ddcb_requ_free(cmd);
  865. return -EFAULT;
  866. }
  867. if (!raw)
  868. rc = genwqe_execute_ddcb(cfile, cmd);
  869. else
  870. rc = __genwqe_execute_raw_ddcb(cd, cmd, filp->f_flags);
  871. /* Copy back only the modifed fields. Do not copy ASIV
  872. back since the copy got modified by the driver. */
  873. if (copy_to_user((void __user *)arg, cmd,
  874. sizeof(*cmd) - DDCB_ASIV_LENGTH)) {
  875. ddcb_requ_free(cmd);
  876. return -EFAULT;
  877. }
  878. ddcb_requ_free(cmd);
  879. return rc;
  880. }
  881. /**
  882. * genwqe_ioctl() - IO control
  883. * @filp: file handle
  884. * @cmd: command identifier (passed from user)
  885. * @arg: argument (passed from user)
  886. *
  887. * Return: 0 success
  888. */
  889. static long genwqe_ioctl(struct file *filp, unsigned int cmd,
  890. unsigned long arg)
  891. {
  892. int rc = 0;
  893. struct genwqe_file *cfile = (struct genwqe_file *)filp->private_data;
  894. struct genwqe_dev *cd = cfile->cd;
  895. struct pci_dev *pci_dev = cd->pci_dev;
  896. struct genwqe_reg_io __user *io;
  897. u64 val;
  898. u32 reg_offs;
  899. /* Return -EIO if card hit EEH */
  900. if (pci_channel_offline(pci_dev))
  901. return -EIO;
  902. if (_IOC_TYPE(cmd) != GENWQE_IOC_CODE)
  903. return -EINVAL;
  904. switch (cmd) {
  905. case GENWQE_GET_CARD_STATE:
  906. put_user(cd->card_state, (enum genwqe_card_state __user *)arg);
  907. return 0;
  908. /* Register access */
  909. case GENWQE_READ_REG64: {
  910. io = (struct genwqe_reg_io __user *)arg;
  911. if (get_user(reg_offs, &io->num))
  912. return -EFAULT;
  913. if ((reg_offs >= cd->mmio_len) || (reg_offs & 0x7))
  914. return -EINVAL;
  915. val = __genwqe_readq(cd, reg_offs);
  916. put_user(val, &io->val64);
  917. return 0;
  918. }
  919. case GENWQE_WRITE_REG64: {
  920. io = (struct genwqe_reg_io __user *)arg;
  921. if (!capable(CAP_SYS_ADMIN))
  922. return -EPERM;
  923. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  924. return -EPERM;
  925. if (get_user(reg_offs, &io->num))
  926. return -EFAULT;
  927. if ((reg_offs >= cd->mmio_len) || (reg_offs & 0x7))
  928. return -EINVAL;
  929. if (get_user(val, &io->val64))
  930. return -EFAULT;
  931. __genwqe_writeq(cd, reg_offs, val);
  932. return 0;
  933. }
  934. case GENWQE_READ_REG32: {
  935. io = (struct genwqe_reg_io __user *)arg;
  936. if (get_user(reg_offs, &io->num))
  937. return -EFAULT;
  938. if ((reg_offs >= cd->mmio_len) || (reg_offs & 0x3))
  939. return -EINVAL;
  940. val = __genwqe_readl(cd, reg_offs);
  941. put_user(val, &io->val64);
  942. return 0;
  943. }
  944. case GENWQE_WRITE_REG32: {
  945. io = (struct genwqe_reg_io __user *)arg;
  946. if (!capable(CAP_SYS_ADMIN))
  947. return -EPERM;
  948. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  949. return -EPERM;
  950. if (get_user(reg_offs, &io->num))
  951. return -EFAULT;
  952. if ((reg_offs >= cd->mmio_len) || (reg_offs & 0x3))
  953. return -EINVAL;
  954. if (get_user(val, &io->val64))
  955. return -EFAULT;
  956. __genwqe_writel(cd, reg_offs, val);
  957. return 0;
  958. }
  959. /* Flash update/reading */
  960. case GENWQE_SLU_UPDATE: {
  961. struct genwqe_bitstream load;
  962. if (!genwqe_is_privileged(cd))
  963. return -EPERM;
  964. if ((filp->f_flags & O_ACCMODE) == O_RDONLY)
  965. return -EPERM;
  966. if (copy_from_user(&load, (void __user *)arg,
  967. sizeof(load)))
  968. return -EFAULT;
  969. rc = do_flash_update(cfile, &load);
  970. if (copy_to_user((void __user *)arg, &load, sizeof(load)))
  971. return -EFAULT;
  972. return rc;
  973. }
  974. case GENWQE_SLU_READ: {
  975. struct genwqe_bitstream load;
  976. if (!genwqe_is_privileged(cd))
  977. return -EPERM;
  978. if (genwqe_flash_readback_fails(cd))
  979. return -ENOSPC; /* known to fail for old versions */
  980. if (copy_from_user(&load, (void __user *)arg, sizeof(load)))
  981. return -EFAULT;
  982. rc = do_flash_read(cfile, &load);
  983. if (copy_to_user((void __user *)arg, &load, sizeof(load)))
  984. return -EFAULT;
  985. return rc;
  986. }
  987. /* memory pinning and unpinning */
  988. case GENWQE_PIN_MEM: {
  989. struct genwqe_mem m;
  990. if (copy_from_user(&m, (void __user *)arg, sizeof(m)))
  991. return -EFAULT;
  992. return genwqe_pin_mem(cfile, &m);
  993. }
  994. case GENWQE_UNPIN_MEM: {
  995. struct genwqe_mem m;
  996. if (copy_from_user(&m, (void __user *)arg, sizeof(m)))
  997. return -EFAULT;
  998. return genwqe_unpin_mem(cfile, &m);
  999. }
  1000. /* launch an DDCB and wait for completion */
  1001. case GENWQE_EXECUTE_DDCB:
  1002. return do_execute_ddcb(cfile, arg, 0);
  1003. case GENWQE_EXECUTE_RAW_DDCB: {
  1004. if (!capable(CAP_SYS_ADMIN))
  1005. return -EPERM;
  1006. return do_execute_ddcb(cfile, arg, 1);
  1007. }
  1008. default:
  1009. return -EINVAL;
  1010. }
  1011. return rc;
  1012. }
  1013. #if defined(CONFIG_COMPAT)
  1014. /**
  1015. * genwqe_compat_ioctl() - Compatibility ioctl
  1016. *
  1017. * Called whenever a 32-bit process running under a 64-bit kernel
  1018. * performs an ioctl on /dev/genwqe<n>_card.
  1019. *
  1020. * @filp: file pointer.
  1021. * @cmd: command.
  1022. * @arg: user argument.
  1023. * Return: zero on success or negative number on failure.
  1024. */
  1025. static long genwqe_compat_ioctl(struct file *filp, unsigned int cmd,
  1026. unsigned long arg)
  1027. {
  1028. return genwqe_ioctl(filp, cmd, arg);
  1029. }
  1030. #endif /* defined(CONFIG_COMPAT) */
  1031. static const struct file_operations genwqe_fops = {
  1032. .owner = THIS_MODULE,
  1033. .open = genwqe_open,
  1034. .fasync = genwqe_fasync,
  1035. .mmap = genwqe_mmap,
  1036. .unlocked_ioctl = genwqe_ioctl,
  1037. #if defined(CONFIG_COMPAT)
  1038. .compat_ioctl = genwqe_compat_ioctl,
  1039. #endif
  1040. .release = genwqe_release,
  1041. };
  1042. static int genwqe_device_initialized(struct genwqe_dev *cd)
  1043. {
  1044. return cd->dev != NULL;
  1045. }
  1046. /**
  1047. * genwqe_device_create() - Create and configure genwqe char device
  1048. * @cd: genwqe device descriptor
  1049. *
  1050. * This function must be called before we create any more genwqe
  1051. * character devices, because it is allocating the major and minor
  1052. * number which are supposed to be used by the client drivers.
  1053. */
  1054. int genwqe_device_create(struct genwqe_dev *cd)
  1055. {
  1056. int rc;
  1057. struct pci_dev *pci_dev = cd->pci_dev;
  1058. /*
  1059. * Here starts the individual setup per client. It must
  1060. * initialize its own cdev data structure with its own fops.
  1061. * The appropriate devnum needs to be created. The ranges must
  1062. * not overlap.
  1063. */
  1064. rc = alloc_chrdev_region(&cd->devnum_genwqe, 0,
  1065. GENWQE_MAX_MINOR, GENWQE_DEVNAME);
  1066. if (rc < 0) {
  1067. dev_err(&pci_dev->dev, "err: alloc_chrdev_region failed\n");
  1068. goto err_dev;
  1069. }
  1070. cdev_init(&cd->cdev_genwqe, &genwqe_fops);
  1071. cd->cdev_genwqe.owner = THIS_MODULE;
  1072. rc = cdev_add(&cd->cdev_genwqe, cd->devnum_genwqe, 1);
  1073. if (rc < 0) {
  1074. dev_err(&pci_dev->dev, "err: cdev_add failed\n");
  1075. goto err_add;
  1076. }
  1077. /*
  1078. * Finally the device in /dev/... must be created. The rule is
  1079. * to use card%d_clientname for each created device.
  1080. */
  1081. cd->dev = device_create_with_groups(cd->class_genwqe,
  1082. &cd->pci_dev->dev,
  1083. cd->devnum_genwqe, cd,
  1084. genwqe_attribute_groups,
  1085. GENWQE_DEVNAME "%u_card",
  1086. cd->card_idx);
  1087. if (IS_ERR(cd->dev)) {
  1088. rc = PTR_ERR(cd->dev);
  1089. goto err_cdev;
  1090. }
  1091. rc = genwqe_init_debugfs(cd);
  1092. if (rc != 0)
  1093. goto err_debugfs;
  1094. return 0;
  1095. err_debugfs:
  1096. device_destroy(cd->class_genwqe, cd->devnum_genwqe);
  1097. err_cdev:
  1098. cdev_del(&cd->cdev_genwqe);
  1099. err_add:
  1100. unregister_chrdev_region(cd->devnum_genwqe, GENWQE_MAX_MINOR);
  1101. err_dev:
  1102. cd->dev = NULL;
  1103. return rc;
  1104. }
  1105. static int genwqe_inform_and_stop_processes(struct genwqe_dev *cd)
  1106. {
  1107. int rc;
  1108. unsigned int i;
  1109. struct pci_dev *pci_dev = cd->pci_dev;
  1110. if (!genwqe_open_files(cd))
  1111. return 0;
  1112. dev_warn(&pci_dev->dev, "[%s] send SIGIO and wait ...\n", __func__);
  1113. rc = genwqe_kill_fasync(cd, SIGIO);
  1114. if (rc > 0) {
  1115. /* give kill_timeout seconds to close file descriptors ... */
  1116. for (i = 0; (i < genwqe_kill_timeout) &&
  1117. genwqe_open_files(cd); i++) {
  1118. dev_info(&pci_dev->dev, " %d sec ...", i);
  1119. cond_resched();
  1120. msleep(1000);
  1121. }
  1122. /* if no open files we can safely continue, else ... */
  1123. if (!genwqe_open_files(cd))
  1124. return 0;
  1125. dev_warn(&pci_dev->dev,
  1126. "[%s] send SIGKILL and wait ...\n", __func__);
  1127. rc = genwqe_force_sig(cd, SIGKILL); /* force terminate */
  1128. if (rc) {
  1129. /* Give kill_timout more seconds to end processes */
  1130. for (i = 0; (i < genwqe_kill_timeout) &&
  1131. genwqe_open_files(cd); i++) {
  1132. dev_warn(&pci_dev->dev, " %d sec ...", i);
  1133. cond_resched();
  1134. msleep(1000);
  1135. }
  1136. }
  1137. }
  1138. return 0;
  1139. }
  1140. /**
  1141. * genwqe_device_remove() - Remove genwqe's char device
  1142. *
  1143. * This function must be called after the client devices are removed
  1144. * because it will free the major/minor number range for the genwqe
  1145. * drivers.
  1146. *
  1147. * This function must be robust enough to be called twice.
  1148. */
  1149. int genwqe_device_remove(struct genwqe_dev *cd)
  1150. {
  1151. int rc;
  1152. struct pci_dev *pci_dev = cd->pci_dev;
  1153. if (!genwqe_device_initialized(cd))
  1154. return 1;
  1155. genwqe_inform_and_stop_processes(cd);
  1156. /*
  1157. * We currently do wait until all filedescriptors are
  1158. * closed. This leads to a problem when we abort the
  1159. * application which will decrease this reference from
  1160. * 1/unused to 0/illegal and not from 2/used 1/empty.
  1161. */
  1162. rc = atomic_read(&cd->cdev_genwqe.kobj.kref.refcount);
  1163. if (rc != 1) {
  1164. dev_err(&pci_dev->dev,
  1165. "[%s] err: cdev_genwqe...refcount=%d\n", __func__, rc);
  1166. panic("Fatal err: cannot free resources with pending references!");
  1167. }
  1168. genqwe_exit_debugfs(cd);
  1169. device_destroy(cd->class_genwqe, cd->devnum_genwqe);
  1170. cdev_del(&cd->cdev_genwqe);
  1171. unregister_chrdev_region(cd->devnum_genwqe, GENWQE_MAX_MINOR);
  1172. cd->dev = NULL;
  1173. return 0;
  1174. }