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