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