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