2 * IBM Accelerator Family 'GenWQE'
4 * (C) Copyright IBM Corp. 2013
6 * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
7 * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
8 * Author: Michael Jung <mijung@gmx.net>
9 * Author: Michael Ruettger <michael@ibmra.de>
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.
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.
22 * Character device representation of the GenWQE device. This allows
23 * user-space applications to communicate with the card.
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>
32 #include <linux/sched/signal.h>
33 #include <linux/wait.h>
34 #include <linux/delay.h>
35 #include <linux/atomic.h>
37 #include "card_base.h"
38 #include "card_ddcb.h"
40 static int genwqe_open_files(struct genwqe_dev
*cd
)
45 spin_lock_irqsave(&cd
->file_lock
, flags
);
46 rc
= list_empty(&cd
->file_list
);
47 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
51 static void genwqe_add_file(struct genwqe_dev
*cd
, struct genwqe_file
*cfile
)
55 cfile
->opener
= get_pid(task_tgid(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
);
61 static int genwqe_del_file(struct genwqe_dev
*cd
, struct genwqe_file
*cfile
)
65 spin_lock_irqsave(&cd
->file_lock
, flags
);
66 list_del(&cfile
->list
);
67 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
68 put_pid(cfile
->opener
);
73 static void genwqe_add_pin(struct genwqe_file
*cfile
, struct dma_mapping
*m
)
77 spin_lock_irqsave(&cfile
->pin_lock
, flags
);
78 list_add(&m
->pin_list
, &cfile
->pin_list
);
79 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
82 static int genwqe_del_pin(struct genwqe_file
*cfile
, struct dma_mapping
*m
)
86 spin_lock_irqsave(&cfile
->pin_lock
, flags
);
87 list_del(&m
->pin_list
);
88 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
94 * genwqe_search_pin() - Search for the mapping for a userspace address
95 * @cfile: Descriptor of opened file
96 * @u_addr: User virtual address
97 * @size: Size of buffer
98 * @dma_addr: DMA address to be updated
100 * Return: Pointer to the corresponding mapping NULL if not found
102 static struct dma_mapping
*genwqe_search_pin(struct genwqe_file
*cfile
,
103 unsigned long u_addr
,
108 struct dma_mapping
*m
;
110 spin_lock_irqsave(&cfile
->pin_lock
, flags
);
112 list_for_each_entry(m
, &cfile
->pin_list
, pin_list
) {
113 if ((((u64
)m
->u_vaddr
) <= (u_addr
)) &&
114 (((u64
)m
->u_vaddr
+ m
->size
) >= (u_addr
+ size
))) {
117 *virt_addr
= m
->k_vaddr
+
118 (u_addr
- (u64
)m
->u_vaddr
);
120 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
124 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
128 static void __genwqe_add_mapping(struct genwqe_file
*cfile
,
129 struct dma_mapping
*dma_map
)
133 spin_lock_irqsave(&cfile
->map_lock
, flags
);
134 list_add(&dma_map
->card_list
, &cfile
->map_list
);
135 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
138 static void __genwqe_del_mapping(struct genwqe_file
*cfile
,
139 struct dma_mapping
*dma_map
)
143 spin_lock_irqsave(&cfile
->map_lock
, flags
);
144 list_del(&dma_map
->card_list
);
145 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
150 * __genwqe_search_mapping() - Search for the mapping for a userspace address
151 * @cfile: descriptor of opened file
152 * @u_addr: user virtual address
153 * @size: size of buffer
154 * @dma_addr: DMA address to be updated
155 * Return: Pointer to the corresponding mapping NULL if not found
157 static struct dma_mapping
*__genwqe_search_mapping(struct genwqe_file
*cfile
,
158 unsigned long u_addr
,
160 dma_addr_t
*dma_addr
,
164 struct dma_mapping
*m
;
165 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
167 spin_lock_irqsave(&cfile
->map_lock
, flags
);
168 list_for_each_entry(m
, &cfile
->map_list
, card_list
) {
170 if ((((u64
)m
->u_vaddr
) <= (u_addr
)) &&
171 (((u64
)m
->u_vaddr
+ m
->size
) >= (u_addr
+ size
))) {
173 /* match found: current is as expected and
176 *dma_addr
= m
->dma_addr
+
177 (u_addr
- (u64
)m
->u_vaddr
);
180 *virt_addr
= m
->k_vaddr
+
181 (u_addr
- (u64
)m
->u_vaddr
);
183 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
187 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
189 dev_err(&pci_dev
->dev
,
190 "[%s] Entry not found: u_addr=%lx, size=%x\n",
191 __func__
, u_addr
, size
);
196 static void genwqe_remove_mappings(struct genwqe_file
*cfile
)
199 struct list_head
*node
, *next
;
200 struct dma_mapping
*dma_map
;
201 struct genwqe_dev
*cd
= cfile
->cd
;
202 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
204 list_for_each_safe(node
, next
, &cfile
->map_list
) {
205 dma_map
= list_entry(node
, struct dma_mapping
, card_list
);
207 list_del_init(&dma_map
->card_list
);
210 * This is really a bug, because those things should
211 * have been already tidied up.
213 * GENWQE_MAPPING_RAW should have been removed via mmunmap().
214 * GENWQE_MAPPING_SGL_TEMP should be removed by tidy up code.
216 dev_err(&pci_dev
->dev
,
217 "[%s] %d. cleanup mapping: u_vaddr=%p u_kaddr=%016lx dma_addr=%lx\n",
218 __func__
, i
++, dma_map
->u_vaddr
,
219 (unsigned long)dma_map
->k_vaddr
,
220 (unsigned long)dma_map
->dma_addr
);
222 if (dma_map
->type
== GENWQE_MAPPING_RAW
) {
223 /* we allocated this dynamically */
224 __genwqe_free_consistent(cd
, dma_map
->size
,
228 } else if (dma_map
->type
== GENWQE_MAPPING_SGL_TEMP
) {
229 /* we use dma_map statically from the request */
230 genwqe_user_vunmap(cd
, dma_map
);
235 static void genwqe_remove_pinnings(struct genwqe_file
*cfile
)
237 struct list_head
*node
, *next
;
238 struct dma_mapping
*dma_map
;
239 struct genwqe_dev
*cd
= cfile
->cd
;
241 list_for_each_safe(node
, next
, &cfile
->pin_list
) {
242 dma_map
= list_entry(node
, struct dma_mapping
, pin_list
);
245 * This is not a bug, because a killed processed might
246 * not call the unpin ioctl, which is supposed to free
249 * Pinnings are dymically allocated and need to be
252 list_del_init(&dma_map
->pin_list
);
253 genwqe_user_vunmap(cd
, dma_map
);
259 * genwqe_kill_fasync() - Send signal to all processes with open GenWQE files
261 * E.g. genwqe_send_signal(cd, SIGIO);
263 static int genwqe_kill_fasync(struct genwqe_dev
*cd
, int sig
)
265 unsigned int files
= 0;
267 struct genwqe_file
*cfile
;
269 spin_lock_irqsave(&cd
->file_lock
, flags
);
270 list_for_each_entry(cfile
, &cd
->file_list
, list
) {
271 if (cfile
->async_queue
)
272 kill_fasync(&cfile
->async_queue
, sig
, POLL_HUP
);
275 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
279 static int genwqe_terminate(struct genwqe_dev
*cd
)
281 unsigned int files
= 0;
283 struct genwqe_file
*cfile
;
285 spin_lock_irqsave(&cd
->file_lock
, flags
);
286 list_for_each_entry(cfile
, &cd
->file_list
, list
) {
287 kill_pid(cfile
->opener
, SIGKILL
, 1);
290 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
295 * genwqe_open() - file open
296 * @inode: file system information
299 * This function is executed whenever an application calls
300 * open("/dev/genwqe",..).
302 * Return: 0 if successful or <0 if errors
304 static int genwqe_open(struct inode
*inode
, struct file
*filp
)
306 struct genwqe_dev
*cd
;
307 struct genwqe_file
*cfile
;
309 cfile
= kzalloc(sizeof(*cfile
), GFP_KERNEL
);
313 cd
= container_of(inode
->i_cdev
, struct genwqe_dev
, cdev_genwqe
);
316 cfile
->client
= NULL
;
318 spin_lock_init(&cfile
->map_lock
); /* list of raw memory allocations */
319 INIT_LIST_HEAD(&cfile
->map_list
);
321 spin_lock_init(&cfile
->pin_lock
); /* list of user pinned memory */
322 INIT_LIST_HEAD(&cfile
->pin_list
);
324 filp
->private_data
= cfile
;
326 genwqe_add_file(cd
, cfile
);
331 * genwqe_fasync() - Setup process to receive SIGIO.
332 * @fd: file descriptor
336 * Sending a signal is working as following:
338 * if (cdev->async_queue)
339 * kill_fasync(&cdev->async_queue, SIGIO, POLL_IN);
341 * Some devices also implement asynchronous notification to indicate
342 * when the device can be written; in this case, of course,
343 * kill_fasync must be called with a mode of POLL_OUT.
345 static int genwqe_fasync(int fd
, struct file
*filp
, int mode
)
347 struct genwqe_file
*cdev
= (struct genwqe_file
*)filp
->private_data
;
349 return fasync_helper(fd
, filp
, mode
, &cdev
->async_queue
);
354 * genwqe_release() - file close
355 * @inode: file system information
358 * This function is executed whenever an application calls 'close(fd_genwqe)'
362 static int genwqe_release(struct inode
*inode
, struct file
*filp
)
364 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
365 struct genwqe_dev
*cd
= cfile
->cd
;
367 /* there must be no entries in these lists! */
368 genwqe_remove_mappings(cfile
);
369 genwqe_remove_pinnings(cfile
);
371 /* remove this filp from the asynchronously notified filp's */
372 genwqe_fasync(-1, filp
, 0);
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!
379 genwqe_del_file(cd
, cfile
);
384 static void genwqe_vma_open(struct vm_area_struct
*vma
)
390 * genwqe_vma_close() - Called each time when vma is unmapped
392 * Free memory which got allocated by GenWQE mmap().
394 static void genwqe_vma_close(struct vm_area_struct
*vma
)
396 unsigned long vsize
= vma
->vm_end
- vma
->vm_start
;
397 struct inode
*inode
= file_inode(vma
->vm_file
);
398 struct dma_mapping
*dma_map
;
399 struct genwqe_dev
*cd
= container_of(inode
->i_cdev
, struct genwqe_dev
,
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
;
405 dma_map
= __genwqe_search_mapping(cfile
, vma
->vm_start
, vsize
,
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
,
414 __genwqe_del_mapping(cfile
, dma_map
);
415 __genwqe_free_consistent(cd
, dma_map
->size
, dma_map
->k_vaddr
,
420 static const struct vm_operations_struct genwqe_vma_ops
= {
421 .open
= genwqe_vma_open
,
422 .close
= genwqe_vma_close
,
426 * genwqe_mmap() - Provide contignous buffers to userspace
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.
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.
437 static int genwqe_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
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
;
448 if (get_order(vsize
) > MAX_ORDER
)
451 dma_map
= kzalloc(sizeof(struct dma_mapping
), GFP_KERNEL
);
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
,
461 if (dma_map
->k_vaddr
== NULL
) {
466 if (capable(CAP_SYS_ADMIN
) && (vsize
> sizeof(dma_addr_t
)))
467 *(dma_addr_t
*)dma_map
->k_vaddr
= dma_map
->dma_addr
;
469 pfn
= virt_to_phys(dma_map
->k_vaddr
) >> PAGE_SHIFT
;
470 rc
= remap_pfn_range(vma
,
480 vma
->vm_private_data
= cfile
;
481 vma
->vm_ops
= &genwqe_vma_ops
;
482 __genwqe_add_mapping(cfile
, dma_map
);
487 __genwqe_free_consistent(cd
, dma_map
->size
,
496 * do_flash_update() - Excute flash update (write image or CVPD)
498 * @load: details about image load
500 * Return: 0 if successful
503 #define FLASH_BLOCK 0x40000 /* we use 256k blocks */
505 static int do_flash_update(struct genwqe_file
*cfile
,
506 struct genwqe_bitstream
*load
)
517 struct genwqe_dev
*cd
= cfile
->cd
;
518 struct file
*filp
= cfile
->filp
;
519 struct pci_dev
*pci_dev
= cd
->pci_dev
;
521 if ((load
->size
& 0x3) != 0)
524 if (((unsigned long)(load
->data_addr
) & ~PAGE_MASK
) != 0)
527 /* FIXME Bits have changed for new service layer! */
528 switch ((char)load
->partition
) {
531 break; /* download/erase_first/part_0 */
534 break; /* download/erase_first/part_1 */
537 break; /* download/erase_first/vpd */
542 buf
= (u8 __user
*)load
->data_addr
;
543 xbuf
= __genwqe_alloc_consistent(cd
, FLASH_BLOCK
, &dma_addr
);
547 blocks_to_flash
= load
->size
/ FLASH_BLOCK
;
549 struct genwqe_ddcb_cmd
*req
;
552 * We must be 4 byte aligned. Buffer must be 0 appened
553 * to have defined values when calculating CRC.
555 tocopy
= min_t(size_t, load
->size
, FLASH_BLOCK
);
557 rc
= copy_from_user(xbuf
, buf
, tocopy
);
562 crc
= genwqe_crc32(xbuf
, tocopy
, 0xffffffff);
564 dev_dbg(&pci_dev
->dev
,
565 "[%s] DMA: %lx CRC: %08x SZ: %ld %d\n",
566 __func__
, (unsigned long)dma_addr
, crc
, tocopy
,
569 /* prepare DDCB for SLU process */
570 req
= ddcb_requ_alloc();
576 req
->cmd
= SLCMD_MOVE_FLASH
;
577 req
->cmdopts
= cmdopts
;
579 /* prepare invariant values */
580 if (genwqe_get_slu_id(cd
) <= 0x2) {
581 *(__be64
*)&req
->__asiv
[0] = cpu_to_be64(dma_addr
);
582 *(__be64
*)&req
->__asiv
[8] = cpu_to_be64(tocopy
);
583 *(__be64
*)&req
->__asiv
[16] = cpu_to_be64(flash
);
584 *(__be32
*)&req
->__asiv
[24] = cpu_to_be32(0);
585 req
->__asiv
[24] = load
->uid
;
586 *(__be32
*)&req
->__asiv
[28] = cpu_to_be32(crc
);
588 /* for simulation only */
589 *(__be64
*)&req
->__asiv
[88] = cpu_to_be64(load
->slu_id
);
590 *(__be64
*)&req
->__asiv
[96] = cpu_to_be64(load
->app_id
);
591 req
->asiv_length
= 32; /* bytes included in crc calc */
592 } else { /* setup DDCB for ATS architecture */
593 *(__be64
*)&req
->asiv
[0] = cpu_to_be64(dma_addr
);
594 *(__be32
*)&req
->asiv
[8] = cpu_to_be32(tocopy
);
595 *(__be32
*)&req
->asiv
[12] = cpu_to_be32(0); /* resvd */
596 *(__be64
*)&req
->asiv
[16] = cpu_to_be64(flash
);
597 *(__be32
*)&req
->asiv
[24] = cpu_to_be32(load
->uid
<<24);
598 *(__be32
*)&req
->asiv
[28] = cpu_to_be32(crc
);
600 /* for simulation only */
601 *(__be64
*)&req
->asiv
[80] = cpu_to_be64(load
->slu_id
);
602 *(__be64
*)&req
->asiv
[88] = cpu_to_be64(load
->app_id
);
605 req
->ats
= 0x4ULL
<< 44;
606 req
->asiv_length
= 40; /* bytes included in crc calc */
610 /* For Genwqe5 we get back the calculated CRC */
611 *(u64
*)&req
->asv
[0] = 0ULL; /* 0x80 */
613 rc
= __genwqe_execute_raw_ddcb(cd
, req
, filp
->f_flags
);
615 load
->retc
= req
->retc
;
616 load
->attn
= req
->attn
;
617 load
->progress
= req
->progress
;
624 if (req
->retc
!= DDCB_RETC_COMPLETE
) {
630 load
->size
-= tocopy
;
638 __genwqe_free_consistent(cd
, FLASH_BLOCK
, xbuf
, dma_addr
);
642 static int do_flash_read(struct genwqe_file
*cfile
,
643 struct genwqe_bitstream
*load
)
645 int rc
, blocks_to_flash
;
652 struct genwqe_dev
*cd
= cfile
->cd
;
653 struct file
*filp
= cfile
->filp
;
654 struct pci_dev
*pci_dev
= cd
->pci_dev
;
655 struct genwqe_ddcb_cmd
*cmd
;
657 if ((load
->size
& 0x3) != 0)
660 if (((unsigned long)(load
->data_addr
) & ~PAGE_MASK
) != 0)
663 /* FIXME Bits have changed for new service layer! */
664 switch ((char)load
->partition
) {
667 break; /* upload/part_0 */
670 break; /* upload/part_1 */
673 break; /* upload/vpd */
678 buf
= (u8 __user
*)load
->data_addr
;
679 xbuf
= __genwqe_alloc_consistent(cd
, FLASH_BLOCK
, &dma_addr
);
683 blocks_to_flash
= load
->size
/ FLASH_BLOCK
;
686 * We must be 4 byte aligned. Buffer must be 0 appened
687 * to have defined values when calculating CRC.
689 tocopy
= min_t(size_t, load
->size
, FLASH_BLOCK
);
691 dev_dbg(&pci_dev
->dev
,
692 "[%s] DMA: %lx SZ: %ld %d\n",
693 __func__
, (unsigned long)dma_addr
, tocopy
,
696 /* prepare DDCB for SLU process */
697 cmd
= ddcb_requ_alloc();
702 cmd
->cmd
= SLCMD_MOVE_FLASH
;
703 cmd
->cmdopts
= cmdopts
;
705 /* prepare invariant values */
706 if (genwqe_get_slu_id(cd
) <= 0x2) {
707 *(__be64
*)&cmd
->__asiv
[0] = cpu_to_be64(dma_addr
);
708 *(__be64
*)&cmd
->__asiv
[8] = cpu_to_be64(tocopy
);
709 *(__be64
*)&cmd
->__asiv
[16] = cpu_to_be64(flash
);
710 *(__be32
*)&cmd
->__asiv
[24] = cpu_to_be32(0);
711 cmd
->__asiv
[24] = load
->uid
;
712 *(__be32
*)&cmd
->__asiv
[28] = cpu_to_be32(0) /* CRC */;
713 cmd
->asiv_length
= 32; /* bytes included in crc calc */
714 } else { /* setup DDCB for ATS architecture */
715 *(__be64
*)&cmd
->asiv
[0] = cpu_to_be64(dma_addr
);
716 *(__be32
*)&cmd
->asiv
[8] = cpu_to_be32(tocopy
);
717 *(__be32
*)&cmd
->asiv
[12] = cpu_to_be32(0); /* resvd */
718 *(__be64
*)&cmd
->asiv
[16] = cpu_to_be64(flash
);
719 *(__be32
*)&cmd
->asiv
[24] = cpu_to_be32(load
->uid
<<24);
720 *(__be32
*)&cmd
->asiv
[28] = cpu_to_be32(0); /* CRC */
723 cmd
->ats
= 0x5ULL
<< 44;
724 cmd
->asiv_length
= 40; /* bytes included in crc calc */
728 /* we only get back the calculated CRC */
729 *(u64
*)&cmd
->asv
[0] = 0ULL; /* 0x80 */
731 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
733 load
->retc
= cmd
->retc
;
734 load
->attn
= cmd
->attn
;
735 load
->progress
= cmd
->progress
;
737 if ((rc
< 0) && (rc
!= -EBADMSG
)) {
742 rc
= copy_to_user(buf
, xbuf
, tocopy
);
749 /* We know that we can get retc 0x104 with CRC err */
750 if (((cmd
->retc
== DDCB_RETC_FAULT
) &&
751 (cmd
->attn
!= 0x02)) || /* Normally ignore CRC error */
752 ((cmd
->retc
== DDCB_RETC_COMPLETE
) &&
753 (cmd
->attn
!= 0x00))) { /* Everything was fine */
759 load
->size
-= tocopy
;
768 __genwqe_free_consistent(cd
, FLASH_BLOCK
, xbuf
, dma_addr
);
772 static int genwqe_pin_mem(struct genwqe_file
*cfile
, struct genwqe_mem
*m
)
775 struct genwqe_dev
*cd
= cfile
->cd
;
776 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
777 struct dma_mapping
*dma_map
;
778 unsigned long map_addr
;
779 unsigned long map_size
;
781 if ((m
->addr
== 0x0) || (m
->size
== 0))
783 if (m
->size
> ULONG_MAX
- PAGE_SIZE
- (m
->addr
& ~PAGE_MASK
))
786 map_addr
= (m
->addr
& PAGE_MASK
);
787 map_size
= round_up(m
->size
+ (m
->addr
& ~PAGE_MASK
), PAGE_SIZE
);
789 dma_map
= kzalloc(sizeof(struct dma_mapping
), GFP_KERNEL
);
793 genwqe_mapping_init(dma_map
, GENWQE_MAPPING_SGL_PINNED
);
794 rc
= genwqe_user_vmap(cd
, dma_map
, (void *)map_addr
, map_size
);
796 dev_err(&pci_dev
->dev
,
797 "[%s] genwqe_user_vmap rc=%d\n", __func__
, rc
);
802 genwqe_add_pin(cfile
, dma_map
);
806 static int genwqe_unpin_mem(struct genwqe_file
*cfile
, struct genwqe_mem
*m
)
808 struct genwqe_dev
*cd
= cfile
->cd
;
809 struct dma_mapping
*dma_map
;
810 unsigned long map_addr
;
811 unsigned long map_size
;
816 map_addr
= (m
->addr
& PAGE_MASK
);
817 map_size
= round_up(m
->size
+ (m
->addr
& ~PAGE_MASK
), PAGE_SIZE
);
819 dma_map
= genwqe_search_pin(cfile
, map_addr
, map_size
, NULL
);
823 genwqe_del_pin(cfile
, dma_map
);
824 genwqe_user_vunmap(cd
, dma_map
);
830 * ddcb_cmd_cleanup() - Remove dynamically created fixup entries
832 * Only if there are any. Pinnings are not removed.
834 static int ddcb_cmd_cleanup(struct genwqe_file
*cfile
, struct ddcb_requ
*req
)
837 struct dma_mapping
*dma_map
;
838 struct genwqe_dev
*cd
= cfile
->cd
;
840 for (i
= 0; i
< DDCB_FIXUPS
; i
++) {
841 dma_map
= &req
->dma_mappings
[i
];
843 if (dma_mapping_used(dma_map
)) {
844 __genwqe_del_mapping(cfile
, dma_map
);
845 genwqe_user_vunmap(cd
, dma_map
);
847 if (req
->sgls
[i
].sgl
!= NULL
)
848 genwqe_free_sync_sgl(cd
, &req
->sgls
[i
]);
854 * ddcb_cmd_fixups() - Establish DMA fixups/sglists for user memory references
856 * Before the DDCB gets executed we need to handle the fixups. We
857 * replace the user-space addresses with DMA addresses or do
858 * additional setup work e.g. generating a scatter-gather list which
859 * is used to describe the memory referred to in the fixup.
861 static int ddcb_cmd_fixups(struct genwqe_file
*cfile
, struct ddcb_requ
*req
)
864 unsigned int asiv_offs
, i
;
865 struct genwqe_dev
*cd
= cfile
->cd
;
866 struct genwqe_ddcb_cmd
*cmd
= &req
->cmd
;
867 struct dma_mapping
*m
;
869 for (i
= 0, asiv_offs
= 0x00; asiv_offs
<= 0x58;
870 i
++, asiv_offs
+= 0x08) {
877 ats_flags
= ATS_GET_FLAGS(cmd
->ats
, asiv_offs
);
882 break; /* nothing to do here */
884 case ATS_TYPE_FLAT_RDWR
:
885 case ATS_TYPE_FLAT_RD
: {
886 u_addr
= be64_to_cpu(*((__be64
*)&cmd
->
888 u_size
= be32_to_cpu(*((__be32
*)&cmd
->
889 asiv
[asiv_offs
+ 0x08]));
892 * No data available. Ignore u_addr in this
893 * case and set addr to 0. Hardware must not
897 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
902 m
= __genwqe_search_mapping(cfile
, u_addr
, u_size
,
909 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
914 case ATS_TYPE_SGL_RDWR
:
915 case ATS_TYPE_SGL_RD
: {
918 u_addr
= be64_to_cpu(*((__be64
*)
919 &cmd
->asiv
[asiv_offs
]));
920 u_size
= be32_to_cpu(*((__be32
*)
921 &cmd
->asiv
[asiv_offs
+ 0x08]));
924 * No data available. Ignore u_addr in this
925 * case and set addr to 0. Hardware must not
926 * fetch the empty sgl.
929 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
934 m
= genwqe_search_pin(cfile
, u_addr
, u_size
, NULL
);
936 page_offs
= (u_addr
-
937 (u64
)m
->u_vaddr
)/PAGE_SIZE
;
939 m
= &req
->dma_mappings
[i
];
941 genwqe_mapping_init(m
,
942 GENWQE_MAPPING_SGL_TEMP
);
944 if (ats_flags
== ATS_TYPE_SGL_RD
)
947 rc
= genwqe_user_vmap(cd
, m
, (void *)u_addr
,
952 __genwqe_add_mapping(cfile
, m
);
956 /* create genwqe style scatter gather list */
957 rc
= genwqe_alloc_sync_sgl(cd
, &req
->sgls
[i
],
958 (void __user
*)u_addr
,
963 genwqe_setup_sgl(cd
, &req
->sgls
[i
],
964 &m
->dma_list
[page_offs
]);
966 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
967 cpu_to_be64(req
->sgls
[i
].sgl_dma_addr
);
979 ddcb_cmd_cleanup(cfile
, req
);
984 * genwqe_execute_ddcb() - Execute DDCB using userspace address fixups
986 * The code will build up the translation tables or lookup the
987 * contignous memory allocation table to find the right translations
990 static int genwqe_execute_ddcb(struct genwqe_file
*cfile
,
991 struct genwqe_ddcb_cmd
*cmd
)
994 struct genwqe_dev
*cd
= cfile
->cd
;
995 struct file
*filp
= cfile
->filp
;
996 struct ddcb_requ
*req
= container_of(cmd
, struct ddcb_requ
, cmd
);
998 rc
= ddcb_cmd_fixups(cfile
, req
);
1002 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
1003 ddcb_cmd_cleanup(cfile
, req
);
1007 static int do_execute_ddcb(struct genwqe_file
*cfile
,
1008 unsigned long arg
, int raw
)
1011 struct genwqe_ddcb_cmd
*cmd
;
1012 struct genwqe_dev
*cd
= cfile
->cd
;
1013 struct file
*filp
= cfile
->filp
;
1015 cmd
= ddcb_requ_alloc();
1019 if (copy_from_user(cmd
, (void __user
*)arg
, sizeof(*cmd
))) {
1020 ddcb_requ_free(cmd
);
1025 rc
= genwqe_execute_ddcb(cfile
, cmd
);
1027 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
1029 /* Copy back only the modifed fields. Do not copy ASIV
1030 back since the copy got modified by the driver. */
1031 if (copy_to_user((void __user
*)arg
, cmd
,
1032 sizeof(*cmd
) - DDCB_ASIV_LENGTH
)) {
1033 ddcb_requ_free(cmd
);
1037 ddcb_requ_free(cmd
);
1042 * genwqe_ioctl() - IO control
1043 * @filp: file handle
1044 * @cmd: command identifier (passed from user)
1045 * @arg: argument (passed from user)
1049 static long genwqe_ioctl(struct file
*filp
, unsigned int cmd
,
1053 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
1054 struct genwqe_dev
*cd
= cfile
->cd
;
1055 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1056 struct genwqe_reg_io __user
*io
;
1060 /* Return -EIO if card hit EEH */
1061 if (pci_channel_offline(pci_dev
))
1064 if (_IOC_TYPE(cmd
) != GENWQE_IOC_CODE
)
1069 case GENWQE_GET_CARD_STATE
:
1070 put_user(cd
->card_state
, (enum genwqe_card_state __user
*)arg
);
1073 /* Register access */
1074 case GENWQE_READ_REG64
: {
1075 io
= (struct genwqe_reg_io __user
*)arg
;
1077 if (get_user(reg_offs
, &io
->num
))
1080 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x7))
1083 val
= __genwqe_readq(cd
, reg_offs
);
1084 put_user(val
, &io
->val64
);
1088 case GENWQE_WRITE_REG64
: {
1089 io
= (struct genwqe_reg_io __user
*)arg
;
1091 if (!capable(CAP_SYS_ADMIN
))
1094 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1097 if (get_user(reg_offs
, &io
->num
))
1100 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x7))
1103 if (get_user(val
, &io
->val64
))
1106 __genwqe_writeq(cd
, reg_offs
, val
);
1110 case GENWQE_READ_REG32
: {
1111 io
= (struct genwqe_reg_io __user
*)arg
;
1113 if (get_user(reg_offs
, &io
->num
))
1116 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x3))
1119 val
= __genwqe_readl(cd
, reg_offs
);
1120 put_user(val
, &io
->val64
);
1124 case GENWQE_WRITE_REG32
: {
1125 io
= (struct genwqe_reg_io __user
*)arg
;
1127 if (!capable(CAP_SYS_ADMIN
))
1130 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1133 if (get_user(reg_offs
, &io
->num
))
1136 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x3))
1139 if (get_user(val
, &io
->val64
))
1142 __genwqe_writel(cd
, reg_offs
, val
);
1146 /* Flash update/reading */
1147 case GENWQE_SLU_UPDATE
: {
1148 struct genwqe_bitstream load
;
1150 if (!genwqe_is_privileged(cd
))
1153 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1156 if (copy_from_user(&load
, (void __user
*)arg
,
1160 rc
= do_flash_update(cfile
, &load
);
1162 if (copy_to_user((void __user
*)arg
, &load
, sizeof(load
)))
1168 case GENWQE_SLU_READ
: {
1169 struct genwqe_bitstream load
;
1171 if (!genwqe_is_privileged(cd
))
1174 if (genwqe_flash_readback_fails(cd
))
1175 return -ENOSPC
; /* known to fail for old versions */
1177 if (copy_from_user(&load
, (void __user
*)arg
, sizeof(load
)))
1180 rc
= do_flash_read(cfile
, &load
);
1182 if (copy_to_user((void __user
*)arg
, &load
, sizeof(load
)))
1188 /* memory pinning and unpinning */
1189 case GENWQE_PIN_MEM
: {
1190 struct genwqe_mem m
;
1192 if (copy_from_user(&m
, (void __user
*)arg
, sizeof(m
)))
1195 return genwqe_pin_mem(cfile
, &m
);
1198 case GENWQE_UNPIN_MEM
: {
1199 struct genwqe_mem m
;
1201 if (copy_from_user(&m
, (void __user
*)arg
, sizeof(m
)))
1204 return genwqe_unpin_mem(cfile
, &m
);
1207 /* launch an DDCB and wait for completion */
1208 case GENWQE_EXECUTE_DDCB
:
1209 return do_execute_ddcb(cfile
, arg
, 0);
1211 case GENWQE_EXECUTE_RAW_DDCB
: {
1213 if (!capable(CAP_SYS_ADMIN
))
1216 return do_execute_ddcb(cfile
, arg
, 1);
1226 #if defined(CONFIG_COMPAT)
1228 * genwqe_compat_ioctl() - Compatibility ioctl
1230 * Called whenever a 32-bit process running under a 64-bit kernel
1231 * performs an ioctl on /dev/genwqe<n>_card.
1233 * @filp: file pointer.
1235 * @arg: user argument.
1236 * Return: zero on success or negative number on failure.
1238 static long genwqe_compat_ioctl(struct file
*filp
, unsigned int cmd
,
1241 return genwqe_ioctl(filp
, cmd
, arg
);
1243 #endif /* defined(CONFIG_COMPAT) */
1245 static const struct file_operations genwqe_fops
= {
1246 .owner
= THIS_MODULE
,
1247 .open
= genwqe_open
,
1248 .fasync
= genwqe_fasync
,
1249 .mmap
= genwqe_mmap
,
1250 .unlocked_ioctl
= genwqe_ioctl
,
1251 #if defined(CONFIG_COMPAT)
1252 .compat_ioctl
= genwqe_compat_ioctl
,
1254 .release
= genwqe_release
,
1257 static int genwqe_device_initialized(struct genwqe_dev
*cd
)
1259 return cd
->dev
!= NULL
;
1263 * genwqe_device_create() - Create and configure genwqe char device
1264 * @cd: genwqe device descriptor
1266 * This function must be called before we create any more genwqe
1267 * character devices, because it is allocating the major and minor
1268 * number which are supposed to be used by the client drivers.
1270 int genwqe_device_create(struct genwqe_dev
*cd
)
1273 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1276 * Here starts the individual setup per client. It must
1277 * initialize its own cdev data structure with its own fops.
1278 * The appropriate devnum needs to be created. The ranges must
1281 rc
= alloc_chrdev_region(&cd
->devnum_genwqe
, 0,
1282 GENWQE_MAX_MINOR
, GENWQE_DEVNAME
);
1284 dev_err(&pci_dev
->dev
, "err: alloc_chrdev_region failed\n");
1288 cdev_init(&cd
->cdev_genwqe
, &genwqe_fops
);
1289 cd
->cdev_genwqe
.owner
= THIS_MODULE
;
1291 rc
= cdev_add(&cd
->cdev_genwqe
, cd
->devnum_genwqe
, 1);
1293 dev_err(&pci_dev
->dev
, "err: cdev_add failed\n");
1298 * Finally the device in /dev/... must be created. The rule is
1299 * to use card%d_clientname for each created device.
1301 cd
->dev
= device_create_with_groups(cd
->class_genwqe
,
1303 cd
->devnum_genwqe
, cd
,
1304 genwqe_attribute_groups
,
1305 GENWQE_DEVNAME
"%u_card",
1307 if (IS_ERR(cd
->dev
)) {
1308 rc
= PTR_ERR(cd
->dev
);
1312 rc
= genwqe_init_debugfs(cd
);
1319 device_destroy(cd
->class_genwqe
, cd
->devnum_genwqe
);
1321 cdev_del(&cd
->cdev_genwqe
);
1323 unregister_chrdev_region(cd
->devnum_genwqe
, GENWQE_MAX_MINOR
);
1329 static int genwqe_inform_and_stop_processes(struct genwqe_dev
*cd
)
1333 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1335 if (!genwqe_open_files(cd
))
1338 dev_warn(&pci_dev
->dev
, "[%s] send SIGIO and wait ...\n", __func__
);
1340 rc
= genwqe_kill_fasync(cd
, SIGIO
);
1342 /* give kill_timeout seconds to close file descriptors ... */
1343 for (i
= 0; (i
< GENWQE_KILL_TIMEOUT
) &&
1344 genwqe_open_files(cd
); i
++) {
1345 dev_info(&pci_dev
->dev
, " %d sec ...", i
);
1351 /* if no open files we can safely continue, else ... */
1352 if (!genwqe_open_files(cd
))
1355 dev_warn(&pci_dev
->dev
,
1356 "[%s] send SIGKILL and wait ...\n", __func__
);
1358 rc
= genwqe_terminate(cd
);
1360 /* Give kill_timout more seconds to end processes */
1361 for (i
= 0; (i
< GENWQE_KILL_TIMEOUT
) &&
1362 genwqe_open_files(cd
); i
++) {
1363 dev_warn(&pci_dev
->dev
, " %d sec ...", i
);
1374 * genwqe_device_remove() - Remove genwqe's char device
1376 * This function must be called after the client devices are removed
1377 * because it will free the major/minor number range for the genwqe
1380 * This function must be robust enough to be called twice.
1382 int genwqe_device_remove(struct genwqe_dev
*cd
)
1385 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1387 if (!genwqe_device_initialized(cd
))
1390 genwqe_inform_and_stop_processes(cd
);
1393 * We currently do wait until all filedescriptors are
1394 * closed. This leads to a problem when we abort the
1395 * application which will decrease this reference from
1396 * 1/unused to 0/illegal and not from 2/used 1/empty.
1398 rc
= kref_read(&cd
->cdev_genwqe
.kobj
.kref
);
1400 dev_err(&pci_dev
->dev
,
1401 "[%s] err: cdev_genwqe...refcount=%d\n", __func__
, rc
);
1402 panic("Fatal err: cannot free resources with pending references!");
1405 genqwe_exit_debugfs(cd
);
1406 device_destroy(cd
->class_genwqe
, cd
->devnum_genwqe
);
1407 cdev_del(&cd
->cdev_genwqe
);
1408 unregister_chrdev_region(cd
->devnum_genwqe
, GENWQE_MAX_MINOR
);