xref: /openbmc/linux/drivers/misc/genwqe/card_utils.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1eb3ae0aaSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2364b40c0SLee Jones /*
367f4addbSFrank Haverkamp  * IBM Accelerator Family 'GenWQE'
467f4addbSFrank Haverkamp  *
567f4addbSFrank Haverkamp  * (C) Copyright IBM Corp. 2013
667f4addbSFrank Haverkamp  *
767f4addbSFrank Haverkamp  * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
867f4addbSFrank Haverkamp  * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
926d8f6f1SFrank Haverkamp  * Author: Michael Jung <mijung@gmx.net>
1067f4addbSFrank Haverkamp  * Author: Michael Ruettger <michael@ibmra.de>
1167f4addbSFrank Haverkamp  */
1267f4addbSFrank Haverkamp 
1367f4addbSFrank Haverkamp /*
1467f4addbSFrank Haverkamp  * Miscelanous functionality used in the other GenWQE driver parts.
1567f4addbSFrank Haverkamp  */
1667f4addbSFrank Haverkamp 
1767f4addbSFrank Haverkamp #include <linux/kernel.h>
1867f4addbSFrank Haverkamp #include <linux/sched.h>
1967f4addbSFrank Haverkamp #include <linux/vmalloc.h>
2067f4addbSFrank Haverkamp #include <linux/page-flags.h>
2167f4addbSFrank Haverkamp #include <linux/scatterlist.h>
2267f4addbSFrank Haverkamp #include <linux/hugetlb.h>
2367f4addbSFrank Haverkamp #include <linux/iommu.h>
2467f4addbSFrank Haverkamp #include <linux/pci.h>
2567f4addbSFrank Haverkamp #include <linux/dma-mapping.h>
2667f4addbSFrank Haverkamp #include <linux/ctype.h>
2767f4addbSFrank Haverkamp #include <linux/module.h>
2867f4addbSFrank Haverkamp #include <linux/platform_device.h>
2967f4addbSFrank Haverkamp #include <linux/delay.h>
30ca5999fdSMike Rapoport #include <linux/pgtable.h>
3167f4addbSFrank Haverkamp 
3267f4addbSFrank Haverkamp #include "genwqe_driver.h"
3367f4addbSFrank Haverkamp #include "card_base.h"
3467f4addbSFrank Haverkamp #include "card_ddcb.h"
3567f4addbSFrank Haverkamp 
3667f4addbSFrank Haverkamp /**
3767f4addbSFrank Haverkamp  * __genwqe_writeq() - Write 64-bit register
3867f4addbSFrank Haverkamp  * @cd:	        genwqe device descriptor
3967f4addbSFrank Haverkamp  * @byte_offs:  byte offset within BAR
4067f4addbSFrank Haverkamp  * @val:        64-bit value
4167f4addbSFrank Haverkamp  *
4267f4addbSFrank Haverkamp  * Return: 0 if success; < 0 if error
4367f4addbSFrank Haverkamp  */
__genwqe_writeq(struct genwqe_dev * cd,u64 byte_offs,u64 val)4467f4addbSFrank Haverkamp int __genwqe_writeq(struct genwqe_dev *cd, u64 byte_offs, u64 val)
4567f4addbSFrank Haverkamp {
46fb145456SKleber Sacilotto de Souza 	struct pci_dev *pci_dev = cd->pci_dev;
47fb145456SKleber Sacilotto de Souza 
4867f4addbSFrank Haverkamp 	if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
4967f4addbSFrank Haverkamp 		return -EIO;
5067f4addbSFrank Haverkamp 
5167f4addbSFrank Haverkamp 	if (cd->mmio == NULL)
5267f4addbSFrank Haverkamp 		return -EIO;
5367f4addbSFrank Haverkamp 
54fb145456SKleber Sacilotto de Souza 	if (pci_channel_offline(pci_dev))
55fb145456SKleber Sacilotto de Souza 		return -EIO;
56fb145456SKleber Sacilotto de Souza 
57a45a0258SFrank Haverkamp 	__raw_writeq((__force u64)cpu_to_be64(val), cd->mmio + byte_offs);
5867f4addbSFrank Haverkamp 	return 0;
5967f4addbSFrank Haverkamp }
6067f4addbSFrank Haverkamp 
6167f4addbSFrank Haverkamp /**
6267f4addbSFrank Haverkamp  * __genwqe_readq() - Read 64-bit register
6367f4addbSFrank Haverkamp  * @cd:         genwqe device descriptor
6467f4addbSFrank Haverkamp  * @byte_offs:  offset within BAR
6567f4addbSFrank Haverkamp  *
6667f4addbSFrank Haverkamp  * Return: value from register
6767f4addbSFrank Haverkamp  */
__genwqe_readq(struct genwqe_dev * cd,u64 byte_offs)6867f4addbSFrank Haverkamp u64 __genwqe_readq(struct genwqe_dev *cd, u64 byte_offs)
6967f4addbSFrank Haverkamp {
7067f4addbSFrank Haverkamp 	if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
7167f4addbSFrank Haverkamp 		return 0xffffffffffffffffull;
7267f4addbSFrank Haverkamp 
7367f4addbSFrank Haverkamp 	if ((cd->err_inject & GENWQE_INJECT_GFIR_FATAL) &&
7467f4addbSFrank Haverkamp 	    (byte_offs == IO_SLC_CFGREG_GFIR))
7567f4addbSFrank Haverkamp 		return 0x000000000000ffffull;
7667f4addbSFrank Haverkamp 
7767f4addbSFrank Haverkamp 	if ((cd->err_inject & GENWQE_INJECT_GFIR_INFO) &&
7867f4addbSFrank Haverkamp 	    (byte_offs == IO_SLC_CFGREG_GFIR))
7967f4addbSFrank Haverkamp 		return 0x00000000ffff0000ull;
8067f4addbSFrank Haverkamp 
8167f4addbSFrank Haverkamp 	if (cd->mmio == NULL)
8267f4addbSFrank Haverkamp 		return 0xffffffffffffffffull;
8367f4addbSFrank Haverkamp 
8458d66ce7SFrank Haverkamp 	return be64_to_cpu((__force __be64)__raw_readq(cd->mmio + byte_offs));
8567f4addbSFrank Haverkamp }
8667f4addbSFrank Haverkamp 
8767f4addbSFrank Haverkamp /**
8867f4addbSFrank Haverkamp  * __genwqe_writel() - Write 32-bit register
8967f4addbSFrank Haverkamp  * @cd:	        genwqe device descriptor
9067f4addbSFrank Haverkamp  * @byte_offs:  byte offset within BAR
9167f4addbSFrank Haverkamp  * @val:        32-bit value
9267f4addbSFrank Haverkamp  *
9367f4addbSFrank Haverkamp  * Return: 0 if success; < 0 if error
9467f4addbSFrank Haverkamp  */
__genwqe_writel(struct genwqe_dev * cd,u64 byte_offs,u32 val)9567f4addbSFrank Haverkamp int __genwqe_writel(struct genwqe_dev *cd, u64 byte_offs, u32 val)
9667f4addbSFrank Haverkamp {
97fb145456SKleber Sacilotto de Souza 	struct pci_dev *pci_dev = cd->pci_dev;
98fb145456SKleber Sacilotto de Souza 
9967f4addbSFrank Haverkamp 	if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
10067f4addbSFrank Haverkamp 		return -EIO;
10167f4addbSFrank Haverkamp 
10267f4addbSFrank Haverkamp 	if (cd->mmio == NULL)
10367f4addbSFrank Haverkamp 		return -EIO;
10467f4addbSFrank Haverkamp 
105fb145456SKleber Sacilotto de Souza 	if (pci_channel_offline(pci_dev))
106fb145456SKleber Sacilotto de Souza 		return -EIO;
107fb145456SKleber Sacilotto de Souza 
10858d66ce7SFrank Haverkamp 	__raw_writel((__force u32)cpu_to_be32(val), cd->mmio + byte_offs);
10967f4addbSFrank Haverkamp 	return 0;
11067f4addbSFrank Haverkamp }
11167f4addbSFrank Haverkamp 
11267f4addbSFrank Haverkamp /**
11367f4addbSFrank Haverkamp  * __genwqe_readl() - Read 32-bit register
11467f4addbSFrank Haverkamp  * @cd:         genwqe device descriptor
11567f4addbSFrank Haverkamp  * @byte_offs:  offset within BAR
11667f4addbSFrank Haverkamp  *
11767f4addbSFrank Haverkamp  * Return: Value from register
11867f4addbSFrank Haverkamp  */
__genwqe_readl(struct genwqe_dev * cd,u64 byte_offs)11967f4addbSFrank Haverkamp u32 __genwqe_readl(struct genwqe_dev *cd, u64 byte_offs)
12067f4addbSFrank Haverkamp {
12167f4addbSFrank Haverkamp 	if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
12267f4addbSFrank Haverkamp 		return 0xffffffff;
12367f4addbSFrank Haverkamp 
12467f4addbSFrank Haverkamp 	if (cd->mmio == NULL)
12567f4addbSFrank Haverkamp 		return 0xffffffff;
12667f4addbSFrank Haverkamp 
12758d66ce7SFrank Haverkamp 	return be32_to_cpu((__force __be32)__raw_readl(cd->mmio + byte_offs));
12867f4addbSFrank Haverkamp }
12967f4addbSFrank Haverkamp 
13067f4addbSFrank Haverkamp /**
13167f4addbSFrank Haverkamp  * genwqe_read_app_id() - Extract app_id
132364b40c0SLee Jones  * @cd:	        genwqe device descriptor
133364b40c0SLee Jones  * @app_name:   carrier used to pass-back name
134364b40c0SLee Jones  * @len:        length of data for name
13567f4addbSFrank Haverkamp  *
13667f4addbSFrank Haverkamp  * app_unitcfg need to be filled with valid data first
13767f4addbSFrank Haverkamp  */
genwqe_read_app_id(struct genwqe_dev * cd,char * app_name,int len)13867f4addbSFrank Haverkamp int genwqe_read_app_id(struct genwqe_dev *cd, char *app_name, int len)
13967f4addbSFrank Haverkamp {
14067f4addbSFrank Haverkamp 	int i, j;
14167f4addbSFrank Haverkamp 	u32 app_id = (u32)cd->app_unitcfg;
14267f4addbSFrank Haverkamp 
14367f4addbSFrank Haverkamp 	memset(app_name, 0, len);
14467f4addbSFrank Haverkamp 	for (i = 0, j = 0; j < min(len, 4); j++) {
14567f4addbSFrank Haverkamp 		char ch = (char)((app_id >> (24 - j*8)) & 0xff);
146d9c11d45SFrank Haverkamp 
14767f4addbSFrank Haverkamp 		if (ch == ' ')
14867f4addbSFrank Haverkamp 			continue;
14967f4addbSFrank Haverkamp 		app_name[i++] = isprint(ch) ? ch : 'X';
15067f4addbSFrank Haverkamp 	}
15167f4addbSFrank Haverkamp 	return i;
15267f4addbSFrank Haverkamp }
15367f4addbSFrank Haverkamp 
1540e656b80SRandy Dunlap #define CRC32_POLYNOMIAL	0x20044009
1550e656b80SRandy Dunlap static u32 crc32_tab[256];	/* crc32 lookup table */
1560e656b80SRandy Dunlap 
15767f4addbSFrank Haverkamp /**
15867f4addbSFrank Haverkamp  * genwqe_init_crc32() - Prepare a lookup table for fast crc32 calculations
15967f4addbSFrank Haverkamp  *
16067f4addbSFrank Haverkamp  * Existing kernel functions seem to use a different polynom,
16167f4addbSFrank Haverkamp  * therefore we could not use them here.
16267f4addbSFrank Haverkamp  *
16367f4addbSFrank Haverkamp  * Genwqe's Polynomial = 0x20044009
16467f4addbSFrank Haverkamp  */
genwqe_init_crc32(void)16567f4addbSFrank Haverkamp void genwqe_init_crc32(void)
16667f4addbSFrank Haverkamp {
16767f4addbSFrank Haverkamp 	int i, j;
16867f4addbSFrank Haverkamp 	u32 crc;
16967f4addbSFrank Haverkamp 
17067f4addbSFrank Haverkamp 	for (i = 0;  i < 256;  i++) {
17167f4addbSFrank Haverkamp 		crc = i << 24;
17267f4addbSFrank Haverkamp 		for (j = 0;  j < 8;  j++) {
17367f4addbSFrank Haverkamp 			if (crc & 0x80000000)
17467f4addbSFrank Haverkamp 				crc = (crc << 1) ^ CRC32_POLYNOMIAL;
17567f4addbSFrank Haverkamp 			else
17667f4addbSFrank Haverkamp 				crc = (crc << 1);
17767f4addbSFrank Haverkamp 		}
17867f4addbSFrank Haverkamp 		crc32_tab[i] = crc;
17967f4addbSFrank Haverkamp 	}
18067f4addbSFrank Haverkamp }
18167f4addbSFrank Haverkamp 
18267f4addbSFrank Haverkamp /**
18367f4addbSFrank Haverkamp  * genwqe_crc32() - Generate 32-bit crc as required for DDCBs
18467f4addbSFrank Haverkamp  * @buff:       pointer to data buffer
18567f4addbSFrank Haverkamp  * @len:        length of data for calculation
18667f4addbSFrank Haverkamp  * @init:       initial crc (0xffffffff at start)
18767f4addbSFrank Haverkamp  *
18867f4addbSFrank Haverkamp  * polynomial = x^32 * + x^29 + x^18 + x^14 + x^3 + 1 (0x20044009)
189364b40c0SLee Jones  *
19067f4addbSFrank Haverkamp  * Example: 4 bytes 0x01 0x02 0x03 0x04 with init=0xffffffff should
19167f4addbSFrank Haverkamp  * result in a crc32 of 0xf33cb7d3.
19267f4addbSFrank Haverkamp  *
19367f4addbSFrank Haverkamp  * The existing kernel crc functions did not cover this polynom yet.
19467f4addbSFrank Haverkamp  *
19567f4addbSFrank Haverkamp  * Return: crc32 checksum.
19667f4addbSFrank Haverkamp  */
genwqe_crc32(u8 * buff,size_t len,u32 init)19767f4addbSFrank Haverkamp u32 genwqe_crc32(u8 *buff, size_t len, u32 init)
19867f4addbSFrank Haverkamp {
19967f4addbSFrank Haverkamp 	int i;
20067f4addbSFrank Haverkamp 	u32 crc;
20167f4addbSFrank Haverkamp 
20267f4addbSFrank Haverkamp 	crc = init;
20367f4addbSFrank Haverkamp 	while (len--) {
20467f4addbSFrank Haverkamp 		i = ((crc >> 24) ^ *buff++) & 0xFF;
20567f4addbSFrank Haverkamp 		crc = (crc << 8) ^ crc32_tab[i];
20667f4addbSFrank Haverkamp 	}
20767f4addbSFrank Haverkamp 	return crc;
20867f4addbSFrank Haverkamp }
20967f4addbSFrank Haverkamp 
__genwqe_alloc_consistent(struct genwqe_dev * cd,size_t size,dma_addr_t * dma_handle)21067f4addbSFrank Haverkamp void *__genwqe_alloc_consistent(struct genwqe_dev *cd, size_t size,
21167f4addbSFrank Haverkamp 			       dma_addr_t *dma_handle)
21267f4addbSFrank Haverkamp {
213*23baf831SKirill A. Shutemov 	if (get_order(size) > MAX_ORDER)
21467f4addbSFrank Haverkamp 		return NULL;
21567f4addbSFrank Haverkamp 
216750afb08SLuis Chamberlain 	return dma_alloc_coherent(&cd->pci_dev->dev, size, dma_handle,
21719f7767eSSebastian Ott 				  GFP_KERNEL);
21867f4addbSFrank Haverkamp }
21967f4addbSFrank Haverkamp 
__genwqe_free_consistent(struct genwqe_dev * cd,size_t size,void * vaddr,dma_addr_t dma_handle)22067f4addbSFrank Haverkamp void __genwqe_free_consistent(struct genwqe_dev *cd, size_t size,
22167f4addbSFrank Haverkamp 			     void *vaddr, dma_addr_t dma_handle)
22267f4addbSFrank Haverkamp {
22367f4addbSFrank Haverkamp 	if (vaddr == NULL)
22467f4addbSFrank Haverkamp 		return;
22567f4addbSFrank Haverkamp 
22619f7767eSSebastian Ott 	dma_free_coherent(&cd->pci_dev->dev, size, vaddr, dma_handle);
22767f4addbSFrank Haverkamp }
22867f4addbSFrank Haverkamp 
genwqe_unmap_pages(struct genwqe_dev * cd,dma_addr_t * dma_list,int num_pages)22967f4addbSFrank Haverkamp static void genwqe_unmap_pages(struct genwqe_dev *cd, dma_addr_t *dma_list,
23067f4addbSFrank Haverkamp 			      int num_pages)
23167f4addbSFrank Haverkamp {
23267f4addbSFrank Haverkamp 	int i;
23367f4addbSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
23467f4addbSFrank Haverkamp 
23567f4addbSFrank Haverkamp 	for (i = 0; (i < num_pages) && (dma_list[i] != 0x0); i++) {
2365e87622cSChristophe JAILLET 		dma_unmap_page(&pci_dev->dev, dma_list[i], PAGE_SIZE,
2375e87622cSChristophe JAILLET 			       DMA_BIDIRECTIONAL);
23867f4addbSFrank Haverkamp 		dma_list[i] = 0x0;
23967f4addbSFrank Haverkamp 	}
24067f4addbSFrank Haverkamp }
24167f4addbSFrank Haverkamp 
genwqe_map_pages(struct genwqe_dev * cd,struct page ** page_list,int num_pages,dma_addr_t * dma_list)24267f4addbSFrank Haverkamp static int genwqe_map_pages(struct genwqe_dev *cd,
24367f4addbSFrank Haverkamp 			   struct page **page_list, int num_pages,
24467f4addbSFrank Haverkamp 			   dma_addr_t *dma_list)
24567f4addbSFrank Haverkamp {
24667f4addbSFrank Haverkamp 	int i;
24767f4addbSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
24867f4addbSFrank Haverkamp 
24967f4addbSFrank Haverkamp 	/* establish DMA mapping for requested pages */
25067f4addbSFrank Haverkamp 	for (i = 0; i < num_pages; i++) {
25167f4addbSFrank Haverkamp 		dma_addr_t daddr;
25267f4addbSFrank Haverkamp 
25367f4addbSFrank Haverkamp 		dma_list[i] = 0x0;
2545e87622cSChristophe JAILLET 		daddr = dma_map_page(&pci_dev->dev, page_list[i],
25567f4addbSFrank Haverkamp 				     0,	 /* map_offs */
25667f4addbSFrank Haverkamp 				     PAGE_SIZE,
2575e87622cSChristophe JAILLET 				     DMA_BIDIRECTIONAL);  /* FIXME rd/rw */
25867f4addbSFrank Haverkamp 
2595e87622cSChristophe JAILLET 		if (dma_mapping_error(&pci_dev->dev, daddr)) {
26067f4addbSFrank Haverkamp 			dev_err(&pci_dev->dev,
26167f4addbSFrank Haverkamp 				"[%s] err: no dma addr daddr=%016llx!\n",
26267f4addbSFrank Haverkamp 				__func__, (long long)daddr);
26367f4addbSFrank Haverkamp 			goto err;
26467f4addbSFrank Haverkamp 		}
26567f4addbSFrank Haverkamp 
26667f4addbSFrank Haverkamp 		dma_list[i] = daddr;
26767f4addbSFrank Haverkamp 	}
26867f4addbSFrank Haverkamp 	return 0;
26967f4addbSFrank Haverkamp 
27067f4addbSFrank Haverkamp  err:
27167f4addbSFrank Haverkamp 	genwqe_unmap_pages(cd, dma_list, num_pages);
27267f4addbSFrank Haverkamp 	return -EIO;
27367f4addbSFrank Haverkamp }
27467f4addbSFrank Haverkamp 
genwqe_sgl_size(int num_pages)27567f4addbSFrank Haverkamp static int genwqe_sgl_size(int num_pages)
27667f4addbSFrank Haverkamp {
27767f4addbSFrank Haverkamp 	int len, num_tlb = num_pages / 7;
27867f4addbSFrank Haverkamp 
27967f4addbSFrank Haverkamp 	len = sizeof(struct sg_entry) * (num_pages+num_tlb + 1);
28067f4addbSFrank Haverkamp 	return roundup(len, PAGE_SIZE);
28167f4addbSFrank Haverkamp }
28267f4addbSFrank Haverkamp 
283364b40c0SLee Jones /*
284718f762eSFrank Haverkamp  * genwqe_alloc_sync_sgl() - Allocate memory for sgl and overlapping pages
285718f762eSFrank Haverkamp  *
286718f762eSFrank Haverkamp  * Allocates memory for sgl and overlapping pages. Pages which might
287718f762eSFrank Haverkamp  * overlap other user-space memory blocks are being cached for DMAs,
288718f762eSFrank Haverkamp  * such that we do not run into syncronization issues. Data is copied
289718f762eSFrank Haverkamp  * from user-space into the cached pages.
290718f762eSFrank Haverkamp  */
genwqe_alloc_sync_sgl(struct genwqe_dev * cd,struct genwqe_sgl * sgl,void __user * user_addr,size_t user_size,int write)291718f762eSFrank Haverkamp int genwqe_alloc_sync_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl,
292de4ce2d1SGuilherme G. Piccoli 			  void __user *user_addr, size_t user_size, int write)
29367f4addbSFrank Haverkamp {
29402241995Szhong jiang 	int ret = -ENOMEM;
29567f4addbSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
29667f4addbSFrank Haverkamp 
297718f762eSFrank Haverkamp 	sgl->fpage_offs = offset_in_page((unsigned long)user_addr);
298718f762eSFrank Haverkamp 	sgl->fpage_size = min_t(size_t, PAGE_SIZE-sgl->fpage_offs, user_size);
299718f762eSFrank Haverkamp 	sgl->nr_pages = DIV_ROUND_UP(sgl->fpage_offs + user_size, PAGE_SIZE);
300718f762eSFrank Haverkamp 	sgl->lpage_size = (user_size - sgl->fpage_size) % PAGE_SIZE;
301718f762eSFrank Haverkamp 
302d9c11d45SFrank Haverkamp 	dev_dbg(&pci_dev->dev, "[%s] uaddr=%p usize=%8ld nr_pages=%ld fpage_offs=%lx fpage_size=%ld lpage_size=%ld\n",
303718f762eSFrank Haverkamp 		__func__, user_addr, user_size, sgl->nr_pages,
304718f762eSFrank Haverkamp 		sgl->fpage_offs, sgl->fpage_size, sgl->lpage_size);
305718f762eSFrank Haverkamp 
306718f762eSFrank Haverkamp 	sgl->user_addr = user_addr;
307718f762eSFrank Haverkamp 	sgl->user_size = user_size;
308de4ce2d1SGuilherme G. Piccoli 	sgl->write = write;
309718f762eSFrank Haverkamp 	sgl->sgl_size = genwqe_sgl_size(sgl->nr_pages);
310718f762eSFrank Haverkamp 
311*23baf831SKirill A. Shutemov 	if (get_order(sgl->sgl_size) > MAX_ORDER) {
31267f4addbSFrank Haverkamp 		dev_err(&pci_dev->dev,
31367f4addbSFrank Haverkamp 			"[%s] err: too much memory requested!\n", __func__);
31402241995Szhong jiang 		return ret;
31567f4addbSFrank Haverkamp 	}
31667f4addbSFrank Haverkamp 
317718f762eSFrank Haverkamp 	sgl->sgl = __genwqe_alloc_consistent(cd, sgl->sgl_size,
318718f762eSFrank Haverkamp 					     &sgl->sgl_dma_addr);
319718f762eSFrank Haverkamp 	if (sgl->sgl == NULL) {
32067f4addbSFrank Haverkamp 		dev_err(&pci_dev->dev,
32167f4addbSFrank Haverkamp 			"[%s] err: no memory available!\n", __func__);
32202241995Szhong jiang 		return ret;
32367f4addbSFrank Haverkamp 	}
32467f4addbSFrank Haverkamp 
325718f762eSFrank Haverkamp 	/* Only use buffering on incomplete pages */
326718f762eSFrank Haverkamp 	if ((sgl->fpage_size != 0) && (sgl->fpage_size != PAGE_SIZE)) {
327718f762eSFrank Haverkamp 		sgl->fpage = __genwqe_alloc_consistent(cd, PAGE_SIZE,
328718f762eSFrank Haverkamp 						       &sgl->fpage_dma_addr);
329718f762eSFrank Haverkamp 		if (sgl->fpage == NULL)
330718f762eSFrank Haverkamp 			goto err_out;
331718f762eSFrank Haverkamp 
332718f762eSFrank Haverkamp 		/* Sync with user memory */
333718f762eSFrank Haverkamp 		if (copy_from_user(sgl->fpage + sgl->fpage_offs,
334718f762eSFrank Haverkamp 				   user_addr, sgl->fpage_size)) {
33502241995Szhong jiang 			ret = -EFAULT;
336718f762eSFrank Haverkamp 			goto err_out;
337718f762eSFrank Haverkamp 		}
338718f762eSFrank Haverkamp 	}
339718f762eSFrank Haverkamp 	if (sgl->lpage_size != 0) {
340718f762eSFrank Haverkamp 		sgl->lpage = __genwqe_alloc_consistent(cd, PAGE_SIZE,
341718f762eSFrank Haverkamp 						       &sgl->lpage_dma_addr);
342718f762eSFrank Haverkamp 		if (sgl->lpage == NULL)
343718f762eSFrank Haverkamp 			goto err_out1;
344718f762eSFrank Haverkamp 
345718f762eSFrank Haverkamp 		/* Sync with user memory */
346718f762eSFrank Haverkamp 		if (copy_from_user(sgl->lpage, user_addr + user_size -
347718f762eSFrank Haverkamp 				   sgl->lpage_size, sgl->lpage_size)) {
34802241995Szhong jiang 			ret = -EFAULT;
349a7a7aeefSGerald Schaefer 			goto err_out2;
350718f762eSFrank Haverkamp 		}
351718f762eSFrank Haverkamp 	}
352718f762eSFrank Haverkamp 	return 0;
353718f762eSFrank Haverkamp 
354a7a7aeefSGerald Schaefer  err_out2:
355a7a7aeefSGerald Schaefer 	__genwqe_free_consistent(cd, PAGE_SIZE, sgl->lpage,
356a7a7aeefSGerald Schaefer 				 sgl->lpage_dma_addr);
357a7a7aeefSGerald Schaefer 	sgl->lpage = NULL;
358a7a7aeefSGerald Schaefer 	sgl->lpage_dma_addr = 0;
359718f762eSFrank Haverkamp  err_out1:
360718f762eSFrank Haverkamp 	__genwqe_free_consistent(cd, PAGE_SIZE, sgl->fpage,
361718f762eSFrank Haverkamp 				 sgl->fpage_dma_addr);
362a7a7aeefSGerald Schaefer 	sgl->fpage = NULL;
363a7a7aeefSGerald Schaefer 	sgl->fpage_dma_addr = 0;
364718f762eSFrank Haverkamp  err_out:
365718f762eSFrank Haverkamp 	__genwqe_free_consistent(cd, sgl->sgl_size, sgl->sgl,
366718f762eSFrank Haverkamp 				 sgl->sgl_dma_addr);
367a7a7aeefSGerald Schaefer 	sgl->sgl = NULL;
368a7a7aeefSGerald Schaefer 	sgl->sgl_dma_addr = 0;
369a7a7aeefSGerald Schaefer 	sgl->sgl_size = 0;
37002241995Szhong jiang 
37102241995Szhong jiang 	return ret;
37267f4addbSFrank Haverkamp }
37367f4addbSFrank Haverkamp 
genwqe_setup_sgl(struct genwqe_dev * cd,struct genwqe_sgl * sgl,dma_addr_t * dma_list)374718f762eSFrank Haverkamp int genwqe_setup_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl,
375718f762eSFrank Haverkamp 		     dma_addr_t *dma_list)
37667f4addbSFrank Haverkamp {
37767f4addbSFrank Haverkamp 	int i = 0, j = 0, p;
37867f4addbSFrank Haverkamp 	unsigned long dma_offs, map_offs;
37967f4addbSFrank Haverkamp 	dma_addr_t prev_daddr = 0;
38067f4addbSFrank Haverkamp 	struct sg_entry *s, *last_s = NULL;
381718f762eSFrank Haverkamp 	size_t size = sgl->user_size;
38267f4addbSFrank Haverkamp 
38367f4addbSFrank Haverkamp 	dma_offs = 128;		/* next block if needed/dma_offset */
384718f762eSFrank Haverkamp 	map_offs = sgl->fpage_offs; /* offset in first page */
38567f4addbSFrank Haverkamp 
386718f762eSFrank Haverkamp 	s = &sgl->sgl[0];	/* first set of 8 entries */
38767f4addbSFrank Haverkamp 	p = 0;			/* page */
388718f762eSFrank Haverkamp 	while (p < sgl->nr_pages) {
38967f4addbSFrank Haverkamp 		dma_addr_t daddr;
39067f4addbSFrank Haverkamp 		unsigned int size_to_map;
39167f4addbSFrank Haverkamp 
39267f4addbSFrank Haverkamp 		/* always write the chaining entry, cleanup is done later */
39367f4addbSFrank Haverkamp 		j = 0;
394718f762eSFrank Haverkamp 		s[j].target_addr = cpu_to_be64(sgl->sgl_dma_addr + dma_offs);
39567f4addbSFrank Haverkamp 		s[j].len	 = cpu_to_be32(128);
39667f4addbSFrank Haverkamp 		s[j].flags	 = cpu_to_be32(SG_CHAINED);
39767f4addbSFrank Haverkamp 		j++;
39867f4addbSFrank Haverkamp 
39967f4addbSFrank Haverkamp 		while (j < 8) {
40067f4addbSFrank Haverkamp 			/* DMA mapping for requested page, offs, size */
40167f4addbSFrank Haverkamp 			size_to_map = min(size, PAGE_SIZE - map_offs);
402718f762eSFrank Haverkamp 
403718f762eSFrank Haverkamp 			if ((p == 0) && (sgl->fpage != NULL)) {
404718f762eSFrank Haverkamp 				daddr = sgl->fpage_dma_addr + map_offs;
405718f762eSFrank Haverkamp 
406718f762eSFrank Haverkamp 			} else if ((p == sgl->nr_pages - 1) &&
407718f762eSFrank Haverkamp 				   (sgl->lpage != NULL)) {
408718f762eSFrank Haverkamp 				daddr = sgl->lpage_dma_addr;
409718f762eSFrank Haverkamp 			} else {
410718f762eSFrank Haverkamp 				daddr = dma_list[p] + map_offs;
411718f762eSFrank Haverkamp 			}
412718f762eSFrank Haverkamp 
41367f4addbSFrank Haverkamp 			size -= size_to_map;
41467f4addbSFrank Haverkamp 			map_offs = 0;
41567f4addbSFrank Haverkamp 
41667f4addbSFrank Haverkamp 			if (prev_daddr == daddr) {
41767f4addbSFrank Haverkamp 				u32 prev_len = be32_to_cpu(last_s->len);
41867f4addbSFrank Haverkamp 
41967f4addbSFrank Haverkamp 				/* pr_info("daddr combining: "
42067f4addbSFrank Haverkamp 					"%016llx/%08x -> %016llx\n",
42167f4addbSFrank Haverkamp 					prev_daddr, prev_len, daddr); */
42267f4addbSFrank Haverkamp 
42367f4addbSFrank Haverkamp 				last_s->len = cpu_to_be32(prev_len +
42467f4addbSFrank Haverkamp 							  size_to_map);
42567f4addbSFrank Haverkamp 
42667f4addbSFrank Haverkamp 				p++; /* process next page */
427718f762eSFrank Haverkamp 				if (p == sgl->nr_pages)
42867f4addbSFrank Haverkamp 					goto fixup;  /* nothing to do */
42967f4addbSFrank Haverkamp 
43067f4addbSFrank Haverkamp 				prev_daddr = daddr + size_to_map;
43167f4addbSFrank Haverkamp 				continue;
43267f4addbSFrank Haverkamp 			}
43367f4addbSFrank Haverkamp 
43467f4addbSFrank Haverkamp 			/* start new entry */
43567f4addbSFrank Haverkamp 			s[j].target_addr = cpu_to_be64(daddr);
43667f4addbSFrank Haverkamp 			s[j].len	 = cpu_to_be32(size_to_map);
43767f4addbSFrank Haverkamp 			s[j].flags	 = cpu_to_be32(SG_DATA);
43867f4addbSFrank Haverkamp 			prev_daddr = daddr + size_to_map;
43967f4addbSFrank Haverkamp 			last_s = &s[j];
44067f4addbSFrank Haverkamp 			j++;
44167f4addbSFrank Haverkamp 
44267f4addbSFrank Haverkamp 			p++;	/* process next page */
443718f762eSFrank Haverkamp 			if (p == sgl->nr_pages)
44467f4addbSFrank Haverkamp 				goto fixup;  /* nothing to do */
44567f4addbSFrank Haverkamp 		}
44667f4addbSFrank Haverkamp 		dma_offs += 128;
44767f4addbSFrank Haverkamp 		s += 8;		/* continue 8 elements further */
44867f4addbSFrank Haverkamp 	}
44967f4addbSFrank Haverkamp  fixup:
4502f097267SMarkus Elfring 	if (j == 1) {		/* combining happened on last entry! */
45167f4addbSFrank Haverkamp 		s -= 8;		/* full shift needed on previous sgl block */
45267f4addbSFrank Haverkamp 		j =  7;		/* shift all elements */
45367f4addbSFrank Haverkamp 	}
45467f4addbSFrank Haverkamp 
45567f4addbSFrank Haverkamp 	for (i = 0; i < j; i++)	/* move elements 1 up */
45667f4addbSFrank Haverkamp 		s[i] = s[i + 1];
45767f4addbSFrank Haverkamp 
45867f4addbSFrank Haverkamp 	s[i].target_addr = cpu_to_be64(0);
45967f4addbSFrank Haverkamp 	s[i].len	 = cpu_to_be32(0);
46067f4addbSFrank Haverkamp 	s[i].flags	 = cpu_to_be32(SG_END_LIST);
46167f4addbSFrank Haverkamp 	return 0;
46267f4addbSFrank Haverkamp }
46367f4addbSFrank Haverkamp 
464718f762eSFrank Haverkamp /**
465718f762eSFrank Haverkamp  * genwqe_free_sync_sgl() - Free memory for sgl and overlapping pages
466364b40c0SLee Jones  * @cd:	        genwqe device descriptor
467364b40c0SLee Jones  * @sgl:        scatter gather list describing user-space memory
468718f762eSFrank Haverkamp  *
469718f762eSFrank Haverkamp  * After the DMA transfer has been completed we free the memory for
4702f097267SMarkus Elfring  * the sgl and the cached pages. Data is being transferred from cached
471718f762eSFrank Haverkamp  * pages into user-space buffers.
472718f762eSFrank Haverkamp  */
genwqe_free_sync_sgl(struct genwqe_dev * cd,struct genwqe_sgl * sgl)473718f762eSFrank Haverkamp int genwqe_free_sync_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl)
47467f4addbSFrank Haverkamp {
47563fa80cdSChristian Engelmayer 	int rc = 0;
476de4ce2d1SGuilherme G. Piccoli 	size_t offset;
477de4ce2d1SGuilherme G. Piccoli 	unsigned long res;
478718f762eSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
479718f762eSFrank Haverkamp 
480718f762eSFrank Haverkamp 	if (sgl->fpage) {
481de4ce2d1SGuilherme G. Piccoli 		if (sgl->write) {
482de4ce2d1SGuilherme G. Piccoli 			res = copy_to_user(sgl->user_addr,
483de4ce2d1SGuilherme G. Piccoli 				sgl->fpage + sgl->fpage_offs, sgl->fpage_size);
484de4ce2d1SGuilherme G. Piccoli 			if (res) {
485de4ce2d1SGuilherme G. Piccoli 				dev_err(&pci_dev->dev,
486de4ce2d1SGuilherme G. Piccoli 					"[%s] err: copying fpage! (res=%lu)\n",
487de4ce2d1SGuilherme G. Piccoli 					__func__, res);
488718f762eSFrank Haverkamp 				rc = -EFAULT;
489718f762eSFrank Haverkamp 			}
490de4ce2d1SGuilherme G. Piccoli 		}
491718f762eSFrank Haverkamp 		__genwqe_free_consistent(cd, PAGE_SIZE, sgl->fpage,
492718f762eSFrank Haverkamp 					 sgl->fpage_dma_addr);
493718f762eSFrank Haverkamp 		sgl->fpage = NULL;
494718f762eSFrank Haverkamp 		sgl->fpage_dma_addr = 0;
495718f762eSFrank Haverkamp 	}
496718f762eSFrank Haverkamp 	if (sgl->lpage) {
497de4ce2d1SGuilherme G. Piccoli 		if (sgl->write) {
498de4ce2d1SGuilherme G. Piccoli 			offset = sgl->user_size - sgl->lpage_size;
499de4ce2d1SGuilherme G. Piccoli 			res = copy_to_user(sgl->user_addr + offset, sgl->lpage,
500de4ce2d1SGuilherme G. Piccoli 					   sgl->lpage_size);
501de4ce2d1SGuilherme G. Piccoli 			if (res) {
502de4ce2d1SGuilherme G. Piccoli 				dev_err(&pci_dev->dev,
503de4ce2d1SGuilherme G. Piccoli 					"[%s] err: copying lpage! (res=%lu)\n",
504de4ce2d1SGuilherme G. Piccoli 					__func__, res);
505718f762eSFrank Haverkamp 				rc = -EFAULT;
506718f762eSFrank Haverkamp 			}
507de4ce2d1SGuilherme G. Piccoli 		}
508718f762eSFrank Haverkamp 		__genwqe_free_consistent(cd, PAGE_SIZE, sgl->lpage,
509718f762eSFrank Haverkamp 					 sgl->lpage_dma_addr);
510718f762eSFrank Haverkamp 		sgl->lpage = NULL;
511718f762eSFrank Haverkamp 		sgl->lpage_dma_addr = 0;
512718f762eSFrank Haverkamp 	}
513718f762eSFrank Haverkamp 	__genwqe_free_consistent(cd, sgl->sgl_size, sgl->sgl,
514718f762eSFrank Haverkamp 				 sgl->sgl_dma_addr);
515718f762eSFrank Haverkamp 
516718f762eSFrank Haverkamp 	sgl->sgl = NULL;
517718f762eSFrank Haverkamp 	sgl->sgl_dma_addr = 0x0;
518718f762eSFrank Haverkamp 	sgl->sgl_size = 0;
519718f762eSFrank Haverkamp 	return rc;
52067f4addbSFrank Haverkamp }
52167f4addbSFrank Haverkamp 
52267f4addbSFrank Haverkamp /**
52367f4addbSFrank Haverkamp  * genwqe_user_vmap() - Map user-space memory to virtual kernel memory
52467f4addbSFrank Haverkamp  * @cd:         pointer to genwqe device
52567f4addbSFrank Haverkamp  * @m:          mapping params
52667f4addbSFrank Haverkamp  * @uaddr:      user virtual address
52767f4addbSFrank Haverkamp  * @size:       size of memory to be mapped
52867f4addbSFrank Haverkamp  *
52967f4addbSFrank Haverkamp  * We need to think about how we could speed this up. Of course it is
53067f4addbSFrank Haverkamp  * not a good idea to do this over and over again, like we are
53167f4addbSFrank Haverkamp  * currently doing it. Nevertheless, I am curious where on the path
53267f4addbSFrank Haverkamp  * the performance is spend. Most probably within the memory
53367f4addbSFrank Haverkamp  * allocation functions, but maybe also in the DMA mapping code.
53467f4addbSFrank Haverkamp  *
53567f4addbSFrank Haverkamp  * Restrictions: The maximum size of the possible mapping currently depends
53667f4addbSFrank Haverkamp  *               on the amount of memory we can get using kzalloc() for the
53767f4addbSFrank Haverkamp  *               page_list and pci_alloc_consistent for the sg_list.
53867f4addbSFrank Haverkamp  *               The sg_list is currently itself not scattered, which could
53967f4addbSFrank Haverkamp  *               be fixed with some effort. The page_list must be split into
54067f4addbSFrank Haverkamp  *               PAGE_SIZE chunks too. All that will make the complicated
54167f4addbSFrank Haverkamp  *               code more complicated.
54267f4addbSFrank Haverkamp  *
54367f4addbSFrank Haverkamp  * Return: 0 if success
54467f4addbSFrank Haverkamp  */
genwqe_user_vmap(struct genwqe_dev * cd,struct dma_mapping * m,void * uaddr,unsigned long size)54567f4addbSFrank Haverkamp int genwqe_user_vmap(struct genwqe_dev *cd, struct dma_mapping *m, void *uaddr,
546658a494aSGuilherme G. Piccoli 		     unsigned long size)
54767f4addbSFrank Haverkamp {
54867f4addbSFrank Haverkamp 	int rc = -EINVAL;
54967f4addbSFrank Haverkamp 	unsigned long data, offs;
55067f4addbSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
55167f4addbSFrank Haverkamp 
55267f4addbSFrank Haverkamp 	if ((uaddr == NULL) || (size == 0)) {
55367f4addbSFrank Haverkamp 		m->size = 0;	/* mark unused and not added */
55467f4addbSFrank Haverkamp 		return -EINVAL;
55567f4addbSFrank Haverkamp 	}
55667f4addbSFrank Haverkamp 	m->u_vaddr = uaddr;
55767f4addbSFrank Haverkamp 	m->size    = size;
55867f4addbSFrank Haverkamp 
55967f4addbSFrank Haverkamp 	/* determine space needed for page_list. */
56067f4addbSFrank Haverkamp 	data = (unsigned long)uaddr;
56167f4addbSFrank Haverkamp 	offs = offset_in_page(data);
562110080ceSDan Carpenter 	if (size > ULONG_MAX - PAGE_SIZE - offs) {
563110080ceSDan Carpenter 		m->size = 0;	/* mark unused and not added */
564110080ceSDan Carpenter 		return -EINVAL;
565110080ceSDan Carpenter 	}
56667f4addbSFrank Haverkamp 	m->nr_pages = DIV_ROUND_UP(offs + size, PAGE_SIZE);
56767f4addbSFrank Haverkamp 
56867f4addbSFrank Haverkamp 	m->page_list = kcalloc(m->nr_pages,
56967f4addbSFrank Haverkamp 			       sizeof(struct page *) + sizeof(dma_addr_t),
57067f4addbSFrank Haverkamp 			       GFP_KERNEL);
57167f4addbSFrank Haverkamp 	if (!m->page_list) {
57267f4addbSFrank Haverkamp 		dev_err(&pci_dev->dev, "err: alloc page_list failed\n");
57367f4addbSFrank Haverkamp 		m->nr_pages = 0;
57467f4addbSFrank Haverkamp 		m->u_vaddr = NULL;
57567f4addbSFrank Haverkamp 		m->size = 0;	/* mark unused and not added */
57667f4addbSFrank Haverkamp 		return -ENOMEM;
57767f4addbSFrank Haverkamp 	}
57867f4addbSFrank Haverkamp 	m->dma_list = (dma_addr_t *)(m->page_list + m->nr_pages);
57967f4addbSFrank Haverkamp 
58067f4addbSFrank Haverkamp 	/* pin user pages in memory */
581ddae1423SJohn Hubbard 	rc = pin_user_pages_fast(data & PAGE_MASK, /* page aligned addr */
58267f4addbSFrank Haverkamp 				 m->nr_pages,
58373b0140bSIra Weiny 				 m->write ? FOLL_WRITE : 0,	/* readable/writable */
58467f4addbSFrank Haverkamp 				 m->page_list);	/* ptrs to pages */
585cf35d6e0SIan Abbott 	if (rc < 0)
586ddae1423SJohn Hubbard 		goto fail_pin_user_pages;
58767f4addbSFrank Haverkamp 
588ddae1423SJohn Hubbard 	/* assumption: pin_user_pages can be killed by signals. */
58967f4addbSFrank Haverkamp 	if (rc < m->nr_pages) {
590ddae1423SJohn Hubbard 		unpin_user_pages_dirty_lock(m->page_list, rc, m->write);
59167f4addbSFrank Haverkamp 		rc = -EFAULT;
592ddae1423SJohn Hubbard 		goto fail_pin_user_pages;
59367f4addbSFrank Haverkamp 	}
59467f4addbSFrank Haverkamp 
59567f4addbSFrank Haverkamp 	rc = genwqe_map_pages(cd, m->page_list, m->nr_pages, m->dma_list);
59667f4addbSFrank Haverkamp 	if (rc != 0)
59767f4addbSFrank Haverkamp 		goto fail_free_user_pages;
59867f4addbSFrank Haverkamp 
59967f4addbSFrank Haverkamp 	return 0;
60067f4addbSFrank Haverkamp 
60167f4addbSFrank Haverkamp  fail_free_user_pages:
602ddae1423SJohn Hubbard 	unpin_user_pages_dirty_lock(m->page_list, m->nr_pages, m->write);
60367f4addbSFrank Haverkamp 
604ddae1423SJohn Hubbard  fail_pin_user_pages:
60567f4addbSFrank Haverkamp 	kfree(m->page_list);
60667f4addbSFrank Haverkamp 	m->page_list = NULL;
60767f4addbSFrank Haverkamp 	m->dma_list = NULL;
60867f4addbSFrank Haverkamp 	m->nr_pages = 0;
60967f4addbSFrank Haverkamp 	m->u_vaddr = NULL;
61067f4addbSFrank Haverkamp 	m->size = 0;		/* mark unused and not added */
61167f4addbSFrank Haverkamp 	return rc;
61267f4addbSFrank Haverkamp }
61367f4addbSFrank Haverkamp 
61467f4addbSFrank Haverkamp /**
61567f4addbSFrank Haverkamp  * genwqe_user_vunmap() - Undo mapping of user-space mem to virtual kernel
61667f4addbSFrank Haverkamp  *                        memory
61767f4addbSFrank Haverkamp  * @cd:         pointer to genwqe device
61867f4addbSFrank Haverkamp  * @m:          mapping params
61967f4addbSFrank Haverkamp  */
genwqe_user_vunmap(struct genwqe_dev * cd,struct dma_mapping * m)620658a494aSGuilherme G. Piccoli int genwqe_user_vunmap(struct genwqe_dev *cd, struct dma_mapping *m)
62167f4addbSFrank Haverkamp {
62267f4addbSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
62367f4addbSFrank Haverkamp 
62467f4addbSFrank Haverkamp 	if (!dma_mapping_used(m)) {
62567f4addbSFrank Haverkamp 		dev_err(&pci_dev->dev, "[%s] err: mapping %p not used!\n",
62667f4addbSFrank Haverkamp 			__func__, m);
62767f4addbSFrank Haverkamp 		return -EINVAL;
62867f4addbSFrank Haverkamp 	}
62967f4addbSFrank Haverkamp 
63067f4addbSFrank Haverkamp 	if (m->dma_list)
63167f4addbSFrank Haverkamp 		genwqe_unmap_pages(cd, m->dma_list, m->nr_pages);
63267f4addbSFrank Haverkamp 
63367f4addbSFrank Haverkamp 	if (m->page_list) {
634ddae1423SJohn Hubbard 		unpin_user_pages_dirty_lock(m->page_list, m->nr_pages,
635ddae1423SJohn Hubbard 					    m->write);
63667f4addbSFrank Haverkamp 		kfree(m->page_list);
63767f4addbSFrank Haverkamp 		m->page_list = NULL;
63867f4addbSFrank Haverkamp 		m->dma_list = NULL;
63967f4addbSFrank Haverkamp 		m->nr_pages = 0;
64067f4addbSFrank Haverkamp 	}
64167f4addbSFrank Haverkamp 
64267f4addbSFrank Haverkamp 	m->u_vaddr = NULL;
64367f4addbSFrank Haverkamp 	m->size = 0;		/* mark as unused and not added */
64467f4addbSFrank Haverkamp 	return 0;
64567f4addbSFrank Haverkamp }
64667f4addbSFrank Haverkamp 
64767f4addbSFrank Haverkamp /**
64867f4addbSFrank Haverkamp  * genwqe_card_type() - Get chip type SLU Configuration Register
64967f4addbSFrank Haverkamp  * @cd:         pointer to the genwqe device descriptor
65067f4addbSFrank Haverkamp  * Return: 0: Altera Stratix-IV 230
65167f4addbSFrank Haverkamp  *         1: Altera Stratix-IV 530
65267f4addbSFrank Haverkamp  *         2: Altera Stratix-V A4
65367f4addbSFrank Haverkamp  *         3: Altera Stratix-V A7
65467f4addbSFrank Haverkamp  */
genwqe_card_type(struct genwqe_dev * cd)65567f4addbSFrank Haverkamp u8 genwqe_card_type(struct genwqe_dev *cd)
65667f4addbSFrank Haverkamp {
65767f4addbSFrank Haverkamp 	u64 card_type = cd->slu_unitcfg;
658d9c11d45SFrank Haverkamp 
65967f4addbSFrank Haverkamp 	return (u8)((card_type & IO_SLU_UNITCFG_TYPE_MASK) >> 20);
66067f4addbSFrank Haverkamp }
66167f4addbSFrank Haverkamp 
66267f4addbSFrank Haverkamp /**
66367f4addbSFrank Haverkamp  * genwqe_card_reset() - Reset the card
66467f4addbSFrank Haverkamp  * @cd:         pointer to the genwqe device descriptor
66567f4addbSFrank Haverkamp  */
genwqe_card_reset(struct genwqe_dev * cd)66667f4addbSFrank Haverkamp int genwqe_card_reset(struct genwqe_dev *cd)
66767f4addbSFrank Haverkamp {
66867f4addbSFrank Haverkamp 	u64 softrst;
66967f4addbSFrank Haverkamp 	struct pci_dev *pci_dev = cd->pci_dev;
67067f4addbSFrank Haverkamp 
67167f4addbSFrank Haverkamp 	if (!genwqe_is_privileged(cd))
67267f4addbSFrank Haverkamp 		return -ENODEV;
67367f4addbSFrank Haverkamp 
67467f4addbSFrank Haverkamp 	/* new SL */
67567f4addbSFrank Haverkamp 	__genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, 0x1ull);
67667f4addbSFrank Haverkamp 	msleep(1000);
67767f4addbSFrank Haverkamp 	__genwqe_readq(cd, IO_HSU_FIR_CLR);
67867f4addbSFrank Haverkamp 	__genwqe_readq(cd, IO_APP_FIR_CLR);
67967f4addbSFrank Haverkamp 	__genwqe_readq(cd, IO_SLU_FIR_CLR);
68067f4addbSFrank Haverkamp 
68167f4addbSFrank Haverkamp 	/*
68267f4addbSFrank Haverkamp 	 * Read-modify-write to preserve the stealth bits
68367f4addbSFrank Haverkamp 	 *
68467f4addbSFrank Haverkamp 	 * For SL >= 039, Stealth WE bit allows removing
68567f4addbSFrank Haverkamp 	 * the read-modify-wrote.
68667f4addbSFrank Haverkamp 	 * r-m-w may require a mask 0x3C to avoid hitting hard
68767f4addbSFrank Haverkamp 	 * reset again for error reset (should be 0, chicken).
68867f4addbSFrank Haverkamp 	 */
68967f4addbSFrank Haverkamp 	softrst = __genwqe_readq(cd, IO_SLC_CFGREG_SOFTRESET) & 0x3cull;
69067f4addbSFrank Haverkamp 	__genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, softrst | 0x2ull);
69167f4addbSFrank Haverkamp 
69267f4addbSFrank Haverkamp 	/* give ERRORRESET some time to finish */
69367f4addbSFrank Haverkamp 	msleep(50);
69467f4addbSFrank Haverkamp 
69567f4addbSFrank Haverkamp 	if (genwqe_need_err_masking(cd)) {
69667f4addbSFrank Haverkamp 		dev_info(&pci_dev->dev,
69767f4addbSFrank Haverkamp 			 "[%s] masking errors for old bitstreams\n", __func__);
69867f4addbSFrank Haverkamp 		__genwqe_writeq(cd, IO_SLC_MISC_DEBUG, 0x0aull);
69967f4addbSFrank Haverkamp 	}
70067f4addbSFrank Haverkamp 	return 0;
70167f4addbSFrank Haverkamp }
70267f4addbSFrank Haverkamp 
genwqe_read_softreset(struct genwqe_dev * cd)70367f4addbSFrank Haverkamp int genwqe_read_softreset(struct genwqe_dev *cd)
70467f4addbSFrank Haverkamp {
70567f4addbSFrank Haverkamp 	u64 bitstream;
70667f4addbSFrank Haverkamp 
70767f4addbSFrank Haverkamp 	if (!genwqe_is_privileged(cd))
70867f4addbSFrank Haverkamp 		return -ENODEV;
70967f4addbSFrank Haverkamp 
71067f4addbSFrank Haverkamp 	bitstream = __genwqe_readq(cd, IO_SLU_BITSTREAM) & 0x1;
71167f4addbSFrank Haverkamp 	cd->softreset = (bitstream == 0) ? 0x8ull : 0xcull;
71267f4addbSFrank Haverkamp 	return 0;
71367f4addbSFrank Haverkamp }
71467f4addbSFrank Haverkamp 
71567f4addbSFrank Haverkamp /**
71667f4addbSFrank Haverkamp  * genwqe_set_interrupt_capability() - Configure MSI capability structure
71767f4addbSFrank Haverkamp  * @cd:         pointer to the device
718364b40c0SLee Jones  * @count:      number of vectors to allocate
71967f4addbSFrank Haverkamp  * Return: 0 if no error
72067f4addbSFrank Haverkamp  */
genwqe_set_interrupt_capability(struct genwqe_dev * cd,int count)72167f4addbSFrank Haverkamp int genwqe_set_interrupt_capability(struct genwqe_dev *cd, int count)
72267f4addbSFrank Haverkamp {
72367f4addbSFrank Haverkamp 	int rc;
72467f4addbSFrank Haverkamp 
725d3f45647SChristoph Hellwig 	rc = pci_alloc_irq_vectors(cd->pci_dev, 1, count, PCI_IRQ_MSI);
7267276883fSSebastian Ott 	if (rc < 0)
72767f4addbSFrank Haverkamp 		return rc;
7287276883fSSebastian Ott 	return 0;
72967f4addbSFrank Haverkamp }
73067f4addbSFrank Haverkamp 
73167f4addbSFrank Haverkamp /**
73267f4addbSFrank Haverkamp  * genwqe_reset_interrupt_capability() - Undo genwqe_set_interrupt_capability()
73367f4addbSFrank Haverkamp  * @cd:         pointer to the device
73467f4addbSFrank Haverkamp  */
genwqe_reset_interrupt_capability(struct genwqe_dev * cd)73567f4addbSFrank Haverkamp void genwqe_reset_interrupt_capability(struct genwqe_dev *cd)
73667f4addbSFrank Haverkamp {
737d3f45647SChristoph Hellwig 	pci_free_irq_vectors(cd->pci_dev);
73867f4addbSFrank Haverkamp }
73967f4addbSFrank Haverkamp 
74067f4addbSFrank Haverkamp /**
74167f4addbSFrank Haverkamp  * set_reg_idx() - Fill array with data. Ignore illegal offsets.
74267f4addbSFrank Haverkamp  * @cd:         card device
74367f4addbSFrank Haverkamp  * @r:          debug register array
74467f4addbSFrank Haverkamp  * @i:          index to desired entry
74567f4addbSFrank Haverkamp  * @m:          maximum possible entries
74667f4addbSFrank Haverkamp  * @addr:       addr which is read
747364b40c0SLee Jones  * @idx:        index in debug array
74867f4addbSFrank Haverkamp  * @val:        read value
74967f4addbSFrank Haverkamp  */
set_reg_idx(struct genwqe_dev * cd,struct genwqe_reg * r,unsigned int * i,unsigned int m,u32 addr,u32 idx,u64 val)75067f4addbSFrank Haverkamp static int set_reg_idx(struct genwqe_dev *cd, struct genwqe_reg *r,
75167f4addbSFrank Haverkamp 		       unsigned int *i, unsigned int m, u32 addr, u32 idx,
75267f4addbSFrank Haverkamp 		       u64 val)
75367f4addbSFrank Haverkamp {
75467f4addbSFrank Haverkamp 	if (WARN_ON_ONCE(*i >= m))
75567f4addbSFrank Haverkamp 		return -EFAULT;
75667f4addbSFrank Haverkamp 
75767f4addbSFrank Haverkamp 	r[*i].addr = addr;
75867f4addbSFrank Haverkamp 	r[*i].idx = idx;
75967f4addbSFrank Haverkamp 	r[*i].val = val;
76067f4addbSFrank Haverkamp 	++*i;
76167f4addbSFrank Haverkamp 	return 0;
76267f4addbSFrank Haverkamp }
76367f4addbSFrank Haverkamp 
set_reg(struct genwqe_dev * cd,struct genwqe_reg * r,unsigned int * i,unsigned int m,u32 addr,u64 val)76467f4addbSFrank Haverkamp static int set_reg(struct genwqe_dev *cd, struct genwqe_reg *r,
76567f4addbSFrank Haverkamp 		   unsigned int *i, unsigned int m, u32 addr, u64 val)
76667f4addbSFrank Haverkamp {
76767f4addbSFrank Haverkamp 	return set_reg_idx(cd, r, i, m, addr, 0, val);
76867f4addbSFrank Haverkamp }
76967f4addbSFrank Haverkamp 
genwqe_read_ffdc_regs(struct genwqe_dev * cd,struct genwqe_reg * regs,unsigned int max_regs,int all)77067f4addbSFrank Haverkamp int genwqe_read_ffdc_regs(struct genwqe_dev *cd, struct genwqe_reg *regs,
77167f4addbSFrank Haverkamp 			 unsigned int max_regs, int all)
77267f4addbSFrank Haverkamp {
77367f4addbSFrank Haverkamp 	unsigned int i, j, idx = 0;
77467f4addbSFrank Haverkamp 	u32 ufir_addr, ufec_addr, sfir_addr, sfec_addr;
77567f4addbSFrank Haverkamp 	u64 gfir, sluid, appid, ufir, ufec, sfir, sfec;
77667f4addbSFrank Haverkamp 
77767f4addbSFrank Haverkamp 	/* Global FIR */
77867f4addbSFrank Haverkamp 	gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
77967f4addbSFrank Haverkamp 	set_reg(cd, regs, &idx, max_regs, IO_SLC_CFGREG_GFIR, gfir);
78067f4addbSFrank Haverkamp 
78167f4addbSFrank Haverkamp 	/* UnitCfg for SLU */
78267f4addbSFrank Haverkamp 	sluid = __genwqe_readq(cd, IO_SLU_UNITCFG); /* 0x00000000 */
78367f4addbSFrank Haverkamp 	set_reg(cd, regs, &idx, max_regs, IO_SLU_UNITCFG, sluid);
78467f4addbSFrank Haverkamp 
78567f4addbSFrank Haverkamp 	/* UnitCfg for APP */
78667f4addbSFrank Haverkamp 	appid = __genwqe_readq(cd, IO_APP_UNITCFG); /* 0x02000000 */
78767f4addbSFrank Haverkamp 	set_reg(cd, regs, &idx, max_regs, IO_APP_UNITCFG, appid);
78867f4addbSFrank Haverkamp 
78967f4addbSFrank Haverkamp 	/* Check all chip Units */
79067f4addbSFrank Haverkamp 	for (i = 0; i < GENWQE_MAX_UNITS; i++) {
79167f4addbSFrank Haverkamp 
79267f4addbSFrank Haverkamp 		/* Unit FIR */
79367f4addbSFrank Haverkamp 		ufir_addr = (i << 24) | 0x008;
79467f4addbSFrank Haverkamp 		ufir = __genwqe_readq(cd, ufir_addr);
79567f4addbSFrank Haverkamp 		set_reg(cd, regs, &idx, max_regs, ufir_addr, ufir);
79667f4addbSFrank Haverkamp 
79767f4addbSFrank Haverkamp 		/* Unit FEC */
79867f4addbSFrank Haverkamp 		ufec_addr = (i << 24) | 0x018;
79967f4addbSFrank Haverkamp 		ufec = __genwqe_readq(cd, ufec_addr);
80067f4addbSFrank Haverkamp 		set_reg(cd, regs, &idx, max_regs, ufec_addr, ufec);
80167f4addbSFrank Haverkamp 
80267f4addbSFrank Haverkamp 		for (j = 0; j < 64; j++) {
80367f4addbSFrank Haverkamp 			/* wherever there is a primary 1, read the 2ndary */
80467f4addbSFrank Haverkamp 			if (!all && (!(ufir & (1ull << j))))
80567f4addbSFrank Haverkamp 				continue;
80667f4addbSFrank Haverkamp 
80767f4addbSFrank Haverkamp 			sfir_addr = (i << 24) | (0x100 + 8 * j);
80867f4addbSFrank Haverkamp 			sfir = __genwqe_readq(cd, sfir_addr);
80967f4addbSFrank Haverkamp 			set_reg(cd, regs, &idx, max_regs, sfir_addr, sfir);
81067f4addbSFrank Haverkamp 
81167f4addbSFrank Haverkamp 			sfec_addr = (i << 24) | (0x300 + 8 * j);
81267f4addbSFrank Haverkamp 			sfec = __genwqe_readq(cd, sfec_addr);
81367f4addbSFrank Haverkamp 			set_reg(cd, regs, &idx, max_regs, sfec_addr, sfec);
81467f4addbSFrank Haverkamp 		}
81567f4addbSFrank Haverkamp 	}
81667f4addbSFrank Haverkamp 
81767f4addbSFrank Haverkamp 	/* fill with invalid data until end */
81867f4addbSFrank Haverkamp 	for (i = idx; i < max_regs; i++) {
81967f4addbSFrank Haverkamp 		regs[i].addr = 0xffffffff;
82067f4addbSFrank Haverkamp 		regs[i].val = 0xffffffffffffffffull;
82167f4addbSFrank Haverkamp 	}
82267f4addbSFrank Haverkamp 	return idx;
82367f4addbSFrank Haverkamp }
82467f4addbSFrank Haverkamp 
82567f4addbSFrank Haverkamp /**
82667f4addbSFrank Haverkamp  * genwqe_ffdc_buff_size() - Calculates the number of dump registers
827364b40c0SLee Jones  * @cd:	        genwqe device descriptor
828364b40c0SLee Jones  * @uid:	unit ID
82967f4addbSFrank Haverkamp  */
genwqe_ffdc_buff_size(struct genwqe_dev * cd,int uid)83067f4addbSFrank Haverkamp int genwqe_ffdc_buff_size(struct genwqe_dev *cd, int uid)
83167f4addbSFrank Haverkamp {
83267f4addbSFrank Haverkamp 	int entries = 0, ring, traps, traces, trace_entries;
83367f4addbSFrank Haverkamp 	u32 eevptr_addr, l_addr, d_len, d_type;
83467f4addbSFrank Haverkamp 	u64 eevptr, val, addr;
83567f4addbSFrank Haverkamp 
83667f4addbSFrank Haverkamp 	eevptr_addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_ERROR_POINTER;
83767f4addbSFrank Haverkamp 	eevptr = __genwqe_readq(cd, eevptr_addr);
83867f4addbSFrank Haverkamp 
83967f4addbSFrank Haverkamp 	if ((eevptr != 0x0) && (eevptr != -1ull)) {
84067f4addbSFrank Haverkamp 		l_addr = GENWQE_UID_OFFS(uid) | eevptr;
84167f4addbSFrank Haverkamp 
84267f4addbSFrank Haverkamp 		while (1) {
84367f4addbSFrank Haverkamp 			val = __genwqe_readq(cd, l_addr);
84467f4addbSFrank Haverkamp 
84567f4addbSFrank Haverkamp 			if ((val == 0x0) || (val == -1ull))
84667f4addbSFrank Haverkamp 				break;
84767f4addbSFrank Haverkamp 
84867f4addbSFrank Haverkamp 			/* 38:24 */
84967f4addbSFrank Haverkamp 			d_len  = (val & 0x0000007fff000000ull) >> 24;
85067f4addbSFrank Haverkamp 
85167f4addbSFrank Haverkamp 			/* 39 */
85267f4addbSFrank Haverkamp 			d_type = (val & 0x0000008000000000ull) >> 36;
85367f4addbSFrank Haverkamp 
85467f4addbSFrank Haverkamp 			if (d_type) {	/* repeat */
85567f4addbSFrank Haverkamp 				entries += d_len;
85667f4addbSFrank Haverkamp 			} else {	/* size in bytes! */
85767f4addbSFrank Haverkamp 				entries += d_len >> 3;
85867f4addbSFrank Haverkamp 			}
85967f4addbSFrank Haverkamp 
86067f4addbSFrank Haverkamp 			l_addr += 8;
86167f4addbSFrank Haverkamp 		}
86267f4addbSFrank Haverkamp 	}
86367f4addbSFrank Haverkamp 
86467f4addbSFrank Haverkamp 	for (ring = 0; ring < 8; ring++) {
86567f4addbSFrank Haverkamp 		addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_DIAG_MAP(ring);
86667f4addbSFrank Haverkamp 		val = __genwqe_readq(cd, addr);
86767f4addbSFrank Haverkamp 
86867f4addbSFrank Haverkamp 		if ((val == 0x0ull) || (val == -1ull))
86967f4addbSFrank Haverkamp 			continue;
87067f4addbSFrank Haverkamp 
87167f4addbSFrank Haverkamp 		traps = (val >> 24) & 0xff;
87267f4addbSFrank Haverkamp 		traces = (val >> 16) & 0xff;
87367f4addbSFrank Haverkamp 		trace_entries = val & 0xffff;
87467f4addbSFrank Haverkamp 
87567f4addbSFrank Haverkamp 		entries += traps + (traces * trace_entries);
87667f4addbSFrank Haverkamp 	}
87767f4addbSFrank Haverkamp 	return entries;
87867f4addbSFrank Haverkamp }
87967f4addbSFrank Haverkamp 
88067f4addbSFrank Haverkamp /**
88167f4addbSFrank Haverkamp  * genwqe_ffdc_buff_read() - Implements LogoutExtendedErrorRegisters procedure
882364b40c0SLee Jones  * @cd:	        genwqe device descriptor
883364b40c0SLee Jones  * @uid:	unit ID
884364b40c0SLee Jones  * @regs:       register information
885364b40c0SLee Jones  * @max_regs:   number of register entries
88667f4addbSFrank Haverkamp  */
genwqe_ffdc_buff_read(struct genwqe_dev * cd,int uid,struct genwqe_reg * regs,unsigned int max_regs)88767f4addbSFrank Haverkamp int genwqe_ffdc_buff_read(struct genwqe_dev *cd, int uid,
88867f4addbSFrank Haverkamp 			  struct genwqe_reg *regs, unsigned int max_regs)
88967f4addbSFrank Haverkamp {
89067f4addbSFrank Haverkamp 	int i, traps, traces, trace, trace_entries, trace_entry, ring;
89167f4addbSFrank Haverkamp 	unsigned int idx = 0;
89267f4addbSFrank Haverkamp 	u32 eevptr_addr, l_addr, d_addr, d_len, d_type;
89367f4addbSFrank Haverkamp 	u64 eevptr, e, val, addr;
89467f4addbSFrank Haverkamp 
89567f4addbSFrank Haverkamp 	eevptr_addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_ERROR_POINTER;
89667f4addbSFrank Haverkamp 	eevptr = __genwqe_readq(cd, eevptr_addr);
89767f4addbSFrank Haverkamp 
89867f4addbSFrank Haverkamp 	if ((eevptr != 0x0) && (eevptr != 0xffffffffffffffffull)) {
89967f4addbSFrank Haverkamp 		l_addr = GENWQE_UID_OFFS(uid) | eevptr;
90067f4addbSFrank Haverkamp 		while (1) {
90167f4addbSFrank Haverkamp 			e = __genwqe_readq(cd, l_addr);
90267f4addbSFrank Haverkamp 			if ((e == 0x0) || (e == 0xffffffffffffffffull))
90367f4addbSFrank Haverkamp 				break;
90467f4addbSFrank Haverkamp 
90567f4addbSFrank Haverkamp 			d_addr = (e & 0x0000000000ffffffull);	    /* 23:0 */
90667f4addbSFrank Haverkamp 			d_len  = (e & 0x0000007fff000000ull) >> 24; /* 38:24 */
90767f4addbSFrank Haverkamp 			d_type = (e & 0x0000008000000000ull) >> 36; /* 39 */
90867f4addbSFrank Haverkamp 			d_addr |= GENWQE_UID_OFFS(uid);
90967f4addbSFrank Haverkamp 
91067f4addbSFrank Haverkamp 			if (d_type) {
91167f4addbSFrank Haverkamp 				for (i = 0; i < (int)d_len; i++) {
91267f4addbSFrank Haverkamp 					val = __genwqe_readq(cd, d_addr);
91367f4addbSFrank Haverkamp 					set_reg_idx(cd, regs, &idx, max_regs,
91467f4addbSFrank Haverkamp 						    d_addr, i, val);
91567f4addbSFrank Haverkamp 				}
91667f4addbSFrank Haverkamp 			} else {
91767f4addbSFrank Haverkamp 				d_len >>= 3; /* Size in bytes! */
91867f4addbSFrank Haverkamp 				for (i = 0; i < (int)d_len; i++, d_addr += 8) {
91967f4addbSFrank Haverkamp 					val = __genwqe_readq(cd, d_addr);
92067f4addbSFrank Haverkamp 					set_reg_idx(cd, regs, &idx, max_regs,
92167f4addbSFrank Haverkamp 						    d_addr, 0, val);
92267f4addbSFrank Haverkamp 				}
92367f4addbSFrank Haverkamp 			}
92467f4addbSFrank Haverkamp 			l_addr += 8;
92567f4addbSFrank Haverkamp 		}
92667f4addbSFrank Haverkamp 	}
92767f4addbSFrank Haverkamp 
92867f4addbSFrank Haverkamp 	/*
92967f4addbSFrank Haverkamp 	 * To save time, there are only 6 traces poplulated on Uid=2,
93067f4addbSFrank Haverkamp 	 * Ring=1. each with iters=512.
93167f4addbSFrank Haverkamp 	 */
93267f4addbSFrank Haverkamp 	for (ring = 0; ring < 8; ring++) { /* 0 is fls, 1 is fds,
93367f4addbSFrank Haverkamp 					      2...7 are ASI rings */
93467f4addbSFrank Haverkamp 		addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_DIAG_MAP(ring);
93567f4addbSFrank Haverkamp 		val = __genwqe_readq(cd, addr);
93667f4addbSFrank Haverkamp 
93767f4addbSFrank Haverkamp 		if ((val == 0x0ull) || (val == -1ull))
93867f4addbSFrank Haverkamp 			continue;
93967f4addbSFrank Haverkamp 
94067f4addbSFrank Haverkamp 		traps = (val >> 24) & 0xff;	/* Number of Traps	*/
94167f4addbSFrank Haverkamp 		traces = (val >> 16) & 0xff;	/* Number of Traces	*/
94267f4addbSFrank Haverkamp 		trace_entries = val & 0xffff;	/* Entries per trace	*/
94367f4addbSFrank Haverkamp 
94467f4addbSFrank Haverkamp 		/* Note: This is a combined loop that dumps both the traps */
94567f4addbSFrank Haverkamp 		/* (for the trace == 0 case) as well as the traces 1 to    */
94667f4addbSFrank Haverkamp 		/* 'traces'.						   */
94767f4addbSFrank Haverkamp 		for (trace = 0; trace <= traces; trace++) {
94867f4addbSFrank Haverkamp 			u32 diag_sel =
94967f4addbSFrank Haverkamp 				GENWQE_EXTENDED_DIAG_SELECTOR(ring, trace);
95067f4addbSFrank Haverkamp 
95167f4addbSFrank Haverkamp 			addr = (GENWQE_UID_OFFS(uid) |
95267f4addbSFrank Haverkamp 				IO_EXTENDED_DIAG_SELECTOR);
95367f4addbSFrank Haverkamp 			__genwqe_writeq(cd, addr, diag_sel);
95467f4addbSFrank Haverkamp 
95567f4addbSFrank Haverkamp 			for (trace_entry = 0;
95667f4addbSFrank Haverkamp 			     trace_entry < (trace ? trace_entries : traps);
95767f4addbSFrank Haverkamp 			     trace_entry++) {
95867f4addbSFrank Haverkamp 				addr = (GENWQE_UID_OFFS(uid) |
95967f4addbSFrank Haverkamp 					IO_EXTENDED_DIAG_READ_MBX);
96067f4addbSFrank Haverkamp 				val = __genwqe_readq(cd, addr);
96167f4addbSFrank Haverkamp 				set_reg_idx(cd, regs, &idx, max_regs, addr,
96267f4addbSFrank Haverkamp 					    (diag_sel<<16) | trace_entry, val);
96367f4addbSFrank Haverkamp 			}
96467f4addbSFrank Haverkamp 		}
96567f4addbSFrank Haverkamp 	}
96667f4addbSFrank Haverkamp 	return 0;
96767f4addbSFrank Haverkamp }
96867f4addbSFrank Haverkamp 
96967f4addbSFrank Haverkamp /**
97067f4addbSFrank Haverkamp  * genwqe_write_vreg() - Write register in virtual window
971364b40c0SLee Jones  * @cd:	        genwqe device descriptor
972364b40c0SLee Jones  * @reg:	register (byte) offset within BAR
973364b40c0SLee Jones  * @val:	value to write
974364b40c0SLee Jones  * @func:	PCI virtual function
97567f4addbSFrank Haverkamp  *
97667f4addbSFrank Haverkamp  * Note, these registers are only accessible to the PF through the
97767f4addbSFrank Haverkamp  * VF-window. It is not intended for the VF to access.
97867f4addbSFrank Haverkamp  */
genwqe_write_vreg(struct genwqe_dev * cd,u32 reg,u64 val,int func)97967f4addbSFrank Haverkamp int genwqe_write_vreg(struct genwqe_dev *cd, u32 reg, u64 val, int func)
98067f4addbSFrank Haverkamp {
98167f4addbSFrank Haverkamp 	__genwqe_writeq(cd, IO_PF_SLC_VIRTUAL_WINDOW, func & 0xf);
98267f4addbSFrank Haverkamp 	__genwqe_writeq(cd, reg, val);
98367f4addbSFrank Haverkamp 	return 0;
98467f4addbSFrank Haverkamp }
98567f4addbSFrank Haverkamp 
98667f4addbSFrank Haverkamp /**
98767f4addbSFrank Haverkamp  * genwqe_read_vreg() - Read register in virtual window
988364b40c0SLee Jones  * @cd:	        genwqe device descriptor
989364b40c0SLee Jones  * @reg:	register (byte) offset within BAR
990364b40c0SLee Jones  * @func:	PCI virtual function
99167f4addbSFrank Haverkamp  *
99267f4addbSFrank Haverkamp  * Note, these registers are only accessible to the PF through the
99367f4addbSFrank Haverkamp  * VF-window. It is not intended for the VF to access.
99467f4addbSFrank Haverkamp  */
genwqe_read_vreg(struct genwqe_dev * cd,u32 reg,int func)99567f4addbSFrank Haverkamp u64 genwqe_read_vreg(struct genwqe_dev *cd, u32 reg, int func)
99667f4addbSFrank Haverkamp {
99767f4addbSFrank Haverkamp 	__genwqe_writeq(cd, IO_PF_SLC_VIRTUAL_WINDOW, func & 0xf);
99867f4addbSFrank Haverkamp 	return __genwqe_readq(cd, reg);
99967f4addbSFrank Haverkamp }
100067f4addbSFrank Haverkamp 
100167f4addbSFrank Haverkamp /**
100267f4addbSFrank Haverkamp  * genwqe_base_clock_frequency() - Deteremine base clock frequency of the card
1003364b40c0SLee Jones  * @cd:	        genwqe device descriptor
100467f4addbSFrank Haverkamp  *
100567f4addbSFrank Haverkamp  * Note: From a design perspective it turned out to be a bad idea to
100667f4addbSFrank Haverkamp  * use codes here to specifiy the frequency/speed values. An old
100767f4addbSFrank Haverkamp  * driver cannot understand new codes and is therefore always a
100867f4addbSFrank Haverkamp  * problem. Better is to measure out the value or put the
100967f4addbSFrank Haverkamp  * speed/frequency directly into a register which is always a valid
101067f4addbSFrank Haverkamp  * value for old as well as for new software.
101167f4addbSFrank Haverkamp  *
101267f4addbSFrank Haverkamp  * Return: Card clock in MHz
101367f4addbSFrank Haverkamp  */
genwqe_base_clock_frequency(struct genwqe_dev * cd)101467f4addbSFrank Haverkamp int genwqe_base_clock_frequency(struct genwqe_dev *cd)
101567f4addbSFrank Haverkamp {
101667f4addbSFrank Haverkamp 	u16 speed;		/*         MHz  MHz  MHz  MHz */
101767f4addbSFrank Haverkamp 	static const int speed_grade[] = { 250, 200, 166, 175 };
101867f4addbSFrank Haverkamp 
101967f4addbSFrank Haverkamp 	speed = (u16)((cd->slu_unitcfg >> 28) & 0x0full);
102067f4addbSFrank Haverkamp 	if (speed >= ARRAY_SIZE(speed_grade))
102167f4addbSFrank Haverkamp 		return 0;	/* illegal value */
102267f4addbSFrank Haverkamp 
102367f4addbSFrank Haverkamp 	return speed_grade[speed];
102467f4addbSFrank Haverkamp }
102567f4addbSFrank Haverkamp 
102667f4addbSFrank Haverkamp /**
102767f4addbSFrank Haverkamp  * genwqe_stop_traps() - Stop traps
1028364b40c0SLee Jones  * @cd:	        genwqe device descriptor
102967f4addbSFrank Haverkamp  *
103067f4addbSFrank Haverkamp  * Before reading out the analysis data, we need to stop the traps.
103167f4addbSFrank Haverkamp  */
genwqe_stop_traps(struct genwqe_dev * cd)103267f4addbSFrank Haverkamp void genwqe_stop_traps(struct genwqe_dev *cd)
103367f4addbSFrank Haverkamp {
103467f4addbSFrank Haverkamp 	__genwqe_writeq(cd, IO_SLC_MISC_DEBUG_SET, 0xcull);
103567f4addbSFrank Haverkamp }
103667f4addbSFrank Haverkamp 
103767f4addbSFrank Haverkamp /**
103867f4addbSFrank Haverkamp  * genwqe_start_traps() - Start traps
1039364b40c0SLee Jones  * @cd:	        genwqe device descriptor
104067f4addbSFrank Haverkamp  *
104167f4addbSFrank Haverkamp  * After having read the data, we can/must enable the traps again.
104267f4addbSFrank Haverkamp  */
genwqe_start_traps(struct genwqe_dev * cd)104367f4addbSFrank Haverkamp void genwqe_start_traps(struct genwqe_dev *cd)
104467f4addbSFrank Haverkamp {
104567f4addbSFrank Haverkamp 	__genwqe_writeq(cd, IO_SLC_MISC_DEBUG_CLR, 0xcull);
104667f4addbSFrank Haverkamp 
104767f4addbSFrank Haverkamp 	if (genwqe_need_err_masking(cd))
104867f4addbSFrank Haverkamp 		__genwqe_writeq(cd, IO_SLC_MISC_DEBUG, 0x0aull);
104967f4addbSFrank Haverkamp }
1050