blob: 22ee0272a1045c4a9fb23dfc17f943abccb7f358 [file] [log] [blame]
Jan Glauber779e6e12008-07-17 17:16:48 +02001/*
2 * driver/s390/cio/qdio_setup.c
3 *
4 * qdio queue initialization
5 *
6 * Copyright (C) IBM Corp. 2008
7 * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
8 */
9#include <linux/kernel.h>
10#include <linux/slab.h>
11#include <asm/qdio.h>
12
13#include "cio.h"
14#include "css.h"
15#include "device.h"
16#include "ioasm.h"
17#include "chsc.h"
18#include "qdio.h"
19#include "qdio_debug.h"
20
21static struct kmem_cache *qdio_q_cache;
22
23/*
24 * qebsm is only available under 64bit but the adapter sets the feature
25 * flag anyway, so we manually override it.
26 */
27static inline int qebsm_possible(void)
28{
29#ifdef CONFIG_64BIT
30 return css_general_characteristics.qebsm;
31#endif
32 return 0;
33}
34
35/*
36 * qib_param_field: pointer to 128 bytes or NULL, if no param field
37 * nr_input_qs: pointer to nr_queues*128 words of data or NULL
38 */
39static void set_impl_params(struct qdio_irq *irq_ptr,
40 unsigned int qib_param_field_format,
41 unsigned char *qib_param_field,
42 unsigned long *input_slib_elements,
43 unsigned long *output_slib_elements)
44{
45 struct qdio_q *q;
46 int i, j;
47
48 if (!irq_ptr)
49 return;
50
51 WARN_ON((unsigned long)&irq_ptr->qib & 0xff);
52 irq_ptr->qib.pfmt = qib_param_field_format;
53 if (qib_param_field)
54 memcpy(irq_ptr->qib.parm, qib_param_field,
55 QDIO_MAX_BUFFERS_PER_Q);
56
57 if (!input_slib_elements)
58 goto output;
59
60 for_each_input_queue(irq_ptr, q, i) {
61 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
62 q->slib->slibe[j].parms =
63 input_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
64 }
65output:
66 if (!output_slib_elements)
67 return;
68
69 for_each_output_queue(irq_ptr, q, i) {
70 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
71 q->slib->slibe[j].parms =
72 output_slib_elements[i * QDIO_MAX_BUFFERS_PER_Q + j];
73 }
74}
75
76static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues)
77{
78 struct qdio_q *q;
79 int i;
80
81 for (i = 0; i < nr_queues; i++) {
82 q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
83 if (!q)
84 return -ENOMEM;
85 WARN_ON((unsigned long)q & 0xff);
86
87 q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
88 if (!q->slib) {
89 kmem_cache_free(qdio_q_cache, q);
90 return -ENOMEM;
91 }
92 WARN_ON((unsigned long)q->slib & 0x7ff);
93 irq_ptr_qs[i] = q;
94 }
95 return 0;
96}
97
98int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs)
99{
100 int rc;
101
102 rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs);
103 if (rc)
104 return rc;
105 rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs);
106 return rc;
107}
108
109static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr,
110 qdio_handler_t *handler, int i)
111{
112 /* must be cleared by every qdio_establish */
113 memset(q, 0, ((char *)&q->slib) - ((char *)q));
114 memset(q->slib, 0, PAGE_SIZE);
115
116 q->irq_ptr = irq_ptr;
117 q->mask = 1 << (31 - i);
118 q->nr = i;
119 q->handler = handler;
120}
121
122static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr,
123 void **sbals_array, char *dbf_text, int i)
124{
125 struct qdio_q *prev;
126 int j;
127
128 QDIO_DBF_TEXT0(0, setup, dbf_text);
129 QDIO_DBF_HEX0(0, setup, &q, sizeof(void *));
130
131 q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2);
132
133 /* fill in sbal */
134 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++) {
135 q->sbal[j] = *sbals_array++;
136 WARN_ON((unsigned long)q->sbal[j] & 0xff);
137 }
138
139 /* fill in slib */
140 if (i > 0) {
141 prev = (q->is_input_q) ? irq_ptr->input_qs[i - 1]
142 : irq_ptr->output_qs[i - 1];
143 prev->slib->nsliba = (unsigned long)q->slib;
144 }
145
146 q->slib->sla = (unsigned long)q->sl;
147 q->slib->slsba = (unsigned long)&q->slsb.val[0];
148
149 /* fill in sl */
150 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
151 q->sl->element[j].sbal = (unsigned long)q->sbal[j];
152
153 QDIO_DBF_TEXT2(0, setup, "sl-sb-b0");
154 QDIO_DBF_HEX2(0, setup, q->sl, sizeof(void *));
155 QDIO_DBF_HEX2(0, setup, &q->slsb, sizeof(void *));
156 QDIO_DBF_HEX2(0, setup, q->sbal, sizeof(void *));
157}
158
159static void setup_queues(struct qdio_irq *irq_ptr,
160 struct qdio_initialize *qdio_init)
161{
162 char dbf_text[20];
163 struct qdio_q *q;
164 void **input_sbal_array = qdio_init->input_sbal_addr_array;
165 void **output_sbal_array = qdio_init->output_sbal_addr_array;
166 int i;
167
Jan Glauber58eb27c2008-08-21 19:46:34 +0200168 sprintf(dbf_text, "qset%4x", qdio_init->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +0200169 QDIO_DBF_TEXT0(0, setup, dbf_text);
170
171 for_each_input_queue(irq_ptr, q, i) {
172 sprintf(dbf_text, "in-q%4x", i);
173 setup_queues_misc(q, irq_ptr, qdio_init->input_handler, i);
174
175 q->is_input_q = 1;
176 spin_lock_init(&q->u.in.lock);
177 setup_storage_lists(q, irq_ptr, input_sbal_array, dbf_text, i);
178 input_sbal_array += QDIO_MAX_BUFFERS_PER_Q;
179
180 if (is_thinint_irq(irq_ptr))
181 tasklet_init(&q->tasklet, tiqdio_inbound_processing,
182 (unsigned long) q);
183 else
184 tasklet_init(&q->tasklet, qdio_inbound_processing,
185 (unsigned long) q);
186 }
187
188 for_each_output_queue(irq_ptr, q, i) {
189 sprintf(dbf_text, "outq%4x", i);
190 setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i);
191
192 q->is_input_q = 0;
193 setup_storage_lists(q, irq_ptr, output_sbal_array,
194 dbf_text, i);
195 output_sbal_array += QDIO_MAX_BUFFERS_PER_Q;
196
197 tasklet_init(&q->tasklet, qdio_outbound_processing,
198 (unsigned long) q);
199 setup_timer(&q->u.out.timer, (void(*)(unsigned long))
200 &qdio_outbound_timer, (unsigned long)q);
201 }
202}
203
204static void process_ac_flags(struct qdio_irq *irq_ptr, unsigned char qdioac)
205{
206 if (qdioac & AC1_SIGA_INPUT_NEEDED)
207 irq_ptr->siga_flag.input = 1;
208 if (qdioac & AC1_SIGA_OUTPUT_NEEDED)
209 irq_ptr->siga_flag.output = 1;
210 if (qdioac & AC1_SIGA_SYNC_NEEDED)
211 irq_ptr->siga_flag.sync = 1;
212 if (qdioac & AC1_AUTOMATIC_SYNC_ON_THININT)
213 irq_ptr->siga_flag.no_sync_ti = 1;
214 if (qdioac & AC1_AUTOMATIC_SYNC_ON_OUT_PCI)
215 irq_ptr->siga_flag.no_sync_out_pci = 1;
216
217 if (irq_ptr->siga_flag.no_sync_out_pci &&
218 irq_ptr->siga_flag.no_sync_ti)
219 irq_ptr->siga_flag.no_sync_out_ti = 1;
220}
221
222static void check_and_setup_qebsm(struct qdio_irq *irq_ptr,
223 unsigned char qdioac, unsigned long token)
224{
225 char dbf_text[15];
226
227 if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM))
228 goto no_qebsm;
229 if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) ||
230 (!(qdioac & AC1_SC_QEBSM_ENABLED)))
231 goto no_qebsm;
232
233 irq_ptr->sch_token = token;
234
235 QDIO_DBF_TEXT0(0, setup, "V=V:1");
236 sprintf(dbf_text, "%8lx", irq_ptr->sch_token);
237 QDIO_DBF_TEXT0(0, setup, dbf_text);
238 return;
239
240no_qebsm:
241 irq_ptr->sch_token = 0;
242 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
243 QDIO_DBF_TEXT0(0, setup, "noV=V");
244}
245
Jan Glauberbbd50e12008-12-25 13:38:43 +0100246/*
247 * If there is a qdio_irq we use the chsc_page and store the information
248 * in the qdio_irq, otherwise we copy it to the specified structure.
249 */
250int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr,
251 struct subchannel_id *schid,
252 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +0200253{
254 struct chsc_ssqd_area *ssqd;
255 int rc;
256
257 QDIO_DBF_TEXT0(0, setup, "getssqd");
Jan Glauberbbd50e12008-12-25 13:38:43 +0100258 if (irq_ptr != NULL)
259 ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page;
260 else
261 ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL);
Jan Glauber779e6e12008-07-17 17:16:48 +0200262 memset(ssqd, 0, PAGE_SIZE);
263
264 ssqd->request = (struct chsc_header) {
265 .length = 0x0010,
266 .code = 0x0024,
267 };
Jan Glauberbbd50e12008-12-25 13:38:43 +0100268 ssqd->first_sch = schid->sch_no;
269 ssqd->last_sch = schid->sch_no;
270 ssqd->ssid = schid->ssid;
Jan Glauber779e6e12008-07-17 17:16:48 +0200271
272 if (chsc(ssqd))
273 return -EIO;
274 rc = chsc_error_from_response(ssqd->response.code);
275 if (rc)
276 return rc;
277
278 if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) ||
279 !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) ||
Jan Glauberbbd50e12008-12-25 13:38:43 +0100280 (ssqd->qdio_ssqd.sch != schid->sch_no))
Jan Glauber779e6e12008-07-17 17:16:48 +0200281 return -EINVAL;
282
Jan Glauberbbd50e12008-12-25 13:38:43 +0100283 if (irq_ptr != NULL)
284 memcpy(&irq_ptr->ssqd_desc, &ssqd->qdio_ssqd,
285 sizeof(struct qdio_ssqd_desc));
286 else {
287 memcpy(data, &ssqd->qdio_ssqd,
288 sizeof(struct qdio_ssqd_desc));
289 free_page((unsigned long)ssqd);
290 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200291 return 0;
292}
293
294void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr)
295{
296 unsigned char qdioac;
297 char dbf_text[15];
298 int rc;
299
Jan Glauberbbd50e12008-12-25 13:38:43 +0100300 rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, NULL);
Jan Glauber779e6e12008-07-17 17:16:48 +0200301 if (rc) {
302 QDIO_DBF_TEXT2(0, setup, "ssqdasig");
Jan Glauber58eb27c2008-08-21 19:46:34 +0200303 sprintf(dbf_text, "schn%4x", irq_ptr->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +0200304 QDIO_DBF_TEXT2(0, setup, dbf_text);
305 sprintf(dbf_text, "rc:%d", rc);
306 QDIO_DBF_TEXT2(0, setup, dbf_text);
307 /* all flags set, worst case */
308 qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED |
309 AC1_SIGA_SYNC_NEEDED;
310 } else
311 qdioac = irq_ptr->ssqd_desc.qdioac1;
312
313 check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token);
314 process_ac_flags(irq_ptr, qdioac);
315
316 sprintf(dbf_text, "qdioac%2x", qdioac);
317 QDIO_DBF_TEXT2(0, setup, dbf_text);
318}
319
320void qdio_release_memory(struct qdio_irq *irq_ptr)
321{
322 struct qdio_q *q;
323 int i;
324
325 /*
326 * Must check queue array manually since irq_ptr->nr_input_queues /
327 * irq_ptr->nr_input_queues may not yet be set.
328 */
329 for (i = 0; i < QDIO_MAX_QUEUES_PER_IRQ; i++) {
330 q = irq_ptr->input_qs[i];
331 if (q) {
332 free_page((unsigned long) q->slib);
333 kmem_cache_free(qdio_q_cache, q);
334 }
335 }
336 for (i = 0; i < QDIO_MAX_QUEUES_PER_IRQ; i++) {
337 q = irq_ptr->output_qs[i];
338 if (q) {
339 free_page((unsigned long) q->slib);
340 kmem_cache_free(qdio_q_cache, q);
341 }
342 }
Jan Glauber3b8e3002008-08-01 16:39:17 +0200343 free_page((unsigned long) irq_ptr->qdr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200344 free_page(irq_ptr->chsc_page);
345 free_page((unsigned long) irq_ptr);
346}
347
348static void __qdio_allocate_fill_qdr(struct qdio_irq *irq_ptr,
349 struct qdio_q **irq_ptr_qs,
350 int i, int nr)
351{
352 irq_ptr->qdr->qdf0[i + nr].sliba =
353 (unsigned long)irq_ptr_qs[i]->slib;
354
355 irq_ptr->qdr->qdf0[i + nr].sla =
356 (unsigned long)irq_ptr_qs[i]->sl;
357
358 irq_ptr->qdr->qdf0[i + nr].slsba =
359 (unsigned long)&irq_ptr_qs[i]->slsb.val[0];
360
361 irq_ptr->qdr->qdf0[i + nr].akey = PAGE_DEFAULT_KEY;
362 irq_ptr->qdr->qdf0[i + nr].bkey = PAGE_DEFAULT_KEY;
363 irq_ptr->qdr->qdf0[i + nr].ckey = PAGE_DEFAULT_KEY;
364 irq_ptr->qdr->qdf0[i + nr].dkey = PAGE_DEFAULT_KEY;
365}
366
367static void setup_qdr(struct qdio_irq *irq_ptr,
368 struct qdio_initialize *qdio_init)
369{
370 int i;
371
372 irq_ptr->qdr->qfmt = qdio_init->q_format;
373 irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs;
374 irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs;
375 irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4; /* size in words */
376 irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4;
377 irq_ptr->qdr->qiba = (unsigned long)&irq_ptr->qib;
378 irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY;
379
380 for (i = 0; i < qdio_init->no_input_qs; i++)
381 __qdio_allocate_fill_qdr(irq_ptr, irq_ptr->input_qs, i, 0);
382
383 for (i = 0; i < qdio_init->no_output_qs; i++)
384 __qdio_allocate_fill_qdr(irq_ptr, irq_ptr->output_qs, i,
385 qdio_init->no_input_qs);
386}
387
388static void setup_qib(struct qdio_irq *irq_ptr,
389 struct qdio_initialize *init_data)
390{
391 if (qebsm_possible())
392 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
393
394 irq_ptr->qib.qfmt = init_data->q_format;
395 if (init_data->no_input_qs)
396 irq_ptr->qib.isliba =
397 (unsigned long)(irq_ptr->input_qs[0]->slib);
398 if (init_data->no_output_qs)
399 irq_ptr->qib.osliba =
400 (unsigned long)(irq_ptr->output_qs[0]->slib);
401 memcpy(irq_ptr->qib.ebcnam, init_data->adapter_name, 8);
402}
403
404int qdio_setup_irq(struct qdio_initialize *init_data)
405{
406 struct ciw *ciw;
407 struct qdio_irq *irq_ptr = init_data->cdev->private->qdio_data;
408 int rc;
409
410 memset(irq_ptr, 0, ((char *)&irq_ptr->qdr) - ((char *)irq_ptr));
411 /* wipes qib.ac, required by ar7063 */
412 memset(irq_ptr->qdr, 0, sizeof(struct qdr));
413
414 irq_ptr->int_parm = init_data->int_parm;
415 irq_ptr->nr_input_qs = init_data->no_input_qs;
416 irq_ptr->nr_output_qs = init_data->no_output_qs;
417
418 irq_ptr->schid = ccw_device_get_subchannel_id(init_data->cdev);
419 irq_ptr->cdev = init_data->cdev;
420 setup_queues(irq_ptr, init_data);
421
422 setup_qib(irq_ptr, init_data);
423 qdio_setup_thinint(irq_ptr);
424 set_impl_params(irq_ptr, init_data->qib_param_field_format,
425 init_data->qib_param_field,
426 init_data->input_slib_elements,
427 init_data->output_slib_elements);
428
429 /* fill input and output descriptors */
430 setup_qdr(irq_ptr, init_data);
431
432 /* qdr, qib, sls, slsbs, slibs, sbales are filled now */
433
434 /* get qdio commands */
435 ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_EQUEUE);
436 if (!ciw) {
437 QDIO_DBF_TEXT2(1, setup, "no eq");
438 rc = -EINVAL;
439 goto out_err;
440 }
441 irq_ptr->equeue = *ciw;
442
443 ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_AQUEUE);
444 if (!ciw) {
445 QDIO_DBF_TEXT2(1, setup, "no aq");
446 rc = -EINVAL;
447 goto out_err;
448 }
449 irq_ptr->aqueue = *ciw;
450
451 /* set new interrupt handler */
452 irq_ptr->orig_handler = init_data->cdev->handler;
453 init_data->cdev->handler = qdio_int_handler;
454 return 0;
455out_err:
456 qdio_release_memory(irq_ptr);
457 return rc;
458}
459
460void qdio_print_subchannel_info(struct qdio_irq *irq_ptr,
461 struct ccw_device *cdev)
462{
463 char s[80];
464
Jan Glauber75f62762008-10-03 21:55:00 +0200465 sprintf(s, "qdio: %s ", dev_name(&cdev->dev));
Jan Glauber779e6e12008-07-17 17:16:48 +0200466 switch (irq_ptr->qib.qfmt) {
467 case QDIO_QETH_QFMT:
Jan Glauber75f62762008-10-03 21:55:00 +0200468 sprintf(s + strlen(s), "OSA ");
Jan Glauber779e6e12008-07-17 17:16:48 +0200469 break;
470 case QDIO_ZFCP_QFMT:
471 sprintf(s + strlen(s), "ZFCP ");
472 break;
473 case QDIO_IQDIO_QFMT:
Jan Glauber75f62762008-10-03 21:55:00 +0200474 sprintf(s + strlen(s), "HS ");
Jan Glauber779e6e12008-07-17 17:16:48 +0200475 break;
476 }
Jan Glauber75f62762008-10-03 21:55:00 +0200477 sprintf(s + strlen(s), "on SC %x using ", irq_ptr->schid.sch_no);
478 sprintf(s + strlen(s), "AI:%d ", is_thinint_irq(irq_ptr));
479 sprintf(s + strlen(s), "QEBSM:%d ", (irq_ptr->sch_token) ? 1 : 0);
480 sprintf(s + strlen(s), "PCI:%d ",
481 (irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED) ? 1 : 0);
482 sprintf(s + strlen(s), "TDD:%d ", css_general_characteristics.aif_tdd);
483 sprintf(s + strlen(s), "SIGA:");
484 sprintf(s + strlen(s), "%s", (irq_ptr->siga_flag.input) ? "R" : " ");
485 sprintf(s + strlen(s), "%s", (irq_ptr->siga_flag.output) ? "W" : " ");
486 sprintf(s + strlen(s), "%s", (irq_ptr->siga_flag.sync) ? "S" : " ");
487 sprintf(s + strlen(s), "%s",
488 (!irq_ptr->siga_flag.no_sync_ti) ? "A" : " ");
489 sprintf(s + strlen(s), "%s",
490 (!irq_ptr->siga_flag.no_sync_out_ti) ? "O" : " ");
491 sprintf(s + strlen(s), "%s",
492 (!irq_ptr->siga_flag.no_sync_out_pci) ? "P" : " ");
Jan Glauber779e6e12008-07-17 17:16:48 +0200493 sprintf(s + strlen(s), "\n");
Jan Glauber75f62762008-10-03 21:55:00 +0200494 printk(KERN_INFO "%s", s);
Jan Glauber779e6e12008-07-17 17:16:48 +0200495}
496
497int __init qdio_setup_init(void)
498{
499 char dbf_text[15];
500
501 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q),
502 256, 0, NULL);
503 if (!qdio_q_cache)
504 return -ENOMEM;
505
506 /* Check for OSA/FCP thin interrupts (bit 67). */
507 sprintf(dbf_text, "thini%1x",
508 (css_general_characteristics.aif_osa) ? 1 : 0);
509 QDIO_DBF_TEXT0(0, setup, dbf_text);
510
511 /* Check for QEBSM support in general (bit 58). */
512 sprintf(dbf_text, "cssQBS:%1x",
513 (qebsm_possible()) ? 1 : 0);
514 QDIO_DBF_TEXT0(0, setup, dbf_text);
515 return 0;
516}
517
Heiko Carstens3f1934b2008-08-01 16:39:20 +0200518void qdio_setup_exit(void)
Jan Glauber779e6e12008-07-17 17:16:48 +0200519{
520 kmem_cache_destroy(qdio_q_cache);
521}