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