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