1 // SPDX-License-Identifier: GPL-2.0-only
3 * IBM Accelerator Family 'GenWQE'
5 * (C) Copyright IBM Corp. 2013
7 * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
8 * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
9 * Author: Michael Jung <mijung@gmx.net>
10 * Author: Michael Ruettger <michael@ibmra.de>
14 * Character device representation of the GenWQE device. This allows
15 * user-space applications to communicate with the card.
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/string.h>
24 #include <linux/sched/signal.h>
25 #include <linux/wait.h>
26 #include <linux/delay.h>
27 #include <linux/atomic.h>
29 #include "card_base.h"
30 #include "card_ddcb.h"
32 static int genwqe_open_files(struct genwqe_dev
*cd
)
37 spin_lock_irqsave(&cd
->file_lock
, flags
);
38 rc
= list_empty(&cd
->file_list
);
39 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
43 static void genwqe_add_file(struct genwqe_dev
*cd
, struct genwqe_file
*cfile
)
47 cfile
->opener
= get_pid(task_tgid(current
));
48 spin_lock_irqsave(&cd
->file_lock
, flags
);
49 list_add(&cfile
->list
, &cd
->file_list
);
50 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
53 static int genwqe_del_file(struct genwqe_dev
*cd
, struct genwqe_file
*cfile
)
57 spin_lock_irqsave(&cd
->file_lock
, flags
);
58 list_del(&cfile
->list
);
59 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
60 put_pid(cfile
->opener
);
65 static void genwqe_add_pin(struct genwqe_file
*cfile
, struct dma_mapping
*m
)
69 spin_lock_irqsave(&cfile
->pin_lock
, flags
);
70 list_add(&m
->pin_list
, &cfile
->pin_list
);
71 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
74 static int genwqe_del_pin(struct genwqe_file
*cfile
, struct dma_mapping
*m
)
78 spin_lock_irqsave(&cfile
->pin_lock
, flags
);
79 list_del(&m
->pin_list
);
80 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
86 * genwqe_search_pin() - Search for the mapping for a userspace address
87 * @cfile: Descriptor of opened file
88 * @u_addr: User virtual address
89 * @size: Size of buffer
90 * @virt_addr: Virtual address to be updated
92 * Return: Pointer to the corresponding mapping NULL if not found
94 static struct dma_mapping
*genwqe_search_pin(struct genwqe_file
*cfile
,
100 struct dma_mapping
*m
;
102 spin_lock_irqsave(&cfile
->pin_lock
, flags
);
104 list_for_each_entry(m
, &cfile
->pin_list
, pin_list
) {
105 if ((((u64
)m
->u_vaddr
) <= (u_addr
)) &&
106 (((u64
)m
->u_vaddr
+ m
->size
) >= (u_addr
+ size
))) {
109 *virt_addr
= m
->k_vaddr
+
110 (u_addr
- (u64
)m
->u_vaddr
);
112 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
116 spin_unlock_irqrestore(&cfile
->pin_lock
, flags
);
120 static void __genwqe_add_mapping(struct genwqe_file
*cfile
,
121 struct dma_mapping
*dma_map
)
125 spin_lock_irqsave(&cfile
->map_lock
, flags
);
126 list_add(&dma_map
->card_list
, &cfile
->map_list
);
127 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
130 static void __genwqe_del_mapping(struct genwqe_file
*cfile
,
131 struct dma_mapping
*dma_map
)
135 spin_lock_irqsave(&cfile
->map_lock
, flags
);
136 list_del(&dma_map
->card_list
);
137 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
142 * __genwqe_search_mapping() - Search for the mapping for a userspace address
143 * @cfile: descriptor of opened file
144 * @u_addr: user virtual address
145 * @size: size of buffer
146 * @dma_addr: DMA address to be updated
147 * @virt_addr: Virtual address to be updated
148 * Return: Pointer to the corresponding mapping NULL if not found
150 static struct dma_mapping
*__genwqe_search_mapping(struct genwqe_file
*cfile
,
151 unsigned long u_addr
,
153 dma_addr_t
*dma_addr
,
157 struct dma_mapping
*m
;
158 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
160 spin_lock_irqsave(&cfile
->map_lock
, flags
);
161 list_for_each_entry(m
, &cfile
->map_list
, card_list
) {
163 if ((((u64
)m
->u_vaddr
) <= (u_addr
)) &&
164 (((u64
)m
->u_vaddr
+ m
->size
) >= (u_addr
+ size
))) {
166 /* match found: current is as expected and
169 *dma_addr
= m
->dma_addr
+
170 (u_addr
- (u64
)m
->u_vaddr
);
173 *virt_addr
= m
->k_vaddr
+
174 (u_addr
- (u64
)m
->u_vaddr
);
176 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
180 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
182 dev_err(&pci_dev
->dev
,
183 "[%s] Entry not found: u_addr=%lx, size=%x\n",
184 __func__
, u_addr
, size
);
189 static void genwqe_remove_mappings(struct genwqe_file
*cfile
)
192 struct list_head
*node
, *next
;
193 struct dma_mapping
*dma_map
;
194 struct genwqe_dev
*cd
= cfile
->cd
;
195 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
197 list_for_each_safe(node
, next
, &cfile
->map_list
) {
198 dma_map
= list_entry(node
, struct dma_mapping
, card_list
);
200 list_del_init(&dma_map
->card_list
);
203 * This is really a bug, because those things should
204 * have been already tidied up.
206 * GENWQE_MAPPING_RAW should have been removed via mmunmap().
207 * GENWQE_MAPPING_SGL_TEMP should be removed by tidy up code.
209 dev_err(&pci_dev
->dev
,
210 "[%s] %d. cleanup mapping: u_vaddr=%p u_kaddr=%016lx dma_addr=%lx\n",
211 __func__
, i
++, dma_map
->u_vaddr
,
212 (unsigned long)dma_map
->k_vaddr
,
213 (unsigned long)dma_map
->dma_addr
);
215 if (dma_map
->type
== GENWQE_MAPPING_RAW
) {
216 /* we allocated this dynamically */
217 __genwqe_free_consistent(cd
, dma_map
->size
,
221 } else if (dma_map
->type
== GENWQE_MAPPING_SGL_TEMP
) {
222 /* we use dma_map statically from the request */
223 genwqe_user_vunmap(cd
, dma_map
);
228 static void genwqe_remove_pinnings(struct genwqe_file
*cfile
)
230 struct list_head
*node
, *next
;
231 struct dma_mapping
*dma_map
;
232 struct genwqe_dev
*cd
= cfile
->cd
;
234 list_for_each_safe(node
, next
, &cfile
->pin_list
) {
235 dma_map
= list_entry(node
, struct dma_mapping
, pin_list
);
238 * This is not a bug, because a killed processed might
239 * not call the unpin ioctl, which is supposed to free
242 * Pinnings are dymically allocated and need to be
245 list_del_init(&dma_map
->pin_list
);
246 genwqe_user_vunmap(cd
, dma_map
);
252 * genwqe_kill_fasync() - Send signal to all processes with open GenWQE files
253 * @cd: GenWQE device information
254 * @sig: Signal to send out
256 * E.g. genwqe_send_signal(cd, SIGIO);
258 static int genwqe_kill_fasync(struct genwqe_dev
*cd
, int sig
)
260 unsigned int files
= 0;
262 struct genwqe_file
*cfile
;
264 spin_lock_irqsave(&cd
->file_lock
, flags
);
265 list_for_each_entry(cfile
, &cd
->file_list
, list
) {
266 if (cfile
->async_queue
)
267 kill_fasync(&cfile
->async_queue
, sig
, POLL_HUP
);
270 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
274 static int genwqe_terminate(struct genwqe_dev
*cd
)
276 unsigned int files
= 0;
278 struct genwqe_file
*cfile
;
280 spin_lock_irqsave(&cd
->file_lock
, flags
);
281 list_for_each_entry(cfile
, &cd
->file_list
, list
) {
282 kill_pid(cfile
->opener
, SIGKILL
, 1);
285 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
290 * genwqe_open() - file open
291 * @inode: file system information
294 * This function is executed whenever an application calls
295 * open("/dev/genwqe",..).
297 * Return: 0 if successful or <0 if errors
299 static int genwqe_open(struct inode
*inode
, struct file
*filp
)
301 struct genwqe_dev
*cd
;
302 struct genwqe_file
*cfile
;
304 cfile
= kzalloc(sizeof(*cfile
), GFP_KERNEL
);
308 cd
= container_of(inode
->i_cdev
, struct genwqe_dev
, cdev_genwqe
);
311 cfile
->client
= NULL
;
313 spin_lock_init(&cfile
->map_lock
); /* list of raw memory allocations */
314 INIT_LIST_HEAD(&cfile
->map_list
);
316 spin_lock_init(&cfile
->pin_lock
); /* list of user pinned memory */
317 INIT_LIST_HEAD(&cfile
->pin_list
);
319 filp
->private_data
= cfile
;
321 genwqe_add_file(cd
, cfile
);
326 * genwqe_fasync() - Setup process to receive SIGIO.
327 * @fd: file descriptor
331 * Sending a signal is working as following:
333 * if (cdev->async_queue)
334 * kill_fasync(&cdev->async_queue, SIGIO, POLL_IN);
336 * Some devices also implement asynchronous notification to indicate
337 * when the device can be written; in this case, of course,
338 * kill_fasync must be called with a mode of POLL_OUT.
340 static int genwqe_fasync(int fd
, struct file
*filp
, int mode
)
342 struct genwqe_file
*cdev
= (struct genwqe_file
*)filp
->private_data
;
344 return fasync_helper(fd
, filp
, mode
, &cdev
->async_queue
);
349 * genwqe_release() - file close
350 * @inode: file system information
353 * This function is executed whenever an application calls 'close(fd_genwqe)'
357 static int genwqe_release(struct inode
*inode
, struct file
*filp
)
359 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
360 struct genwqe_dev
*cd
= cfile
->cd
;
362 /* there must be no entries in these lists! */
363 genwqe_remove_mappings(cfile
);
364 genwqe_remove_pinnings(cfile
);
366 /* remove this filp from the asynchronously notified filp's */
367 genwqe_fasync(-1, filp
, 0);
370 * For this to work we must not release cd when this cfile is
371 * not yet released, otherwise the list entry is invalid,
372 * because the list itself gets reinstantiated!
374 genwqe_del_file(cd
, cfile
);
379 static void genwqe_vma_open(struct vm_area_struct
*vma
)
385 * genwqe_vma_close() - Called each time when vma is unmapped
386 * @vma: VMA area to close
388 * Free memory which got allocated by GenWQE mmap().
390 static void genwqe_vma_close(struct vm_area_struct
*vma
)
392 unsigned long vsize
= vma
->vm_end
- vma
->vm_start
;
393 struct inode
*inode
= file_inode(vma
->vm_file
);
394 struct dma_mapping
*dma_map
;
395 struct genwqe_dev
*cd
= container_of(inode
->i_cdev
, struct genwqe_dev
,
397 struct pci_dev
*pci_dev
= cd
->pci_dev
;
398 dma_addr_t d_addr
= 0;
399 struct genwqe_file
*cfile
= vma
->vm_private_data
;
401 dma_map
= __genwqe_search_mapping(cfile
, vma
->vm_start
, vsize
,
403 if (dma_map
== NULL
) {
404 dev_err(&pci_dev
->dev
,
405 " [%s] err: mapping not found: v=%lx, p=%lx s=%lx\n",
406 __func__
, vma
->vm_start
, vma
->vm_pgoff
<< PAGE_SHIFT
,
410 __genwqe_del_mapping(cfile
, dma_map
);
411 __genwqe_free_consistent(cd
, dma_map
->size
, dma_map
->k_vaddr
,
416 static const struct vm_operations_struct genwqe_vma_ops
= {
417 .open
= genwqe_vma_open
,
418 .close
= genwqe_vma_close
,
422 * genwqe_mmap() - Provide contignous buffers to userspace
423 * @filp: File pointer (unused)
424 * @vma: VMA area to map
426 * We use mmap() to allocate contignous buffers used for DMA
427 * transfers. After the buffer is allocated we remap it to user-space
428 * and remember a reference to our dma_mapping data structure, where
429 * we store the associated DMA address and allocated size.
431 * When we receive a DDCB execution request with the ATS bits set to
432 * plain buffer, we lookup our dma_mapping list to find the
433 * corresponding DMA address for the associated user-space address.
435 static int genwqe_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
438 unsigned long pfn
, vsize
= vma
->vm_end
- vma
->vm_start
;
439 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
440 struct genwqe_dev
*cd
= cfile
->cd
;
441 struct dma_mapping
*dma_map
;
446 if (get_order(vsize
) > MAX_ORDER
)
449 dma_map
= kzalloc(sizeof(struct dma_mapping
), GFP_KERNEL
);
453 genwqe_mapping_init(dma_map
, GENWQE_MAPPING_RAW
);
454 dma_map
->u_vaddr
= (void *)vma
->vm_start
;
455 dma_map
->size
= vsize
;
456 dma_map
->nr_pages
= DIV_ROUND_UP(vsize
, PAGE_SIZE
);
457 dma_map
->k_vaddr
= __genwqe_alloc_consistent(cd
, vsize
,
459 if (dma_map
->k_vaddr
== NULL
) {
464 if (capable(CAP_SYS_ADMIN
) && (vsize
> sizeof(dma_addr_t
)))
465 *(dma_addr_t
*)dma_map
->k_vaddr
= dma_map
->dma_addr
;
467 pfn
= virt_to_phys(dma_map
->k_vaddr
) >> PAGE_SHIFT
;
468 rc
= remap_pfn_range(vma
,
478 vma
->vm_private_data
= cfile
;
479 vma
->vm_ops
= &genwqe_vma_ops
;
480 __genwqe_add_mapping(cfile
, dma_map
);
485 __genwqe_free_consistent(cd
, dma_map
->size
,
493 #define FLASH_BLOCK 0x40000 /* we use 256k blocks */
496 * do_flash_update() - Excute flash update (write image or CVPD)
497 * @cfile: Descriptor of opened file
498 * @load: details about image load
500 * Return: 0 if successful
502 static int do_flash_update(struct genwqe_file
*cfile
,
503 struct genwqe_bitstream
*load
)
514 struct genwqe_dev
*cd
= cfile
->cd
;
515 struct file
*filp
= cfile
->filp
;
516 struct pci_dev
*pci_dev
= cd
->pci_dev
;
518 if ((load
->size
& 0x3) != 0)
521 if (((unsigned long)(load
->data_addr
) & ~PAGE_MASK
) != 0)
524 /* FIXME Bits have changed for new service layer! */
525 switch ((char)load
->partition
) {
528 break; /* download/erase_first/part_0 */
531 break; /* download/erase_first/part_1 */
534 break; /* download/erase_first/vpd */
539 buf
= (u8 __user
*)load
->data_addr
;
540 xbuf
= __genwqe_alloc_consistent(cd
, FLASH_BLOCK
, &dma_addr
);
544 blocks_to_flash
= load
->size
/ FLASH_BLOCK
;
546 struct genwqe_ddcb_cmd
*req
;
549 * We must be 4 byte aligned. Buffer must be 0 appened
550 * to have defined values when calculating CRC.
552 tocopy
= min_t(size_t, load
->size
, FLASH_BLOCK
);
554 rc
= copy_from_user(xbuf
, buf
, tocopy
);
559 crc
= genwqe_crc32(xbuf
, tocopy
, 0xffffffff);
561 dev_dbg(&pci_dev
->dev
,
562 "[%s] DMA: %lx CRC: %08x SZ: %ld %d\n",
563 __func__
, (unsigned long)dma_addr
, crc
, tocopy
,
566 /* prepare DDCB for SLU process */
567 req
= ddcb_requ_alloc();
573 req
->cmd
= SLCMD_MOVE_FLASH
;
574 req
->cmdopts
= cmdopts
;
576 /* prepare invariant values */
577 if (genwqe_get_slu_id(cd
) <= 0x2) {
578 *(__be64
*)&req
->__asiv
[0] = cpu_to_be64(dma_addr
);
579 *(__be64
*)&req
->__asiv
[8] = cpu_to_be64(tocopy
);
580 *(__be64
*)&req
->__asiv
[16] = cpu_to_be64(flash
);
581 *(__be32
*)&req
->__asiv
[24] = cpu_to_be32(0);
582 req
->__asiv
[24] = load
->uid
;
583 *(__be32
*)&req
->__asiv
[28] = cpu_to_be32(crc
);
585 /* for simulation only */
586 *(__be64
*)&req
->__asiv
[88] = cpu_to_be64(load
->slu_id
);
587 *(__be64
*)&req
->__asiv
[96] = cpu_to_be64(load
->app_id
);
588 req
->asiv_length
= 32; /* bytes included in crc calc */
589 } else { /* setup DDCB for ATS architecture */
590 *(__be64
*)&req
->asiv
[0] = cpu_to_be64(dma_addr
);
591 *(__be32
*)&req
->asiv
[8] = cpu_to_be32(tocopy
);
592 *(__be32
*)&req
->asiv
[12] = cpu_to_be32(0); /* resvd */
593 *(__be64
*)&req
->asiv
[16] = cpu_to_be64(flash
);
594 *(__be32
*)&req
->asiv
[24] = cpu_to_be32(load
->uid
<<24);
595 *(__be32
*)&req
->asiv
[28] = cpu_to_be32(crc
);
597 /* for simulation only */
598 *(__be64
*)&req
->asiv
[80] = cpu_to_be64(load
->slu_id
);
599 *(__be64
*)&req
->asiv
[88] = cpu_to_be64(load
->app_id
);
602 req
->ats
= 0x4ULL
<< 44;
603 req
->asiv_length
= 40; /* bytes included in crc calc */
607 /* For Genwqe5 we get back the calculated CRC */
608 *(u64
*)&req
->asv
[0] = 0ULL; /* 0x80 */
610 rc
= __genwqe_execute_raw_ddcb(cd
, req
, filp
->f_flags
);
612 load
->retc
= req
->retc
;
613 load
->attn
= req
->attn
;
614 load
->progress
= req
->progress
;
621 if (req
->retc
!= DDCB_RETC_COMPLETE
) {
627 load
->size
-= tocopy
;
635 __genwqe_free_consistent(cd
, FLASH_BLOCK
, xbuf
, dma_addr
);
639 static int do_flash_read(struct genwqe_file
*cfile
,
640 struct genwqe_bitstream
*load
)
642 int rc
, blocks_to_flash
;
649 struct genwqe_dev
*cd
= cfile
->cd
;
650 struct file
*filp
= cfile
->filp
;
651 struct pci_dev
*pci_dev
= cd
->pci_dev
;
652 struct genwqe_ddcb_cmd
*cmd
;
654 if ((load
->size
& 0x3) != 0)
657 if (((unsigned long)(load
->data_addr
) & ~PAGE_MASK
) != 0)
660 /* FIXME Bits have changed for new service layer! */
661 switch ((char)load
->partition
) {
664 break; /* upload/part_0 */
667 break; /* upload/part_1 */
670 break; /* upload/vpd */
675 buf
= (u8 __user
*)load
->data_addr
;
676 xbuf
= __genwqe_alloc_consistent(cd
, FLASH_BLOCK
, &dma_addr
);
680 blocks_to_flash
= load
->size
/ FLASH_BLOCK
;
683 * We must be 4 byte aligned. Buffer must be 0 appened
684 * to have defined values when calculating CRC.
686 tocopy
= min_t(size_t, load
->size
, FLASH_BLOCK
);
688 dev_dbg(&pci_dev
->dev
,
689 "[%s] DMA: %lx SZ: %ld %d\n",
690 __func__
, (unsigned long)dma_addr
, tocopy
,
693 /* prepare DDCB for SLU process */
694 cmd
= ddcb_requ_alloc();
699 cmd
->cmd
= SLCMD_MOVE_FLASH
;
700 cmd
->cmdopts
= cmdopts
;
702 /* prepare invariant values */
703 if (genwqe_get_slu_id(cd
) <= 0x2) {
704 *(__be64
*)&cmd
->__asiv
[0] = cpu_to_be64(dma_addr
);
705 *(__be64
*)&cmd
->__asiv
[8] = cpu_to_be64(tocopy
);
706 *(__be64
*)&cmd
->__asiv
[16] = cpu_to_be64(flash
);
707 *(__be32
*)&cmd
->__asiv
[24] = cpu_to_be32(0);
708 cmd
->__asiv
[24] = load
->uid
;
709 *(__be32
*)&cmd
->__asiv
[28] = cpu_to_be32(0) /* CRC */;
710 cmd
->asiv_length
= 32; /* bytes included in crc calc */
711 } else { /* setup DDCB for ATS architecture */
712 *(__be64
*)&cmd
->asiv
[0] = cpu_to_be64(dma_addr
);
713 *(__be32
*)&cmd
->asiv
[8] = cpu_to_be32(tocopy
);
714 *(__be32
*)&cmd
->asiv
[12] = cpu_to_be32(0); /* resvd */
715 *(__be64
*)&cmd
->asiv
[16] = cpu_to_be64(flash
);
716 *(__be32
*)&cmd
->asiv
[24] = cpu_to_be32(load
->uid
<<24);
717 *(__be32
*)&cmd
->asiv
[28] = cpu_to_be32(0); /* CRC */
720 cmd
->ats
= 0x5ULL
<< 44;
721 cmd
->asiv_length
= 40; /* bytes included in crc calc */
725 /* we only get back the calculated CRC */
726 *(u64
*)&cmd
->asv
[0] = 0ULL; /* 0x80 */
728 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
730 load
->retc
= cmd
->retc
;
731 load
->attn
= cmd
->attn
;
732 load
->progress
= cmd
->progress
;
734 if ((rc
< 0) && (rc
!= -EBADMSG
)) {
739 rc
= copy_to_user(buf
, xbuf
, tocopy
);
746 /* We know that we can get retc 0x104 with CRC err */
747 if (((cmd
->retc
== DDCB_RETC_FAULT
) &&
748 (cmd
->attn
!= 0x02)) || /* Normally ignore CRC error */
749 ((cmd
->retc
== DDCB_RETC_COMPLETE
) &&
750 (cmd
->attn
!= 0x00))) { /* Everything was fine */
756 load
->size
-= tocopy
;
765 __genwqe_free_consistent(cd
, FLASH_BLOCK
, xbuf
, dma_addr
);
769 static int genwqe_pin_mem(struct genwqe_file
*cfile
, struct genwqe_mem
*m
)
772 struct genwqe_dev
*cd
= cfile
->cd
;
773 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
774 struct dma_mapping
*dma_map
;
775 unsigned long map_addr
;
776 unsigned long map_size
;
778 if ((m
->addr
== 0x0) || (m
->size
== 0))
780 if (m
->size
> ULONG_MAX
- PAGE_SIZE
- (m
->addr
& ~PAGE_MASK
))
783 map_addr
= (m
->addr
& PAGE_MASK
);
784 map_size
= round_up(m
->size
+ (m
->addr
& ~PAGE_MASK
), PAGE_SIZE
);
786 dma_map
= kzalloc(sizeof(struct dma_mapping
), GFP_KERNEL
);
790 genwqe_mapping_init(dma_map
, GENWQE_MAPPING_SGL_PINNED
);
791 rc
= genwqe_user_vmap(cd
, dma_map
, (void *)map_addr
, map_size
);
793 dev_err(&pci_dev
->dev
,
794 "[%s] genwqe_user_vmap rc=%d\n", __func__
, rc
);
799 genwqe_add_pin(cfile
, dma_map
);
803 static int genwqe_unpin_mem(struct genwqe_file
*cfile
, struct genwqe_mem
*m
)
805 struct genwqe_dev
*cd
= cfile
->cd
;
806 struct dma_mapping
*dma_map
;
807 unsigned long map_addr
;
808 unsigned long map_size
;
813 map_addr
= (m
->addr
& PAGE_MASK
);
814 map_size
= round_up(m
->size
+ (m
->addr
& ~PAGE_MASK
), PAGE_SIZE
);
816 dma_map
= genwqe_search_pin(cfile
, map_addr
, map_size
, NULL
);
820 genwqe_del_pin(cfile
, dma_map
);
821 genwqe_user_vunmap(cd
, dma_map
);
827 * ddcb_cmd_cleanup() - Remove dynamically created fixup entries
828 * @cfile: Descriptor of opened file
829 * @req: DDCB work request
831 * Only if there are any. Pinnings are not removed.
833 static int ddcb_cmd_cleanup(struct genwqe_file
*cfile
, struct ddcb_requ
*req
)
836 struct dma_mapping
*dma_map
;
837 struct genwqe_dev
*cd
= cfile
->cd
;
839 for (i
= 0; i
< DDCB_FIXUPS
; i
++) {
840 dma_map
= &req
->dma_mappings
[i
];
842 if (dma_mapping_used(dma_map
)) {
843 __genwqe_del_mapping(cfile
, dma_map
);
844 genwqe_user_vunmap(cd
, dma_map
);
846 if (req
->sgls
[i
].sgl
!= NULL
)
847 genwqe_free_sync_sgl(cd
, &req
->sgls
[i
]);
853 * ddcb_cmd_fixups() - Establish DMA fixups/sglists for user memory references
854 * @cfile: Descriptor of opened file
855 * @req: DDCB work request
857 * Before the DDCB gets executed we need to handle the fixups. We
858 * replace the user-space addresses with DMA addresses or do
859 * additional setup work e.g. generating a scatter-gather list which
860 * is used to describe the memory referred to in the fixup.
862 static int ddcb_cmd_fixups(struct genwqe_file
*cfile
, struct ddcb_requ
*req
)
865 unsigned int asiv_offs
, i
;
866 struct genwqe_dev
*cd
= cfile
->cd
;
867 struct genwqe_ddcb_cmd
*cmd
= &req
->cmd
;
868 struct dma_mapping
*m
;
870 for (i
= 0, asiv_offs
= 0x00; asiv_offs
<= 0x58;
871 i
++, asiv_offs
+= 0x08) {
878 ats_flags
= ATS_GET_FLAGS(cmd
->ats
, asiv_offs
);
883 break; /* nothing to do here */
885 case ATS_TYPE_FLAT_RDWR
:
886 case ATS_TYPE_FLAT_RD
: {
887 u_addr
= be64_to_cpu(*((__be64
*)&cmd
->
889 u_size
= be32_to_cpu(*((__be32
*)&cmd
->
890 asiv
[asiv_offs
+ 0x08]));
893 * No data available. Ignore u_addr in this
894 * case and set addr to 0. Hardware must not
898 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
903 m
= __genwqe_search_mapping(cfile
, u_addr
, u_size
,
910 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
915 case ATS_TYPE_SGL_RDWR
:
916 case ATS_TYPE_SGL_RD
: {
919 u_addr
= be64_to_cpu(*((__be64
*)
920 &cmd
->asiv
[asiv_offs
]));
921 u_size
= be32_to_cpu(*((__be32
*)
922 &cmd
->asiv
[asiv_offs
+ 0x08]));
925 * No data available. Ignore u_addr in this
926 * case and set addr to 0. Hardware must not
927 * fetch the empty sgl.
930 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
935 m
= genwqe_search_pin(cfile
, u_addr
, u_size
, NULL
);
937 page_offs
= (u_addr
-
938 (u64
)m
->u_vaddr
)/PAGE_SIZE
;
940 m
= &req
->dma_mappings
[i
];
942 genwqe_mapping_init(m
,
943 GENWQE_MAPPING_SGL_TEMP
);
945 if (ats_flags
== ATS_TYPE_SGL_RD
)
948 rc
= genwqe_user_vmap(cd
, m
, (void *)u_addr
,
953 __genwqe_add_mapping(cfile
, m
);
957 /* create genwqe style scatter gather list */
958 rc
= genwqe_alloc_sync_sgl(cd
, &req
->sgls
[i
],
959 (void __user
*)u_addr
,
964 genwqe_setup_sgl(cd
, &req
->sgls
[i
],
965 &m
->dma_list
[page_offs
]);
967 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
968 cpu_to_be64(req
->sgls
[i
].sgl_dma_addr
);
980 ddcb_cmd_cleanup(cfile
, req
);
985 * genwqe_execute_ddcb() - Execute DDCB using userspace address fixups
986 * @cfile: Descriptor of opened file
987 * @cmd: Command identifier (passed from user)
989 * The code will build up the translation tables or lookup the
990 * contignous memory allocation table to find the right translations
993 static int genwqe_execute_ddcb(struct genwqe_file
*cfile
,
994 struct genwqe_ddcb_cmd
*cmd
)
997 struct genwqe_dev
*cd
= cfile
->cd
;
998 struct file
*filp
= cfile
->filp
;
999 struct ddcb_requ
*req
= container_of(cmd
, struct ddcb_requ
, cmd
);
1001 rc
= ddcb_cmd_fixups(cfile
, req
);
1005 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
1006 ddcb_cmd_cleanup(cfile
, req
);
1010 static int do_execute_ddcb(struct genwqe_file
*cfile
,
1011 unsigned long arg
, int raw
)
1014 struct genwqe_ddcb_cmd
*cmd
;
1015 struct genwqe_dev
*cd
= cfile
->cd
;
1016 struct file
*filp
= cfile
->filp
;
1018 cmd
= ddcb_requ_alloc();
1022 if (copy_from_user(cmd
, (void __user
*)arg
, sizeof(*cmd
))) {
1023 ddcb_requ_free(cmd
);
1028 rc
= genwqe_execute_ddcb(cfile
, cmd
);
1030 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
1032 /* Copy back only the modifed fields. Do not copy ASIV
1033 back since the copy got modified by the driver. */
1034 if (copy_to_user((void __user
*)arg
, cmd
,
1035 sizeof(*cmd
) - DDCB_ASIV_LENGTH
)) {
1036 ddcb_requ_free(cmd
);
1040 ddcb_requ_free(cmd
);
1045 * genwqe_ioctl() - IO control
1046 * @filp: file handle
1047 * @cmd: command identifier (passed from user)
1048 * @arg: argument (passed from user)
1052 static long genwqe_ioctl(struct file
*filp
, unsigned int cmd
,
1056 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
1057 struct genwqe_dev
*cd
= cfile
->cd
;
1058 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1059 struct genwqe_reg_io __user
*io
;
1063 /* Return -EIO if card hit EEH */
1064 if (pci_channel_offline(pci_dev
))
1067 if (_IOC_TYPE(cmd
) != GENWQE_IOC_CODE
)
1072 case GENWQE_GET_CARD_STATE
:
1073 put_user(cd
->card_state
, (enum genwqe_card_state __user
*)arg
);
1076 /* Register access */
1077 case GENWQE_READ_REG64
: {
1078 io
= (struct genwqe_reg_io __user
*)arg
;
1080 if (get_user(reg_offs
, &io
->num
))
1083 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x7))
1086 val
= __genwqe_readq(cd
, reg_offs
);
1087 put_user(val
, &io
->val64
);
1091 case GENWQE_WRITE_REG64
: {
1092 io
= (struct genwqe_reg_io __user
*)arg
;
1094 if (!capable(CAP_SYS_ADMIN
))
1097 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1100 if (get_user(reg_offs
, &io
->num
))
1103 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x7))
1106 if (get_user(val
, &io
->val64
))
1109 __genwqe_writeq(cd
, reg_offs
, val
);
1113 case GENWQE_READ_REG32
: {
1114 io
= (struct genwqe_reg_io __user
*)arg
;
1116 if (get_user(reg_offs
, &io
->num
))
1119 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x3))
1122 val
= __genwqe_readl(cd
, reg_offs
);
1123 put_user(val
, &io
->val64
);
1127 case GENWQE_WRITE_REG32
: {
1128 io
= (struct genwqe_reg_io __user
*)arg
;
1130 if (!capable(CAP_SYS_ADMIN
))
1133 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1136 if (get_user(reg_offs
, &io
->num
))
1139 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x3))
1142 if (get_user(val
, &io
->val64
))
1145 __genwqe_writel(cd
, reg_offs
, val
);
1149 /* Flash update/reading */
1150 case GENWQE_SLU_UPDATE
: {
1151 struct genwqe_bitstream load
;
1153 if (!genwqe_is_privileged(cd
))
1156 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1159 if (copy_from_user(&load
, (void __user
*)arg
,
1163 rc
= do_flash_update(cfile
, &load
);
1165 if (copy_to_user((void __user
*)arg
, &load
, sizeof(load
)))
1171 case GENWQE_SLU_READ
: {
1172 struct genwqe_bitstream load
;
1174 if (!genwqe_is_privileged(cd
))
1177 if (genwqe_flash_readback_fails(cd
))
1178 return -ENOSPC
; /* known to fail for old versions */
1180 if (copy_from_user(&load
, (void __user
*)arg
, sizeof(load
)))
1183 rc
= do_flash_read(cfile
, &load
);
1185 if (copy_to_user((void __user
*)arg
, &load
, sizeof(load
)))
1191 /* memory pinning and unpinning */
1192 case GENWQE_PIN_MEM
: {
1193 struct genwqe_mem m
;
1195 if (copy_from_user(&m
, (void __user
*)arg
, sizeof(m
)))
1198 return genwqe_pin_mem(cfile
, &m
);
1201 case GENWQE_UNPIN_MEM
: {
1202 struct genwqe_mem m
;
1204 if (copy_from_user(&m
, (void __user
*)arg
, sizeof(m
)))
1207 return genwqe_unpin_mem(cfile
, &m
);
1210 /* launch an DDCB and wait for completion */
1211 case GENWQE_EXECUTE_DDCB
:
1212 return do_execute_ddcb(cfile
, arg
, 0);
1214 case GENWQE_EXECUTE_RAW_DDCB
: {
1216 if (!capable(CAP_SYS_ADMIN
))
1219 return do_execute_ddcb(cfile
, arg
, 1);
1229 static const struct file_operations genwqe_fops
= {
1230 .owner
= THIS_MODULE
,
1231 .open
= genwqe_open
,
1232 .fasync
= genwqe_fasync
,
1233 .mmap
= genwqe_mmap
,
1234 .unlocked_ioctl
= genwqe_ioctl
,
1235 .compat_ioctl
= compat_ptr_ioctl
,
1236 .release
= genwqe_release
,
1239 static int genwqe_device_initialized(struct genwqe_dev
*cd
)
1241 return cd
->dev
!= NULL
;
1245 * genwqe_device_create() - Create and configure genwqe char device
1246 * @cd: genwqe device descriptor
1248 * This function must be called before we create any more genwqe
1249 * character devices, because it is allocating the major and minor
1250 * number which are supposed to be used by the client drivers.
1252 int genwqe_device_create(struct genwqe_dev
*cd
)
1255 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1258 * Here starts the individual setup per client. It must
1259 * initialize its own cdev data structure with its own fops.
1260 * The appropriate devnum needs to be created. The ranges must
1263 rc
= alloc_chrdev_region(&cd
->devnum_genwqe
, 0,
1264 GENWQE_MAX_MINOR
, GENWQE_DEVNAME
);
1266 dev_err(&pci_dev
->dev
, "err: alloc_chrdev_region failed\n");
1270 cdev_init(&cd
->cdev_genwqe
, &genwqe_fops
);
1271 cd
->cdev_genwqe
.owner
= THIS_MODULE
;
1273 rc
= cdev_add(&cd
->cdev_genwqe
, cd
->devnum_genwqe
, 1);
1275 dev_err(&pci_dev
->dev
, "err: cdev_add failed\n");
1280 * Finally the device in /dev/... must be created. The rule is
1281 * to use card%d_clientname for each created device.
1283 cd
->dev
= device_create_with_groups(cd
->class_genwqe
,
1285 cd
->devnum_genwqe
, cd
,
1286 genwqe_attribute_groups
,
1287 GENWQE_DEVNAME
"%u_card",
1289 if (IS_ERR(cd
->dev
)) {
1290 rc
= PTR_ERR(cd
->dev
);
1294 genwqe_init_debugfs(cd
);
1299 cdev_del(&cd
->cdev_genwqe
);
1301 unregister_chrdev_region(cd
->devnum_genwqe
, GENWQE_MAX_MINOR
);
1307 static int genwqe_inform_and_stop_processes(struct genwqe_dev
*cd
)
1311 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1313 if (!genwqe_open_files(cd
))
1316 dev_warn(&pci_dev
->dev
, "[%s] send SIGIO and wait ...\n", __func__
);
1318 rc
= genwqe_kill_fasync(cd
, SIGIO
);
1320 /* give kill_timeout seconds to close file descriptors ... */
1321 for (i
= 0; (i
< GENWQE_KILL_TIMEOUT
) &&
1322 genwqe_open_files(cd
); i
++) {
1323 dev_info(&pci_dev
->dev
, " %d sec ...", i
);
1329 /* if no open files we can safely continue, else ... */
1330 if (!genwqe_open_files(cd
))
1333 dev_warn(&pci_dev
->dev
,
1334 "[%s] send SIGKILL and wait ...\n", __func__
);
1336 rc
= genwqe_terminate(cd
);
1338 /* Give kill_timout more seconds to end processes */
1339 for (i
= 0; (i
< GENWQE_KILL_TIMEOUT
) &&
1340 genwqe_open_files(cd
); i
++) {
1341 dev_warn(&pci_dev
->dev
, " %d sec ...", i
);
1352 * genwqe_device_remove() - Remove genwqe's char device
1353 * @cd: GenWQE device information
1355 * This function must be called after the client devices are removed
1356 * because it will free the major/minor number range for the genwqe
1359 * This function must be robust enough to be called twice.
1361 int genwqe_device_remove(struct genwqe_dev
*cd
)
1364 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1366 if (!genwqe_device_initialized(cd
))
1369 genwqe_inform_and_stop_processes(cd
);
1372 * We currently do wait until all filedescriptors are
1373 * closed. This leads to a problem when we abort the
1374 * application which will decrease this reference from
1375 * 1/unused to 0/illegal and not from 2/used 1/empty.
1377 rc
= kref_read(&cd
->cdev_genwqe
.kobj
.kref
);
1379 dev_err(&pci_dev
->dev
,
1380 "[%s] err: cdev_genwqe...refcount=%d\n", __func__
, rc
);
1381 panic("Fatal err: cannot free resources with pending references!");
1384 genqwe_exit_debugfs(cd
);
1385 device_destroy(cd
->class_genwqe
, cd
->devnum_genwqe
);
1386 cdev_del(&cd
->cdev_genwqe
);
1387 unregister_chrdev_region(cd
->devnum_genwqe
, GENWQE_MAX_MINOR
);