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 28 /** 29 * qdio_free_buffers() - free qdio buffers 30 * @buf: array of pointers to qdio buffers 31 * @count: number of qdio buffers to free 32 */ 33 void qdio_free_buffers(struct qdio_buffer **buf, unsigned int count) 34 { 35 int pos; 36 37 for (pos = 0; pos < count; pos += QBUFF_PER_PAGE) 38 free_page((unsigned long) buf[pos]); 39 } 40 EXPORT_SYMBOL_GPL(qdio_free_buffers); 41 42 /** 43 * qdio_alloc_buffers() - allocate qdio buffers 44 * @buf: array of pointers to qdio buffers 45 * @count: number of qdio buffers to allocate 46 */ 47 int qdio_alloc_buffers(struct qdio_buffer **buf, unsigned int count) 48 { 49 int pos; 50 51 for (pos = 0; pos < count; pos += QBUFF_PER_PAGE) { 52 buf[pos] = (void *) get_zeroed_page(GFP_KERNEL); 53 if (!buf[pos]) { 54 qdio_free_buffers(buf, count); 55 return -ENOMEM; 56 } 57 } 58 for (pos = 0; pos < count; pos++) 59 if (pos % QBUFF_PER_PAGE) 60 buf[pos] = buf[pos - 1] + 1; 61 return 0; 62 } 63 EXPORT_SYMBOL_GPL(qdio_alloc_buffers); 64 65 /** 66 * qdio_reset_buffers() - reset qdio buffers 67 * @buf: array of pointers to qdio buffers 68 * @count: number of qdio buffers that will be zeroed 69 */ 70 void qdio_reset_buffers(struct qdio_buffer **buf, unsigned int count) 71 { 72 int pos; 73 74 for (pos = 0; pos < count; pos++) 75 memset(buf[pos], 0, sizeof(struct qdio_buffer)); 76 } 77 EXPORT_SYMBOL_GPL(qdio_reset_buffers); 78 79 static void __qdio_free_queues(struct qdio_q **queues, unsigned int count) 80 { 81 struct qdio_q *q; 82 unsigned int i; 83 84 for (i = 0; i < count; i++) { 85 q = queues[i]; 86 free_page((unsigned long) q->slib); 87 kmem_cache_free(qdio_q_cache, q); 88 } 89 } 90 91 void qdio_free_queues(struct qdio_irq *irq_ptr) 92 { 93 __qdio_free_queues(irq_ptr->input_qs, irq_ptr->max_input_qs); 94 irq_ptr->max_input_qs = 0; 95 96 __qdio_free_queues(irq_ptr->output_qs, irq_ptr->max_output_qs); 97 irq_ptr->max_output_qs = 0; 98 } 99 100 static int __qdio_allocate_qs(struct qdio_q **irq_ptr_qs, int nr_queues) 101 { 102 struct qdio_q *q; 103 int i; 104 105 for (i = 0; i < nr_queues; i++) { 106 q = kmem_cache_zalloc(qdio_q_cache, GFP_KERNEL); 107 if (!q) { 108 __qdio_free_queues(irq_ptr_qs, i); 109 return -ENOMEM; 110 } 111 112 q->slib = (struct slib *) __get_free_page(GFP_KERNEL); 113 if (!q->slib) { 114 kmem_cache_free(qdio_q_cache, q); 115 __qdio_free_queues(irq_ptr_qs, i); 116 return -ENOMEM; 117 } 118 irq_ptr_qs[i] = q; 119 } 120 return 0; 121 } 122 123 int qdio_allocate_qs(struct qdio_irq *irq_ptr, int nr_input_qs, int nr_output_qs) 124 { 125 int rc; 126 127 rc = __qdio_allocate_qs(irq_ptr->input_qs, nr_input_qs); 128 if (rc) 129 return rc; 130 131 rc = __qdio_allocate_qs(irq_ptr->output_qs, nr_output_qs); 132 if (rc) { 133 __qdio_free_queues(irq_ptr->input_qs, nr_input_qs); 134 return rc; 135 } 136 137 irq_ptr->max_input_qs = nr_input_qs; 138 irq_ptr->max_output_qs = nr_output_qs; 139 return 0; 140 } 141 142 static void setup_queues_misc(struct qdio_q *q, struct qdio_irq *irq_ptr, 143 qdio_handler_t *handler, int i) 144 { 145 struct slib *slib = q->slib; 146 147 /* queue must be cleared for qdio_establish */ 148 memset(q, 0, sizeof(*q)); 149 memset(slib, 0, PAGE_SIZE); 150 q->slib = slib; 151 q->irq_ptr = irq_ptr; 152 q->mask = 1 << (31 - i); 153 q->nr = i; 154 q->handler = handler; 155 } 156 157 static void setup_storage_lists(struct qdio_q *q, struct qdio_irq *irq_ptr, 158 struct qdio_buffer **sbals_array, int i) 159 { 160 struct qdio_q *prev; 161 int j; 162 163 DBF_HEX(&q, sizeof(void *)); 164 q->sl = (struct sl *)((char *)q->slib + PAGE_SIZE / 2); 165 166 /* fill in sbal */ 167 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++) 168 q->sbal[j] = *sbals_array++; 169 170 /* fill in slib */ 171 if (i > 0) { 172 prev = (q->is_input_q) ? irq_ptr->input_qs[i - 1] 173 : irq_ptr->output_qs[i - 1]; 174 prev->slib->nsliba = (unsigned long)q->slib; 175 } 176 177 q->slib->sla = (unsigned long)q->sl; 178 q->slib->slsba = (unsigned long)&q->slsb.val[0]; 179 180 /* fill in sl */ 181 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++) 182 q->sl->element[j].sbal = virt_to_dma64(q->sbal[j]); 183 } 184 185 static void setup_queues(struct qdio_irq *irq_ptr, 186 struct qdio_initialize *qdio_init) 187 { 188 struct qdio_q *q; 189 int i; 190 191 for_each_input_queue(irq_ptr, q, i) { 192 DBF_EVENT("inq:%1d", i); 193 setup_queues_misc(q, irq_ptr, qdio_init->input_handler, i); 194 195 q->is_input_q = 1; 196 197 setup_storage_lists(q, irq_ptr, 198 qdio_init->input_sbal_addr_array[i], i); 199 } 200 201 for_each_output_queue(irq_ptr, q, i) { 202 DBF_EVENT("outq:%1d", i); 203 setup_queues_misc(q, irq_ptr, qdio_init->output_handler, i); 204 205 q->is_input_q = 0; 206 setup_storage_lists(q, irq_ptr, 207 qdio_init->output_sbal_addr_array[i], i); 208 } 209 } 210 211 static void check_and_setup_qebsm(struct qdio_irq *irq_ptr, 212 unsigned char qdioac, unsigned long token) 213 { 214 if (!(irq_ptr->qib.rflags & QIB_RFLAGS_ENABLE_QEBSM)) 215 goto no_qebsm; 216 if (!(qdioac & AC1_SC_QEBSM_AVAILABLE) || 217 (!(qdioac & AC1_SC_QEBSM_ENABLED))) 218 goto no_qebsm; 219 220 irq_ptr->sch_token = token; 221 222 DBF_EVENT("V=V:1"); 223 DBF_EVENT("%8lx", irq_ptr->sch_token); 224 return; 225 226 no_qebsm: 227 irq_ptr->sch_token = 0; 228 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM; 229 DBF_EVENT("noV=V"); 230 } 231 232 /* 233 * If there is a qdio_irq we use the chsc_page and store the information 234 * in the qdio_irq, otherwise we copy it to the specified structure. 235 */ 236 int qdio_setup_get_ssqd(struct qdio_irq *irq_ptr, 237 struct subchannel_id *schid, 238 struct qdio_ssqd_desc *data) 239 { 240 struct chsc_ssqd_area *ssqd; 241 int rc; 242 243 DBF_EVENT("getssqd:%4x", schid->sch_no); 244 if (!irq_ptr) { 245 ssqd = (struct chsc_ssqd_area *)__get_free_page(GFP_KERNEL); 246 if (!ssqd) 247 return -ENOMEM; 248 } else { 249 ssqd = (struct chsc_ssqd_area *)irq_ptr->chsc_page; 250 } 251 252 rc = chsc_ssqd(*schid, ssqd); 253 if (rc) 254 goto out; 255 256 if (!(ssqd->qdio_ssqd.flags & CHSC_FLAG_QDIO_CAPABILITY) || 257 !(ssqd->qdio_ssqd.flags & CHSC_FLAG_VALIDITY) || 258 (ssqd->qdio_ssqd.sch != schid->sch_no)) 259 rc = -EINVAL; 260 261 if (!rc) 262 memcpy(data, &ssqd->qdio_ssqd, sizeof(*data)); 263 264 out: 265 if (!irq_ptr) 266 free_page((unsigned long)ssqd); 267 268 return rc; 269 } 270 271 void qdio_setup_ssqd_info(struct qdio_irq *irq_ptr) 272 { 273 unsigned char qdioac; 274 int rc; 275 276 rc = qdio_setup_get_ssqd(irq_ptr, &irq_ptr->schid, &irq_ptr->ssqd_desc); 277 if (rc) { 278 DBF_ERROR("%4x ssqd ERR", irq_ptr->schid.sch_no); 279 DBF_ERROR("rc:%x", rc); 280 /* all flags set, worst case */ 281 qdioac = AC1_SIGA_INPUT_NEEDED | AC1_SIGA_OUTPUT_NEEDED | 282 AC1_SIGA_SYNC_NEEDED; 283 } else 284 qdioac = irq_ptr->ssqd_desc.qdioac1; 285 286 check_and_setup_qebsm(irq_ptr, qdioac, irq_ptr->ssqd_desc.sch_token); 287 irq_ptr->qdioac1 = qdioac; 288 DBF_EVENT("ac 1:%2x 2:%4x", qdioac, irq_ptr->ssqd_desc.qdioac2); 289 DBF_EVENT("3:%4x qib:%4x", irq_ptr->ssqd_desc.qdioac3, irq_ptr->qib.ac); 290 } 291 292 static void qdio_fill_qdr_desc(struct qdesfmt0 *desc, struct qdio_q *queue) 293 { 294 desc->sliba = virt_to_dma64(queue->slib); 295 desc->sla = virt_to_dma64(queue->sl); 296 desc->slsba = virt_to_dma64(&queue->slsb); 297 298 desc->akey = PAGE_DEFAULT_KEY >> 4; 299 desc->bkey = PAGE_DEFAULT_KEY >> 4; 300 desc->ckey = PAGE_DEFAULT_KEY >> 4; 301 desc->dkey = PAGE_DEFAULT_KEY >> 4; 302 } 303 304 static void setup_qdr(struct qdio_irq *irq_ptr, 305 struct qdio_initialize *qdio_init) 306 { 307 struct qdesfmt0 *desc = &irq_ptr->qdr->qdf0[0]; 308 int i; 309 310 memset(irq_ptr->qdr, 0, sizeof(struct qdr)); 311 312 irq_ptr->qdr->qfmt = qdio_init->q_format; 313 irq_ptr->qdr->ac = qdio_init->qdr_ac; 314 irq_ptr->qdr->iqdcnt = qdio_init->no_input_qs; 315 irq_ptr->qdr->oqdcnt = qdio_init->no_output_qs; 316 irq_ptr->qdr->iqdsz = sizeof(struct qdesfmt0) / 4; /* size in words */ 317 irq_ptr->qdr->oqdsz = sizeof(struct qdesfmt0) / 4; 318 irq_ptr->qdr->qiba = virt_to_dma64(&irq_ptr->qib); 319 irq_ptr->qdr->qkey = PAGE_DEFAULT_KEY >> 4; 320 321 for (i = 0; i < qdio_init->no_input_qs; i++) 322 qdio_fill_qdr_desc(desc++, irq_ptr->input_qs[i]); 323 324 for (i = 0; i < qdio_init->no_output_qs; i++) 325 qdio_fill_qdr_desc(desc++, irq_ptr->output_qs[i]); 326 } 327 328 static void setup_qib(struct qdio_irq *irq_ptr, 329 struct qdio_initialize *init_data) 330 { 331 memset(&irq_ptr->qib, 0, sizeof(irq_ptr->qib)); 332 333 irq_ptr->qib.qfmt = init_data->q_format; 334 irq_ptr->qib.pfmt = init_data->qib_param_field_format; 335 336 irq_ptr->qib.rflags = init_data->qib_rflags; 337 if (css_general_characteristics.qebsm) 338 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM; 339 340 if (init_data->no_input_qs) 341 irq_ptr->qib.isliba = 342 (unsigned long)(irq_ptr->input_qs[0]->slib); 343 if (init_data->no_output_qs) 344 irq_ptr->qib.osliba = 345 (unsigned long)(irq_ptr->output_qs[0]->slib); 346 memcpy(irq_ptr->qib.ebcnam, dev_name(&irq_ptr->cdev->dev), 8); 347 ASCEBC(irq_ptr->qib.ebcnam, 8); 348 349 if (init_data->qib_param_field) 350 memcpy(irq_ptr->qib.parm, init_data->qib_param_field, 351 sizeof(irq_ptr->qib.parm)); 352 } 353 354 void qdio_setup_irq(struct qdio_irq *irq_ptr, struct qdio_initialize *init_data) 355 { 356 struct ccw_device *cdev = irq_ptr->cdev; 357 358 irq_ptr->qdioac1 = 0; 359 memset(&irq_ptr->ssqd_desc, 0, sizeof(irq_ptr->ssqd_desc)); 360 memset(&irq_ptr->perf_stat, 0, sizeof(irq_ptr->perf_stat)); 361 362 irq_ptr->debugfs_dev = NULL; 363 irq_ptr->sch_token = irq_ptr->perf_stat_enabled = 0; 364 irq_ptr->state = QDIO_IRQ_STATE_INACTIVE; 365 irq_ptr->error_handler = init_data->input_handler; 366 367 irq_ptr->int_parm = init_data->int_parm; 368 irq_ptr->nr_input_qs = init_data->no_input_qs; 369 irq_ptr->nr_output_qs = init_data->no_output_qs; 370 ccw_device_get_schid(cdev, &irq_ptr->schid); 371 setup_queues(irq_ptr, init_data); 372 373 irq_ptr->irq_poll = init_data->irq_poll; 374 set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state); 375 376 setup_qib(irq_ptr, init_data); 377 378 /* fill input and output descriptors */ 379 setup_qdr(irq_ptr, init_data); 380 381 /* qdr, qib, sls, slsbs, slibs, sbales are filled now */ 382 383 /* set our IRQ handler */ 384 spin_lock_irq(get_ccwdev_lock(cdev)); 385 irq_ptr->orig_handler = cdev->handler; 386 cdev->handler = qdio_int_handler; 387 spin_unlock_irq(get_ccwdev_lock(cdev)); 388 } 389 390 void qdio_shutdown_irq(struct qdio_irq *irq) 391 { 392 struct ccw_device *cdev = irq->cdev; 393 394 /* restore IRQ handler */ 395 spin_lock_irq(get_ccwdev_lock(cdev)); 396 cdev->handler = irq->orig_handler; 397 cdev->private->intparm = 0; 398 spin_unlock_irq(get_ccwdev_lock(cdev)); 399 } 400 401 void qdio_print_subchannel_info(struct qdio_irq *irq_ptr) 402 { 403 dev_info(&irq_ptr->cdev->dev, 404 "qdio: %s on SC %x using AI:%d QEBSM:%d PRI:%d TDD:%d SIGA:%s%s%s\n", 405 (irq_ptr->qib.qfmt == QDIO_QETH_QFMT) ? "OSA" : 406 ((irq_ptr->qib.qfmt == QDIO_ZFCP_QFMT) ? "ZFCP" : "HS"), 407 irq_ptr->schid.sch_no, 408 is_thinint_irq(irq_ptr), 409 (irq_ptr->sch_token) ? 1 : 0, 410 pci_out_supported(irq_ptr) ? 1 : 0, 411 css_general_characteristics.aif_tdd, 412 qdio_need_siga_in(irq_ptr) ? "R" : " ", 413 qdio_need_siga_out(irq_ptr) ? "W" : " ", 414 qdio_need_siga_sync(irq_ptr) ? "S" : " "); 415 } 416 417 int __init qdio_setup_init(void) 418 { 419 qdio_q_cache = kmem_cache_create("qdio_q", sizeof(struct qdio_q), 420 256, 0, NULL); 421 if (!qdio_q_cache) 422 return -ENOMEM; 423 424 /* Check for OSA/FCP thin interrupts (bit 67). */ 425 DBF_EVENT("thinint:%1d", 426 (css_general_characteristics.aif_osa) ? 1 : 0); 427 428 /* Check for QEBSM support in general (bit 58). */ 429 DBF_EVENT("cssQEBSM:%1d", css_general_characteristics.qebsm); 430 431 return 0; 432 } 433 434 void qdio_setup_exit(void) 435 { 436 kmem_cache_destroy(qdio_q_cache); 437 } 438