1
2
3
4
5
6
7
8#include <linux/kernel.h>
9#include <linux/slab.h>
10#include <linux/export.h>
11#include <linux/io.h>
12#include <asm/qdio.h>
13
14#include "cio.h"
15#include "css.h"
16#include "device.h"
17#include "ioasm.h"
18#include "chsc.h"
19#include "qdio.h"
20#include "qdio_debug.h"
21
22#define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
23
24static struct kmem_cache *qdio_q_cache;
25static struct kmem_cache *qdio_aob_cache;
26
27struct qaob *qdio_allocate_aob(void)
28{
29 return kmem_cache_zalloc(qdio_aob_cache, GFP_ATOMIC);
30}
31
32void qdio_release_aob(struct qaob *aob)
33{
34 kmem_cache_free(qdio_aob_cache, aob);
35}
36EXPORT_SYMBOL_GPL(qdio_release_aob);
37
38
39
40
41
42
43void qdio_free_buffers(struct qdio_buffer **buf, unsigned int count)
44{
45 int pos;
46
47 for (pos = 0; pos < count; pos += QBUFF_PER_PAGE)
48 free_page((unsigned long) buf[pos]);
49}
50EXPORT_SYMBOL_GPL(qdio_free_buffers);
51
52
53
54
55
56
57int qdio_alloc_buffers(struct qdio_buffer **buf, unsigned int count)
58{
59 int pos;
60
61 for (pos = 0; pos < count; pos += QBUFF_PER_PAGE) {
62 buf[pos] = (void *) get_zeroed_page(GFP_KERNEL);
63 if (!buf[pos]) {
64 qdio_free_buffers(buf, count);
65 return -ENOMEM;
66 }
67 }
68 for (pos = 0; pos < count; pos++)
69 if (pos % QBUFF_PER_PAGE)
70 buf[pos] = buf[pos - 1] + 1;
71 return 0;
72}
73EXPORT_SYMBOL_GPL(qdio_alloc_buffers);
74
75
76
77
78
79
80void qdio_reset_buffers(struct qdio_buffer **buf, unsigned int count)
81{
82 int pos;
83
84 for (pos = 0; pos < count; pos++)
85 memset(buf[pos], 0, sizeof(struct qdio_buffer));
86}
87EXPORT_SYMBOL_GPL(qdio_reset_buffers);
88
89
90
91
92
93static inline int qebsm_possible(void)
94{
95 return css_general_characteristics.qebsm;
96}
97
98
99
100
101
102static void set_impl_params(struct qdio_irq *irq_ptr,
103 unsigned int qib_param_field_format,
104 unsigned char *qib_param_field,
105 unsigned long *input_slib_elements,
106 unsigned long *output_slib_elements)
107{
108 struct qdio_q *q;
109 int i, j;
110
111 if (!irq_ptr)
112 return;
113
114 irq_ptr->qib.pfmt = qib_param_field_format;
115 if (qib_param_field)
116 memcpy(irq_ptr->qib.parm, qib_param_field,
117 sizeof(irq_ptr->qib.parm));
118
119 if (!input_slib_elements)
120 goto output;
121
122 for_each_input_queue(irq_ptr, q, i) {
123 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
124 q->slib->slibe[j].parms =
125 input_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
126 }
127output:
128 if (!output_slib_elements)
129 return;
130
131 for_each_output_queue(irq_ptr, q, i) {
132 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
133 q->slib->slibe[j].parms =
134 output_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
135 }
136}
137
138static void __qdio_free_queues(struct qdio_q **queues, unsigned int count)
139{
140 struct qdio_q *q;
141 unsigned int i;
142
143 for (i = 0; i < count; i++) {
144 q = queues[i];
145 free_page((unsigned long) q->slib);
146 kmem_cache_free(qdio_q_cache, q);
147 }
148}
149
150void qdio_free_queues(struct qdio_irq *irq_ptr)
151{
152 __qdio_free_queues(irq_ptr->input_qs, irq_ptr->max_input_qs);
153 irq_ptr->max_input_qs = 0;
154
155 __qdio_free_queues(irq_ptr->output_qs, irq_ptr->max_output_qs);
156 irq_ptr->max_output_qs = 0;
157}
158
159static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues)
160{
161 struct qdio_q *q;
162 int i;
163
164 for (i = 0; i < nr_queues; i++) {
165 q = kmem_cache_zalloc(qdio_q_cache, GFP_KERNEL);
166 if (!q) {
167 __qdio_free_queues(irq_ptr_qs, i);
168 return -ENOMEM;
169 }
170
171 q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
172 if (!q->slib) {
173 kmem_cache_free(qdio_q_cache, q);
174 __qdio_free_queues(irq_ptr_qs, i);
175 return -ENOMEM;
176 }
177 irq_ptr_qs[i] = q;
178 }
179 return 0;
180}
181
182int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs)
183{
184 int rc;
185
186 rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs);
187 if (rc)
188 return rc;
189
190 rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs);
191 if (rc) {
192 __qdio_free_queues(irq_ptr->input_qs, nr_input_qs);
193 return rc;
194 }
195
196 irq_ptr->max_input_qs = nr_input_qs;
197 irq_ptr->max_output_qs = nr_output_qs;
198 return 0;
199}
200
201static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr,
202 qdio_handler_t *handler, int i)
203{
204 struct slib *slib = q->slib;
205
206
207 memset(q, 0, sizeof(*q));
208 memset(slib, 0, PAGE_SIZE);
209 q->slib = slib;
210 q->irq_ptr = irq_ptr;
211 q->mask = 1 << (31 - i);
212 q->nr = i;
213 q->handler = handler;
214}
215
216static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr,
217 struct qdio_buffer **sbals_array, int i)
218{
219 struct qdio_q *prev;
220 int j;
221
222 DBF_HEX(&q, sizeof(void *));
223 q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2);
224
225
226 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
227 q->sbal[j] = *sbals_array++;
228
229
230 if (i > 0) {
231 prev = (q->is_input_q) ? irq_ptr->input_qs[i - 1]
232 : irq_ptr->output_qs[i - 1];
233 prev->slib->nsliba = (unsigned long)q->slib;
234 }
235
236 q->slib->sla = (unsigned long)q->sl;
237 q->slib->slsba = (unsigned long)&q->slsb.val[0];
238
239
240 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
241 q->sl->element[j].sbal = virt_to_phys(q->sbal[j]);
242}
243
244static void setup_queues(struct qdio_irq *irq_ptr,
245 struct qdio_initialize *qdio_init)
246{
247 struct qdio_q *q;
248 struct qdio_outbuf_state *output_sbal_state_array =
249 qdio_init->output_sbal_state_array;
250 int i;
251
252 for_each_input_queue(irq_ptr, q, i) {
253 DBF_EVENT("inq:%1d", i);
254 setup_queues_misc(q, irq_ptr, qdio_init->input_handler, i);
255
256 q->is_input_q = 1;
257
258 setup_storage_lists(q, irq_ptr,
259 qdio_init->input_sbal_addr_array[i], i);
260
261 if (is_thinint_irq(irq_ptr)) {
262 tasklet_init(&q->tasklet, tiqdio_inbound_processing,
263 (unsigned long) q);
264 } else {
265 tasklet_init(&q->tasklet, qdio_inbound_processing,
266 (unsigned long) q);
267 }
268 }
269
270 for_each_output_queue(irq_ptr, q, i) {
271 DBF_EVENT("outq:%1d", i);
272 setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i);
273
274 q->u.out.sbal_state = output_sbal_state_array;
275 output_sbal_state_array += QDIO_MAX_BUFFERS_PER_Q;
276
277 q->is_input_q = 0;
278 setup_storage_lists(q, irq_ptr,
279 qdio_init->output_sbal_addr_array[i], i);
280
281 tasklet_init(&q->tasklet, qdio_outbound_processing,
282 (unsigned long) q);
283 timer_setup(&q->u.out.timer, qdio_outbound_timer, 0);
284 }
285}
286
287static void process_ac_flags(struct qdio_irq *irq_ptr, unsigned char qdioac)
288{
289 if (qdioac & AC1_SIGA_INPUT_NEEDED)
290 irq_ptr->siga_flag.input = 1;
291 if (qdioac & AC1_SIGA_OUTPUT_NEEDED)
292 irq_ptr->siga_flag.output = 1;
293 if (qdioac & AC1_SIGA_SYNC_NEEDED)
294 irq_ptr->siga_flag.sync = 1;
295 if (!(qdioac & AC1_AUTOMATIC_SYNC_ON_THININT))
296 irq_ptr->siga_flag.sync_after_ai = 1;
297 if (!(qdioac & AC1_AUTOMATIC_SYNC_ON_OUT_PCI))
298 irq_ptr->siga_flag.sync_out_after_pci = 1;
299}
300
301static void check_and_setup_qebsm(struct qdio_irq *irq_ptr,
302 unsigned char qdioac, unsigned long token)
303{
304 if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM))
305 goto no_qebsm;
306 if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) ||
307 (!(qdioac & AC1_SC_QEBSM_ENABLED)))
308 goto no_qebsm;
309
310 irq_ptr->sch_token = token;
311
312 DBF_EVENT("V=V:1");
313 DBF_EVENT("%8lx", irq_ptr->sch_token);
314 return;
315
316no_qebsm:
317 irq_ptr->sch_token = 0;
318 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
319 DBF_EVENT("noV=V");
320}
321
322
323
324
325
326int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr,
327 struct subchannel_id *schid,
328 struct qdio_ssqd_desc *data)
329{
330 struct chsc_ssqd_area *ssqd;
331 int rc;
332
333 DBF_EVENT("getssqd:%4x", schid->sch_no);
334 if (!irq_ptr) {
335 ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL);
336 if (!ssqd)
337 return -ENOMEM;
338 } else {
339 ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page;
340 }
341
342 rc = chsc_ssqd(*schid, ssqd);
343 if (rc)
344 goto out;
345
346 if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) ||
347 !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) ||
348 (ssqd->qdio_ssqd.sch != schid->sch_no))
349 rc = -EINVAL;
350
351 if (!rc)
352 memcpy(data, &ssqd->qdio_ssqd, sizeof(*data));
353
354out:
355 if (!irq_ptr)
356 free_page((unsigned long)ssqd);
357
358 return rc;
359}
360
361void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr)
362{
363 unsigned char qdioac;
364 int rc;
365
366 rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, &irq_ptr->ssqd_desc);
367 if (rc) {
368 DBF_ERROR("%4x ssqd ERR", irq_ptr->schid.sch_no);
369 DBF_ERROR("rc:%x", rc);
370
371 qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED |
372 AC1_SIGA_SYNC_NEEDED;
373 } else
374 qdioac = irq_ptr->ssqd_desc.qdioac1;
375
376 check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token);
377 process_ac_flags(irq_ptr, qdioac);
378 DBF_EVENT("ac 1:%2x 2:%4x", qdioac, irq_ptr->ssqd_desc.qdioac2);
379 DBF_EVENT("3:%4x qib:%4x", irq_ptr->ssqd_desc.qdioac3, irq_ptr->qib.ac);
380}
381
382void qdio_free_async_data(struct qdio_irq *irq_ptr)
383{
384 struct qdio_q *q;
385 int i;
386
387 for (i = 0; i < irq_ptr->max_output_qs; i++) {
388 q = irq_ptr->output_qs[i];
389 if (q->u.out.use_cq) {
390 unsigned int n;
391
392 for (n = 0; n < QDIO_MAX_BUFFERS_PER_Q; n++) {
393 struct qaob *aob = q->u.out.aobs[n];
394
395 if (aob) {
396 qdio_release_aob(aob);
397 q->u.out.aobs[n] = NULL;
398 }
399 }
400
401 qdio_disable_async_operation(&q->u.out);
402 }
403 }
404}
405
406static void __qdio_allocate_fill_qdr(struct qdio_irq *irq_ptr,
407 struct qdio_q **irq_ptr_qs,
408 int i, int nr)
409{
410 irq_ptr->qdr->qdf0[i + nr].sliba =
411 (unsigned long)irq_ptr_qs[i]->slib;
412
413 irq_ptr->qdr->qdf0[i + nr].sla =
414 (unsigned long)irq_ptr_qs[i]->sl;
415
416 irq_ptr->qdr->qdf0[i + nr].slsba =
417 (unsigned long)&irq_ptr_qs[i]->slsb.val[0];
418
419 irq_ptr->qdr->qdf0[i + nr].akey = PAGE_DEFAULT_KEY >> 4;
420 irq_ptr->qdr->qdf0[i + nr].bkey = PAGE_DEFAULT_KEY >> 4;
421 irq_ptr->qdr->qdf0[i + nr].ckey = PAGE_DEFAULT_KEY >> 4;
422 irq_ptr->qdr->qdf0[i + nr].dkey = PAGE_DEFAULT_KEY >> 4;
423}
424
425static void setup_qdr(struct qdio_irq *irq_ptr,
426 struct qdio_initialize *qdio_init)
427{
428 int i;
429
430 irq_ptr->qdr->qfmt = qdio_init->q_format;
431 irq_ptr->qdr->ac = qdio_init->qdr_ac;
432 irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs;
433 irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs;
434 irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4;
435 irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4;
436 irq_ptr->qdr->qiba = (unsigned long)&irq_ptr->qib;
437 irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY >> 4;
438
439 for (i = 0; i < qdio_init->no_input_qs; i++)
440 __qdio_allocate_fill_qdr(irq_ptr, irq_ptr->input_qs, i, 0);
441
442 for (i = 0; i < qdio_init->no_output_qs; i++)
443 __qdio_allocate_fill_qdr(irq_ptr, irq_ptr->output_qs, i,
444 qdio_init->no_input_qs);
445}
446
447static void setup_qib(struct qdio_irq *irq_ptr,
448 struct qdio_initialize *init_data)
449{
450 if (qebsm_possible())
451 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
452
453 irq_ptr->qib.rflags |= init_data->qib_rflags;
454
455 irq_ptr->qib.qfmt = init_data->q_format;
456 if (init_data->no_input_qs)
457 irq_ptr->qib.isliba =
458 (unsigned long)(irq_ptr->input_qs[0]->slib);
459 if (init_data->no_output_qs)
460 irq_ptr->qib.osliba =
461 (unsigned long)(irq_ptr->output_qs[0]->slib);
462 memcpy(irq_ptr->qib.ebcnam, init_data->adapter_name, 8);
463}
464
465int qdio_setup_irq(struct qdio_irq *irq_ptr, struct qdio_initialize *init_data)
466{
467 struct ccw_device *cdev = irq_ptr->cdev;
468 struct ciw *ciw;
469
470 memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib));
471 memset(&irq_ptr->siga_flag, 0, sizeof(irq_ptr->siga_flag));
472 memset(&irq_ptr->ccw, 0, sizeof(irq_ptr->ccw));
473 memset(&irq_ptr->ssqd_desc, 0, sizeof(irq_ptr->ssqd_desc));
474 memset(&irq_ptr->perf_stat, 0, sizeof(irq_ptr->perf_stat));
475
476 irq_ptr->debugfs_dev = NULL;
477 irq_ptr->sch_token = irq_ptr->perf_stat_enabled = 0;
478 irq_ptr->state = QDIO_IRQ_STATE_INACTIVE;
479
480
481 memset(irq_ptr->qdr, 0, sizeof(struct qdr));
482
483 irq_ptr->int_parm = init_data->int_parm;
484 irq_ptr->nr_input_qs = init_data->no_input_qs;
485 irq_ptr->nr_output_qs = init_data->no_output_qs;
486 irq_ptr->scan_threshold = init_data->scan_threshold;
487 ccw_device_get_schid(cdev, &irq_ptr->schid);
488 setup_queues(irq_ptr, init_data);
489
490 if (init_data->irq_poll) {
491 irq_ptr->irq_poll = init_data->irq_poll;
492 set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
493 } else {
494 irq_ptr->irq_poll = NULL;
495 }
496
497 setup_qib(irq_ptr, init_data);
498 set_impl_params(irq_ptr, init_data->qib_param_field_format,
499 init_data->qib_param_field,
500 init_data->input_slib_elements,
501 init_data->output_slib_elements);
502
503
504 setup_qdr(irq_ptr, init_data);
505
506
507
508
509 spin_lock_irq(get_ccwdev_lock(cdev));
510 irq_ptr->orig_handler = cdev->handler;
511 cdev->handler = qdio_int_handler;
512 spin_unlock_irq(get_ccwdev_lock(cdev));
513
514
515 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_EQUEUE);
516 if (!ciw) {
517 DBF_ERROR("%4x NO EQ", irq_ptr->schid.sch_no);
518 return -EINVAL;
519 }
520 irq_ptr->equeue = *ciw;
521
522 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_AQUEUE);
523 if (!ciw) {
524 DBF_ERROR("%4x NO AQ", irq_ptr->schid.sch_no);
525 return -EINVAL;
526 }
527 irq_ptr->aqueue = *ciw;
528
529 return 0;
530}
531
532void qdio_shutdown_irq(struct qdio_irq *irq)
533{
534 struct ccw_device *cdev = irq->cdev;
535
536
537 spin_lock_irq(get_ccwdev_lock(cdev));
538 cdev->handler = irq->orig_handler;
539 cdev->private->intparm = 0;
540 spin_unlock_irq(get_ccwdev_lock(cdev));
541}
542
543void qdio_print_subchannel_info(struct qdio_irq *irq_ptr)
544{
545 char s[80];
546
547 snprintf(s, 80, "qdio: %s %s on SC %x using "
548 "AI:%d QEBSM:%d PRI:%d TDD:%d SIGA:%s%s%s%s%s\n",
549 dev_name(&irq_ptr->cdev->dev),
550 (irq_ptr->qib.qfmt == QDIO_QETH_QFMT) ? "OSA" :
551 ((irq_ptr->qib.qfmt == QDIO_ZFCP_QFMT) ? "ZFCP" : "HS"),
552 irq_ptr->schid.sch_no,
553 is_thinint_irq(irq_ptr),
554 (irq_ptr->sch_token) ? 1 : 0,
555 pci_out_supported(irq_ptr) ? 1 : 0,
556 css_general_characteristics.aif_tdd,
557 (irq_ptr->siga_flag.input) ? "R" : " ",
558 (irq_ptr->siga_flag.output) ? "W" : " ",
559 (irq_ptr->siga_flag.sync) ? "S" : " ",
560 (irq_ptr->siga_flag.sync_after_ai) ? "A" : " ",
561 (irq_ptr->siga_flag.sync_out_after_pci) ? "P" : " ");
562 printk(KERN_INFO "%s", s);
563}
564
565int qdio_enable_async_operation(struct qdio_output_q *outq)
566{
567 outq->aobs = kcalloc(QDIO_MAX_BUFFERS_PER_Q, sizeof(struct qaob *),
568 GFP_KERNEL);
569 if (!outq->aobs) {
570 outq->use_cq = 0;
571 return -ENOMEM;
572 }
573 outq->use_cq = 1;
574 return 0;
575}
576
577void qdio_disable_async_operation(struct qdio_output_q *q)
578{
579 kfree(q->aobs);
580 q->aobs = NULL;
581 q->use_cq = 0;
582}
583
584int __init qdio_setup_init(void)
585{
586 int rc;
587
588 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
589 256, 0, NULL);
590 if (!qdio_q_cache)
591 return -ENOMEM;
592
593 qdio_aob_cache = kmem_cache_create("qdio_aob",
594 sizeof(struct qaob),
595 sizeof(struct qaob),
596 0,
597 NULL);
598 if (!qdio_aob_cache) {
599 rc = -ENOMEM;
600 goto free_qdio_q_cache;
601 }
602
603
604 DBF_EVENT("thinint:%1d",
605 (css_general_characteristics.aif_osa) ? 1 : 0);
606
607
608 DBF_EVENT("cssQEBSM:%1d", (qebsm_possible()) ? 1 : 0);
609 rc = 0;
610out:
611 return rc;
612free_qdio_q_cache:
613 kmem_cache_destroy(qdio_q_cache);
614 goto out;
615}
616
617void qdio_setup_exit(void)
618{
619 kmem_cache_destroy(qdio_aob_cache);
620 kmem_cache_destroy(qdio_q_cache);
621}
622