1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2016, 2023 4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com> 5 * 6 * Adjunct processor bus, queue related code. 7 */ 8 9 #define pr_fmt(fmt) "ap: " fmt 10 11 #include <linux/export.h> 12 #include <linux/init.h> 13 #include <linux/slab.h> 14 #include <asm/facility.h> 15 16 #define CREATE_TRACE_POINTS 17 #include <asm/trace/ap.h> 18 19 #include "ap_bus.h" 20 #include "ap_debug.h" 21 22 EXPORT_TRACEPOINT_SYMBOL(s390_ap_nqap); 23 EXPORT_TRACEPOINT_SYMBOL(s390_ap_dqap); 24 25 static void __ap_flush_queue(struct ap_queue *aq); 26 27 /* 28 * some AP queue helper functions 29 */ 30 31 static inline bool ap_q_supported_in_se(struct ap_queue *aq) 32 { 33 return aq->card->hwinfo.ep11 || aq->card->hwinfo.accel; 34 } 35 36 static inline bool ap_q_supports_bind(struct ap_queue *aq) 37 { 38 return aq->card->hwinfo.ep11 || aq->card->hwinfo.accel; 39 } 40 41 static inline bool ap_q_supports_assoc(struct ap_queue *aq) 42 { 43 return aq->card->hwinfo.ep11; 44 } 45 46 static inline bool ap_q_needs_bind(struct ap_queue *aq) 47 { 48 return ap_q_supports_bind(aq) && ap_sb_available(); 49 } 50 51 /** 52 * ap_queue_enable_irq(): Enable interrupt support on this AP queue. 53 * @aq: The AP queue 54 * @ind: the notification indicator byte 55 * 56 * Enables interruption on AP queue via ap_aqic(). Based on the return 57 * value it waits a while and tests the AP queue if interrupts 58 * have been switched on using ap_test_queue(). 59 */ 60 static int ap_queue_enable_irq(struct ap_queue *aq, void *ind) 61 { 62 union ap_qirq_ctrl qirqctrl = { .value = 0 }; 63 struct ap_queue_status status; 64 65 qirqctrl.ir = 1; 66 qirqctrl.isc = AP_ISC; 67 status = ap_aqic(aq->qid, qirqctrl, virt_to_phys(ind)); 68 if (status.async) 69 return -EPERM; 70 switch (status.response_code) { 71 case AP_RESPONSE_NORMAL: 72 case AP_RESPONSE_OTHERWISE_CHANGED: 73 return 0; 74 case AP_RESPONSE_Q_NOT_AVAIL: 75 case AP_RESPONSE_DECONFIGURED: 76 case AP_RESPONSE_CHECKSTOPPED: 77 case AP_RESPONSE_INVALID_ADDRESS: 78 pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n", 79 AP_QID_CARD(aq->qid), 80 AP_QID_QUEUE(aq->qid)); 81 return -EOPNOTSUPP; 82 case AP_RESPONSE_RESET_IN_PROGRESS: 83 case AP_RESPONSE_BUSY: 84 default: 85 return -EBUSY; 86 } 87 } 88 89 /** 90 * __ap_send(): Send message to adjunct processor queue. 91 * @qid: The AP queue number 92 * @psmid: The program supplied message identifier 93 * @msg: The message text 94 * @msglen: The message length 95 * @special: Special Bit 96 * 97 * Returns AP queue status structure. 98 * Condition code 1 on NQAP can't happen because the L bit is 1. 99 * Condition code 2 on NQAP also means the send is incomplete, 100 * because a segment boundary was reached. The NQAP is repeated. 101 */ 102 static inline struct ap_queue_status 103 __ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen, 104 int special) 105 { 106 struct ap_queue_status status; 107 108 if (special) 109 qid |= 0x400000UL; 110 111 status = ap_nqap(qid, psmid, msg, msglen); 112 113 trace_s390_ap_nqap(AP_QID_CARD(qid), AP_QID_QUEUE(qid), 114 status.value, psmid); 115 116 return status; 117 } 118 119 /* State machine definitions and helpers */ 120 121 static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq) 122 { 123 return AP_SM_WAIT_NONE; 124 } 125 126 /** 127 * ap_sm_recv(): Receive pending reply messages from an AP queue but do 128 * not change the state of the device. 129 * @aq: pointer to the AP queue 130 * 131 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT 132 */ 133 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq) 134 { 135 struct ap_queue_status status; 136 struct ap_message *ap_msg; 137 bool found = false; 138 size_t reslen; 139 unsigned long resgr0 = 0; 140 int parts = 0; 141 142 /* 143 * DQAP loop until response code and resgr0 indicate that 144 * the msg is totally received. As we use the very same buffer 145 * the msg is overwritten with each invocation. That's intended 146 * and the receiver of the msg is informed with a msg rc code 147 * of EMSGSIZE in such a case. 148 */ 149 do { 150 status = ap_dqap(aq->qid, &aq->reply->psmid, 151 aq->reply->msg, aq->reply->bufsize, 152 &aq->reply->len, &reslen, &resgr0); 153 parts++; 154 } while (status.response_code == 0xFF && resgr0 != 0); 155 156 trace_s390_ap_dqap(AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid), 157 status.value, aq->reply->psmid); 158 159 switch (status.response_code) { 160 case AP_RESPONSE_NORMAL: 161 print_hex_dump_debug("aprpl: ", DUMP_PREFIX_ADDRESS, 16, 1, 162 aq->reply->msg, aq->reply->len, false); 163 aq->queue_count = max_t(int, 0, aq->queue_count - 1); 164 if (!status.queue_empty && !aq->queue_count) 165 aq->queue_count++; 166 if (aq->queue_count > 0) 167 mod_timer(&aq->timeout, 168 jiffies + aq->request_timeout); 169 list_for_each_entry(ap_msg, &aq->pendingq, list) { 170 if (ap_msg->psmid != aq->reply->psmid) 171 continue; 172 list_del_init(&ap_msg->list); 173 aq->pendingq_count--; 174 if (parts > 1) { 175 ap_msg->rc = -EMSGSIZE; 176 ap_msg->receive(aq, ap_msg, NULL); 177 } else { 178 ap_msg->receive(aq, ap_msg, aq->reply); 179 } 180 found = true; 181 break; 182 } 183 if (!found) { 184 AP_DBF_WARN("%s unassociated reply psmid=0x%016lx on 0x%02x.%04x\n", 185 __func__, aq->reply->psmid, 186 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 187 } 188 fallthrough; 189 case AP_RESPONSE_NO_PENDING_REPLY: 190 if (!status.queue_empty || aq->queue_count <= 0) 191 break; 192 /* The card shouldn't forget requests but who knows. */ 193 aq->queue_count = 0; 194 list_splice_init(&aq->pendingq, &aq->requestq); 195 aq->requestq_count += aq->pendingq_count; 196 pr_debug("queue 0x%02x.%04x rescheduled %d reqs (new req %d)\n", 197 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid), 198 aq->pendingq_count, aq->requestq_count); 199 aq->pendingq_count = 0; 200 break; 201 default: 202 break; 203 } 204 return status; 205 } 206 207 /** 208 * ap_sm_read(): Receive pending reply messages from an AP queue. 209 * @aq: pointer to the AP queue 210 * 211 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT 212 */ 213 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq) 214 { 215 struct ap_queue_status status; 216 217 if (!aq->reply) 218 return AP_SM_WAIT_NONE; 219 status = ap_sm_recv(aq); 220 if (status.async) 221 return AP_SM_WAIT_NONE; 222 switch (status.response_code) { 223 case AP_RESPONSE_NORMAL: 224 if (aq->queue_count > 0) { 225 aq->sm_state = AP_SM_STATE_WORKING; 226 return AP_SM_WAIT_AGAIN; 227 } 228 aq->sm_state = AP_SM_STATE_IDLE; 229 break; 230 case AP_RESPONSE_NO_PENDING_REPLY: 231 if (aq->queue_count > 0) 232 return status.irq_enabled ? 233 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_HIGH_TIMEOUT; 234 aq->sm_state = AP_SM_STATE_IDLE; 235 break; 236 default: 237 aq->dev_state = AP_DEV_STATE_ERROR; 238 aq->last_err_rc = status.response_code; 239 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 240 __func__, status.response_code, 241 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 242 return AP_SM_WAIT_NONE; 243 } 244 /* Check and maybe enable irq support (again) on this queue */ 245 if (!status.irq_enabled && status.queue_empty) { 246 void *lsi_ptr = ap_airq_ptr(); 247 248 if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0) { 249 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT; 250 return AP_SM_WAIT_AGAIN; 251 } 252 } 253 return AP_SM_WAIT_NONE; 254 } 255 256 /** 257 * ap_sm_write(): Send messages from the request queue to an AP queue. 258 * @aq: pointer to the AP queue 259 * 260 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT 261 */ 262 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq) 263 { 264 struct ap_queue_status status; 265 struct ap_message *ap_msg; 266 ap_qid_t qid = aq->qid; 267 268 if (aq->requestq_count <= 0) 269 return AP_SM_WAIT_NONE; 270 271 /* Start the next request on the queue. */ 272 ap_msg = list_entry(aq->requestq.next, struct ap_message, list); 273 print_hex_dump_debug("apreq: ", DUMP_PREFIX_ADDRESS, 16, 1, 274 ap_msg->msg, ap_msg->len, false); 275 status = __ap_send(qid, ap_msg->psmid, 276 ap_msg->msg, ap_msg->len, 277 ap_msg->flags & AP_MSG_FLAG_SPECIAL); 278 if (status.async) 279 return AP_SM_WAIT_NONE; 280 switch (status.response_code) { 281 case AP_RESPONSE_NORMAL: 282 aq->queue_count = max_t(int, 1, aq->queue_count + 1); 283 if (aq->queue_count == 1) 284 mod_timer(&aq->timeout, jiffies + aq->request_timeout); 285 list_move_tail(&ap_msg->list, &aq->pendingq); 286 aq->requestq_count--; 287 aq->pendingq_count++; 288 if (aq->queue_count < aq->card->hwinfo.qd + 1) { 289 aq->sm_state = AP_SM_STATE_WORKING; 290 return AP_SM_WAIT_AGAIN; 291 } 292 fallthrough; 293 case AP_RESPONSE_Q_FULL: 294 aq->sm_state = AP_SM_STATE_QUEUE_FULL; 295 return status.irq_enabled ? 296 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_HIGH_TIMEOUT; 297 case AP_RESPONSE_RESET_IN_PROGRESS: 298 aq->sm_state = AP_SM_STATE_RESET_WAIT; 299 return AP_SM_WAIT_LOW_TIMEOUT; 300 case AP_RESPONSE_INVALID_DOMAIN: 301 AP_DBF_WARN("%s RESPONSE_INVALID_DOMAIN on NQAP\n", __func__); 302 fallthrough; 303 case AP_RESPONSE_MESSAGE_TOO_BIG: 304 case AP_RESPONSE_REQ_FAC_NOT_INST: 305 list_del_init(&ap_msg->list); 306 aq->requestq_count--; 307 ap_msg->rc = -EINVAL; 308 ap_msg->receive(aq, ap_msg, NULL); 309 return AP_SM_WAIT_AGAIN; 310 default: 311 aq->dev_state = AP_DEV_STATE_ERROR; 312 aq->last_err_rc = status.response_code; 313 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 314 __func__, status.response_code, 315 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 316 return AP_SM_WAIT_NONE; 317 } 318 } 319 320 /** 321 * ap_sm_read_write(): Send and receive messages to/from an AP queue. 322 * @aq: pointer to the AP queue 323 * 324 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT 325 */ 326 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq) 327 { 328 return min(ap_sm_read(aq), ap_sm_write(aq)); 329 } 330 331 /** 332 * ap_sm_reset(): Reset an AP queue. 333 * @aq: The AP queue 334 * 335 * Submit the Reset command to an AP queue. 336 */ 337 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq) 338 { 339 struct ap_queue_status status; 340 341 status = ap_rapq(aq->qid, aq->rapq_fbit); 342 if (status.async) 343 return AP_SM_WAIT_NONE; 344 switch (status.response_code) { 345 case AP_RESPONSE_NORMAL: 346 case AP_RESPONSE_RESET_IN_PROGRESS: 347 aq->sm_state = AP_SM_STATE_RESET_WAIT; 348 aq->rapq_fbit = 0; 349 return AP_SM_WAIT_LOW_TIMEOUT; 350 default: 351 aq->dev_state = AP_DEV_STATE_ERROR; 352 aq->last_err_rc = status.response_code; 353 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 354 __func__, status.response_code, 355 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 356 return AP_SM_WAIT_NONE; 357 } 358 } 359 360 /** 361 * ap_sm_reset_wait(): Test queue for completion of the reset operation 362 * @aq: pointer to the AP queue 363 * 364 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0. 365 */ 366 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq) 367 { 368 struct ap_queue_status status; 369 struct ap_tapq_hwinfo hwinfo; 370 void *lsi_ptr; 371 372 /* Get the status with TAPQ */ 373 status = ap_test_queue(aq->qid, 1, &hwinfo); 374 375 switch (status.response_code) { 376 case AP_RESPONSE_NORMAL: 377 aq->se_bstate = hwinfo.bs; 378 lsi_ptr = ap_airq_ptr(); 379 if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0) 380 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT; 381 else 382 aq->sm_state = (aq->queue_count > 0) ? 383 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE; 384 return AP_SM_WAIT_AGAIN; 385 case AP_RESPONSE_BUSY: 386 case AP_RESPONSE_RESET_IN_PROGRESS: 387 return AP_SM_WAIT_LOW_TIMEOUT; 388 case AP_RESPONSE_Q_NOT_AVAIL: 389 case AP_RESPONSE_DECONFIGURED: 390 case AP_RESPONSE_CHECKSTOPPED: 391 default: 392 aq->dev_state = AP_DEV_STATE_ERROR; 393 aq->last_err_rc = status.response_code; 394 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 395 __func__, status.response_code, 396 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 397 return AP_SM_WAIT_NONE; 398 } 399 } 400 401 /** 402 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement 403 * @aq: pointer to the AP queue 404 * 405 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0. 406 */ 407 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq) 408 { 409 struct ap_queue_status status; 410 411 if (aq->queue_count > 0 && aq->reply) 412 /* Try to read a completed message and get the status */ 413 status = ap_sm_recv(aq); 414 else 415 /* Get the status with TAPQ */ 416 status = ap_tapq(aq->qid, NULL); 417 418 if (status.irq_enabled == 1) { 419 /* Irqs are now enabled */ 420 aq->sm_state = (aq->queue_count > 0) ? 421 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE; 422 } 423 424 switch (status.response_code) { 425 case AP_RESPONSE_NORMAL: 426 if (aq->queue_count > 0) 427 return AP_SM_WAIT_AGAIN; 428 fallthrough; 429 case AP_RESPONSE_NO_PENDING_REPLY: 430 return AP_SM_WAIT_LOW_TIMEOUT; 431 default: 432 aq->dev_state = AP_DEV_STATE_ERROR; 433 aq->last_err_rc = status.response_code; 434 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 435 __func__, status.response_code, 436 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 437 return AP_SM_WAIT_NONE; 438 } 439 } 440 441 /** 442 * ap_sm_assoc_wait(): Test queue for completion of a pending 443 * association request. 444 * @aq: pointer to the AP queue 445 */ 446 static enum ap_sm_wait ap_sm_assoc_wait(struct ap_queue *aq) 447 { 448 struct ap_queue_status status; 449 struct ap_tapq_hwinfo hwinfo; 450 451 status = ap_test_queue(aq->qid, 1, &hwinfo); 452 /* handle asynchronous error on this queue */ 453 if (status.async && status.response_code) { 454 aq->dev_state = AP_DEV_STATE_ERROR; 455 aq->last_err_rc = status.response_code; 456 AP_DBF_WARN("%s asynch RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 457 __func__, status.response_code, 458 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 459 return AP_SM_WAIT_NONE; 460 } 461 if (status.response_code > AP_RESPONSE_BUSY) { 462 aq->dev_state = AP_DEV_STATE_ERROR; 463 aq->last_err_rc = status.response_code; 464 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 465 __func__, status.response_code, 466 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 467 return AP_SM_WAIT_NONE; 468 } 469 470 /* update queue's SE bind state */ 471 aq->se_bstate = hwinfo.bs; 472 473 /* check bs bits */ 474 switch (hwinfo.bs) { 475 case AP_BS_Q_USABLE: 476 /* association is through */ 477 aq->sm_state = AP_SM_STATE_IDLE; 478 pr_debug("queue 0x%02x.%04x associated with %u\n", 479 AP_QID_CARD(aq->qid), 480 AP_QID_QUEUE(aq->qid), aq->assoc_idx); 481 ap_send_se_assoc_uevent(&aq->ap_dev, aq->assoc_idx); 482 return AP_SM_WAIT_NONE; 483 case AP_BS_Q_USABLE_NO_SECURE_KEY: 484 /* association still pending */ 485 return AP_SM_WAIT_LOW_TIMEOUT; 486 default: 487 /* reset from 'outside' happened or no idea at all */ 488 aq->assoc_idx = ASSOC_IDX_INVALID; 489 aq->dev_state = AP_DEV_STATE_ERROR; 490 aq->last_err_rc = status.response_code; 491 AP_DBF_WARN("%s bs 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n", 492 __func__, hwinfo.bs, 493 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 494 return AP_SM_WAIT_NONE; 495 } 496 } 497 498 /* 499 * AP state machine jump table 500 */ 501 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = { 502 [AP_SM_STATE_RESET_START] = { 503 [AP_SM_EVENT_POLL] = ap_sm_reset, 504 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop, 505 }, 506 [AP_SM_STATE_RESET_WAIT] = { 507 [AP_SM_EVENT_POLL] = ap_sm_reset_wait, 508 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop, 509 }, 510 [AP_SM_STATE_SETIRQ_WAIT] = { 511 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait, 512 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop, 513 }, 514 [AP_SM_STATE_IDLE] = { 515 [AP_SM_EVENT_POLL] = ap_sm_write, 516 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop, 517 }, 518 [AP_SM_STATE_WORKING] = { 519 [AP_SM_EVENT_POLL] = ap_sm_read_write, 520 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset, 521 }, 522 [AP_SM_STATE_QUEUE_FULL] = { 523 [AP_SM_EVENT_POLL] = ap_sm_read, 524 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset, 525 }, 526 [AP_SM_STATE_ASSOC_WAIT] = { 527 [AP_SM_EVENT_POLL] = ap_sm_assoc_wait, 528 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset, 529 }, 530 }; 531 532 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event) 533 { 534 if (aq->config && !aq->chkstop && 535 aq->dev_state > AP_DEV_STATE_UNINITIATED) 536 return ap_jumptable[aq->sm_state][event](aq); 537 else 538 return AP_SM_WAIT_NONE; 539 } 540 541 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event) 542 { 543 enum ap_sm_wait wait; 544 545 while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN) 546 ; 547 return wait; 548 } 549 550 /* 551 * AP queue related attributes. 552 */ 553 static ssize_t request_count_show(struct device *dev, 554 struct device_attribute *attr, 555 char *buf) 556 { 557 struct ap_queue *aq = to_ap_queue(dev); 558 bool valid = false; 559 u64 req_cnt; 560 561 spin_lock_bh(&aq->lock); 562 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 563 req_cnt = aq->total_request_count; 564 valid = true; 565 } 566 spin_unlock_bh(&aq->lock); 567 568 if (valid) 569 return sysfs_emit(buf, "%llu\n", req_cnt); 570 else 571 return sysfs_emit(buf, "-\n"); 572 } 573 574 static ssize_t request_count_store(struct device *dev, 575 struct device_attribute *attr, 576 const char *buf, size_t count) 577 { 578 struct ap_queue *aq = to_ap_queue(dev); 579 580 spin_lock_bh(&aq->lock); 581 aq->total_request_count = 0; 582 spin_unlock_bh(&aq->lock); 583 584 return count; 585 } 586 587 static DEVICE_ATTR_RW(request_count); 588 589 static ssize_t requestq_count_show(struct device *dev, 590 struct device_attribute *attr, char *buf) 591 { 592 struct ap_queue *aq = to_ap_queue(dev); 593 unsigned int reqq_cnt = 0; 594 595 spin_lock_bh(&aq->lock); 596 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) 597 reqq_cnt = aq->requestq_count; 598 spin_unlock_bh(&aq->lock); 599 return sysfs_emit(buf, "%d\n", reqq_cnt); 600 } 601 602 static DEVICE_ATTR_RO(requestq_count); 603 604 static ssize_t pendingq_count_show(struct device *dev, 605 struct device_attribute *attr, char *buf) 606 { 607 struct ap_queue *aq = to_ap_queue(dev); 608 unsigned int penq_cnt = 0; 609 610 spin_lock_bh(&aq->lock); 611 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) 612 penq_cnt = aq->pendingq_count; 613 spin_unlock_bh(&aq->lock); 614 return sysfs_emit(buf, "%d\n", penq_cnt); 615 } 616 617 static DEVICE_ATTR_RO(pendingq_count); 618 619 static ssize_t reset_show(struct device *dev, 620 struct device_attribute *attr, char *buf) 621 { 622 struct ap_queue *aq = to_ap_queue(dev); 623 int rc = 0; 624 625 spin_lock_bh(&aq->lock); 626 switch (aq->sm_state) { 627 case AP_SM_STATE_RESET_START: 628 case AP_SM_STATE_RESET_WAIT: 629 rc = sysfs_emit(buf, "Reset in progress.\n"); 630 break; 631 case AP_SM_STATE_WORKING: 632 case AP_SM_STATE_QUEUE_FULL: 633 rc = sysfs_emit(buf, "Reset Timer armed.\n"); 634 break; 635 default: 636 rc = sysfs_emit(buf, "No Reset Timer set.\n"); 637 } 638 spin_unlock_bh(&aq->lock); 639 return rc; 640 } 641 642 static ssize_t reset_store(struct device *dev, 643 struct device_attribute *attr, 644 const char *buf, size_t count) 645 { 646 struct ap_queue *aq = to_ap_queue(dev); 647 648 spin_lock_bh(&aq->lock); 649 __ap_flush_queue(aq); 650 aq->sm_state = AP_SM_STATE_RESET_START; 651 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL)); 652 spin_unlock_bh(&aq->lock); 653 654 AP_DBF_INFO("%s reset queue=%02x.%04x triggered by user\n", 655 __func__, AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 656 657 return count; 658 } 659 660 static DEVICE_ATTR_RW(reset); 661 662 static ssize_t interrupt_show(struct device *dev, 663 struct device_attribute *attr, char *buf) 664 { 665 struct ap_queue *aq = to_ap_queue(dev); 666 struct ap_queue_status status; 667 int rc = 0; 668 669 spin_lock_bh(&aq->lock); 670 if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT) { 671 rc = sysfs_emit(buf, "Enable Interrupt pending.\n"); 672 } else { 673 status = ap_tapq(aq->qid, NULL); 674 if (status.irq_enabled) 675 rc = sysfs_emit(buf, "Interrupts enabled.\n"); 676 else 677 rc = sysfs_emit(buf, "Interrupts disabled.\n"); 678 } 679 spin_unlock_bh(&aq->lock); 680 681 return rc; 682 } 683 684 static DEVICE_ATTR_RO(interrupt); 685 686 static ssize_t config_show(struct device *dev, 687 struct device_attribute *attr, char *buf) 688 { 689 struct ap_queue *aq = to_ap_queue(dev); 690 int rc; 691 692 spin_lock_bh(&aq->lock); 693 rc = sysfs_emit(buf, "%d\n", aq->config ? 1 : 0); 694 spin_unlock_bh(&aq->lock); 695 return rc; 696 } 697 698 static DEVICE_ATTR_RO(config); 699 700 static ssize_t chkstop_show(struct device *dev, 701 struct device_attribute *attr, char *buf) 702 { 703 struct ap_queue *aq = to_ap_queue(dev); 704 int rc; 705 706 spin_lock_bh(&aq->lock); 707 rc = sysfs_emit(buf, "%d\n", aq->chkstop ? 1 : 0); 708 spin_unlock_bh(&aq->lock); 709 return rc; 710 } 711 712 static DEVICE_ATTR_RO(chkstop); 713 714 static ssize_t ap_functions_show(struct device *dev, 715 struct device_attribute *attr, char *buf) 716 { 717 struct ap_queue *aq = to_ap_queue(dev); 718 struct ap_queue_status status; 719 struct ap_tapq_hwinfo hwinfo; 720 721 status = ap_test_queue(aq->qid, 1, &hwinfo); 722 if (status.response_code > AP_RESPONSE_BUSY) { 723 pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n", 724 status.response_code, 725 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 726 return -EIO; 727 } 728 729 return sysfs_emit(buf, "0x%08X\n", hwinfo.fac); 730 } 731 732 static DEVICE_ATTR_RO(ap_functions); 733 734 static ssize_t driver_override_show(struct device *dev, 735 struct device_attribute *attr, 736 char *buf) 737 { 738 guard(spinlock)(&dev->driver_override.lock); 739 return sysfs_emit(buf, "%s\n", dev->driver_override.name ?: ""); 740 } 741 742 static ssize_t driver_override_store(struct device *dev, 743 struct device_attribute *attr, 744 const char *buf, size_t count) 745 { 746 int rc = -EINVAL; 747 bool old_value; 748 749 if (mutex_lock_interruptible(&ap_attr_mutex)) 750 return -ERESTARTSYS; 751 752 /* Do not allow driver override if apmask/aqmask is in use */ 753 if (ap_apmask_aqmask_in_use) 754 goto out; 755 756 old_value = device_has_driver_override(dev); 757 rc = __device_set_driver_override(dev, buf, count); 758 if (rc) 759 goto out; 760 if (old_value && !device_has_driver_override(dev)) 761 --ap_driver_override_ctr; 762 else if (!old_value && device_has_driver_override(dev)) 763 ++ap_driver_override_ctr; 764 765 rc = count; 766 767 out: 768 mutex_unlock(&ap_attr_mutex); 769 return rc; 770 } 771 772 static DEVICE_ATTR_RW(driver_override); 773 774 #ifdef CONFIG_AP_DEBUG 775 static ssize_t states_show(struct device *dev, 776 struct device_attribute *attr, char *buf) 777 { 778 struct ap_queue *aq = to_ap_queue(dev); 779 int rc = 0; 780 781 spin_lock_bh(&aq->lock); 782 /* queue device state */ 783 switch (aq->dev_state) { 784 case AP_DEV_STATE_UNINITIATED: 785 rc = sysfs_emit(buf, "UNINITIATED\n"); 786 break; 787 case AP_DEV_STATE_OPERATING: 788 rc = sysfs_emit(buf, "OPERATING"); 789 break; 790 case AP_DEV_STATE_SHUTDOWN: 791 rc = sysfs_emit(buf, "SHUTDOWN"); 792 break; 793 case AP_DEV_STATE_ERROR: 794 rc = sysfs_emit(buf, "ERROR"); 795 break; 796 default: 797 rc = sysfs_emit(buf, "UNKNOWN"); 798 } 799 /* state machine state */ 800 if (aq->dev_state) { 801 switch (aq->sm_state) { 802 case AP_SM_STATE_RESET_START: 803 rc += sysfs_emit_at(buf, rc, " [RESET_START]\n"); 804 break; 805 case AP_SM_STATE_RESET_WAIT: 806 rc += sysfs_emit_at(buf, rc, " [RESET_WAIT]\n"); 807 break; 808 case AP_SM_STATE_SETIRQ_WAIT: 809 rc += sysfs_emit_at(buf, rc, " [SETIRQ_WAIT]\n"); 810 break; 811 case AP_SM_STATE_IDLE: 812 rc += sysfs_emit_at(buf, rc, " [IDLE]\n"); 813 break; 814 case AP_SM_STATE_WORKING: 815 rc += sysfs_emit_at(buf, rc, " [WORKING]\n"); 816 break; 817 case AP_SM_STATE_QUEUE_FULL: 818 rc += sysfs_emit_at(buf, rc, " [FULL]\n"); 819 break; 820 case AP_SM_STATE_ASSOC_WAIT: 821 rc += sysfs_emit_at(buf, rc, " [ASSOC_WAIT]\n"); 822 break; 823 default: 824 rc += sysfs_emit_at(buf, rc, " [UNKNOWN]\n"); 825 } 826 } 827 spin_unlock_bh(&aq->lock); 828 829 return rc; 830 } 831 static DEVICE_ATTR_RO(states); 832 833 static ssize_t last_err_rc_show(struct device *dev, 834 struct device_attribute *attr, char *buf) 835 { 836 struct ap_queue *aq = to_ap_queue(dev); 837 int rc; 838 839 spin_lock_bh(&aq->lock); 840 rc = aq->last_err_rc; 841 spin_unlock_bh(&aq->lock); 842 843 switch (rc) { 844 case AP_RESPONSE_NORMAL: 845 return sysfs_emit(buf, "NORMAL\n"); 846 case AP_RESPONSE_Q_NOT_AVAIL: 847 return sysfs_emit(buf, "Q_NOT_AVAIL\n"); 848 case AP_RESPONSE_RESET_IN_PROGRESS: 849 return sysfs_emit(buf, "RESET_IN_PROGRESS\n"); 850 case AP_RESPONSE_DECONFIGURED: 851 return sysfs_emit(buf, "DECONFIGURED\n"); 852 case AP_RESPONSE_CHECKSTOPPED: 853 return sysfs_emit(buf, "CHECKSTOPPED\n"); 854 case AP_RESPONSE_BUSY: 855 return sysfs_emit(buf, "BUSY\n"); 856 case AP_RESPONSE_INVALID_ADDRESS: 857 return sysfs_emit(buf, "INVALID_ADDRESS\n"); 858 case AP_RESPONSE_OTHERWISE_CHANGED: 859 return sysfs_emit(buf, "OTHERWISE_CHANGED\n"); 860 case AP_RESPONSE_Q_FULL: 861 return sysfs_emit(buf, "Q_FULL/NO_PENDING_REPLY\n"); 862 case AP_RESPONSE_INDEX_TOO_BIG: 863 return sysfs_emit(buf, "INDEX_TOO_BIG\n"); 864 case AP_RESPONSE_NO_FIRST_PART: 865 return sysfs_emit(buf, "NO_FIRST_PART\n"); 866 case AP_RESPONSE_MESSAGE_TOO_BIG: 867 return sysfs_emit(buf, "MESSAGE_TOO_BIG\n"); 868 case AP_RESPONSE_REQ_FAC_NOT_INST: 869 return sysfs_emit(buf, "REQ_FAC_NOT_INST\n"); 870 default: 871 return sysfs_emit(buf, "response code %d\n", rc); 872 } 873 } 874 static DEVICE_ATTR_RO(last_err_rc); 875 #endif 876 877 static struct attribute *ap_queue_dev_attrs[] = { 878 &dev_attr_request_count.attr, 879 &dev_attr_requestq_count.attr, 880 &dev_attr_pendingq_count.attr, 881 &dev_attr_reset.attr, 882 &dev_attr_interrupt.attr, 883 &dev_attr_config.attr, 884 &dev_attr_chkstop.attr, 885 &dev_attr_ap_functions.attr, 886 &dev_attr_driver_override.attr, 887 #ifdef CONFIG_AP_DEBUG 888 &dev_attr_states.attr, 889 &dev_attr_last_err_rc.attr, 890 #endif 891 NULL 892 }; 893 894 static struct attribute_group ap_queue_dev_attr_group = { 895 .attrs = ap_queue_dev_attrs 896 }; 897 898 static const struct attribute_group *ap_queue_dev_attr_groups[] = { 899 &ap_queue_dev_attr_group, 900 NULL 901 }; 902 903 static struct device_type ap_queue_type = { 904 .name = "ap_queue", 905 .groups = ap_queue_dev_attr_groups, 906 }; 907 908 static ssize_t se_bind_show(struct device *dev, 909 struct device_attribute *attr, char *buf) 910 { 911 struct ap_queue *aq = to_ap_queue(dev); 912 struct ap_queue_status status; 913 struct ap_tapq_hwinfo hwinfo; 914 915 if (!ap_q_supports_bind(aq)) 916 return sysfs_emit(buf, "-\n"); 917 918 status = ap_test_queue(aq->qid, 1, &hwinfo); 919 if (status.response_code > AP_RESPONSE_BUSY) { 920 pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n", 921 status.response_code, 922 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 923 return -EIO; 924 } 925 926 /* update queue's SE bind state */ 927 spin_lock_bh(&aq->lock); 928 aq->se_bstate = hwinfo.bs; 929 spin_unlock_bh(&aq->lock); 930 931 switch (hwinfo.bs) { 932 case AP_BS_Q_USABLE: 933 case AP_BS_Q_USABLE_NO_SECURE_KEY: 934 return sysfs_emit(buf, "bound\n"); 935 default: 936 return sysfs_emit(buf, "unbound\n"); 937 } 938 } 939 940 static ssize_t se_bind_store(struct device *dev, 941 struct device_attribute *attr, 942 const char *buf, size_t count) 943 { 944 struct ap_queue *aq = to_ap_queue(dev); 945 struct ap_queue_status status; 946 struct ap_tapq_hwinfo hwinfo; 947 bool value; 948 int rc; 949 950 if (!ap_q_supports_bind(aq)) 951 return -EINVAL; 952 953 /* only 0 (unbind) and 1 (bind) allowed */ 954 rc = kstrtobool(buf, &value); 955 if (rc) 956 return rc; 957 958 if (!value) { 959 /* Unbind. Set F bit arg and trigger RAPQ */ 960 spin_lock_bh(&aq->lock); 961 __ap_flush_queue(aq); 962 aq->rapq_fbit = 1; 963 _ap_queue_init_state(aq); 964 spin_unlock_bh(&aq->lock); 965 return count; 966 } 967 968 /* lock this ap to have fetch and update in an atomic way */ 969 spin_lock_bh(&aq->lock); 970 971 /* Bind. Check current SE bind state */ 972 status = ap_test_queue(aq->qid, 1, &hwinfo); 973 if (status.response_code) { 974 AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n", 975 __func__, status.response_code, 976 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 977 rc = -EIO; 978 goto error; 979 } 980 981 /* Update BS state */ 982 aq->se_bstate = hwinfo.bs; 983 if (hwinfo.bs != AP_BS_Q_AVAIL_FOR_BINDING) { 984 AP_DBF_WARN("%s bind attempt with bs %d on queue 0x%02x.%04x\n", 985 __func__, hwinfo.bs, 986 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 987 rc = -EINVAL; 988 goto error; 989 } 990 991 /* Check SM state */ 992 if (aq->sm_state < AP_SM_STATE_IDLE) { 993 rc = -EBUSY; 994 goto error; 995 } 996 997 /* invoke BAPQ */ 998 status = ap_bapq(aq->qid); 999 if (status.response_code) { 1000 AP_DBF_WARN("%s RC 0x%02x on bapq(0x%02x.%04x)\n", 1001 __func__, status.response_code, 1002 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1003 rc = -EIO; 1004 goto error; 1005 } 1006 aq->assoc_idx = ASSOC_IDX_INVALID; 1007 1008 /* verify SE bind state */ 1009 status = ap_test_queue(aq->qid, 1, &hwinfo); 1010 if (status.response_code) { 1011 AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n", 1012 __func__, status.response_code, 1013 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1014 rc = -EIO; 1015 goto error; 1016 } 1017 aq->se_bstate = hwinfo.bs; 1018 if (!(hwinfo.bs == AP_BS_Q_USABLE || 1019 hwinfo.bs == AP_BS_Q_USABLE_NO_SECURE_KEY)) { 1020 AP_DBF_WARN("%s BAPQ success, but bs shows %d on queue 0x%02x.%04x\n", 1021 __func__, hwinfo.bs, 1022 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1023 rc = -EIO; 1024 goto error; 1025 } 1026 1027 /* SE bind was successful */ 1028 spin_unlock_bh(&aq->lock); 1029 1030 AP_DBF_INFO("%s bapq(0x%02x.%04x) success\n", __func__, 1031 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1032 ap_send_se_bind_uevent(&aq->ap_dev); 1033 1034 return count; 1035 1036 error: 1037 spin_unlock_bh(&aq->lock); 1038 return rc; 1039 } 1040 1041 static DEVICE_ATTR_RW(se_bind); 1042 1043 static ssize_t se_associate_show(struct device *dev, 1044 struct device_attribute *attr, char *buf) 1045 { 1046 struct ap_queue *aq = to_ap_queue(dev); 1047 struct ap_queue_status status; 1048 struct ap_tapq_hwinfo hwinfo; 1049 1050 if (!ap_q_supports_assoc(aq)) 1051 return sysfs_emit(buf, "-\n"); 1052 1053 status = ap_test_queue(aq->qid, 1, &hwinfo); 1054 if (status.response_code > AP_RESPONSE_BUSY) { 1055 pr_debug("RC 0x%02x on tapq(0x%02x.%04x)\n", 1056 status.response_code, 1057 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1058 return -EIO; 1059 } 1060 1061 /* update queue's SE bind state */ 1062 spin_lock_bh(&aq->lock); 1063 aq->se_bstate = hwinfo.bs; 1064 spin_unlock_bh(&aq->lock); 1065 1066 switch (hwinfo.bs) { 1067 case AP_BS_Q_USABLE: 1068 if (aq->assoc_idx == ASSOC_IDX_INVALID) { 1069 AP_DBF_WARN("%s AP_BS_Q_USABLE but invalid assoc_idx\n", __func__); 1070 return -EIO; 1071 } 1072 return sysfs_emit(buf, "associated %u\n", aq->assoc_idx); 1073 case AP_BS_Q_USABLE_NO_SECURE_KEY: 1074 if (aq->assoc_idx != ASSOC_IDX_INVALID) 1075 return sysfs_emit(buf, "association pending\n"); 1076 fallthrough; 1077 default: 1078 return sysfs_emit(buf, "unassociated\n"); 1079 } 1080 } 1081 1082 static ssize_t se_associate_store(struct device *dev, 1083 struct device_attribute *attr, 1084 const char *buf, size_t count) 1085 { 1086 struct ap_queue *aq = to_ap_queue(dev); 1087 struct ap_queue_status status; 1088 struct ap_tapq_hwinfo hwinfo; 1089 unsigned int value; 1090 int rc; 1091 1092 if (!ap_q_supports_assoc(aq)) 1093 return -EINVAL; 1094 1095 /* association index needs to be >= 0 */ 1096 rc = kstrtouint(buf, 0, &value); 1097 if (rc) 1098 return rc; 1099 if (value >= ASSOC_IDX_INVALID) 1100 return -EINVAL; 1101 1102 /* lock this ap to have fetch and update in an atomic way */ 1103 spin_lock_bh(&aq->lock); 1104 1105 /* check current SE bind state */ 1106 status = ap_test_queue(aq->qid, 1, &hwinfo); 1107 if (status.response_code) { 1108 AP_DBF_WARN("%s RC 0x%02x on tapq(0x%02x.%04x)\n", 1109 __func__, status.response_code, 1110 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1111 rc = -EIO; 1112 goto out; 1113 } 1114 aq->se_bstate = hwinfo.bs; 1115 if (hwinfo.bs != AP_BS_Q_USABLE_NO_SECURE_KEY) { 1116 AP_DBF_WARN("%s association attempt with bs %d on queue 0x%02x.%04x\n", 1117 __func__, hwinfo.bs, 1118 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1119 rc = -EINVAL; 1120 goto out; 1121 } 1122 1123 /* check SM state */ 1124 if (aq->sm_state != AP_SM_STATE_IDLE) { 1125 rc = -EBUSY; 1126 goto out; 1127 } 1128 1129 /* trigger the asynchronous association request */ 1130 status = ap_aapq(aq->qid, value); 1131 switch (status.response_code) { 1132 case AP_RESPONSE_NORMAL: 1133 case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS: 1134 aq->sm_state = AP_SM_STATE_ASSOC_WAIT; 1135 aq->assoc_idx = value; 1136 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL)); 1137 rc = count; 1138 break; 1139 default: 1140 AP_DBF_WARN("%s RC 0x%02x on aapq(0x%02x.%04x)\n", 1141 __func__, status.response_code, 1142 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid)); 1143 rc = -EIO; 1144 break; 1145 } 1146 1147 out: 1148 spin_unlock_bh(&aq->lock); 1149 return rc; 1150 } 1151 1152 static DEVICE_ATTR_RW(se_associate); 1153 1154 static struct attribute *ap_queue_dev_sb_attrs[] = { 1155 &dev_attr_se_bind.attr, 1156 &dev_attr_se_associate.attr, 1157 NULL 1158 }; 1159 1160 static struct attribute_group ap_queue_dev_sb_attr_group = { 1161 .attrs = ap_queue_dev_sb_attrs 1162 }; 1163 1164 static const struct attribute_group *ap_queue_dev_sb_attr_groups[] = { 1165 &ap_queue_dev_sb_attr_group, 1166 NULL 1167 }; 1168 1169 static void ap_queue_device_release(struct device *dev) 1170 { 1171 struct ap_queue *aq = to_ap_queue(dev); 1172 1173 spin_lock_bh(&ap_queues_lock); 1174 hash_del(&aq->hnode); 1175 spin_unlock_bh(&ap_queues_lock); 1176 1177 kfree(aq); 1178 } 1179 1180 struct ap_queue *ap_queue_create(ap_qid_t qid, struct ap_card *ac) 1181 { 1182 struct ap_queue *aq; 1183 1184 aq = kzalloc_obj(*aq); 1185 if (!aq) 1186 return NULL; 1187 aq->card = ac; 1188 aq->ap_dev.device.release = ap_queue_device_release; 1189 aq->ap_dev.device.type = &ap_queue_type; 1190 aq->ap_dev.device_type = ac->ap_dev.device_type; 1191 /* in SE environment add bind/associate attributes group */ 1192 if (ap_is_se_guest() && ap_q_supported_in_se(aq)) 1193 aq->ap_dev.device.groups = ap_queue_dev_sb_attr_groups; 1194 aq->qid = qid; 1195 spin_lock_init(&aq->lock); 1196 INIT_LIST_HEAD(&aq->pendingq); 1197 INIT_LIST_HEAD(&aq->requestq); 1198 timer_setup(&aq->timeout, ap_request_timeout, 0); 1199 1200 return aq; 1201 } 1202 1203 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply) 1204 { 1205 aq->reply = reply; 1206 1207 spin_lock_bh(&aq->lock); 1208 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL)); 1209 spin_unlock_bh(&aq->lock); 1210 } 1211 EXPORT_SYMBOL(ap_queue_init_reply); 1212 1213 /** 1214 * ap_queue_message(): Queue a request to an AP device. 1215 * @aq: The AP device to queue the message to 1216 * @ap_msg: The message that is to be added 1217 */ 1218 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg) 1219 { 1220 int rc = 0; 1221 1222 /* msg needs to have a valid receive-callback */ 1223 BUG_ON(!ap_msg->receive); 1224 1225 spin_lock_bh(&aq->lock); 1226 1227 /* only allow to queue new messages if device state is ok */ 1228 if (aq->dev_state == AP_DEV_STATE_OPERATING) { 1229 list_add_tail(&ap_msg->list, &aq->requestq); 1230 aq->requestq_count++; 1231 aq->total_request_count++; 1232 atomic64_inc(&aq->card->total_request_count); 1233 } else { 1234 rc = -ENODEV; 1235 } 1236 1237 /* Send/receive as many request from the queue as possible. */ 1238 ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL)); 1239 1240 spin_unlock_bh(&aq->lock); 1241 1242 return rc; 1243 } 1244 EXPORT_SYMBOL(ap_queue_message); 1245 1246 /** 1247 * ap_queue_usable(): Check if queue is usable just now. 1248 * @aq: The AP queue device to test for usability. 1249 * This function is intended for the scheduler to query if it makes 1250 * sense to enqueue a message into this AP queue device by calling 1251 * ap_queue_message(). The perspective is very short-term as the 1252 * state machine and device state(s) may change at any time. 1253 */ 1254 bool ap_queue_usable(struct ap_queue *aq) 1255 { 1256 bool rc = true; 1257 1258 spin_lock_bh(&aq->lock); 1259 1260 /* check for not configured or checkstopped */ 1261 if (!aq->config || aq->chkstop) { 1262 rc = false; 1263 goto unlock_and_out; 1264 } 1265 1266 /* device state needs to be ok */ 1267 if (aq->dev_state != AP_DEV_STATE_OPERATING) { 1268 rc = false; 1269 goto unlock_and_out; 1270 } 1271 1272 /* SE guest's queues additionally need to be bound */ 1273 if (ap_is_se_guest()) { 1274 if (!ap_q_supported_in_se(aq)) { 1275 rc = false; 1276 goto unlock_and_out; 1277 } 1278 if (ap_q_needs_bind(aq) && 1279 !(aq->se_bstate == AP_BS_Q_USABLE || 1280 aq->se_bstate == AP_BS_Q_USABLE_NO_SECURE_KEY)) 1281 rc = false; 1282 } 1283 1284 unlock_and_out: 1285 spin_unlock_bh(&aq->lock); 1286 return rc; 1287 } 1288 EXPORT_SYMBOL(ap_queue_usable); 1289 1290 /** 1291 * ap_cancel_message(): Cancel a crypto request. 1292 * @aq: The AP device that has the message queued 1293 * @ap_msg: The message that is to be removed 1294 * 1295 * Cancel a crypto request. This is done by removing the request 1296 * from the device pending or request queue. Note that the 1297 * request stays on the AP queue. When it finishes the message 1298 * reply will be discarded because the psmid can't be found. 1299 */ 1300 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg) 1301 { 1302 struct ap_message *tmp; 1303 1304 spin_lock_bh(&aq->lock); 1305 if (!list_empty(&ap_msg->list)) { 1306 list_for_each_entry(tmp, &aq->pendingq, list) 1307 if (tmp->psmid == ap_msg->psmid) { 1308 aq->pendingq_count--; 1309 goto found; 1310 } 1311 aq->requestq_count--; 1312 found: 1313 list_del_init(&ap_msg->list); 1314 } 1315 spin_unlock_bh(&aq->lock); 1316 } 1317 EXPORT_SYMBOL(ap_cancel_message); 1318 1319 /** 1320 * __ap_flush_queue(): Flush requests. 1321 * @aq: Pointer to the AP queue 1322 * 1323 * Flush all requests from the request/pending queue of an AP device. 1324 */ 1325 static void __ap_flush_queue(struct ap_queue *aq) 1326 { 1327 struct ap_message *ap_msg, *next; 1328 1329 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) { 1330 list_del_init(&ap_msg->list); 1331 aq->pendingq_count--; 1332 ap_msg->rc = -EAGAIN; 1333 ap_msg->receive(aq, ap_msg, NULL); 1334 } 1335 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) { 1336 list_del_init(&ap_msg->list); 1337 aq->requestq_count--; 1338 ap_msg->rc = -EAGAIN; 1339 ap_msg->receive(aq, ap_msg, NULL); 1340 } 1341 aq->queue_count = 0; 1342 } 1343 1344 void ap_flush_queue(struct ap_queue *aq) 1345 { 1346 spin_lock_bh(&aq->lock); 1347 __ap_flush_queue(aq); 1348 spin_unlock_bh(&aq->lock); 1349 } 1350 EXPORT_SYMBOL(ap_flush_queue); 1351 1352 void ap_queue_prepare_remove(struct ap_queue *aq) 1353 { 1354 spin_lock_bh(&aq->lock); 1355 /* flush queue */ 1356 __ap_flush_queue(aq); 1357 /* move queue device state to SHUTDOWN in progress */ 1358 aq->dev_state = AP_DEV_STATE_SHUTDOWN; 1359 spin_unlock_bh(&aq->lock); 1360 timer_delete_sync(&aq->timeout); 1361 } 1362 1363 void ap_queue_remove(struct ap_queue *aq) 1364 { 1365 /* 1366 * all messages have been flushed and the device state 1367 * is SHUTDOWN. Now reset with zero which also clears 1368 * the irq registration and move the device state 1369 * to the initial value AP_DEV_STATE_UNINITIATED. 1370 */ 1371 spin_lock_bh(&aq->lock); 1372 ap_zapq(aq->qid, 0); 1373 aq->dev_state = AP_DEV_STATE_UNINITIATED; 1374 spin_unlock_bh(&aq->lock); 1375 } 1376 1377 void _ap_queue_init_state(struct ap_queue *aq) 1378 { 1379 aq->dev_state = AP_DEV_STATE_OPERATING; 1380 aq->sm_state = AP_SM_STATE_RESET_START; 1381 aq->last_err_rc = 0; 1382 aq->assoc_idx = ASSOC_IDX_INVALID; 1383 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL)); 1384 } 1385 1386 void ap_queue_init_state(struct ap_queue *aq) 1387 { 1388 spin_lock_bh(&aq->lock); 1389 _ap_queue_init_state(aq); 1390 spin_unlock_bh(&aq->lock); 1391 } 1392 EXPORT_SYMBOL(ap_queue_init_state); 1393