Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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