xref: /openbmc/linux/drivers/s390/cio/qdio_setup.c (revision 249592bf)
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 static struct kmem_cache *qdio_aob_cache;
28 
29 struct qaob *qdio_allocate_aob(void)
30 {
31 	return kmem_cache_zalloc(qdio_aob_cache, GFP_ATOMIC);
32 }
33 EXPORT_SYMBOL_GPL(qdio_allocate_aob);
34 
35 void qdio_release_aob(struct qaob *aob)
36 {
37 	kmem_cache_free(qdio_aob_cache, aob);
38 }
39 EXPORT_SYMBOL_GPL(qdio_release_aob);
40 
41 /**
42  * qdio_free_buffers() - free qdio buffers
43  * @buf: array of pointers to qdio buffers
44  * @count: number of qdio buffers to free
45  */
46 void qdio_free_buffers(struct qdio_buffer **buf, unsigned int count)
47 {
48 	int pos;
49 
50 	for (pos = 0; pos < count; pos += QBUFF_PER_PAGE)
51 		free_page((unsigned long) buf[pos]);
52 }
53 EXPORT_SYMBOL_GPL(qdio_free_buffers);
54 
55 /**
56  * qdio_alloc_buffers() - allocate qdio buffers
57  * @buf: array of pointers to qdio buffers
58  * @count: number of qdio buffers to allocate
59  */
60 int qdio_alloc_buffers(struct qdio_buffer **buf, unsigned int count)
61 {
62 	int pos;
63 
64 	for (pos = 0; pos < count; pos += QBUFF_PER_PAGE) {
65 		buf[pos] = (void *) get_zeroed_page(GFP_KERNEL);
66 		if (!buf[pos]) {
67 			qdio_free_buffers(buf, count);
68 			return -ENOMEM;
69 		}
70 	}
71 	for (pos = 0; pos < count; pos++)
72 		if (pos % QBUFF_PER_PAGE)
73 			buf[pos] = buf[pos - 1] + 1;
74 	return 0;
75 }
76 EXPORT_SYMBOL_GPL(qdio_alloc_buffers);
77 
78 /**
79  * qdio_reset_buffers() - reset qdio buffers
80  * @buf: array of pointers to qdio buffers
81  * @count: number of qdio buffers that will be zeroed
82  */
83 void qdio_reset_buffers(struct qdio_buffer **buf, unsigned int count)
84 {
85 	int pos;
86 
87 	for (pos = 0; pos < count; pos++)
88 		memset(buf[pos], 0, sizeof(struct qdio_buffer));
89 }
90 EXPORT_SYMBOL_GPL(qdio_reset_buffers);
91 
92 /*
93  * qebsm is only available under 64bit but the adapter sets the feature
94  * flag anyway, so we manually override it.
95  */
96 static inline int qebsm_possible(void)
97 {
98 	return css_general_characteristics.qebsm;
99 }
100 
101 /*
102  * qib_param_field: pointer to 128 bytes or NULL, if no param field
103  * nr_input_qs: pointer to nr_queues*128 words of data or NULL
104  */
105 static void set_impl_params(struct qdio_irq *irq_ptr,
106 			    unsigned int qib_param_field_format,
107 			    unsigned char *qib_param_field,
108 			    unsigned long *input_slib_elements,
109 			    unsigned long *output_slib_elements)
110 {
111 	struct qdio_q *q;
112 	int i, j;
113 
114 	if (!irq_ptr)
115 		return;
116 
117 	irq_ptr->qib.pfmt = qib_param_field_format;
118 	if (qib_param_field)
119 		memcpy(irq_ptr->qib.parm, qib_param_field,
120 		       sizeof(irq_ptr->qib.parm));
121 
122 	if (!input_slib_elements)
123 		goto output;
124 
125 	for_each_input_queue(irq_ptr, q, i) {
126 		for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
127 			q->slib->slibe[j].parms =
128 				input_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
129 	}
130 output:
131 	if (!output_slib_elements)
132 		return;
133 
134 	for_each_output_queue(irq_ptr, q, i) {
135 		for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
136 			q->slib->slibe[j].parms =
137 				output_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
138 	}
139 }
140 
141 static void __qdio_free_queues(struct qdio_q **queues, unsigned int count)
142 {
143 	struct qdio_q *q;
144 	unsigned int i;
145 
146 	for (i = 0; i < count; i++) {
147 		q = queues[i];
148 		free_page((unsigned long) q->slib);
149 		kmem_cache_free(qdio_q_cache, q);
150 	}
151 }
152 
153 void qdio_free_queues(struct qdio_irq *irq_ptr)
154 {
155 	__qdio_free_queues(irq_ptr->input_qs, irq_ptr->max_input_qs);
156 	irq_ptr->max_input_qs = 0;
157 
158 	__qdio_free_queues(irq_ptr->output_qs, irq_ptr->max_output_qs);
159 	irq_ptr->max_output_qs = 0;
160 }
161 
162 static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues)
163 {
164 	struct qdio_q *q;
165 	int i;
166 
167 	for (i = 0; i < nr_queues; i++) {
168 		q = kmem_cache_zalloc(qdio_q_cache, GFP_KERNEL);
169 		if (!q) {
170 			__qdio_free_queues(irq_ptr_qs, i);
171 			return -ENOMEM;
172 		}
173 
174 		q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
175 		if (!q->slib) {
176 			kmem_cache_free(qdio_q_cache, q);
177 			__qdio_free_queues(irq_ptr_qs, i);
178 			return -ENOMEM;
179 		}
180 		irq_ptr_qs[i] = q;
181 	}
182 	return 0;
183 }
184 
185 int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs)
186 {
187 	int rc;
188 
189 	rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs);
190 	if (rc)
191 		return rc;
192 
193 	rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs);
194 	if (rc) {
195 		__qdio_free_queues(irq_ptr->input_qs, nr_input_qs);
196 		return rc;
197 	}
198 
199 	irq_ptr->max_input_qs = nr_input_qs;
200 	irq_ptr->max_output_qs = nr_output_qs;
201 	return 0;
202 }
203 
204 static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr,
205 			      qdio_handler_t *handler, int i)
206 {
207 	struct slib *slib = q->slib;
208 
209 	/* queue must be cleared for qdio_establish */
210 	memset(q, 0, sizeof(*q));
211 	memset(slib, 0, PAGE_SIZE);
212 	q->slib = slib;
213 	q->irq_ptr = irq_ptr;
214 	q->mask = 1 << (31 - i);
215 	q->nr = i;
216 	q->handler = handler;
217 }
218 
219 static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr,
220 				struct qdio_buffer **sbals_array, int i)
221 {
222 	struct qdio_q *prev;
223 	int j;
224 
225 	DBF_HEX(&q, sizeof(void *));
226 	q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2);
227 
228 	/* fill in sbal */
229 	for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
230 		q->sbal[j] = *sbals_array++;
231 
232 	/* fill in slib */
233 	if (i > 0) {
234 		prev = (q->is_input_q) ? irq_ptr->input_qs[i - 1]
235 			: irq_ptr->output_qs[i - 1];
236 		prev->slib->nsliba = (unsigned long)q->slib;
237 	}
238 
239 	q->slib->sla = (unsigned long)q->sl;
240 	q->slib->slsba = (unsigned long)&q->slsb.val[0];
241 
242 	/* fill in sl */
243 	for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
244 		q->sl->element[j].sbal = virt_to_phys(q->sbal[j]);
245 }
246 
247 static void setup_queues(struct qdio_irq *irq_ptr,
248 			 struct qdio_initialize *qdio_init)
249 {
250 	struct qdio_q *q;
251 	int i;
252 
253 	for_each_input_queue(irq_ptr, q, i) {
254 		DBF_EVENT("inq:%1d", i);
255 		setup_queues_misc(q, irq_ptr, qdio_init->input_handler, i);
256 
257 		q->is_input_q = 1;
258 
259 		setup_storage_lists(q, irq_ptr,
260 				    qdio_init->input_sbal_addr_array[i], i);
261 	}
262 
263 	for_each_output_queue(irq_ptr, q, i) {
264 		DBF_EVENT("outq:%1d", i);
265 		setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i);
266 
267 		q->is_input_q = 0;
268 		setup_storage_lists(q, irq_ptr,
269 				    qdio_init->output_sbal_addr_array[i], i);
270 
271 		tasklet_setup(&q->u.out.tasklet, qdio_outbound_tasklet);
272 		timer_setup(&q->u.out.timer, qdio_outbound_timer, 0);
273 	}
274 }
275 
276 static void process_ac_flags(struct qdio_irq *irq_ptr, unsigned char qdioac)
277 {
278 	if (qdioac & AC1_SIGA_INPUT_NEEDED)
279 		irq_ptr->siga_flag.input = 1;
280 	if (qdioac & AC1_SIGA_OUTPUT_NEEDED)
281 		irq_ptr->siga_flag.output = 1;
282 	if (qdioac & AC1_SIGA_SYNC_NEEDED)
283 		irq_ptr->siga_flag.sync = 1;
284 	if (!(qdioac & AC1_AUTOMATIC_SYNC_ON_THININT))
285 		irq_ptr->siga_flag.sync_after_ai = 1;
286 	if (!(qdioac & AC1_AUTOMATIC_SYNC_ON_OUT_PCI))
287 		irq_ptr->siga_flag.sync_out_after_pci = 1;
288 }
289 
290 static void check_and_setup_qebsm(struct qdio_irq *irq_ptr,
291 				  unsigned char qdioac, unsigned long token)
292 {
293 	if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM))
294 		goto no_qebsm;
295 	if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) ||
296 	    (!(qdioac & AC1_SC_QEBSM_ENABLED)))
297 		goto no_qebsm;
298 
299 	irq_ptr->sch_token = token;
300 
301 	DBF_EVENT("V=V:1");
302 	DBF_EVENT("%8lx", irq_ptr->sch_token);
303 	return;
304 
305 no_qebsm:
306 	irq_ptr->sch_token = 0;
307 	irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
308 	DBF_EVENT("noV=V");
309 }
310 
311 /*
312  * If there is a qdio_irq we use the chsc_page and store the information
313  * in the qdio_irq, otherwise we copy it to the specified structure.
314  */
315 int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr,
316 			struct subchannel_id *schid,
317 			struct qdio_ssqd_desc *data)
318 {
319 	struct chsc_ssqd_area *ssqd;
320 	int rc;
321 
322 	DBF_EVENT("getssqd:%4x", schid->sch_no);
323 	if (!irq_ptr) {
324 		ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL);
325 		if (!ssqd)
326 			return -ENOMEM;
327 	} else {
328 		ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page;
329 	}
330 
331 	rc = chsc_ssqd(*schid, ssqd);
332 	if (rc)
333 		goto out;
334 
335 	if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) ||
336 	    !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) ||
337 	    (ssqd->qdio_ssqd.sch != schid->sch_no))
338 		rc = -EINVAL;
339 
340 	if (!rc)
341 		memcpy(data, &ssqd->qdio_ssqd, sizeof(*data));
342 
343 out:
344 	if (!irq_ptr)
345 		free_page((unsigned long)ssqd);
346 
347 	return rc;
348 }
349 
350 void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr)
351 {
352 	unsigned char qdioac;
353 	int rc;
354 
355 	rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, &irq_ptr->ssqd_desc);
356 	if (rc) {
357 		DBF_ERROR("%4x ssqd ERR", irq_ptr->schid.sch_no);
358 		DBF_ERROR("rc:%x", rc);
359 		/* all flags set, worst case */
360 		qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED |
361 			 AC1_SIGA_SYNC_NEEDED;
362 	} else
363 		qdioac = irq_ptr->ssqd_desc.qdioac1;
364 
365 	check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token);
366 	process_ac_flags(irq_ptr, qdioac);
367 	DBF_EVENT("ac 1:%2x 2:%4x", qdioac, irq_ptr->ssqd_desc.qdioac2);
368 	DBF_EVENT("3:%4x qib:%4x", irq_ptr->ssqd_desc.qdioac3, irq_ptr->qib.ac);
369 }
370 
371 static void qdio_fill_qdr_desc(struct qdesfmt0 *desc, struct qdio_q *queue)
372 {
373 	desc->sliba = virt_to_phys(queue->slib);
374 	desc->sla = virt_to_phys(queue->sl);
375 	desc->slsba = virt_to_phys(&queue->slsb);
376 
377 	desc->akey = PAGE_DEFAULT_KEY >> 4;
378 	desc->bkey = PAGE_DEFAULT_KEY >> 4;
379 	desc->ckey = PAGE_DEFAULT_KEY >> 4;
380 	desc->dkey = PAGE_DEFAULT_KEY >> 4;
381 }
382 
383 static void setup_qdr(struct qdio_irq *irq_ptr,
384 		      struct qdio_initialize *qdio_init)
385 {
386 	struct qdesfmt0 *desc = &irq_ptr->qdr->qdf0[0];
387 	int i;
388 
389 	irq_ptr->qdr->qfmt = qdio_init->q_format;
390 	irq_ptr->qdr->ac = qdio_init->qdr_ac;
391 	irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs;
392 	irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs;
393 	irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4; /* size in words */
394 	irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4;
395 	irq_ptr->qdr->qiba = virt_to_phys(&irq_ptr->qib);
396 	irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY >> 4;
397 
398 	for (i = 0; i < qdio_init->no_input_qs; i++)
399 		qdio_fill_qdr_desc(desc++, irq_ptr->input_qs[i]);
400 
401 	for (i = 0; i < qdio_init->no_output_qs; i++)
402 		qdio_fill_qdr_desc(desc++, irq_ptr->output_qs[i]);
403 }
404 
405 static void setup_qib(struct qdio_irq *irq_ptr,
406 		      struct qdio_initialize *init_data)
407 {
408 	if (qebsm_possible())
409 		irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
410 
411 	irq_ptr->qib.rflags |= init_data->qib_rflags;
412 
413 	irq_ptr->qib.qfmt = init_data->q_format;
414 	if (init_data->no_input_qs)
415 		irq_ptr->qib.isliba =
416 			(unsigned long)(irq_ptr->input_qs[0]->slib);
417 	if (init_data->no_output_qs)
418 		irq_ptr->qib.osliba =
419 			(unsigned long)(irq_ptr->output_qs[0]->slib);
420 	memcpy(irq_ptr->qib.ebcnam, dev_name(&irq_ptr->cdev->dev), 8);
421 	ASCEBC(irq_ptr->qib.ebcnam, 8);
422 }
423 
424 int qdio_setup_irq(struct qdio_irq *irq_ptr, struct qdio_initialize *init_data)
425 {
426 	struct ccw_device *cdev = irq_ptr->cdev;
427 	struct ciw *ciw;
428 
429 	memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib));
430 	memset(&irq_ptr->siga_flag, 0, sizeof(irq_ptr->siga_flag));
431 	memset(&irq_ptr->ccw, 0, sizeof(irq_ptr->ccw));
432 	memset(&irq_ptr->ssqd_desc, 0, sizeof(irq_ptr->ssqd_desc));
433 	memset(&irq_ptr->perf_stat, 0, sizeof(irq_ptr->perf_stat));
434 
435 	irq_ptr->debugfs_dev = NULL;
436 	irq_ptr->sch_token = irq_ptr->perf_stat_enabled = 0;
437 	irq_ptr->state = QDIO_IRQ_STATE_INACTIVE;
438 
439 	/* wipes qib.ac, required by ar7063 */
440 	memset(irq_ptr->qdr, 0, sizeof(struct qdr));
441 
442 	irq_ptr->int_parm = init_data->int_parm;
443 	irq_ptr->nr_input_qs = init_data->no_input_qs;
444 	irq_ptr->nr_output_qs = init_data->no_output_qs;
445 	irq_ptr->scan_threshold = init_data->scan_threshold;
446 	ccw_device_get_schid(cdev, &irq_ptr->schid);
447 	setup_queues(irq_ptr, init_data);
448 
449 	irq_ptr->irq_poll = init_data->irq_poll;
450 	set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
451 
452 	setup_qib(irq_ptr, init_data);
453 	set_impl_params(irq_ptr, init_data->qib_param_field_format,
454 			init_data->qib_param_field,
455 			init_data->input_slib_elements,
456 			init_data->output_slib_elements);
457 
458 	/* fill input and output descriptors */
459 	setup_qdr(irq_ptr, init_data);
460 
461 	/* qdr, qib, sls, slsbs, slibs, sbales are filled now */
462 
463 	/* set our IRQ handler */
464 	spin_lock_irq(get_ccwdev_lock(cdev));
465 	irq_ptr->orig_handler = cdev->handler;
466 	cdev->handler = qdio_int_handler;
467 	spin_unlock_irq(get_ccwdev_lock(cdev));
468 
469 	/* get qdio commands */
470 	ciw = ccw_device_get_ciw(cdev, CIW_TYPE_EQUEUE);
471 	if (!ciw) {
472 		DBF_ERROR("%4x NO EQ", irq_ptr->schid.sch_no);
473 		return -EINVAL;
474 	}
475 	irq_ptr->equeue = *ciw;
476 
477 	ciw = ccw_device_get_ciw(cdev, CIW_TYPE_AQUEUE);
478 	if (!ciw) {
479 		DBF_ERROR("%4x NO AQ", irq_ptr->schid.sch_no);
480 		return -EINVAL;
481 	}
482 	irq_ptr->aqueue = *ciw;
483 
484 	return 0;
485 }
486 
487 void qdio_shutdown_irq(struct qdio_irq *irq)
488 {
489 	struct ccw_device *cdev = irq->cdev;
490 
491 	/* restore IRQ handler */
492 	spin_lock_irq(get_ccwdev_lock(cdev));
493 	cdev->handler = irq->orig_handler;
494 	cdev->private->intparm = 0;
495 	spin_unlock_irq(get_ccwdev_lock(cdev));
496 }
497 
498 void qdio_print_subchannel_info(struct qdio_irq *irq_ptr)
499 {
500 	char s[80];
501 
502 	snprintf(s, 80, "qdio: %s %s on SC %x using "
503 		 "AI:%d QEBSM:%d PRI:%d TDD:%d SIGA:%s%s%s%s%s\n",
504 		 dev_name(&irq_ptr->cdev->dev),
505 		 (irq_ptr->qib.qfmt == QDIO_QETH_QFMT) ? "OSA" :
506 			((irq_ptr->qib.qfmt == QDIO_ZFCP_QFMT) ? "ZFCP" : "HS"),
507 		 irq_ptr->schid.sch_no,
508 		 is_thinint_irq(irq_ptr),
509 		 (irq_ptr->sch_token) ? 1 : 0,
510 		 pci_out_supported(irq_ptr) ? 1 : 0,
511 		 css_general_characteristics.aif_tdd,
512 		 (irq_ptr->siga_flag.input) ? "R" : " ",
513 		 (irq_ptr->siga_flag.output) ? "W" : " ",
514 		 (irq_ptr->siga_flag.sync) ? "S" : " ",
515 		 (irq_ptr->siga_flag.sync_after_ai) ? "A" : " ",
516 		 (irq_ptr->siga_flag.sync_out_after_pci) ? "P" : " ");
517 	printk(KERN_INFO "%s", s);
518 }
519 
520 int __init qdio_setup_init(void)
521 {
522 	int rc;
523 
524 	qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
525 					 256, 0, NULL);
526 	if (!qdio_q_cache)
527 		return -ENOMEM;
528 
529 	qdio_aob_cache = kmem_cache_create("qdio_aob",
530 					sizeof(struct qaob),
531 					sizeof(struct qaob),
532 					0,
533 					NULL);
534 	if (!qdio_aob_cache) {
535 		rc = -ENOMEM;
536 		goto free_qdio_q_cache;
537 	}
538 
539 	/* Check for OSA/FCP thin interrupts (bit 67). */
540 	DBF_EVENT("thinint:%1d",
541 		  (css_general_characteristics.aif_osa) ? 1 : 0);
542 
543 	/* Check for QEBSM support in general (bit 58). */
544 	DBF_EVENT("cssQEBSM:%1d", (qebsm_possible()) ? 1 : 0);
545 	rc = 0;
546 out:
547 	return rc;
548 free_qdio_q_cache:
549 	kmem_cache_destroy(qdio_q_cache);
550 	goto out;
551 }
552 
553 void qdio_setup_exit(void)
554 {
555 	kmem_cache_destroy(qdio_aob_cache);
556 	kmem_cache_destroy(qdio_q_cache);
557 }
558