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