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