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 * @dma_addr: DMA 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 * Return: Pointer to the corresponding mapping NULL if not found
149 static struct dma_mapping
*__genwqe_search_mapping(struct genwqe_file
*cfile
,
150 unsigned long u_addr
,
152 dma_addr_t
*dma_addr
,
156 struct dma_mapping
*m
;
157 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
159 spin_lock_irqsave(&cfile
->map_lock
, flags
);
160 list_for_each_entry(m
, &cfile
->map_list
, card_list
) {
162 if ((((u64
)m
->u_vaddr
) <= (u_addr
)) &&
163 (((u64
)m
->u_vaddr
+ m
->size
) >= (u_addr
+ size
))) {
165 /* match found: current is as expected and
168 *dma_addr
= m
->dma_addr
+
169 (u_addr
- (u64
)m
->u_vaddr
);
172 *virt_addr
= m
->k_vaddr
+
173 (u_addr
- (u64
)m
->u_vaddr
);
175 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
179 spin_unlock_irqrestore(&cfile
->map_lock
, flags
);
181 dev_err(&pci_dev
->dev
,
182 "[%s] Entry not found: u_addr=%lx, size=%x\n",
183 __func__
, u_addr
, size
);
188 static void genwqe_remove_mappings(struct genwqe_file
*cfile
)
191 struct list_head
*node
, *next
;
192 struct dma_mapping
*dma_map
;
193 struct genwqe_dev
*cd
= cfile
->cd
;
194 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
196 list_for_each_safe(node
, next
, &cfile
->map_list
) {
197 dma_map
= list_entry(node
, struct dma_mapping
, card_list
);
199 list_del_init(&dma_map
->card_list
);
202 * This is really a bug, because those things should
203 * have been already tidied up.
205 * GENWQE_MAPPING_RAW should have been removed via mmunmap().
206 * GENWQE_MAPPING_SGL_TEMP should be removed by tidy up code.
208 dev_err(&pci_dev
->dev
,
209 "[%s] %d. cleanup mapping: u_vaddr=%p u_kaddr=%016lx dma_addr=%lx\n",
210 __func__
, i
++, dma_map
->u_vaddr
,
211 (unsigned long)dma_map
->k_vaddr
,
212 (unsigned long)dma_map
->dma_addr
);
214 if (dma_map
->type
== GENWQE_MAPPING_RAW
) {
215 /* we allocated this dynamically */
216 __genwqe_free_consistent(cd
, dma_map
->size
,
220 } else if (dma_map
->type
== GENWQE_MAPPING_SGL_TEMP
) {
221 /* we use dma_map statically from the request */
222 genwqe_user_vunmap(cd
, dma_map
);
227 static void genwqe_remove_pinnings(struct genwqe_file
*cfile
)
229 struct list_head
*node
, *next
;
230 struct dma_mapping
*dma_map
;
231 struct genwqe_dev
*cd
= cfile
->cd
;
233 list_for_each_safe(node
, next
, &cfile
->pin_list
) {
234 dma_map
= list_entry(node
, struct dma_mapping
, pin_list
);
237 * This is not a bug, because a killed processed might
238 * not call the unpin ioctl, which is supposed to free
241 * Pinnings are dymically allocated and need to be
244 list_del_init(&dma_map
->pin_list
);
245 genwqe_user_vunmap(cd
, dma_map
);
251 * genwqe_kill_fasync() - Send signal to all processes with open GenWQE files
253 * E.g. genwqe_send_signal(cd, SIGIO);
255 static int genwqe_kill_fasync(struct genwqe_dev
*cd
, int sig
)
257 unsigned int files
= 0;
259 struct genwqe_file
*cfile
;
261 spin_lock_irqsave(&cd
->file_lock
, flags
);
262 list_for_each_entry(cfile
, &cd
->file_list
, list
) {
263 if (cfile
->async_queue
)
264 kill_fasync(&cfile
->async_queue
, sig
, POLL_HUP
);
267 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
271 static int genwqe_terminate(struct genwqe_dev
*cd
)
273 unsigned int files
= 0;
275 struct genwqe_file
*cfile
;
277 spin_lock_irqsave(&cd
->file_lock
, flags
);
278 list_for_each_entry(cfile
, &cd
->file_list
, list
) {
279 kill_pid(cfile
->opener
, SIGKILL
, 1);
282 spin_unlock_irqrestore(&cd
->file_lock
, flags
);
287 * genwqe_open() - file open
288 * @inode: file system information
291 * This function is executed whenever an application calls
292 * open("/dev/genwqe",..).
294 * Return: 0 if successful or <0 if errors
296 static int genwqe_open(struct inode
*inode
, struct file
*filp
)
298 struct genwqe_dev
*cd
;
299 struct genwqe_file
*cfile
;
301 cfile
= kzalloc(sizeof(*cfile
), GFP_KERNEL
);
305 cd
= container_of(inode
->i_cdev
, struct genwqe_dev
, cdev_genwqe
);
308 cfile
->client
= NULL
;
310 spin_lock_init(&cfile
->map_lock
); /* list of raw memory allocations */
311 INIT_LIST_HEAD(&cfile
->map_list
);
313 spin_lock_init(&cfile
->pin_lock
); /* list of user pinned memory */
314 INIT_LIST_HEAD(&cfile
->pin_list
);
316 filp
->private_data
= cfile
;
318 genwqe_add_file(cd
, cfile
);
323 * genwqe_fasync() - Setup process to receive SIGIO.
324 * @fd: file descriptor
328 * Sending a signal is working as following:
330 * if (cdev->async_queue)
331 * kill_fasync(&cdev->async_queue, SIGIO, POLL_IN);
333 * Some devices also implement asynchronous notification to indicate
334 * when the device can be written; in this case, of course,
335 * kill_fasync must be called with a mode of POLL_OUT.
337 static int genwqe_fasync(int fd
, struct file
*filp
, int mode
)
339 struct genwqe_file
*cdev
= (struct genwqe_file
*)filp
->private_data
;
341 return fasync_helper(fd
, filp
, mode
, &cdev
->async_queue
);
346 * genwqe_release() - file close
347 * @inode: file system information
350 * This function is executed whenever an application calls 'close(fd_genwqe)'
354 static int genwqe_release(struct inode
*inode
, struct file
*filp
)
356 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
357 struct genwqe_dev
*cd
= cfile
->cd
;
359 /* there must be no entries in these lists! */
360 genwqe_remove_mappings(cfile
);
361 genwqe_remove_pinnings(cfile
);
363 /* remove this filp from the asynchronously notified filp's */
364 genwqe_fasync(-1, filp
, 0);
367 * For this to work we must not release cd when this cfile is
368 * not yet released, otherwise the list entry is invalid,
369 * because the list itself gets reinstantiated!
371 genwqe_del_file(cd
, cfile
);
376 static void genwqe_vma_open(struct vm_area_struct
*vma
)
382 * genwqe_vma_close() - Called each time when vma is unmapped
384 * Free memory which got allocated by GenWQE mmap().
386 static void genwqe_vma_close(struct vm_area_struct
*vma
)
388 unsigned long vsize
= vma
->vm_end
- vma
->vm_start
;
389 struct inode
*inode
= file_inode(vma
->vm_file
);
390 struct dma_mapping
*dma_map
;
391 struct genwqe_dev
*cd
= container_of(inode
->i_cdev
, struct genwqe_dev
,
393 struct pci_dev
*pci_dev
= cd
->pci_dev
;
394 dma_addr_t d_addr
= 0;
395 struct genwqe_file
*cfile
= vma
->vm_private_data
;
397 dma_map
= __genwqe_search_mapping(cfile
, vma
->vm_start
, vsize
,
399 if (dma_map
== NULL
) {
400 dev_err(&pci_dev
->dev
,
401 " [%s] err: mapping not found: v=%lx, p=%lx s=%lx\n",
402 __func__
, vma
->vm_start
, vma
->vm_pgoff
<< PAGE_SHIFT
,
406 __genwqe_del_mapping(cfile
, dma_map
);
407 __genwqe_free_consistent(cd
, dma_map
->size
, dma_map
->k_vaddr
,
412 static const struct vm_operations_struct genwqe_vma_ops
= {
413 .open
= genwqe_vma_open
,
414 .close
= genwqe_vma_close
,
418 * genwqe_mmap() - Provide contignous buffers to userspace
420 * We use mmap() to allocate contignous buffers used for DMA
421 * transfers. After the buffer is allocated we remap it to user-space
422 * and remember a reference to our dma_mapping data structure, where
423 * we store the associated DMA address and allocated size.
425 * When we receive a DDCB execution request with the ATS bits set to
426 * plain buffer, we lookup our dma_mapping list to find the
427 * corresponding DMA address for the associated user-space address.
429 static int genwqe_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
432 unsigned long pfn
, vsize
= vma
->vm_end
- vma
->vm_start
;
433 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
434 struct genwqe_dev
*cd
= cfile
->cd
;
435 struct dma_mapping
*dma_map
;
440 if (get_order(vsize
) > MAX_ORDER
)
443 dma_map
= kzalloc(sizeof(struct dma_mapping
), GFP_KERNEL
);
447 genwqe_mapping_init(dma_map
, GENWQE_MAPPING_RAW
);
448 dma_map
->u_vaddr
= (void *)vma
->vm_start
;
449 dma_map
->size
= vsize
;
450 dma_map
->nr_pages
= DIV_ROUND_UP(vsize
, PAGE_SIZE
);
451 dma_map
->k_vaddr
= __genwqe_alloc_consistent(cd
, vsize
,
453 if (dma_map
->k_vaddr
== NULL
) {
458 if (capable(CAP_SYS_ADMIN
) && (vsize
> sizeof(dma_addr_t
)))
459 *(dma_addr_t
*)dma_map
->k_vaddr
= dma_map
->dma_addr
;
461 pfn
= virt_to_phys(dma_map
->k_vaddr
) >> PAGE_SHIFT
;
462 rc
= remap_pfn_range(vma
,
472 vma
->vm_private_data
= cfile
;
473 vma
->vm_ops
= &genwqe_vma_ops
;
474 __genwqe_add_mapping(cfile
, dma_map
);
479 __genwqe_free_consistent(cd
, dma_map
->size
,
488 * do_flash_update() - Excute flash update (write image or CVPD)
490 * @load: details about image load
492 * Return: 0 if successful
495 #define FLASH_BLOCK 0x40000 /* we use 256k blocks */
497 static int do_flash_update(struct genwqe_file
*cfile
,
498 struct genwqe_bitstream
*load
)
509 struct genwqe_dev
*cd
= cfile
->cd
;
510 struct file
*filp
= cfile
->filp
;
511 struct pci_dev
*pci_dev
= cd
->pci_dev
;
513 if ((load
->size
& 0x3) != 0)
516 if (((unsigned long)(load
->data_addr
) & ~PAGE_MASK
) != 0)
519 /* FIXME Bits have changed for new service layer! */
520 switch ((char)load
->partition
) {
523 break; /* download/erase_first/part_0 */
526 break; /* download/erase_first/part_1 */
529 break; /* download/erase_first/vpd */
534 buf
= (u8 __user
*)load
->data_addr
;
535 xbuf
= __genwqe_alloc_consistent(cd
, FLASH_BLOCK
, &dma_addr
);
539 blocks_to_flash
= load
->size
/ FLASH_BLOCK
;
541 struct genwqe_ddcb_cmd
*req
;
544 * We must be 4 byte aligned. Buffer must be 0 appened
545 * to have defined values when calculating CRC.
547 tocopy
= min_t(size_t, load
->size
, FLASH_BLOCK
);
549 rc
= copy_from_user(xbuf
, buf
, tocopy
);
554 crc
= genwqe_crc32(xbuf
, tocopy
, 0xffffffff);
556 dev_dbg(&pci_dev
->dev
,
557 "[%s] DMA: %lx CRC: %08x SZ: %ld %d\n",
558 __func__
, (unsigned long)dma_addr
, crc
, tocopy
,
561 /* prepare DDCB for SLU process */
562 req
= ddcb_requ_alloc();
568 req
->cmd
= SLCMD_MOVE_FLASH
;
569 req
->cmdopts
= cmdopts
;
571 /* prepare invariant values */
572 if (genwqe_get_slu_id(cd
) <= 0x2) {
573 *(__be64
*)&req
->__asiv
[0] = cpu_to_be64(dma_addr
);
574 *(__be64
*)&req
->__asiv
[8] = cpu_to_be64(tocopy
);
575 *(__be64
*)&req
->__asiv
[16] = cpu_to_be64(flash
);
576 *(__be32
*)&req
->__asiv
[24] = cpu_to_be32(0);
577 req
->__asiv
[24] = load
->uid
;
578 *(__be32
*)&req
->__asiv
[28] = cpu_to_be32(crc
);
580 /* for simulation only */
581 *(__be64
*)&req
->__asiv
[88] = cpu_to_be64(load
->slu_id
);
582 *(__be64
*)&req
->__asiv
[96] = cpu_to_be64(load
->app_id
);
583 req
->asiv_length
= 32; /* bytes included in crc calc */
584 } else { /* setup DDCB for ATS architecture */
585 *(__be64
*)&req
->asiv
[0] = cpu_to_be64(dma_addr
);
586 *(__be32
*)&req
->asiv
[8] = cpu_to_be32(tocopy
);
587 *(__be32
*)&req
->asiv
[12] = cpu_to_be32(0); /* resvd */
588 *(__be64
*)&req
->asiv
[16] = cpu_to_be64(flash
);
589 *(__be32
*)&req
->asiv
[24] = cpu_to_be32(load
->uid
<<24);
590 *(__be32
*)&req
->asiv
[28] = cpu_to_be32(crc
);
592 /* for simulation only */
593 *(__be64
*)&req
->asiv
[80] = cpu_to_be64(load
->slu_id
);
594 *(__be64
*)&req
->asiv
[88] = cpu_to_be64(load
->app_id
);
597 req
->ats
= 0x4ULL
<< 44;
598 req
->asiv_length
= 40; /* bytes included in crc calc */
602 /* For Genwqe5 we get back the calculated CRC */
603 *(u64
*)&req
->asv
[0] = 0ULL; /* 0x80 */
605 rc
= __genwqe_execute_raw_ddcb(cd
, req
, filp
->f_flags
);
607 load
->retc
= req
->retc
;
608 load
->attn
= req
->attn
;
609 load
->progress
= req
->progress
;
616 if (req
->retc
!= DDCB_RETC_COMPLETE
) {
622 load
->size
-= tocopy
;
630 __genwqe_free_consistent(cd
, FLASH_BLOCK
, xbuf
, dma_addr
);
634 static int do_flash_read(struct genwqe_file
*cfile
,
635 struct genwqe_bitstream
*load
)
637 int rc
, blocks_to_flash
;
644 struct genwqe_dev
*cd
= cfile
->cd
;
645 struct file
*filp
= cfile
->filp
;
646 struct pci_dev
*pci_dev
= cd
->pci_dev
;
647 struct genwqe_ddcb_cmd
*cmd
;
649 if ((load
->size
& 0x3) != 0)
652 if (((unsigned long)(load
->data_addr
) & ~PAGE_MASK
) != 0)
655 /* FIXME Bits have changed for new service layer! */
656 switch ((char)load
->partition
) {
659 break; /* upload/part_0 */
662 break; /* upload/part_1 */
665 break; /* upload/vpd */
670 buf
= (u8 __user
*)load
->data_addr
;
671 xbuf
= __genwqe_alloc_consistent(cd
, FLASH_BLOCK
, &dma_addr
);
675 blocks_to_flash
= load
->size
/ FLASH_BLOCK
;
678 * We must be 4 byte aligned. Buffer must be 0 appened
679 * to have defined values when calculating CRC.
681 tocopy
= min_t(size_t, load
->size
, FLASH_BLOCK
);
683 dev_dbg(&pci_dev
->dev
,
684 "[%s] DMA: %lx SZ: %ld %d\n",
685 __func__
, (unsigned long)dma_addr
, tocopy
,
688 /* prepare DDCB for SLU process */
689 cmd
= ddcb_requ_alloc();
694 cmd
->cmd
= SLCMD_MOVE_FLASH
;
695 cmd
->cmdopts
= cmdopts
;
697 /* prepare invariant values */
698 if (genwqe_get_slu_id(cd
) <= 0x2) {
699 *(__be64
*)&cmd
->__asiv
[0] = cpu_to_be64(dma_addr
);
700 *(__be64
*)&cmd
->__asiv
[8] = cpu_to_be64(tocopy
);
701 *(__be64
*)&cmd
->__asiv
[16] = cpu_to_be64(flash
);
702 *(__be32
*)&cmd
->__asiv
[24] = cpu_to_be32(0);
703 cmd
->__asiv
[24] = load
->uid
;
704 *(__be32
*)&cmd
->__asiv
[28] = cpu_to_be32(0) /* CRC */;
705 cmd
->asiv_length
= 32; /* bytes included in crc calc */
706 } else { /* setup DDCB for ATS architecture */
707 *(__be64
*)&cmd
->asiv
[0] = cpu_to_be64(dma_addr
);
708 *(__be32
*)&cmd
->asiv
[8] = cpu_to_be32(tocopy
);
709 *(__be32
*)&cmd
->asiv
[12] = cpu_to_be32(0); /* resvd */
710 *(__be64
*)&cmd
->asiv
[16] = cpu_to_be64(flash
);
711 *(__be32
*)&cmd
->asiv
[24] = cpu_to_be32(load
->uid
<<24);
712 *(__be32
*)&cmd
->asiv
[28] = cpu_to_be32(0); /* CRC */
715 cmd
->ats
= 0x5ULL
<< 44;
716 cmd
->asiv_length
= 40; /* bytes included in crc calc */
720 /* we only get back the calculated CRC */
721 *(u64
*)&cmd
->asv
[0] = 0ULL; /* 0x80 */
723 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
725 load
->retc
= cmd
->retc
;
726 load
->attn
= cmd
->attn
;
727 load
->progress
= cmd
->progress
;
729 if ((rc
< 0) && (rc
!= -EBADMSG
)) {
734 rc
= copy_to_user(buf
, xbuf
, tocopy
);
741 /* We know that we can get retc 0x104 with CRC err */
742 if (((cmd
->retc
== DDCB_RETC_FAULT
) &&
743 (cmd
->attn
!= 0x02)) || /* Normally ignore CRC error */
744 ((cmd
->retc
== DDCB_RETC_COMPLETE
) &&
745 (cmd
->attn
!= 0x00))) { /* Everything was fine */
751 load
->size
-= tocopy
;
760 __genwqe_free_consistent(cd
, FLASH_BLOCK
, xbuf
, dma_addr
);
764 static int genwqe_pin_mem(struct genwqe_file
*cfile
, struct genwqe_mem
*m
)
767 struct genwqe_dev
*cd
= cfile
->cd
;
768 struct pci_dev
*pci_dev
= cfile
->cd
->pci_dev
;
769 struct dma_mapping
*dma_map
;
770 unsigned long map_addr
;
771 unsigned long map_size
;
773 if ((m
->addr
== 0x0) || (m
->size
== 0))
775 if (m
->size
> ULONG_MAX
- PAGE_SIZE
- (m
->addr
& ~PAGE_MASK
))
778 map_addr
= (m
->addr
& PAGE_MASK
);
779 map_size
= round_up(m
->size
+ (m
->addr
& ~PAGE_MASK
), PAGE_SIZE
);
781 dma_map
= kzalloc(sizeof(struct dma_mapping
), GFP_KERNEL
);
785 genwqe_mapping_init(dma_map
, GENWQE_MAPPING_SGL_PINNED
);
786 rc
= genwqe_user_vmap(cd
, dma_map
, (void *)map_addr
, map_size
);
788 dev_err(&pci_dev
->dev
,
789 "[%s] genwqe_user_vmap rc=%d\n", __func__
, rc
);
794 genwqe_add_pin(cfile
, dma_map
);
798 static int genwqe_unpin_mem(struct genwqe_file
*cfile
, struct genwqe_mem
*m
)
800 struct genwqe_dev
*cd
= cfile
->cd
;
801 struct dma_mapping
*dma_map
;
802 unsigned long map_addr
;
803 unsigned long map_size
;
808 map_addr
= (m
->addr
& PAGE_MASK
);
809 map_size
= round_up(m
->size
+ (m
->addr
& ~PAGE_MASK
), PAGE_SIZE
);
811 dma_map
= genwqe_search_pin(cfile
, map_addr
, map_size
, NULL
);
815 genwqe_del_pin(cfile
, dma_map
);
816 genwqe_user_vunmap(cd
, dma_map
);
822 * ddcb_cmd_cleanup() - Remove dynamically created fixup entries
824 * Only if there are any. Pinnings are not removed.
826 static int ddcb_cmd_cleanup(struct genwqe_file
*cfile
, struct ddcb_requ
*req
)
829 struct dma_mapping
*dma_map
;
830 struct genwqe_dev
*cd
= cfile
->cd
;
832 for (i
= 0; i
< DDCB_FIXUPS
; i
++) {
833 dma_map
= &req
->dma_mappings
[i
];
835 if (dma_mapping_used(dma_map
)) {
836 __genwqe_del_mapping(cfile
, dma_map
);
837 genwqe_user_vunmap(cd
, dma_map
);
839 if (req
->sgls
[i
].sgl
!= NULL
)
840 genwqe_free_sync_sgl(cd
, &req
->sgls
[i
]);
846 * ddcb_cmd_fixups() - Establish DMA fixups/sglists for user memory references
848 * Before the DDCB gets executed we need to handle the fixups. We
849 * replace the user-space addresses with DMA addresses or do
850 * additional setup work e.g. generating a scatter-gather list which
851 * is used to describe the memory referred to in the fixup.
853 static int ddcb_cmd_fixups(struct genwqe_file
*cfile
, struct ddcb_requ
*req
)
856 unsigned int asiv_offs
, i
;
857 struct genwqe_dev
*cd
= cfile
->cd
;
858 struct genwqe_ddcb_cmd
*cmd
= &req
->cmd
;
859 struct dma_mapping
*m
;
861 for (i
= 0, asiv_offs
= 0x00; asiv_offs
<= 0x58;
862 i
++, asiv_offs
+= 0x08) {
869 ats_flags
= ATS_GET_FLAGS(cmd
->ats
, asiv_offs
);
874 break; /* nothing to do here */
876 case ATS_TYPE_FLAT_RDWR
:
877 case ATS_TYPE_FLAT_RD
: {
878 u_addr
= be64_to_cpu(*((__be64
*)&cmd
->
880 u_size
= be32_to_cpu(*((__be32
*)&cmd
->
881 asiv
[asiv_offs
+ 0x08]));
884 * No data available. Ignore u_addr in this
885 * case and set addr to 0. Hardware must not
889 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
894 m
= __genwqe_search_mapping(cfile
, u_addr
, u_size
,
901 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
906 case ATS_TYPE_SGL_RDWR
:
907 case ATS_TYPE_SGL_RD
: {
910 u_addr
= be64_to_cpu(*((__be64
*)
911 &cmd
->asiv
[asiv_offs
]));
912 u_size
= be32_to_cpu(*((__be32
*)
913 &cmd
->asiv
[asiv_offs
+ 0x08]));
916 * No data available. Ignore u_addr in this
917 * case and set addr to 0. Hardware must not
918 * fetch the empty sgl.
921 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
926 m
= genwqe_search_pin(cfile
, u_addr
, u_size
, NULL
);
928 page_offs
= (u_addr
-
929 (u64
)m
->u_vaddr
)/PAGE_SIZE
;
931 m
= &req
->dma_mappings
[i
];
933 genwqe_mapping_init(m
,
934 GENWQE_MAPPING_SGL_TEMP
);
936 if (ats_flags
== ATS_TYPE_SGL_RD
)
939 rc
= genwqe_user_vmap(cd
, m
, (void *)u_addr
,
944 __genwqe_add_mapping(cfile
, m
);
948 /* create genwqe style scatter gather list */
949 rc
= genwqe_alloc_sync_sgl(cd
, &req
->sgls
[i
],
950 (void __user
*)u_addr
,
955 genwqe_setup_sgl(cd
, &req
->sgls
[i
],
956 &m
->dma_list
[page_offs
]);
958 *((__be64
*)&cmd
->asiv
[asiv_offs
]) =
959 cpu_to_be64(req
->sgls
[i
].sgl_dma_addr
);
971 ddcb_cmd_cleanup(cfile
, req
);
976 * genwqe_execute_ddcb() - Execute DDCB using userspace address fixups
978 * The code will build up the translation tables or lookup the
979 * contignous memory allocation table to find the right translations
982 static int genwqe_execute_ddcb(struct genwqe_file
*cfile
,
983 struct genwqe_ddcb_cmd
*cmd
)
986 struct genwqe_dev
*cd
= cfile
->cd
;
987 struct file
*filp
= cfile
->filp
;
988 struct ddcb_requ
*req
= container_of(cmd
, struct ddcb_requ
, cmd
);
990 rc
= ddcb_cmd_fixups(cfile
, req
);
994 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
995 ddcb_cmd_cleanup(cfile
, req
);
999 static int do_execute_ddcb(struct genwqe_file
*cfile
,
1000 unsigned long arg
, int raw
)
1003 struct genwqe_ddcb_cmd
*cmd
;
1004 struct genwqe_dev
*cd
= cfile
->cd
;
1005 struct file
*filp
= cfile
->filp
;
1007 cmd
= ddcb_requ_alloc();
1011 if (copy_from_user(cmd
, (void __user
*)arg
, sizeof(*cmd
))) {
1012 ddcb_requ_free(cmd
);
1017 rc
= genwqe_execute_ddcb(cfile
, cmd
);
1019 rc
= __genwqe_execute_raw_ddcb(cd
, cmd
, filp
->f_flags
);
1021 /* Copy back only the modifed fields. Do not copy ASIV
1022 back since the copy got modified by the driver. */
1023 if (copy_to_user((void __user
*)arg
, cmd
,
1024 sizeof(*cmd
) - DDCB_ASIV_LENGTH
)) {
1025 ddcb_requ_free(cmd
);
1029 ddcb_requ_free(cmd
);
1034 * genwqe_ioctl() - IO control
1035 * @filp: file handle
1036 * @cmd: command identifier (passed from user)
1037 * @arg: argument (passed from user)
1041 static long genwqe_ioctl(struct file
*filp
, unsigned int cmd
,
1045 struct genwqe_file
*cfile
= (struct genwqe_file
*)filp
->private_data
;
1046 struct genwqe_dev
*cd
= cfile
->cd
;
1047 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1048 struct genwqe_reg_io __user
*io
;
1052 /* Return -EIO if card hit EEH */
1053 if (pci_channel_offline(pci_dev
))
1056 if (_IOC_TYPE(cmd
) != GENWQE_IOC_CODE
)
1061 case GENWQE_GET_CARD_STATE
:
1062 put_user(cd
->card_state
, (enum genwqe_card_state __user
*)arg
);
1065 /* Register access */
1066 case GENWQE_READ_REG64
: {
1067 io
= (struct genwqe_reg_io __user
*)arg
;
1069 if (get_user(reg_offs
, &io
->num
))
1072 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x7))
1075 val
= __genwqe_readq(cd
, reg_offs
);
1076 put_user(val
, &io
->val64
);
1080 case GENWQE_WRITE_REG64
: {
1081 io
= (struct genwqe_reg_io __user
*)arg
;
1083 if (!capable(CAP_SYS_ADMIN
))
1086 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1089 if (get_user(reg_offs
, &io
->num
))
1092 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x7))
1095 if (get_user(val
, &io
->val64
))
1098 __genwqe_writeq(cd
, reg_offs
, val
);
1102 case GENWQE_READ_REG32
: {
1103 io
= (struct genwqe_reg_io __user
*)arg
;
1105 if (get_user(reg_offs
, &io
->num
))
1108 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x3))
1111 val
= __genwqe_readl(cd
, reg_offs
);
1112 put_user(val
, &io
->val64
);
1116 case GENWQE_WRITE_REG32
: {
1117 io
= (struct genwqe_reg_io __user
*)arg
;
1119 if (!capable(CAP_SYS_ADMIN
))
1122 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1125 if (get_user(reg_offs
, &io
->num
))
1128 if ((reg_offs
>= cd
->mmio_len
) || (reg_offs
& 0x3))
1131 if (get_user(val
, &io
->val64
))
1134 __genwqe_writel(cd
, reg_offs
, val
);
1138 /* Flash update/reading */
1139 case GENWQE_SLU_UPDATE
: {
1140 struct genwqe_bitstream load
;
1142 if (!genwqe_is_privileged(cd
))
1145 if ((filp
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1148 if (copy_from_user(&load
, (void __user
*)arg
,
1152 rc
= do_flash_update(cfile
, &load
);
1154 if (copy_to_user((void __user
*)arg
, &load
, sizeof(load
)))
1160 case GENWQE_SLU_READ
: {
1161 struct genwqe_bitstream load
;
1163 if (!genwqe_is_privileged(cd
))
1166 if (genwqe_flash_readback_fails(cd
))
1167 return -ENOSPC
; /* known to fail for old versions */
1169 if (copy_from_user(&load
, (void __user
*)arg
, sizeof(load
)))
1172 rc
= do_flash_read(cfile
, &load
);
1174 if (copy_to_user((void __user
*)arg
, &load
, sizeof(load
)))
1180 /* memory pinning and unpinning */
1181 case GENWQE_PIN_MEM
: {
1182 struct genwqe_mem m
;
1184 if (copy_from_user(&m
, (void __user
*)arg
, sizeof(m
)))
1187 return genwqe_pin_mem(cfile
, &m
);
1190 case GENWQE_UNPIN_MEM
: {
1191 struct genwqe_mem m
;
1193 if (copy_from_user(&m
, (void __user
*)arg
, sizeof(m
)))
1196 return genwqe_unpin_mem(cfile
, &m
);
1199 /* launch an DDCB and wait for completion */
1200 case GENWQE_EXECUTE_DDCB
:
1201 return do_execute_ddcb(cfile
, arg
, 0);
1203 case GENWQE_EXECUTE_RAW_DDCB
: {
1205 if (!capable(CAP_SYS_ADMIN
))
1208 return do_execute_ddcb(cfile
, arg
, 1);
1218 static const struct file_operations genwqe_fops
= {
1219 .owner
= THIS_MODULE
,
1220 .open
= genwqe_open
,
1221 .fasync
= genwqe_fasync
,
1222 .mmap
= genwqe_mmap
,
1223 .unlocked_ioctl
= genwqe_ioctl
,
1224 .compat_ioctl
= compat_ptr_ioctl
,
1225 .release
= genwqe_release
,
1228 static int genwqe_device_initialized(struct genwqe_dev
*cd
)
1230 return cd
->dev
!= NULL
;
1234 * genwqe_device_create() - Create and configure genwqe char device
1235 * @cd: genwqe device descriptor
1237 * This function must be called before we create any more genwqe
1238 * character devices, because it is allocating the major and minor
1239 * number which are supposed to be used by the client drivers.
1241 int genwqe_device_create(struct genwqe_dev
*cd
)
1244 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1247 * Here starts the individual setup per client. It must
1248 * initialize its own cdev data structure with its own fops.
1249 * The appropriate devnum needs to be created. The ranges must
1252 rc
= alloc_chrdev_region(&cd
->devnum_genwqe
, 0,
1253 GENWQE_MAX_MINOR
, GENWQE_DEVNAME
);
1255 dev_err(&pci_dev
->dev
, "err: alloc_chrdev_region failed\n");
1259 cdev_init(&cd
->cdev_genwqe
, &genwqe_fops
);
1260 cd
->cdev_genwqe
.owner
= THIS_MODULE
;
1262 rc
= cdev_add(&cd
->cdev_genwqe
, cd
->devnum_genwqe
, 1);
1264 dev_err(&pci_dev
->dev
, "err: cdev_add failed\n");
1269 * Finally the device in /dev/... must be created. The rule is
1270 * to use card%d_clientname for each created device.
1272 cd
->dev
= device_create_with_groups(cd
->class_genwqe
,
1274 cd
->devnum_genwqe
, cd
,
1275 genwqe_attribute_groups
,
1276 GENWQE_DEVNAME
"%u_card",
1278 if (IS_ERR(cd
->dev
)) {
1279 rc
= PTR_ERR(cd
->dev
);
1283 genwqe_init_debugfs(cd
);
1288 cdev_del(&cd
->cdev_genwqe
);
1290 unregister_chrdev_region(cd
->devnum_genwqe
, GENWQE_MAX_MINOR
);
1296 static int genwqe_inform_and_stop_processes(struct genwqe_dev
*cd
)
1300 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1302 if (!genwqe_open_files(cd
))
1305 dev_warn(&pci_dev
->dev
, "[%s] send SIGIO and wait ...\n", __func__
);
1307 rc
= genwqe_kill_fasync(cd
, SIGIO
);
1309 /* give kill_timeout seconds to close file descriptors ... */
1310 for (i
= 0; (i
< GENWQE_KILL_TIMEOUT
) &&
1311 genwqe_open_files(cd
); i
++) {
1312 dev_info(&pci_dev
->dev
, " %d sec ...", i
);
1318 /* if no open files we can safely continue, else ... */
1319 if (!genwqe_open_files(cd
))
1322 dev_warn(&pci_dev
->dev
,
1323 "[%s] send SIGKILL and wait ...\n", __func__
);
1325 rc
= genwqe_terminate(cd
);
1327 /* Give kill_timout more seconds to end processes */
1328 for (i
= 0; (i
< GENWQE_KILL_TIMEOUT
) &&
1329 genwqe_open_files(cd
); i
++) {
1330 dev_warn(&pci_dev
->dev
, " %d sec ...", i
);
1341 * genwqe_device_remove() - Remove genwqe's char device
1343 * This function must be called after the client devices are removed
1344 * because it will free the major/minor number range for the genwqe
1347 * This function must be robust enough to be called twice.
1349 int genwqe_device_remove(struct genwqe_dev
*cd
)
1352 struct pci_dev
*pci_dev
= cd
->pci_dev
;
1354 if (!genwqe_device_initialized(cd
))
1357 genwqe_inform_and_stop_processes(cd
);
1360 * We currently do wait until all filedescriptors are
1361 * closed. This leads to a problem when we abort the
1362 * application which will decrease this reference from
1363 * 1/unused to 0/illegal and not from 2/used 1/empty.
1365 rc
= kref_read(&cd
->cdev_genwqe
.kobj
.kref
);
1367 dev_err(&pci_dev
->dev
,
1368 "[%s] err: cdev_genwqe...refcount=%d\n", __func__
, rc
);
1369 panic("Fatal err: cannot free resources with pending references!");
1372 genqwe_exit_debugfs(cd
);
1373 device_destroy(cd
->class_genwqe
, cd
->devnum_genwqe
);
1374 cdev_del(&cd
->cdev_genwqe
);
1375 unregister_chrdev_region(cd
->devnum_genwqe
, GENWQE_MAX_MINOR
);