1 // SPDX-License-Identifier: GPL-2.0
3 * qdio queue initialization
5 * Copyright IBM Corp. 2008
6 * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
13 #include <asm/ebcdic.h>
22 #include "qdio_debug.h"
24 #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
26 static struct kmem_cache *qdio_q_cache;
29 * qdio_free_buffers() - free qdio buffers
30 * @buf: array of pointers to qdio buffers
31 * @count: number of qdio buffers to free
33 void qdio_free_buffers(struct qdio_buffer **buf, unsigned int count)
37 for (pos = 0; pos < count; pos += QBUFF_PER_PAGE)
38 free_page((unsigned long) buf[pos]);
40 EXPORT_SYMBOL_GPL(qdio_free_buffers);
43 * qdio_alloc_buffers() - allocate qdio buffers
44 * @buf: array of pointers to qdio buffers
45 * @count: number of qdio buffers to allocate
47 int qdio_alloc_buffers(struct qdio_buffer **buf, unsigned int count)
51 for (pos = 0; pos < count; pos += QBUFF_PER_PAGE) {
52 buf[pos] = (void *) get_zeroed_page(GFP_KERNEL);
54 qdio_free_buffers(buf, count);
58 for (pos = 0; pos < count; pos++)
59 if (pos % QBUFF_PER_PAGE)
60 buf[pos] = buf[pos - 1] + 1;
63 EXPORT_SYMBOL_GPL(qdio_alloc_buffers);
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
70 void qdio_reset_buffers(struct qdio_buffer **buf, unsigned int count)
74 for (pos = 0; pos < count; pos++)
75 memset(buf[pos], 0, sizeof(struct qdio_buffer));
77 EXPORT_SYMBOL_GPL(qdio_reset_buffers);
79 static void __qdio_free_queues(struct qdio_q **queues, unsigned int count)
84 for (i = 0; i < count; i++) {
86 free_page((unsigned long) q->slib);
87 kmem_cache_free(qdio_q_cache, q);
91 void qdio_free_queues(struct qdio_irq *irq_ptr)
93 __qdio_free_queues(irq_ptr->input_qs, irq_ptr->max_input_qs);
94 irq_ptr->max_input_qs = 0;
96 __qdio_free_queues(irq_ptr->output_qs, irq_ptr->max_output_qs);
97 irq_ptr->max_output_qs = 0;
100 static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues)
105 for (i = 0; i < nr_queues; i++) {
106 q = kmem_cache_zalloc(qdio_q_cache, GFP_KERNEL);
108 __qdio_free_queues(irq_ptr_qs, i);
112 q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
114 kmem_cache_free(qdio_q_cache, q);
115 __qdio_free_queues(irq_ptr_qs, i);
123 int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs)
127 rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs);
131 rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs);
133 __qdio_free_queues(irq_ptr->input_qs, nr_input_qs);
137 irq_ptr->max_input_qs = nr_input_qs;
138 irq_ptr->max_output_qs = nr_output_qs;
142 static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr,
143 qdio_handler_t *handler, int i)
145 struct slib *slib = q->slib;
147 /* queue must be cleared for qdio_establish */
148 memset(q, 0, sizeof(*q));
149 memset(slib, 0, PAGE_SIZE);
151 q->irq_ptr = irq_ptr;
152 q->mask = 1 << (31 - i);
154 q->handler = handler;
157 static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr,
158 struct qdio_buffer **sbals_array, int i)
163 DBF_HEX(&q, sizeof(void *));
164 q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2);
167 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
168 q->sbal[j] = *sbals_array++;
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;
177 q->slib->sla = (unsigned long)q->sl;
178 q->slib->slsba = (unsigned long)&q->slsb.val[0];
181 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
182 q->sl->element[j].sbal = virt_to_phys(q->sbal[j]);
185 static void setup_queues(struct qdio_irq *irq_ptr,
186 struct qdio_initialize *qdio_init)
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);
197 setup_storage_lists(q, irq_ptr,
198 qdio_init->input_sbal_addr_array[i], i);
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);
206 setup_storage_lists(q, irq_ptr,
207 qdio_init->output_sbal_addr_array[i], i);
211 static void check_and_setup_qebsm(struct qdio_irq *irq_ptr,
212 unsigned char qdioac, unsigned long token)
214 if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM))
216 if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) ||
217 (!(qdioac & AC1_SC_QEBSM_ENABLED)))
220 irq_ptr->sch_token = token;
223 DBF_EVENT("%8lx", irq_ptr->sch_token);
227 irq_ptr->sch_token = 0;
228 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
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.
236 int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr,
237 struct subchannel_id *schid,
238 struct qdio_ssqd_desc *data)
240 struct chsc_ssqd_area *ssqd;
243 DBF_EVENT("getssqd:%4x", schid->sch_no);
245 ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL);
249 ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page;
252 rc = chsc_ssqd(*schid, ssqd);
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))
262 memcpy(data, &ssqd->qdio_ssqd, sizeof(*data));
266 free_page((unsigned long)ssqd);
271 void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr)
273 unsigned char qdioac;
276 rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, &irq_ptr->ssqd_desc);
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;
284 qdioac = irq_ptr->ssqd_desc.qdioac1;
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);
292 static void qdio_fill_qdr_desc(struct qdesfmt0 *desc, struct qdio_q *queue)
294 desc->sliba = virt_to_phys(queue->slib);
295 desc->sla = virt_to_phys(queue->sl);
296 desc->slsba = virt_to_phys(&queue->slsb);
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;
304 static void setup_qdr(struct qdio_irq *irq_ptr,
305 struct qdio_initialize *qdio_init)
307 struct qdesfmt0 *desc = &irq_ptr->qdr->qdf0[0];
310 memset(irq_ptr->qdr, 0, sizeof(struct qdr));
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;
321 for (i = 0; i < qdio_init->no_input_qs; i++)
322 qdio_fill_qdr_desc(desc++, irq_ptr->input_qs[i]);
324 for (i = 0; i < qdio_init->no_output_qs; i++)
325 qdio_fill_qdr_desc(desc++, irq_ptr->output_qs[i]);
328 static void setup_qib(struct qdio_irq *irq_ptr,
329 struct qdio_initialize *init_data)
331 memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib));
333 irq_ptr->qib.qfmt = init_data->q_format;
334 irq_ptr->qib.pfmt = init_data->qib_param_field_format;
336 irq_ptr->qib.rflags = init_data->qib_rflags;
337 if (css_general_characteristics.qebsm)
338 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
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);
349 if (init_data->qib_param_field)
350 memcpy(irq_ptr->qib.parm, init_data->qib_param_field,
351 sizeof(irq_ptr->qib.parm));
354 void qdio_setup_irq(struct qdio_irq *irq_ptr, struct qdio_initialize *init_data)
356 struct ccw_device *cdev = irq_ptr->cdev;
358 irq_ptr->qdioac1 = 0;
359 memset(&irq_ptr->ssqd_desc, 0, sizeof(irq_ptr->ssqd_desc));
360 memset(&irq_ptr->perf_stat, 0, sizeof(irq_ptr->perf_stat));
362 irq_ptr->debugfs_dev = NULL;
363 irq_ptr->sch_token = irq_ptr->perf_stat_enabled = 0;
364 irq_ptr->state = QDIO_IRQ_STATE_INACTIVE;
365 irq_ptr->error_handler = init_data->input_handler;
367 irq_ptr->int_parm = init_data->int_parm;
368 irq_ptr->nr_input_qs = init_data->no_input_qs;
369 irq_ptr->nr_output_qs = init_data->no_output_qs;
370 ccw_device_get_schid(cdev, &irq_ptr->schid);
371 setup_queues(irq_ptr, init_data);
373 irq_ptr->irq_poll = init_data->irq_poll;
374 set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
376 setup_qib(irq_ptr, init_data);
378 /* fill input and output descriptors */
379 setup_qdr(irq_ptr, init_data);
381 /* qdr, qib, sls, slsbs, slibs, sbales are filled now */
383 /* set our IRQ handler */
384 spin_lock_irq(get_ccwdev_lock(cdev));
385 irq_ptr->orig_handler = cdev->handler;
386 cdev->handler = qdio_int_handler;
387 spin_unlock_irq(get_ccwdev_lock(cdev));
390 void qdio_shutdown_irq(struct qdio_irq *irq)
392 struct ccw_device *cdev = irq->cdev;
394 /* restore IRQ handler */
395 spin_lock_irq(get_ccwdev_lock(cdev));
396 cdev->handler = irq->orig_handler;
397 cdev->private->intparm = 0;
398 spin_unlock_irq(get_ccwdev_lock(cdev));
401 void qdio_print_subchannel_info(struct qdio_irq *irq_ptr)
403 dev_info(&irq_ptr->cdev->dev,
404 "qdio: %s on SC %x using AI:%d QEBSM:%d PRI:%d TDD:%d SIGA:%s%s%s\n",
405 (irq_ptr->qib.qfmt == QDIO_QETH_QFMT) ? "OSA" :
406 ((irq_ptr->qib.qfmt == QDIO_ZFCP_QFMT) ? "ZFCP" : "HS"),
407 irq_ptr->schid.sch_no,
408 is_thinint_irq(irq_ptr),
409 (irq_ptr->sch_token) ? 1 : 0,
410 pci_out_supported(irq_ptr) ? 1 : 0,
411 css_general_characteristics.aif_tdd,
412 qdio_need_siga_in(irq_ptr) ? "R" : " ",
413 qdio_need_siga_out(irq_ptr) ? "W" : " ",
414 qdio_need_siga_sync(irq_ptr) ? "S" : " ");
417 int __init qdio_setup_init(void)
419 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
424 /* Check for OSA/FCP thin interrupts (bit 67). */
425 DBF_EVENT("thinint:%1d",
426 (css_general_characteristics.aif_osa) ? 1 : 0);
428 /* Check for QEBSM support in general (bit 58). */
429 DBF_EVENT("cssQEBSM:%1d", css_general_characteristics.qebsm);
434 void qdio_setup_exit(void)
436 kmem_cache_destroy(qdio_q_cache);