1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * qdio queue initialization
4  *
5  * Copyright IBM Corp. 2008
6  * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
7  */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <linux/io.h>
12 
13 #include <asm/ebcdic.h>
14 #include <asm/qdio.h>
15 
16 #include "cio.h"
17 #include "css.h"
18 #include "device.h"
19 #include "ioasm.h"
20 #include "chsc.h"
21 #include "qdio.h"
22 #include "qdio_debug.h"
23 
24 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
25 
26 static struct kmem_cache *qdio_q_cache;
27 
28 /**
29  * qdio_free_buffers() - free qdio buffers
30  * @buf: array of pointers to qdio buffers
31  * @count: number of qdio buffers to free
32  */
qdio_free_buffers(struct qdio_buffer ** buf,unsigned int count)33 void qdio_free_buffers(struct qdio_buffer **buf, unsigned int count)
34 {
35 	int pos;
36 
37 	for (pos = 0; pos < count; pos += QBUFF_PER_PAGE)
38 		free_page((unsigned long) buf[pos]);
39 }
40 EXPORT_SYMBOL_GPL(qdio_free_buffers);
41 
42 /**
43  * qdio_alloc_buffers() - allocate qdio buffers
44  * @buf: array of pointers to qdio buffers
45  * @count: number of qdio buffers to allocate
46  */
qdio_alloc_buffers(struct qdio_buffer ** buf,unsigned int count)47 int qdio_alloc_buffers(struct qdio_buffer **buf, unsigned int count)
48 {
49 	int pos;
50 
51 	for (pos = 0; pos < count; pos += QBUFF_PER_PAGE) {
52 		buf[pos] = (void *) get_zeroed_page(GFP_KERNEL);
53 		if (!buf[pos]) {
54 			qdio_free_buffers(buf, count);
55 			return -ENOMEM;
56 		}
57 	}
58 	for (pos = 0; pos < count; pos++)
59 		if (pos % QBUFF_PER_PAGE)
60 			buf[pos] = buf[pos - 1] + 1;
61 	return 0;
62 }
63 EXPORT_SYMBOL_GPL(qdio_alloc_buffers);
64 
65 /**
66  * qdio_reset_buffers() - reset qdio buffers
67  * @buf: array of pointers to qdio buffers
68  * @count: number of qdio buffers that will be zeroed
69  */
qdio_reset_buffers(struct qdio_buffer ** buf,unsigned int count)70 void qdio_reset_buffers(struct qdio_buffer **buf, unsigned int count)
71 {
72 	int pos;
73 
74 	for (pos = 0; pos < count; pos++)
75 		memset(buf[pos], 0, sizeof(struct qdio_buffer));
76 }
77 EXPORT_SYMBOL_GPL(qdio_reset_buffers);
78 
__qdio_free_queues(struct qdio_q ** queues,unsigned int count)79 static void __qdio_free_queues(struct qdio_q **queues, unsigned int count)
80 {
81 	struct qdio_q *q;
82 	unsigned int i;
83 
84 	for (i = 0; i < count; i++) {
85 		q = queues[i];
86 		free_page((unsigned long) q->slib);
87 		kmem_cache_free(qdio_q_cache, q);
88 	}
89 }
90 
qdio_free_queues(struct qdio_irq * irq_ptr)91 void qdio_free_queues(struct qdio_irq *irq_ptr)
92 {
93 	__qdio_free_queues(irq_ptr->input_qs, irq_ptr->max_input_qs);
94 	irq_ptr->max_input_qs = 0;
95 
96 	__qdio_free_queues(irq_ptr->output_qs, irq_ptr->max_output_qs);
97 	irq_ptr->max_output_qs = 0;
98 }
99 
__qdio_allocate_qs(struct qdio_q ** irq_ptr_qs,int nr_queues)100 static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues)
101 {
102 	struct qdio_q *q;
103 	int i;
104 
105 	for (i = 0; i < nr_queues; i++) {
106 		q = kmem_cache_zalloc(qdio_q_cache, GFP_KERNEL);
107 		if (!q) {
108 			__qdio_free_queues(irq_ptr_qs, i);
109 			return -ENOMEM;
110 		}
111 
112 		q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
113 		if (!q->slib) {
114 			kmem_cache_free(qdio_q_cache, q);
115 			__qdio_free_queues(irq_ptr_qs, i);
116 			return -ENOMEM;
117 		}
118 		irq_ptr_qs[i] = q;
119 	}
120 	return 0;
121 }
122 
qdio_allocate_qs(struct qdio_irq * irq_ptr,int nr_input_qs,int nr_output_qs)123 int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs)
124 {
125 	int rc;
126 
127 	rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs);
128 	if (rc)
129 		return rc;
130 
131 	rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs);
132 	if (rc) {
133 		__qdio_free_queues(irq_ptr->input_qs, nr_input_qs);
134 		return rc;
135 	}
136 
137 	irq_ptr->max_input_qs = nr_input_qs;
138 	irq_ptr->max_output_qs = nr_output_qs;
139 	return 0;
140 }
141 
setup_queues_misc(struct qdio_q * q,struct qdio_irq * irq_ptr,qdio_handler_t * handler,int i)142 static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr,
143 			      qdio_handler_t *handler, int i)
144 {
145 	struct slib *slib = q->slib;
146 
147 	/* queue must be cleared for qdio_establish */
148 	memset(q, 0, sizeof(*q));
149 	memset(slib, 0, PAGE_SIZE);
150 	q->slib = slib;
151 	q->irq_ptr = irq_ptr;
152 	q->mask = 1 << (31 - i);
153 	q->nr = i;
154 	q->handler = handler;
155 }
156 
setup_storage_lists(struct qdio_q * q,struct qdio_irq * irq_ptr,struct qdio_buffer ** sbals_array,int i)157 static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr,
158 				struct qdio_buffer **sbals_array, int i)
159 {
160 	struct qdio_q *prev;
161 	int j;
162 
163 	DBF_HEX(&q, sizeof(void *));
164 	q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2);
165 
166 	/* fill in sbal */
167 	for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
168 		q->sbal[j] = *sbals_array++;
169 
170 	/* fill in slib */
171 	if (i > 0) {
172 		prev = (q->is_input_q) ? irq_ptr->input_qs[i - 1]
173 			: irq_ptr->output_qs[i - 1];
174 		prev->slib->nsliba = (unsigned long)q->slib;
175 	}
176 
177 	q->slib->sla = (unsigned long)q->sl;
178 	q->slib->slsba = (unsigned long)&q->slsb.val[0];
179 
180 	/* fill in sl */
181 	for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
182 		q->sl->element[j].sbal = virt_to_phys(q->sbal[j]);
183 }
184 
setup_queues(struct qdio_irq * irq_ptr,struct qdio_initialize * qdio_init)185 static void setup_queues(struct qdio_irq *irq_ptr,
186 			 struct qdio_initialize *qdio_init)
187 {
188 	struct qdio_q *q;
189 	int i;
190 
191 	for_each_input_queue(irq_ptr, q, i) {
192 		DBF_EVENT("inq:%1d", i);
193 		setup_queues_misc(q, irq_ptr, qdio_init->input_handler, i);
194 
195 		q->is_input_q = 1;
196 
197 		setup_storage_lists(q, irq_ptr,
198 				    qdio_init->input_sbal_addr_array[i], i);
199 	}
200 
201 	for_each_output_queue(irq_ptr, q, i) {
202 		DBF_EVENT("outq:%1d", i);
203 		setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i);
204 
205 		q->is_input_q = 0;
206 		setup_storage_lists(q, irq_ptr,
207 				    qdio_init->output_sbal_addr_array[i], i);
208 	}
209 }
210 
check_and_setup_qebsm(struct qdio_irq * irq_ptr,unsigned char qdioac,unsigned long token)211 static void check_and_setup_qebsm(struct qdio_irq *irq_ptr,
212 				  unsigned char qdioac, unsigned long token)
213 {
214 	if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM))
215 		goto no_qebsm;
216 	if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) ||
217 	    (!(qdioac & AC1_SC_QEBSM_ENABLED)))
218 		goto no_qebsm;
219 
220 	irq_ptr->sch_token = token;
221 
222 	DBF_EVENT("V=V:1");
223 	DBF_EVENT("%8lx", irq_ptr->sch_token);
224 	return;
225 
226 no_qebsm:
227 	irq_ptr->sch_token = 0;
228 	irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
229 	DBF_EVENT("noV=V");
230 }
231 
232 /*
233  * If there is a qdio_irq we use the chsc_page and store the information
234  * in the qdio_irq, otherwise we copy it to the specified structure.
235  */
qdio_setup_get_ssqd(struct qdio_irq * irq_ptr,struct subchannel_id * schid,struct qdio_ssqd_desc * data)236 int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr,
237 			struct subchannel_id *schid,
238 			struct qdio_ssqd_desc *data)
239 {
240 	struct chsc_ssqd_area *ssqd;
241 	int rc;
242 
243 	DBF_EVENT("getssqd:%4x", schid->sch_no);
244 	if (!irq_ptr) {
245 		ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL);
246 		if (!ssqd)
247 			return -ENOMEM;
248 	} else {
249 		ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page;
250 	}
251 
252 	rc = chsc_ssqd(*schid, ssqd);
253 	if (rc)
254 		goto out;
255 
256 	if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) ||
257 	    !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) ||
258 	    (ssqd->qdio_ssqd.sch != schid->sch_no))
259 		rc = -EINVAL;
260 
261 	if (!rc)
262 		memcpy(data, &ssqd->qdio_ssqd, sizeof(*data));
263 
264 out:
265 	if (!irq_ptr)
266 		free_page((unsigned long)ssqd);
267 
268 	return rc;
269 }
270 
qdio_setup_ssqd_info(struct qdio_irq * irq_ptr)271 void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr)
272 {
273 	unsigned char qdioac;
274 	int rc;
275 
276 	rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, &irq_ptr->ssqd_desc);
277 	if (rc) {
278 		DBF_ERROR("%4x ssqd ERR", irq_ptr->schid.sch_no);
279 		DBF_ERROR("rc:%x", rc);
280 		/* all flags set, worst case */
281 		qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED |
282 			 AC1_SIGA_SYNC_NEEDED;
283 	} else
284 		qdioac = irq_ptr->ssqd_desc.qdioac1;
285 
286 	check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token);
287 	irq_ptr->qdioac1 = qdioac;
288 	DBF_EVENT("ac 1:%2x 2:%4x", qdioac, irq_ptr->ssqd_desc.qdioac2);
289 	DBF_EVENT("3:%4x qib:%4x", irq_ptr->ssqd_desc.qdioac3, irq_ptr->qib.ac);
290 }
291 
qdio_fill_qdr_desc(struct qdesfmt0 * desc,struct qdio_q * queue)292 static void qdio_fill_qdr_desc(struct qdesfmt0 *desc, struct qdio_q *queue)
293 {
294 	desc->sliba = virt_to_phys(queue->slib);
295 	desc->sla = virt_to_phys(queue->sl);
296 	desc->slsba = virt_to_phys(&queue->slsb);
297 
298 	desc->akey = PAGE_DEFAULT_KEY >> 4;
299 	desc->bkey = PAGE_DEFAULT_KEY >> 4;
300 	desc->ckey = PAGE_DEFAULT_KEY >> 4;
301 	desc->dkey = PAGE_DEFAULT_KEY >> 4;
302 }
303 
setup_qdr(struct qdio_irq * irq_ptr,struct qdio_initialize * qdio_init)304 static void setup_qdr(struct qdio_irq *irq_ptr,
305 		      struct qdio_initialize *qdio_init)
306 {
307 	struct qdesfmt0 *desc = &irq_ptr->qdr->qdf0[0];
308 	int i;
309 
310 	memset(irq_ptr->qdr, 0, sizeof(struct qdr));
311 
312 	irq_ptr->qdr->qfmt = qdio_init->q_format;
313 	irq_ptr->qdr->ac = qdio_init->qdr_ac;
314 	irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs;
315 	irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs;
316 	irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4; /* size in words */
317 	irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4;
318 	irq_ptr->qdr->qiba = virt_to_phys(&irq_ptr->qib);
319 	irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY >> 4;
320 
321 	for (i = 0; i < qdio_init->no_input_qs; i++)
322 		qdio_fill_qdr_desc(desc++, irq_ptr->input_qs[i]);
323 
324 	for (i = 0; i < qdio_init->no_output_qs; i++)
325 		qdio_fill_qdr_desc(desc++, irq_ptr->output_qs[i]);
326 }
327 
setup_qib(struct qdio_irq * irq_ptr,struct qdio_initialize * init_data)328 static void setup_qib(struct qdio_irq *irq_ptr,
329 		      struct qdio_initialize *init_data)
330 {
331 	memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib));
332 
333 	irq_ptr->qib.qfmt = init_data->q_format;
334 	irq_ptr->qib.pfmt = init_data->qib_param_field_format;
335 
336 	irq_ptr->qib.rflags = init_data->qib_rflags;
337 	if (css_general_characteristics.qebsm)
338 		irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
339 
340 	if (init_data->no_input_qs)
341 		irq_ptr->qib.isliba =
342 			(unsigned long)(irq_ptr->input_qs[0]->slib);
343 	if (init_data->no_output_qs)
344 		irq_ptr->qib.osliba =
345 			(unsigned long)(irq_ptr->output_qs[0]->slib);
346 	memcpy(irq_ptr->qib.ebcnam, dev_name(&irq_ptr->cdev->dev), 8);
347 	ASCEBC(irq_ptr->qib.ebcnam, 8);
348 
349 	if (init_data->qib_param_field)
350 		memcpy(irq_ptr->qib.parm, init_data->qib_param_field,
351 		       sizeof(irq_ptr->qib.parm));
352 }
353 
qdio_setup_irq(struct qdio_irq * irq_ptr,struct qdio_initialize * init_data)354 int qdio_setup_irq(struct qdio_irq *irq_ptr, struct qdio_initialize *init_data)
355 {
356 	struct ccw_device *cdev = irq_ptr->cdev;
357 	struct ciw *ciw;
358 
359 	irq_ptr->qdioac1 = 0;
360 	memset(&irq_ptr->ccw, 0, sizeof(irq_ptr->ccw));
361 	memset(&irq_ptr->ssqd_desc, 0, sizeof(irq_ptr->ssqd_desc));
362 	memset(&irq_ptr->perf_stat, 0, sizeof(irq_ptr->perf_stat));
363 
364 	irq_ptr->debugfs_dev = NULL;
365 	irq_ptr->sch_token = irq_ptr->perf_stat_enabled = 0;
366 	irq_ptr->state = QDIO_IRQ_STATE_INACTIVE;
367 
368 	irq_ptr->int_parm = init_data->int_parm;
369 	irq_ptr->nr_input_qs = init_data->no_input_qs;
370 	irq_ptr->nr_output_qs = init_data->no_output_qs;
371 	ccw_device_get_schid(cdev, &irq_ptr->schid);
372 	setup_queues(irq_ptr, init_data);
373 
374 	irq_ptr->irq_poll = init_data->irq_poll;
375 	set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
376 
377 	setup_qib(irq_ptr, init_data);
378 
379 	/* fill input and output descriptors */
380 	setup_qdr(irq_ptr, init_data);
381 
382 	/* qdr, qib, sls, slsbs, slibs, sbales are filled now */
383 
384 	/* set our IRQ handler */
385 	spin_lock_irq(get_ccwdev_lock(cdev));
386 	irq_ptr->orig_handler = cdev->handler;
387 	cdev->handler = qdio_int_handler;
388 	spin_unlock_irq(get_ccwdev_lock(cdev));
389 
390 	/* get qdio commands */
391 	ciw = ccw_device_get_ciw(cdev, CIW_TYPE_EQUEUE);
392 	if (!ciw) {
393 		DBF_ERROR("%4x NO EQ", irq_ptr->schid.sch_no);
394 		return -EINVAL;
395 	}
396 	irq_ptr->equeue = *ciw;
397 
398 	ciw = ccw_device_get_ciw(cdev, CIW_TYPE_AQUEUE);
399 	if (!ciw) {
400 		DBF_ERROR("%4x NO AQ", irq_ptr->schid.sch_no);
401 		return -EINVAL;
402 	}
403 	irq_ptr->aqueue = *ciw;
404 
405 	return 0;
406 }
407 
qdio_shutdown_irq(struct qdio_irq * irq)408 void qdio_shutdown_irq(struct qdio_irq *irq)
409 {
410 	struct ccw_device *cdev = irq->cdev;
411 
412 	/* restore IRQ handler */
413 	spin_lock_irq(get_ccwdev_lock(cdev));
414 	cdev->handler = irq->orig_handler;
415 	cdev->private->intparm = 0;
416 	spin_unlock_irq(get_ccwdev_lock(cdev));
417 }
418 
qdio_print_subchannel_info(struct qdio_irq * irq_ptr)419 void qdio_print_subchannel_info(struct qdio_irq *irq_ptr)
420 {
421 	dev_info(&irq_ptr->cdev->dev,
422 		 "qdio: %s on SC %x using AI:%d QEBSM:%d PRI:%d TDD:%d SIGA:%s%s%s\n",
423 		 (irq_ptr->qib.qfmt == QDIO_QETH_QFMT) ? "OSA" :
424 			((irq_ptr->qib.qfmt == QDIO_ZFCP_QFMT) ? "ZFCP" : "HS"),
425 		 irq_ptr->schid.sch_no,
426 		 is_thinint_irq(irq_ptr),
427 		 (irq_ptr->sch_token) ? 1 : 0,
428 		 pci_out_supported(irq_ptr) ? 1 : 0,
429 		 css_general_characteristics.aif_tdd,
430 		 qdio_need_siga_in(irq_ptr) ? "R" : " ",
431 		 qdio_need_siga_out(irq_ptr) ? "W" : " ",
432 		 qdio_need_siga_sync(irq_ptr) ? "S" : " ");
433 }
434 
qdio_setup_init(void)435 int __init qdio_setup_init(void)
436 {
437 	qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
438 					 256, 0, NULL);
439 	if (!qdio_q_cache)
440 		return -ENOMEM;
441 
442 	/* Check for OSA/FCP thin interrupts (bit 67). */
443 	DBF_EVENT("thinint:%1d",
444 		  (css_general_characteristics.aif_osa) ? 1 : 0);
445 
446 	/* Check for QEBSM support in general (bit 58). */
447 	DBF_EVENT("cssQEBSM:%1d", css_general_characteristics.qebsm);
448 
449 	return 0;
450 }
451 
qdio_setup_exit(void)452 void qdio_setup_exit(void)
453 {
454 	kmem_cache_destroy(qdio_q_cache);
455 }
456