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 264 for_each_output_queue(irq_ptr, q, i) { 265 DBF_EVENT("outq:%1d", i); 266 setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i); 267 268 q->u.out.sbal_state = output_sbal_state_array; 269 output_sbal_state_array += QDIO_MAX_BUFFERS_PER_Q; 270 271 q->is_input_q = 0; 272 setup_storage_lists(q, irq_ptr, 273 qdio_init->output_sbal_addr_array[i], i); 274 275 tasklet_setup(&q->u.out.tasklet, qdio_outbound_tasklet); 276 timer_setup(&q->u.out.timer, qdio_outbound_timer, 0); 277 } 278 } 279 280 static void process_ac_flags(struct qdio_irq *irq_ptr, unsigned char qdioac) 281 { 282 if (qdioac & AC1_SIGA_INPUT_NEEDED) 283 irq_ptr->siga_flag.input = 1; 284 if (qdioac & AC1_SIGA_OUTPUT_NEEDED) 285 irq_ptr->siga_flag.output = 1; 286 if (qdioac & AC1_SIGA_SYNC_NEEDED) 287 irq_ptr->siga_flag.sync = 1; 288 if (!(qdioac & AC1_AUTOMATIC_SYNC_ON_THININT)) 289 irq_ptr->siga_flag.sync_after_ai = 1; 290 if (!(qdioac & AC1_AUTOMATIC_SYNC_ON_OUT_PCI)) 291 irq_ptr->siga_flag.sync_out_after_pci = 1; 292 } 293 294 static void check_and_setup_qebsm(struct qdio_irq *irq_ptr, 295 unsigned char qdioac, unsigned long token) 296 { 297 if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM)) 298 goto no_qebsm; 299 if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) || 300 (!(qdioac & AC1_SC_QEBSM_ENABLED))) 301 goto no_qebsm; 302 303 irq_ptr->sch_token = token; 304 305 DBF_EVENT("V=V:1"); 306 DBF_EVENT("%8lx", irq_ptr->sch_token); 307 return; 308 309 no_qebsm: 310 irq_ptr->sch_token = 0; 311 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM; 312 DBF_EVENT("noV=V"); 313 } 314 315 /* 316 * If there is a qdio_irq we use the chsc_page and store the information 317 * in the qdio_irq, otherwise we copy it to the specified structure. 318 */ 319 int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr, 320 struct subchannel_id *schid, 321 struct qdio_ssqd_desc *data) 322 { 323 struct chsc_ssqd_area *ssqd; 324 int rc; 325 326 DBF_EVENT("getssqd:%4x", schid->sch_no); 327 if (!irq_ptr) { 328 ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL); 329 if (!ssqd) 330 return -ENOMEM; 331 } else { 332 ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page; 333 } 334 335 rc = chsc_ssqd(*schid, ssqd); 336 if (rc) 337 goto out; 338 339 if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) || 340 !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) || 341 (ssqd->qdio_ssqd.sch != schid->sch_no)) 342 rc = -EINVAL; 343 344 if (!rc) 345 memcpy(data, &ssqd->qdio_ssqd, sizeof(*data)); 346 347 out: 348 if (!irq_ptr) 349 free_page((unsigned long)ssqd); 350 351 return rc; 352 } 353 354 void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr) 355 { 356 unsigned char qdioac; 357 int rc; 358 359 rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, &irq_ptr->ssqd_desc); 360 if (rc) { 361 DBF_ERROR("%4x ssqd ERR", irq_ptr->schid.sch_no); 362 DBF_ERROR("rc:%x", rc); 363 /* all flags set, worst case */ 364 qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED | 365 AC1_SIGA_SYNC_NEEDED; 366 } else 367 qdioac = irq_ptr->ssqd_desc.qdioac1; 368 369 check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token); 370 process_ac_flags(irq_ptr, qdioac); 371 DBF_EVENT("ac 1:%2x 2:%4x", qdioac, irq_ptr->ssqd_desc.qdioac2); 372 DBF_EVENT("3:%4x qib:%4x", irq_ptr->ssqd_desc.qdioac3, irq_ptr->qib.ac); 373 } 374 375 void qdio_free_async_data(struct qdio_irq *irq_ptr) 376 { 377 struct qdio_q *q; 378 int i; 379 380 for (i = 0; i < irq_ptr->max_output_qs; i++) { 381 q = irq_ptr->output_qs[i]; 382 if (q->u.out.use_cq) { 383 unsigned int n; 384 385 for (n = 0; n < QDIO_MAX_BUFFERS_PER_Q; n++) { 386 struct qaob *aob = q->u.out.aobs[n]; 387 388 if (aob) { 389 qdio_release_aob(aob); 390 q->u.out.aobs[n] = NULL; 391 } 392 } 393 394 qdio_disable_async_operation(&q->u.out); 395 } 396 } 397 } 398 399 static void qdio_fill_qdr_desc(struct qdesfmt0 *desc, struct qdio_q *queue) 400 { 401 desc->sliba = virt_to_phys(queue->slib); 402 desc->sla = virt_to_phys(queue->sl); 403 desc->slsba = virt_to_phys(&queue->slsb); 404 405 desc->akey = PAGE_DEFAULT_KEY >> 4; 406 desc->bkey = PAGE_DEFAULT_KEY >> 4; 407 desc->ckey = PAGE_DEFAULT_KEY >> 4; 408 desc->dkey = PAGE_DEFAULT_KEY >> 4; 409 } 410 411 static void setup_qdr(struct qdio_irq *irq_ptr, 412 struct qdio_initialize *qdio_init) 413 { 414 struct qdesfmt0 *desc = &irq_ptr->qdr->qdf0[0]; 415 int i; 416 417 irq_ptr->qdr->qfmt = qdio_init->q_format; 418 irq_ptr->qdr->ac = qdio_init->qdr_ac; 419 irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs; 420 irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs; 421 irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4; /* size in words */ 422 irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4; 423 irq_ptr->qdr->qiba = virt_to_phys(&irq_ptr->qib); 424 irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY >> 4; 425 426 for (i = 0; i < qdio_init->no_input_qs; i++) 427 qdio_fill_qdr_desc(desc++, irq_ptr->input_qs[i]); 428 429 for (i = 0; i < qdio_init->no_output_qs; i++) 430 qdio_fill_qdr_desc(desc++, irq_ptr->output_qs[i]); 431 } 432 433 static void setup_qib(struct qdio_irq *irq_ptr, 434 struct qdio_initialize *init_data) 435 { 436 if (qebsm_possible()) 437 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM; 438 439 irq_ptr->qib.rflags |= init_data->qib_rflags; 440 441 irq_ptr->qib.qfmt = init_data->q_format; 442 if (init_data->no_input_qs) 443 irq_ptr->qib.isliba = 444 (unsigned long)(irq_ptr->input_qs[0]->slib); 445 if (init_data->no_output_qs) 446 irq_ptr->qib.osliba = 447 (unsigned long)(irq_ptr->output_qs[0]->slib); 448 memcpy(irq_ptr->qib.ebcnam, dev_name(&irq_ptr->cdev->dev), 8); 449 ASCEBC(irq_ptr->qib.ebcnam, 8); 450 } 451 452 int qdio_setup_irq(struct qdio_irq *irq_ptr, struct qdio_initialize *init_data) 453 { 454 struct ccw_device *cdev = irq_ptr->cdev; 455 struct ciw *ciw; 456 457 memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib)); 458 memset(&irq_ptr->siga_flag, 0, sizeof(irq_ptr->siga_flag)); 459 memset(&irq_ptr->ccw, 0, sizeof(irq_ptr->ccw)); 460 memset(&irq_ptr->ssqd_desc, 0, sizeof(irq_ptr->ssqd_desc)); 461 memset(&irq_ptr->perf_stat, 0, sizeof(irq_ptr->perf_stat)); 462 463 irq_ptr->debugfs_dev = NULL; 464 irq_ptr->sch_token = irq_ptr->perf_stat_enabled = 0; 465 irq_ptr->state = QDIO_IRQ_STATE_INACTIVE; 466 467 /* wipes qib.ac, required by ar7063 */ 468 memset(irq_ptr->qdr, 0, sizeof(struct qdr)); 469 470 irq_ptr->int_parm = init_data->int_parm; 471 irq_ptr->nr_input_qs = init_data->no_input_qs; 472 irq_ptr->nr_output_qs = init_data->no_output_qs; 473 irq_ptr->scan_threshold = init_data->scan_threshold; 474 ccw_device_get_schid(cdev, &irq_ptr->schid); 475 setup_queues(irq_ptr, init_data); 476 477 irq_ptr->irq_poll = init_data->irq_poll; 478 set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state); 479 480 setup_qib(irq_ptr, init_data); 481 set_impl_params(irq_ptr, init_data->qib_param_field_format, 482 init_data->qib_param_field, 483 init_data->input_slib_elements, 484 init_data->output_slib_elements); 485 486 /* fill input and output descriptors */ 487 setup_qdr(irq_ptr, init_data); 488 489 /* qdr, qib, sls, slsbs, slibs, sbales are filled now */ 490 491 /* set our IRQ handler */ 492 spin_lock_irq(get_ccwdev_lock(cdev)); 493 irq_ptr->orig_handler = cdev->handler; 494 cdev->handler = qdio_int_handler; 495 spin_unlock_irq(get_ccwdev_lock(cdev)); 496 497 /* get qdio commands */ 498 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_EQUEUE); 499 if (!ciw) { 500 DBF_ERROR("%4x NO EQ", irq_ptr->schid.sch_no); 501 return -EINVAL; 502 } 503 irq_ptr->equeue = *ciw; 504 505 ciw = ccw_device_get_ciw(cdev, CIW_TYPE_AQUEUE); 506 if (!ciw) { 507 DBF_ERROR("%4x NO AQ", irq_ptr->schid.sch_no); 508 return -EINVAL; 509 } 510 irq_ptr->aqueue = *ciw; 511 512 return 0; 513 } 514 515 void qdio_shutdown_irq(struct qdio_irq *irq) 516 { 517 struct ccw_device *cdev = irq->cdev; 518 519 /* restore IRQ handler */ 520 spin_lock_irq(get_ccwdev_lock(cdev)); 521 cdev->handler = irq->orig_handler; 522 cdev->private->intparm = 0; 523 spin_unlock_irq(get_ccwdev_lock(cdev)); 524 } 525 526 void qdio_print_subchannel_info(struct qdio_irq *irq_ptr) 527 { 528 char s[80]; 529 530 snprintf(s, 80, "qdio: %s %s on SC %x using " 531 "AI:%d QEBSM:%d PRI:%d TDD:%d SIGA:%s%s%s%s%s\n", 532 dev_name(&irq_ptr->cdev->dev), 533 (irq_ptr->qib.qfmt == QDIO_QETH_QFMT) ? "OSA" : 534 ((irq_ptr->qib.qfmt == QDIO_ZFCP_QFMT) ? "ZFCP" : "HS"), 535 irq_ptr->schid.sch_no, 536 is_thinint_irq(irq_ptr), 537 (irq_ptr->sch_token) ? 1 : 0, 538 pci_out_supported(irq_ptr) ? 1 : 0, 539 css_general_characteristics.aif_tdd, 540 (irq_ptr->siga_flag.input) ? "R" : " ", 541 (irq_ptr->siga_flag.output) ? "W" : " ", 542 (irq_ptr->siga_flag.sync) ? "S" : " ", 543 (irq_ptr->siga_flag.sync_after_ai) ? "A" : " ", 544 (irq_ptr->siga_flag.sync_out_after_pci) ? "P" : " "); 545 printk(KERN_INFO "%s", s); 546 } 547 548 int qdio_enable_async_operation(struct qdio_output_q *outq) 549 { 550 outq->aobs = kcalloc(QDIO_MAX_BUFFERS_PER_Q, sizeof(struct qaob *), 551 GFP_KERNEL); 552 if (!outq->aobs) { 553 outq->use_cq = 0; 554 return -ENOMEM; 555 } 556 outq->use_cq = 1; 557 return 0; 558 } 559 560 void qdio_disable_async_operation(struct qdio_output_q *q) 561 { 562 kfree(q->aobs); 563 q->aobs = NULL; 564 q->use_cq = 0; 565 } 566 567 int __init qdio_setup_init(void) 568 { 569 int rc; 570 571 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q), 572 256, 0, NULL); 573 if (!qdio_q_cache) 574 return -ENOMEM; 575 576 qdio_aob_cache = kmem_cache_create("qdio_aob", 577 sizeof(struct qaob), 578 sizeof(struct qaob), 579 0, 580 NULL); 581 if (!qdio_aob_cache) { 582 rc = -ENOMEM; 583 goto free_qdio_q_cache; 584 } 585 586 /* Check for OSA/FCP thin interrupts (bit 67). */ 587 DBF_EVENT("thinint:%1d", 588 (css_general_characteristics.aif_osa) ? 1 : 0); 589 590 /* Check for QEBSM support in general (bit 58). */ 591 DBF_EVENT("cssQEBSM:%1d", (qebsm_possible()) ? 1 : 0); 592 rc = 0; 593 out: 594 return rc; 595 free_qdio_q_cache: 596 kmem_cache_destroy(qdio_q_cache); 597 goto out; 598 } 599 600 void qdio_setup_exit(void) 601 { 602 kmem_cache_destroy(qdio_aob_cache); 603 kmem_cache_destroy(qdio_q_cache); 604 } 605