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