xref: /openbmc/linux/arch/s390/include/asm/idals.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c6557e7fSMartin Schwidefsky /*
3c6557e7fSMartin Schwidefsky  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4c6557e7fSMartin Schwidefsky  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
5c6557e7fSMartin Schwidefsky  * Bugreports.to..: <Linux390@de.ibm.com>
6a53c8fabSHeiko Carstens  * Copyright IBM Corp. 2000
7a53c8fabSHeiko Carstens  *
8c6557e7fSMartin Schwidefsky  * History of changes
9c6557e7fSMartin Schwidefsky  * 07/24/00 new file
10c6557e7fSMartin Schwidefsky  * 05/04/02 code restructuring.
11c6557e7fSMartin Schwidefsky  */
12c6557e7fSMartin Schwidefsky 
13c6557e7fSMartin Schwidefsky #ifndef _S390_IDALS_H
14c6557e7fSMartin Schwidefsky #define _S390_IDALS_H
15c6557e7fSMartin Schwidefsky 
16c6557e7fSMartin Schwidefsky #include <linux/errno.h>
17c6557e7fSMartin Schwidefsky #include <linux/err.h>
18c6557e7fSMartin Schwidefsky #include <linux/types.h>
19c6557e7fSMartin Schwidefsky #include <linux/slab.h>
20c6557e7fSMartin Schwidefsky #include <asm/cio.h>
217c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
22c6557e7fSMartin Schwidefsky 
23c6557e7fSMartin Schwidefsky #define IDA_SIZE_LOG 12 /* 11 for 2k , 12 for 4k */
24c6557e7fSMartin Schwidefsky #define IDA_BLOCK_SIZE (1L<<IDA_SIZE_LOG)
25c6557e7fSMartin Schwidefsky 
26*6a6dc14aSEric Farman #define IDA_2K_SIZE_LOG 11
27*6a6dc14aSEric Farman #define IDA_2K_BLOCK_SIZE (1L << IDA_2K_SIZE_LOG)
28*6a6dc14aSEric Farman 
29c6557e7fSMartin Schwidefsky /*
30c6557e7fSMartin Schwidefsky  * Test if an address/length pair needs an idal list.
31c6557e7fSMartin Schwidefsky  */
32c6557e7fSMartin Schwidefsky static inline int
idal_is_needed(void * vaddr,unsigned int length)33c6557e7fSMartin Schwidefsky idal_is_needed(void *vaddr, unsigned int length)
34c6557e7fSMartin Schwidefsky {
35c6557e7fSMartin Schwidefsky 	return ((__pa(vaddr) + length - 1) >> 31) != 0;
36c6557e7fSMartin Schwidefsky }
37c6557e7fSMartin Schwidefsky 
38c6557e7fSMartin Schwidefsky 
39c6557e7fSMartin Schwidefsky /*
40c6557e7fSMartin Schwidefsky  * Return the number of idal words needed for an address/length pair.
41c6557e7fSMartin Schwidefsky  */
idal_nr_words(void * vaddr,unsigned int length)42f3eb5384SStefan Weinhuber static inline unsigned int idal_nr_words(void *vaddr, unsigned int length)
43c6557e7fSMartin Schwidefsky {
44c6557e7fSMartin Schwidefsky 	return ((__pa(vaddr) & (IDA_BLOCK_SIZE-1)) + length +
45c6557e7fSMartin Schwidefsky 		(IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
46c6557e7fSMartin Schwidefsky }
47c6557e7fSMartin Schwidefsky 
48c6557e7fSMartin Schwidefsky /*
49*6a6dc14aSEric Farman  * Return the number of 2K IDA words needed for an address/length pair.
50*6a6dc14aSEric Farman  */
idal_2k_nr_words(void * vaddr,unsigned int length)51*6a6dc14aSEric Farman static inline unsigned int idal_2k_nr_words(void *vaddr, unsigned int length)
52*6a6dc14aSEric Farman {
53*6a6dc14aSEric Farman 	return ((__pa(vaddr) & (IDA_2K_BLOCK_SIZE - 1)) + length +
54*6a6dc14aSEric Farman 		(IDA_2K_BLOCK_SIZE - 1)) >> IDA_2K_SIZE_LOG;
55*6a6dc14aSEric Farman }
56*6a6dc14aSEric Farman 
57*6a6dc14aSEric Farman /*
58c6557e7fSMartin Schwidefsky  * Create the list of idal words for an address/length pair.
59c6557e7fSMartin Schwidefsky  */
idal_create_words(unsigned long * idaws,void * vaddr,unsigned int length)60f3eb5384SStefan Weinhuber static inline unsigned long *idal_create_words(unsigned long *idaws,
61f3eb5384SStefan Weinhuber 					       void *vaddr, unsigned int length)
62c6557e7fSMartin Schwidefsky {
63c6557e7fSMartin Schwidefsky 	unsigned long paddr;
64c6557e7fSMartin Schwidefsky 	unsigned int cidaw;
65c6557e7fSMartin Schwidefsky 
66c6557e7fSMartin Schwidefsky 	paddr = __pa(vaddr);
67c6557e7fSMartin Schwidefsky 	cidaw = ((paddr & (IDA_BLOCK_SIZE-1)) + length +
68c6557e7fSMartin Schwidefsky 		 (IDA_BLOCK_SIZE-1)) >> IDA_SIZE_LOG;
69c6557e7fSMartin Schwidefsky 	*idaws++ = paddr;
70c6557e7fSMartin Schwidefsky 	paddr &= -IDA_BLOCK_SIZE;
71c6557e7fSMartin Schwidefsky 	while (--cidaw > 0) {
72c6557e7fSMartin Schwidefsky 		paddr += IDA_BLOCK_SIZE;
73c6557e7fSMartin Schwidefsky 		*idaws++ = paddr;
74c6557e7fSMartin Schwidefsky 	}
75c6557e7fSMartin Schwidefsky 	return idaws;
76c6557e7fSMartin Schwidefsky }
77c6557e7fSMartin Schwidefsky 
78c6557e7fSMartin Schwidefsky /*
79c6557e7fSMartin Schwidefsky  * Sets the address of the data in CCW.
80c6557e7fSMartin Schwidefsky  * If necessary it allocates an IDAL and sets the appropriate flags.
81c6557e7fSMartin Schwidefsky  */
82c6557e7fSMartin Schwidefsky static inline int
set_normalized_cda(struct ccw1 * ccw,void * vaddr)83c6557e7fSMartin Schwidefsky set_normalized_cda(struct ccw1 * ccw, void *vaddr)
84c6557e7fSMartin Schwidefsky {
85c6557e7fSMartin Schwidefsky 	unsigned int nridaws;
86c6557e7fSMartin Schwidefsky 	unsigned long *idal;
87c6557e7fSMartin Schwidefsky 
88c6557e7fSMartin Schwidefsky 	if (ccw->flags & CCW_FLAG_IDA)
89c6557e7fSMartin Schwidefsky 		return -EINVAL;
90c6557e7fSMartin Schwidefsky 	nridaws = idal_nr_words(vaddr, ccw->count);
91c6557e7fSMartin Schwidefsky 	if (nridaws > 0) {
92c6557e7fSMartin Schwidefsky 		idal = kmalloc(nridaws * sizeof(unsigned long),
93c6557e7fSMartin Schwidefsky 			       GFP_ATOMIC | GFP_DMA );
94c6557e7fSMartin Schwidefsky 		if (idal == NULL)
95c6557e7fSMartin Schwidefsky 			return -ENOMEM;
96c6557e7fSMartin Schwidefsky 		idal_create_words(idal, vaddr, ccw->count);
97c6557e7fSMartin Schwidefsky 		ccw->flags |= CCW_FLAG_IDA;
98c6557e7fSMartin Schwidefsky 		vaddr = idal;
99c6557e7fSMartin Schwidefsky 	}
100c6557e7fSMartin Schwidefsky 	ccw->cda = (__u32)(unsigned long) vaddr;
101c6557e7fSMartin Schwidefsky 	return 0;
102c6557e7fSMartin Schwidefsky }
103c6557e7fSMartin Schwidefsky 
104c6557e7fSMartin Schwidefsky /*
105c6557e7fSMartin Schwidefsky  * Releases any allocated IDAL related to the CCW.
106c6557e7fSMartin Schwidefsky  */
107c6557e7fSMartin Schwidefsky static inline void
clear_normalized_cda(struct ccw1 * ccw)108c6557e7fSMartin Schwidefsky clear_normalized_cda(struct ccw1 * ccw)
109c6557e7fSMartin Schwidefsky {
110c6557e7fSMartin Schwidefsky 	if (ccw->flags & CCW_FLAG_IDA) {
111c6557e7fSMartin Schwidefsky 		kfree((void *)(unsigned long) ccw->cda);
112c6557e7fSMartin Schwidefsky 		ccw->flags &= ~CCW_FLAG_IDA;
113c6557e7fSMartin Schwidefsky 	}
114c6557e7fSMartin Schwidefsky 	ccw->cda = 0;
115c6557e7fSMartin Schwidefsky }
116c6557e7fSMartin Schwidefsky 
117c6557e7fSMartin Schwidefsky /*
118c6557e7fSMartin Schwidefsky  * Idal buffer extension
119c6557e7fSMartin Schwidefsky  */
120c6557e7fSMartin Schwidefsky struct idal_buffer {
121c6557e7fSMartin Schwidefsky 	size_t size;
122c6557e7fSMartin Schwidefsky 	size_t page_order;
1235224f790SGustavo A. R. Silva 	void *data[];
124c6557e7fSMartin Schwidefsky };
125c6557e7fSMartin Schwidefsky 
126c6557e7fSMartin Schwidefsky /*
127c6557e7fSMartin Schwidefsky  * Allocate an idal buffer
128c6557e7fSMartin Schwidefsky  */
129c6557e7fSMartin Schwidefsky static inline struct idal_buffer *
idal_buffer_alloc(size_t size,int page_order)130c6557e7fSMartin Schwidefsky idal_buffer_alloc(size_t size, int page_order)
131c6557e7fSMartin Schwidefsky {
132c6557e7fSMartin Schwidefsky 	struct idal_buffer *ib;
133c6557e7fSMartin Schwidefsky 	int nr_chunks, nr_ptrs, i;
134c6557e7fSMartin Schwidefsky 
135c6557e7fSMartin Schwidefsky 	nr_ptrs = (size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
136c6557e7fSMartin Schwidefsky 	nr_chunks = (4096 << page_order) >> IDA_SIZE_LOG;
13731885a8dSxiaolinkui 	ib = kmalloc(struct_size(ib, data, nr_ptrs), GFP_DMA | GFP_KERNEL);
138c6557e7fSMartin Schwidefsky 	if (ib == NULL)
139c6557e7fSMartin Schwidefsky 		return ERR_PTR(-ENOMEM);
140c6557e7fSMartin Schwidefsky 	ib->size = size;
141c6557e7fSMartin Schwidefsky 	ib->page_order = page_order;
142c6557e7fSMartin Schwidefsky 	for (i = 0; i < nr_ptrs; i++) {
143c6557e7fSMartin Schwidefsky 		if ((i & (nr_chunks - 1)) != 0) {
144c6557e7fSMartin Schwidefsky 			ib->data[i] = ib->data[i-1] + IDA_BLOCK_SIZE;
145c6557e7fSMartin Schwidefsky 			continue;
146c6557e7fSMartin Schwidefsky 		}
147c6557e7fSMartin Schwidefsky 		ib->data[i] = (void *)
148c6557e7fSMartin Schwidefsky 			__get_free_pages(GFP_KERNEL, page_order);
149c6557e7fSMartin Schwidefsky 		if (ib->data[i] != NULL)
150c6557e7fSMartin Schwidefsky 			continue;
151c6557e7fSMartin Schwidefsky 		// Not enough memory
152c6557e7fSMartin Schwidefsky 		while (i >= nr_chunks) {
153c6557e7fSMartin Schwidefsky 			i -= nr_chunks;
154c6557e7fSMartin Schwidefsky 			free_pages((unsigned long) ib->data[i],
155c6557e7fSMartin Schwidefsky 				   ib->page_order);
156c6557e7fSMartin Schwidefsky 		}
157c6557e7fSMartin Schwidefsky 		kfree(ib);
158c6557e7fSMartin Schwidefsky 		return ERR_PTR(-ENOMEM);
159c6557e7fSMartin Schwidefsky 	}
160c6557e7fSMartin Schwidefsky 	return ib;
161c6557e7fSMartin Schwidefsky }
162c6557e7fSMartin Schwidefsky 
163c6557e7fSMartin Schwidefsky /*
164c6557e7fSMartin Schwidefsky  * Free an idal buffer.
165c6557e7fSMartin Schwidefsky  */
166c6557e7fSMartin Schwidefsky static inline void
idal_buffer_free(struct idal_buffer * ib)167c6557e7fSMartin Schwidefsky idal_buffer_free(struct idal_buffer *ib)
168c6557e7fSMartin Schwidefsky {
169c6557e7fSMartin Schwidefsky 	int nr_chunks, nr_ptrs, i;
170c6557e7fSMartin Schwidefsky 
171c6557e7fSMartin Schwidefsky 	nr_ptrs = (ib->size + IDA_BLOCK_SIZE - 1) >> IDA_SIZE_LOG;
172c6557e7fSMartin Schwidefsky 	nr_chunks = (4096 << ib->page_order) >> IDA_SIZE_LOG;
173c6557e7fSMartin Schwidefsky 	for (i = 0; i < nr_ptrs; i += nr_chunks)
174c6557e7fSMartin Schwidefsky 		free_pages((unsigned long) ib->data[i], ib->page_order);
175c6557e7fSMartin Schwidefsky 	kfree(ib);
176c6557e7fSMartin Schwidefsky }
177c6557e7fSMartin Schwidefsky 
178c6557e7fSMartin Schwidefsky /*
179c6557e7fSMartin Schwidefsky  * Test if a idal list is really needed.
180c6557e7fSMartin Schwidefsky  */
181c6557e7fSMartin Schwidefsky static inline int
__idal_buffer_is_needed(struct idal_buffer * ib)182c6557e7fSMartin Schwidefsky __idal_buffer_is_needed(struct idal_buffer *ib)
183c6557e7fSMartin Schwidefsky {
184c6557e7fSMartin Schwidefsky 	return ib->size > (4096ul << ib->page_order) ||
185c6557e7fSMartin Schwidefsky 		idal_is_needed(ib->data[0], ib->size);
186c6557e7fSMartin Schwidefsky }
187c6557e7fSMartin Schwidefsky 
188c6557e7fSMartin Schwidefsky /*
189c6557e7fSMartin Schwidefsky  * Set channel data address to idal buffer.
190c6557e7fSMartin Schwidefsky  */
191c6557e7fSMartin Schwidefsky static inline void
idal_buffer_set_cda(struct idal_buffer * ib,struct ccw1 * ccw)192c6557e7fSMartin Schwidefsky idal_buffer_set_cda(struct idal_buffer *ib, struct ccw1 *ccw)
193c6557e7fSMartin Schwidefsky {
194c6557e7fSMartin Schwidefsky 	if (__idal_buffer_is_needed(ib)) {
195c6557e7fSMartin Schwidefsky 		// setup idals;
196c6557e7fSMartin Schwidefsky 		ccw->cda = (u32)(addr_t) ib->data;
197c6557e7fSMartin Schwidefsky 		ccw->flags |= CCW_FLAG_IDA;
198c6557e7fSMartin Schwidefsky 	} else
199c6557e7fSMartin Schwidefsky 		// we do not need idals - use direct addressing
200c6557e7fSMartin Schwidefsky 		ccw->cda = (u32)(addr_t) ib->data[0];
201c6557e7fSMartin Schwidefsky 	ccw->count = ib->size;
202c6557e7fSMartin Schwidefsky }
203c6557e7fSMartin Schwidefsky 
204c6557e7fSMartin Schwidefsky /*
205c6557e7fSMartin Schwidefsky  * Copy count bytes from an idal buffer to user memory
206c6557e7fSMartin Schwidefsky  */
207c6557e7fSMartin Schwidefsky static inline size_t
idal_buffer_to_user(struct idal_buffer * ib,void __user * to,size_t count)208c6557e7fSMartin Schwidefsky idal_buffer_to_user(struct idal_buffer *ib, void __user *to, size_t count)
209c6557e7fSMartin Schwidefsky {
210c6557e7fSMartin Schwidefsky 	size_t left;
211c6557e7fSMartin Schwidefsky 	int i;
212c6557e7fSMartin Schwidefsky 
213c6557e7fSMartin Schwidefsky 	BUG_ON(count > ib->size);
214c6557e7fSMartin Schwidefsky 	for (i = 0; count > IDA_BLOCK_SIZE; i++) {
215c6557e7fSMartin Schwidefsky 		left = copy_to_user(to, ib->data[i], IDA_BLOCK_SIZE);
216c6557e7fSMartin Schwidefsky 		if (left)
217c6557e7fSMartin Schwidefsky 			return left + count - IDA_BLOCK_SIZE;
218c6557e7fSMartin Schwidefsky 		to = (void __user *) to + IDA_BLOCK_SIZE;
219c6557e7fSMartin Schwidefsky 		count -= IDA_BLOCK_SIZE;
220c6557e7fSMartin Schwidefsky 	}
221c6557e7fSMartin Schwidefsky 	return copy_to_user(to, ib->data[i], count);
222c6557e7fSMartin Schwidefsky }
223c6557e7fSMartin Schwidefsky 
224c6557e7fSMartin Schwidefsky /*
225c6557e7fSMartin Schwidefsky  * Copy count bytes from user memory to an idal buffer
226c6557e7fSMartin Schwidefsky  */
227c6557e7fSMartin Schwidefsky static inline size_t
idal_buffer_from_user(struct idal_buffer * ib,const void __user * from,size_t count)228c6557e7fSMartin Schwidefsky idal_buffer_from_user(struct idal_buffer *ib, const void __user *from, size_t count)
229c6557e7fSMartin Schwidefsky {
230c6557e7fSMartin Schwidefsky 	size_t left;
231c6557e7fSMartin Schwidefsky 	int i;
232c6557e7fSMartin Schwidefsky 
233c6557e7fSMartin Schwidefsky 	BUG_ON(count > ib->size);
234c6557e7fSMartin Schwidefsky 	for (i = 0; count > IDA_BLOCK_SIZE; i++) {
235c6557e7fSMartin Schwidefsky 		left = copy_from_user(ib->data[i], from, IDA_BLOCK_SIZE);
236c6557e7fSMartin Schwidefsky 		if (left)
237c6557e7fSMartin Schwidefsky 			return left + count - IDA_BLOCK_SIZE;
238c6557e7fSMartin Schwidefsky 		from = (void __user *) from + IDA_BLOCK_SIZE;
239c6557e7fSMartin Schwidefsky 		count -= IDA_BLOCK_SIZE;
240c6557e7fSMartin Schwidefsky 	}
241c6557e7fSMartin Schwidefsky 	return copy_from_user(ib->data[i], from, count);
242c6557e7fSMartin Schwidefsky }
243c6557e7fSMartin Schwidefsky 
244c6557e7fSMartin Schwidefsky #endif
245