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