1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2016
4 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Adjunct processor bus, queue related code.
9 #define KMSG_COMPONENT "ap"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/facility.h>
19 static void __ap_flush_queue(struct ap_queue *aq);
22 * ap_queue_enable_irq(): Enable interrupt support on this AP queue.
24 * @ind: the notification indicator byte
26 * Enables interruption on AP queue via ap_aqic(). Based on the return
27 * value it waits a while and tests the AP queue if interrupts
28 * have been switched on using ap_test_queue().
30 static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
32 struct ap_queue_status status;
33 struct ap_qirq_ctrl qirqctrl = { 0 };
36 qirqctrl.isc = AP_ISC;
37 status = ap_aqic(aq->qid, qirqctrl, ind);
38 switch (status.response_code) {
39 case AP_RESPONSE_NORMAL:
40 case AP_RESPONSE_OTHERWISE_CHANGED:
42 case AP_RESPONSE_Q_NOT_AVAIL:
43 case AP_RESPONSE_DECONFIGURED:
44 case AP_RESPONSE_CHECKSTOPPED:
45 case AP_RESPONSE_INVALID_ADDRESS:
46 pr_err("Registering adapter interrupts for AP device %02x.%04x failed\n",
48 AP_QID_QUEUE(aq->qid));
50 case AP_RESPONSE_RESET_IN_PROGRESS:
51 case AP_RESPONSE_BUSY:
58 * __ap_send(): Send message to adjunct processor queue.
59 * @qid: The AP queue number
60 * @psmid: The program supplied message identifier
61 * @msg: The message text
62 * @length: The message length
63 * @special: Special Bit
65 * Returns AP queue status structure.
66 * Condition code 1 on NQAP can't happen because the L bit is 1.
67 * Condition code 2 on NQAP also means the send is incomplete,
68 * because a segment boundary was reached. The NQAP is repeated.
70 static inline struct ap_queue_status
71 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
76 return ap_nqap(qid, psmid, msg, length);
79 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
81 struct ap_queue_status status;
83 status = __ap_send(qid, psmid, msg, length, 0);
84 switch (status.response_code) {
85 case AP_RESPONSE_NORMAL:
87 case AP_RESPONSE_Q_FULL:
88 case AP_RESPONSE_RESET_IN_PROGRESS:
90 case AP_RESPONSE_REQ_FAC_NOT_INST:
92 default: /* Device is gone. */
96 EXPORT_SYMBOL(ap_send);
98 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
100 struct ap_queue_status status;
104 status = ap_dqap(qid, psmid, msg, length, NULL, NULL);
105 switch (status.response_code) {
106 case AP_RESPONSE_NORMAL:
108 case AP_RESPONSE_NO_PENDING_REPLY:
109 if (status.queue_empty)
112 case AP_RESPONSE_RESET_IN_PROGRESS:
118 EXPORT_SYMBOL(ap_recv);
120 /* State machine definitions and helpers */
122 static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
124 return AP_SM_WAIT_NONE;
128 * ap_sm_recv(): Receive pending reply messages from an AP queue but do
129 * not change the state of the device.
130 * @aq: pointer to the AP queue
132 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
134 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
136 struct ap_queue_status status;
137 struct ap_message *ap_msg;
140 unsigned long resgr0 = 0;
144 * DQAP loop until response code and resgr0 indicate that
145 * the msg is totally received. As we use the very same buffer
146 * the msg is overwritten with each invocation. That's intended
147 * and the receiver of the msg is informed with a msg rc code
148 * of EMSGSIZE in such a case.
151 status = ap_dqap(aq->qid, &aq->reply->psmid,
152 aq->reply->msg, aq->reply->bufsize,
155 } while (status.response_code == 0xFF && resgr0 != 0);
157 switch (status.response_code) {
158 case AP_RESPONSE_NORMAL:
159 aq->queue_count = max_t(int, 0, aq->queue_count - 1);
160 if (!status.queue_empty && !aq->queue_count)
162 if (aq->queue_count > 0)
163 mod_timer(&aq->timeout,
164 jiffies + aq->request_timeout);
165 list_for_each_entry(ap_msg, &aq->pendingq, list) {
166 if (ap_msg->psmid != aq->reply->psmid)
168 list_del_init(&ap_msg->list);
169 aq->pendingq_count--;
171 ap_msg->rc = -EMSGSIZE;
172 ap_msg->receive(aq, ap_msg, NULL);
174 ap_msg->receive(aq, ap_msg, aq->reply);
180 AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
181 __func__, aq->reply->psmid,
182 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
185 case AP_RESPONSE_NO_PENDING_REPLY:
186 if (!status.queue_empty || aq->queue_count <= 0)
188 /* The card shouldn't forget requests but who knows. */
190 list_splice_init(&aq->pendingq, &aq->requestq);
191 aq->requestq_count += aq->pendingq_count;
192 aq->pendingq_count = 0;
201 * ap_sm_read(): Receive pending reply messages from an AP queue.
202 * @aq: pointer to the AP queue
204 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
206 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
208 struct ap_queue_status status;
211 return AP_SM_WAIT_NONE;
212 status = ap_sm_recv(aq);
213 switch (status.response_code) {
214 case AP_RESPONSE_NORMAL:
215 if (aq->queue_count > 0) {
216 aq->sm_state = AP_SM_STATE_WORKING;
217 return AP_SM_WAIT_AGAIN;
219 aq->sm_state = AP_SM_STATE_IDLE;
220 return AP_SM_WAIT_NONE;
221 case AP_RESPONSE_NO_PENDING_REPLY:
222 if (aq->queue_count > 0)
223 return aq->interrupt ?
224 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
225 aq->sm_state = AP_SM_STATE_IDLE;
226 return AP_SM_WAIT_NONE;
228 aq->dev_state = AP_DEV_STATE_ERROR;
229 aq->last_err_rc = status.response_code;
230 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
231 __func__, status.response_code,
232 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
233 return AP_SM_WAIT_NONE;
238 * ap_sm_write(): Send messages from the request queue to an AP queue.
239 * @aq: pointer to the AP queue
241 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
243 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
245 struct ap_queue_status status;
246 struct ap_message *ap_msg;
247 ap_qid_t qid = aq->qid;
249 if (aq->requestq_count <= 0)
250 return AP_SM_WAIT_NONE;
252 /* Start the next request on the queue. */
253 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
254 #ifdef CONFIG_ZCRYPT_DEBUG
255 if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
256 AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
257 __func__, ap_msg->fi.cmd);
261 status = __ap_send(qid, ap_msg->psmid,
262 ap_msg->msg, ap_msg->len,
263 ap_msg->flags & AP_MSG_FLAG_SPECIAL);
264 switch (status.response_code) {
265 case AP_RESPONSE_NORMAL:
266 aq->queue_count = max_t(int, 1, aq->queue_count + 1);
267 if (aq->queue_count == 1)
268 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
269 list_move_tail(&ap_msg->list, &aq->pendingq);
270 aq->requestq_count--;
271 aq->pendingq_count++;
272 if (aq->queue_count < aq->card->queue_depth) {
273 aq->sm_state = AP_SM_STATE_WORKING;
274 return AP_SM_WAIT_AGAIN;
277 case AP_RESPONSE_Q_FULL:
278 aq->sm_state = AP_SM_STATE_QUEUE_FULL;
279 return aq->interrupt ?
280 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
281 case AP_RESPONSE_RESET_IN_PROGRESS:
282 aq->sm_state = AP_SM_STATE_RESET_WAIT;
283 return AP_SM_WAIT_TIMEOUT;
284 case AP_RESPONSE_INVALID_DOMAIN:
285 AP_DBF_WARN("%s RESPONSE_INVALID_DOMAIN on NQAP\n", __func__);
287 case AP_RESPONSE_MESSAGE_TOO_BIG:
288 case AP_RESPONSE_REQ_FAC_NOT_INST:
289 list_del_init(&ap_msg->list);
290 aq->requestq_count--;
291 ap_msg->rc = -EINVAL;
292 ap_msg->receive(aq, ap_msg, NULL);
293 return AP_SM_WAIT_AGAIN;
295 aq->dev_state = AP_DEV_STATE_ERROR;
296 aq->last_err_rc = status.response_code;
297 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
298 __func__, status.response_code,
299 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
300 return AP_SM_WAIT_NONE;
305 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
306 * @aq: pointer to the AP queue
308 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
310 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
312 return min(ap_sm_read(aq), ap_sm_write(aq));
316 * ap_sm_reset(): Reset an AP queue.
319 * Submit the Reset command to an AP queue.
321 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
323 struct ap_queue_status status;
325 status = ap_rapq(aq->qid);
326 switch (status.response_code) {
327 case AP_RESPONSE_NORMAL:
328 case AP_RESPONSE_RESET_IN_PROGRESS:
329 aq->sm_state = AP_SM_STATE_RESET_WAIT;
330 aq->interrupt = false;
331 return AP_SM_WAIT_TIMEOUT;
333 aq->dev_state = AP_DEV_STATE_ERROR;
334 aq->last_err_rc = status.response_code;
335 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
336 __func__, status.response_code,
337 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
338 return AP_SM_WAIT_NONE;
343 * ap_sm_reset_wait(): Test queue for completion of the reset operation
344 * @aq: pointer to the AP queue
346 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
348 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
350 struct ap_queue_status status;
353 if (aq->queue_count > 0 && aq->reply)
354 /* Try to read a completed message and get the status */
355 status = ap_sm_recv(aq);
357 /* Get the status with TAPQ */
358 status = ap_tapq(aq->qid, NULL);
360 switch (status.response_code) {
361 case AP_RESPONSE_NORMAL:
362 lsi_ptr = ap_airq_ptr();
363 if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
364 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
366 aq->sm_state = (aq->queue_count > 0) ?
367 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
368 return AP_SM_WAIT_AGAIN;
369 case AP_RESPONSE_BUSY:
370 case AP_RESPONSE_RESET_IN_PROGRESS:
371 return AP_SM_WAIT_TIMEOUT;
372 case AP_RESPONSE_Q_NOT_AVAIL:
373 case AP_RESPONSE_DECONFIGURED:
374 case AP_RESPONSE_CHECKSTOPPED:
376 aq->dev_state = AP_DEV_STATE_ERROR;
377 aq->last_err_rc = status.response_code;
378 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
379 __func__, status.response_code,
380 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
381 return AP_SM_WAIT_NONE;
386 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
387 * @aq: pointer to the AP queue
389 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
391 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
393 struct ap_queue_status status;
395 if (aq->queue_count > 0 && aq->reply)
396 /* Try to read a completed message and get the status */
397 status = ap_sm_recv(aq);
399 /* Get the status with TAPQ */
400 status = ap_tapq(aq->qid, NULL);
402 if (status.irq_enabled == 1) {
403 /* Irqs are now enabled */
404 aq->interrupt = true;
405 aq->sm_state = (aq->queue_count > 0) ?
406 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
409 switch (status.response_code) {
410 case AP_RESPONSE_NORMAL:
411 if (aq->queue_count > 0)
412 return AP_SM_WAIT_AGAIN;
414 case AP_RESPONSE_NO_PENDING_REPLY:
415 return AP_SM_WAIT_TIMEOUT;
417 aq->dev_state = AP_DEV_STATE_ERROR;
418 aq->last_err_rc = status.response_code;
419 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
420 __func__, status.response_code,
421 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
422 return AP_SM_WAIT_NONE;
427 * AP state machine jump table
429 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
430 [AP_SM_STATE_RESET_START] = {
431 [AP_SM_EVENT_POLL] = ap_sm_reset,
432 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
434 [AP_SM_STATE_RESET_WAIT] = {
435 [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
436 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
438 [AP_SM_STATE_SETIRQ_WAIT] = {
439 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
440 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
442 [AP_SM_STATE_IDLE] = {
443 [AP_SM_EVENT_POLL] = ap_sm_write,
444 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
446 [AP_SM_STATE_WORKING] = {
447 [AP_SM_EVENT_POLL] = ap_sm_read_write,
448 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
450 [AP_SM_STATE_QUEUE_FULL] = {
451 [AP_SM_EVENT_POLL] = ap_sm_read,
452 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
456 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
458 if (aq->config && !aq->chkstop &&
459 aq->dev_state > AP_DEV_STATE_UNINITIATED)
460 return ap_jumptable[aq->sm_state][event](aq);
462 return AP_SM_WAIT_NONE;
465 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
467 enum ap_sm_wait wait;
469 while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
475 * AP queue related attributes.
477 static ssize_t request_count_show(struct device *dev,
478 struct device_attribute *attr,
481 struct ap_queue *aq = to_ap_queue(dev);
485 spin_lock_bh(&aq->lock);
486 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
487 req_cnt = aq->total_request_count;
490 spin_unlock_bh(&aq->lock);
493 return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
495 return scnprintf(buf, PAGE_SIZE, "-\n");
498 static ssize_t request_count_store(struct device *dev,
499 struct device_attribute *attr,
500 const char *buf, size_t count)
502 struct ap_queue *aq = to_ap_queue(dev);
504 spin_lock_bh(&aq->lock);
505 aq->total_request_count = 0;
506 spin_unlock_bh(&aq->lock);
511 static DEVICE_ATTR_RW(request_count);
513 static ssize_t requestq_count_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 struct ap_queue *aq = to_ap_queue(dev);
517 unsigned int reqq_cnt = 0;
519 spin_lock_bh(&aq->lock);
520 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
521 reqq_cnt = aq->requestq_count;
522 spin_unlock_bh(&aq->lock);
523 return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
526 static DEVICE_ATTR_RO(requestq_count);
528 static ssize_t pendingq_count_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
531 struct ap_queue *aq = to_ap_queue(dev);
532 unsigned int penq_cnt = 0;
534 spin_lock_bh(&aq->lock);
535 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
536 penq_cnt = aq->pendingq_count;
537 spin_unlock_bh(&aq->lock);
538 return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
541 static DEVICE_ATTR_RO(pendingq_count);
543 static ssize_t reset_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct ap_queue *aq = to_ap_queue(dev);
549 spin_lock_bh(&aq->lock);
550 switch (aq->sm_state) {
551 case AP_SM_STATE_RESET_START:
552 case AP_SM_STATE_RESET_WAIT:
553 rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
555 case AP_SM_STATE_WORKING:
556 case AP_SM_STATE_QUEUE_FULL:
557 rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
560 rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
562 spin_unlock_bh(&aq->lock);
566 static ssize_t reset_store(struct device *dev,
567 struct device_attribute *attr,
568 const char *buf, size_t count)
570 struct ap_queue *aq = to_ap_queue(dev);
572 spin_lock_bh(&aq->lock);
573 __ap_flush_queue(aq);
574 aq->sm_state = AP_SM_STATE_RESET_START;
575 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
576 spin_unlock_bh(&aq->lock);
578 AP_DBF_INFO("%s reset queue=%02x.%04x triggered by user\n",
579 __func__, AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
584 static DEVICE_ATTR_RW(reset);
586 static ssize_t interrupt_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
589 struct ap_queue *aq = to_ap_queue(dev);
592 spin_lock_bh(&aq->lock);
593 if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
594 rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
595 else if (aq->interrupt)
596 rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
598 rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
599 spin_unlock_bh(&aq->lock);
603 static DEVICE_ATTR_RO(interrupt);
605 static ssize_t config_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
608 struct ap_queue *aq = to_ap_queue(dev);
611 spin_lock_bh(&aq->lock);
612 rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
613 spin_unlock_bh(&aq->lock);
617 static DEVICE_ATTR_RO(config);
619 static ssize_t chkstop_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
622 struct ap_queue *aq = to_ap_queue(dev);
625 spin_lock_bh(&aq->lock);
626 rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->chkstop ? 1 : 0);
627 spin_unlock_bh(&aq->lock);
631 static DEVICE_ATTR_RO(chkstop);
633 #ifdef CONFIG_ZCRYPT_DEBUG
634 static ssize_t states_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
637 struct ap_queue *aq = to_ap_queue(dev);
640 spin_lock_bh(&aq->lock);
641 /* queue device state */
642 switch (aq->dev_state) {
643 case AP_DEV_STATE_UNINITIATED:
644 rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
646 case AP_DEV_STATE_OPERATING:
647 rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
649 case AP_DEV_STATE_SHUTDOWN:
650 rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
652 case AP_DEV_STATE_ERROR:
653 rc = scnprintf(buf, PAGE_SIZE, "ERROR");
656 rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
658 /* state machine state */
660 switch (aq->sm_state) {
661 case AP_SM_STATE_RESET_START:
662 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
665 case AP_SM_STATE_RESET_WAIT:
666 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
669 case AP_SM_STATE_SETIRQ_WAIT:
670 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
673 case AP_SM_STATE_IDLE:
674 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
677 case AP_SM_STATE_WORKING:
678 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
681 case AP_SM_STATE_QUEUE_FULL:
682 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
686 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
690 spin_unlock_bh(&aq->lock);
694 static DEVICE_ATTR_RO(states);
696 static ssize_t last_err_rc_show(struct device *dev,
697 struct device_attribute *attr, char *buf)
699 struct ap_queue *aq = to_ap_queue(dev);
702 spin_lock_bh(&aq->lock);
703 rc = aq->last_err_rc;
704 spin_unlock_bh(&aq->lock);
707 case AP_RESPONSE_NORMAL:
708 return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
709 case AP_RESPONSE_Q_NOT_AVAIL:
710 return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
711 case AP_RESPONSE_RESET_IN_PROGRESS:
712 return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
713 case AP_RESPONSE_DECONFIGURED:
714 return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
715 case AP_RESPONSE_CHECKSTOPPED:
716 return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
717 case AP_RESPONSE_BUSY:
718 return scnprintf(buf, PAGE_SIZE, "BUSY\n");
719 case AP_RESPONSE_INVALID_ADDRESS:
720 return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
721 case AP_RESPONSE_OTHERWISE_CHANGED:
722 return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
723 case AP_RESPONSE_Q_FULL:
724 return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
725 case AP_RESPONSE_INDEX_TOO_BIG:
726 return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
727 case AP_RESPONSE_NO_FIRST_PART:
728 return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
729 case AP_RESPONSE_MESSAGE_TOO_BIG:
730 return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
731 case AP_RESPONSE_REQ_FAC_NOT_INST:
732 return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
734 return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
737 static DEVICE_ATTR_RO(last_err_rc);
740 static struct attribute *ap_queue_dev_attrs[] = {
741 &dev_attr_request_count.attr,
742 &dev_attr_requestq_count.attr,
743 &dev_attr_pendingq_count.attr,
744 &dev_attr_reset.attr,
745 &dev_attr_interrupt.attr,
746 &dev_attr_config.attr,
747 &dev_attr_chkstop.attr,
748 #ifdef CONFIG_ZCRYPT_DEBUG
749 &dev_attr_states.attr,
750 &dev_attr_last_err_rc.attr,
755 static struct attribute_group ap_queue_dev_attr_group = {
756 .attrs = ap_queue_dev_attrs
759 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
760 &ap_queue_dev_attr_group,
764 static struct device_type ap_queue_type = {
766 .groups = ap_queue_dev_attr_groups,
769 static void ap_queue_device_release(struct device *dev)
771 struct ap_queue *aq = to_ap_queue(dev);
773 spin_lock_bh(&ap_queues_lock);
774 hash_del(&aq->hnode);
775 spin_unlock_bh(&ap_queues_lock);
780 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
784 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
787 aq->ap_dev.device.release = ap_queue_device_release;
788 aq->ap_dev.device.type = &ap_queue_type;
789 aq->ap_dev.device_type = device_type;
791 aq->interrupt = false;
792 spin_lock_init(&aq->lock);
793 INIT_LIST_HEAD(&aq->pendingq);
794 INIT_LIST_HEAD(&aq->requestq);
795 timer_setup(&aq->timeout, ap_request_timeout, 0);
800 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
804 spin_lock_bh(&aq->lock);
805 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
806 spin_unlock_bh(&aq->lock);
808 EXPORT_SYMBOL(ap_queue_init_reply);
811 * ap_queue_message(): Queue a request to an AP device.
812 * @aq: The AP device to queue the message to
813 * @ap_msg: The message that is to be added
815 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
819 /* msg needs to have a valid receive-callback */
820 BUG_ON(!ap_msg->receive);
822 spin_lock_bh(&aq->lock);
824 /* only allow to queue new messages if device state is ok */
825 if (aq->dev_state == AP_DEV_STATE_OPERATING) {
826 list_add_tail(&ap_msg->list, &aq->requestq);
827 aq->requestq_count++;
828 aq->total_request_count++;
829 atomic64_inc(&aq->card->total_request_count);
834 /* Send/receive as many request from the queue as possible. */
835 ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
837 spin_unlock_bh(&aq->lock);
841 EXPORT_SYMBOL(ap_queue_message);
844 * ap_cancel_message(): Cancel a crypto request.
845 * @aq: The AP device that has the message queued
846 * @ap_msg: The message that is to be removed
848 * Cancel a crypto request. This is done by removing the request
849 * from the device pending or request queue. Note that the
850 * request stays on the AP queue. When it finishes the message
851 * reply will be discarded because the psmid can't be found.
853 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
855 struct ap_message *tmp;
857 spin_lock_bh(&aq->lock);
858 if (!list_empty(&ap_msg->list)) {
859 list_for_each_entry(tmp, &aq->pendingq, list)
860 if (tmp->psmid == ap_msg->psmid) {
861 aq->pendingq_count--;
864 aq->requestq_count--;
866 list_del_init(&ap_msg->list);
868 spin_unlock_bh(&aq->lock);
870 EXPORT_SYMBOL(ap_cancel_message);
873 * __ap_flush_queue(): Flush requests.
874 * @aq: Pointer to the AP queue
876 * Flush all requests from the request/pending queue of an AP device.
878 static void __ap_flush_queue(struct ap_queue *aq)
880 struct ap_message *ap_msg, *next;
882 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
883 list_del_init(&ap_msg->list);
884 aq->pendingq_count--;
885 ap_msg->rc = -EAGAIN;
886 ap_msg->receive(aq, ap_msg, NULL);
888 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
889 list_del_init(&ap_msg->list);
890 aq->requestq_count--;
891 ap_msg->rc = -EAGAIN;
892 ap_msg->receive(aq, ap_msg, NULL);
897 void ap_flush_queue(struct ap_queue *aq)
899 spin_lock_bh(&aq->lock);
900 __ap_flush_queue(aq);
901 spin_unlock_bh(&aq->lock);
903 EXPORT_SYMBOL(ap_flush_queue);
905 void ap_queue_prepare_remove(struct ap_queue *aq)
907 spin_lock_bh(&aq->lock);
909 __ap_flush_queue(aq);
910 /* move queue device state to SHUTDOWN in progress */
911 aq->dev_state = AP_DEV_STATE_SHUTDOWN;
912 spin_unlock_bh(&aq->lock);
913 del_timer_sync(&aq->timeout);
916 void ap_queue_remove(struct ap_queue *aq)
919 * all messages have been flushed and the device state
920 * is SHUTDOWN. Now reset with zero which also clears
921 * the irq registration and move the device state
922 * to the initial value AP_DEV_STATE_UNINITIATED.
924 spin_lock_bh(&aq->lock);
926 aq->dev_state = AP_DEV_STATE_UNINITIATED;
927 spin_unlock_bh(&aq->lock);
930 void ap_queue_init_state(struct ap_queue *aq)
932 spin_lock_bh(&aq->lock);
933 aq->dev_state = AP_DEV_STATE_OPERATING;
934 aq->sm_state = AP_SM_STATE_RESET_START;
936 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
937 spin_unlock_bh(&aq->lock);
939 EXPORT_SYMBOL(ap_queue_init_state);