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.
23 * @qid: The AP queue number
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);
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 status = ap_dqap(aq->qid, &aq->reply->psmid,
141 aq->reply->msg, aq->reply->len);
142 switch (status.response_code) {
143 case AP_RESPONSE_NORMAL:
144 aq->queue_count = max_t(int, 0, aq->queue_count - 1);
145 if (!status.queue_empty && !aq->queue_count)
147 if (aq->queue_count > 0)
148 mod_timer(&aq->timeout,
149 jiffies + aq->request_timeout);
150 list_for_each_entry(ap_msg, &aq->pendingq, list) {
151 if (ap_msg->psmid != aq->reply->psmid)
153 list_del_init(&ap_msg->list);
154 aq->pendingq_count--;
155 ap_msg->receive(aq, ap_msg, aq->reply);
160 AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
161 __func__, aq->reply->psmid,
162 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
165 case AP_RESPONSE_NO_PENDING_REPLY:
166 if (!status.queue_empty || aq->queue_count <= 0)
168 /* The card shouldn't forget requests but who knows. */
170 list_splice_init(&aq->pendingq, &aq->requestq);
171 aq->requestq_count += aq->pendingq_count;
172 aq->pendingq_count = 0;
181 * ap_sm_read(): Receive pending reply messages from an AP queue.
182 * @aq: pointer to the AP queue
184 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
186 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
188 struct ap_queue_status status;
191 return AP_SM_WAIT_NONE;
192 status = ap_sm_recv(aq);
193 switch (status.response_code) {
194 case AP_RESPONSE_NORMAL:
195 if (aq->queue_count > 0) {
196 aq->sm_state = AP_SM_STATE_WORKING;
197 return AP_SM_WAIT_AGAIN;
199 aq->sm_state = AP_SM_STATE_IDLE;
200 return AP_SM_WAIT_NONE;
201 case AP_RESPONSE_NO_PENDING_REPLY:
202 if (aq->queue_count > 0)
203 return aq->interrupt ?
204 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
205 aq->sm_state = AP_SM_STATE_IDLE;
206 return AP_SM_WAIT_NONE;
208 aq->dev_state = AP_DEV_STATE_ERROR;
209 aq->last_err_rc = status.response_code;
210 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
211 __func__, status.response_code,
212 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
213 return AP_SM_WAIT_NONE;
218 * ap_sm_write(): Send messages from the request queue to an AP queue.
219 * @aq: pointer to the AP queue
221 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
223 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
225 struct ap_queue_status status;
226 struct ap_message *ap_msg;
227 ap_qid_t qid = aq->qid;
229 if (aq->requestq_count <= 0)
230 return AP_SM_WAIT_NONE;
231 /* Start the next request on the queue. */
232 ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
233 #ifdef CONFIG_ZCRYPT_DEBUG
234 if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
235 AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
236 __func__, ap_msg->fi.cmd);
240 status = __ap_send(qid, ap_msg->psmid,
241 ap_msg->msg, ap_msg->len,
242 ap_msg->flags & AP_MSG_FLAG_SPECIAL);
243 switch (status.response_code) {
244 case AP_RESPONSE_NORMAL:
245 aq->queue_count = max_t(int, 1, aq->queue_count + 1);
246 if (aq->queue_count == 1)
247 mod_timer(&aq->timeout, jiffies + aq->request_timeout);
248 list_move_tail(&ap_msg->list, &aq->pendingq);
249 aq->requestq_count--;
250 aq->pendingq_count++;
251 if (aq->queue_count < aq->card->queue_depth) {
252 aq->sm_state = AP_SM_STATE_WORKING;
253 return AP_SM_WAIT_AGAIN;
256 case AP_RESPONSE_Q_FULL:
257 aq->sm_state = AP_SM_STATE_QUEUE_FULL;
258 return aq->interrupt ?
259 AP_SM_WAIT_INTERRUPT : AP_SM_WAIT_TIMEOUT;
260 case AP_RESPONSE_RESET_IN_PROGRESS:
261 aq->sm_state = AP_SM_STATE_RESET_WAIT;
262 return AP_SM_WAIT_TIMEOUT;
263 case AP_RESPONSE_INVALID_DOMAIN:
264 AP_DBF(DBF_WARN, "AP_RESPONSE_INVALID_DOMAIN on NQAP\n");
266 case AP_RESPONSE_MESSAGE_TOO_BIG:
267 case AP_RESPONSE_REQ_FAC_NOT_INST:
268 list_del_init(&ap_msg->list);
269 aq->requestq_count--;
270 ap_msg->rc = -EINVAL;
271 ap_msg->receive(aq, ap_msg, NULL);
272 return AP_SM_WAIT_AGAIN;
274 aq->dev_state = AP_DEV_STATE_ERROR;
275 aq->last_err_rc = status.response_code;
276 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
277 __func__, status.response_code,
278 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
279 return AP_SM_WAIT_NONE;
284 * ap_sm_read_write(): Send and receive messages to/from an AP queue.
285 * @aq: pointer to the AP queue
287 * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
289 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
291 return min(ap_sm_read(aq), ap_sm_write(aq));
295 * ap_sm_reset(): Reset an AP queue.
296 * @qid: The AP queue number
298 * Submit the Reset command to an AP queue.
300 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
302 struct ap_queue_status status;
304 status = ap_rapq(aq->qid);
305 switch (status.response_code) {
306 case AP_RESPONSE_NORMAL:
307 case AP_RESPONSE_RESET_IN_PROGRESS:
308 aq->sm_state = AP_SM_STATE_RESET_WAIT;
309 aq->interrupt = false;
310 return AP_SM_WAIT_TIMEOUT;
312 aq->dev_state = AP_DEV_STATE_ERROR;
313 aq->last_err_rc = status.response_code;
314 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
315 __func__, status.response_code,
316 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
317 return AP_SM_WAIT_NONE;
322 * ap_sm_reset_wait(): Test queue for completion of the reset operation
323 * @aq: pointer to the AP queue
325 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
327 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
329 struct ap_queue_status status;
332 if (aq->queue_count > 0 && aq->reply)
333 /* Try to read a completed message and get the status */
334 status = ap_sm_recv(aq);
336 /* Get the status with TAPQ */
337 status = ap_tapq(aq->qid, NULL);
339 switch (status.response_code) {
340 case AP_RESPONSE_NORMAL:
341 lsi_ptr = ap_airq_ptr();
342 if (lsi_ptr && ap_queue_enable_irq(aq, lsi_ptr) == 0)
343 aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
345 aq->sm_state = (aq->queue_count > 0) ?
346 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
347 return AP_SM_WAIT_AGAIN;
348 case AP_RESPONSE_BUSY:
349 case AP_RESPONSE_RESET_IN_PROGRESS:
350 return AP_SM_WAIT_TIMEOUT;
351 case AP_RESPONSE_Q_NOT_AVAIL:
352 case AP_RESPONSE_DECONFIGURED:
353 case AP_RESPONSE_CHECKSTOPPED:
355 aq->dev_state = AP_DEV_STATE_ERROR;
356 aq->last_err_rc = status.response_code;
357 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
358 __func__, status.response_code,
359 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
360 return AP_SM_WAIT_NONE;
365 * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
366 * @aq: pointer to the AP queue
368 * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
370 static enum ap_sm_wait ap_sm_setirq_wait(struct ap_queue *aq)
372 struct ap_queue_status status;
374 if (aq->queue_count > 0 && aq->reply)
375 /* Try to read a completed message and get the status */
376 status = ap_sm_recv(aq);
378 /* Get the status with TAPQ */
379 status = ap_tapq(aq->qid, NULL);
381 if (status.irq_enabled == 1) {
382 /* Irqs are now enabled */
383 aq->interrupt = true;
384 aq->sm_state = (aq->queue_count > 0) ?
385 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
388 switch (status.response_code) {
389 case AP_RESPONSE_NORMAL:
390 if (aq->queue_count > 0)
391 return AP_SM_WAIT_AGAIN;
393 case AP_RESPONSE_NO_PENDING_REPLY:
394 return AP_SM_WAIT_TIMEOUT;
396 aq->dev_state = AP_DEV_STATE_ERROR;
397 aq->last_err_rc = status.response_code;
398 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
399 __func__, status.response_code,
400 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
401 return AP_SM_WAIT_NONE;
406 * AP state machine jump table
408 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
409 [AP_SM_STATE_RESET_START] = {
410 [AP_SM_EVENT_POLL] = ap_sm_reset,
411 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
413 [AP_SM_STATE_RESET_WAIT] = {
414 [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
415 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
417 [AP_SM_STATE_SETIRQ_WAIT] = {
418 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
419 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
421 [AP_SM_STATE_IDLE] = {
422 [AP_SM_EVENT_POLL] = ap_sm_write,
423 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
425 [AP_SM_STATE_WORKING] = {
426 [AP_SM_EVENT_POLL] = ap_sm_read_write,
427 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
429 [AP_SM_STATE_QUEUE_FULL] = {
430 [AP_SM_EVENT_POLL] = ap_sm_read,
431 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
435 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
437 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
438 return ap_jumptable[aq->sm_state][event](aq);
440 return AP_SM_WAIT_NONE;
443 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
445 enum ap_sm_wait wait;
447 while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
453 * AP queue related attributes.
455 static ssize_t request_count_show(struct device *dev,
456 struct device_attribute *attr,
459 struct ap_queue *aq = to_ap_queue(dev);
463 spin_lock_bh(&aq->lock);
464 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
465 req_cnt = aq->total_request_count;
468 spin_unlock_bh(&aq->lock);
471 return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
473 return scnprintf(buf, PAGE_SIZE, "-\n");
476 static ssize_t request_count_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
480 struct ap_queue *aq = to_ap_queue(dev);
482 spin_lock_bh(&aq->lock);
483 aq->total_request_count = 0;
484 spin_unlock_bh(&aq->lock);
489 static DEVICE_ATTR_RW(request_count);
491 static ssize_t requestq_count_show(struct device *dev,
492 struct device_attribute *attr, char *buf)
494 struct ap_queue *aq = to_ap_queue(dev);
495 unsigned int reqq_cnt = 0;
497 spin_lock_bh(&aq->lock);
498 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
499 reqq_cnt = aq->requestq_count;
500 spin_unlock_bh(&aq->lock);
501 return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
504 static DEVICE_ATTR_RO(requestq_count);
506 static ssize_t pendingq_count_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
509 struct ap_queue *aq = to_ap_queue(dev);
510 unsigned int penq_cnt = 0;
512 spin_lock_bh(&aq->lock);
513 if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
514 penq_cnt = aq->pendingq_count;
515 spin_unlock_bh(&aq->lock);
516 return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
519 static DEVICE_ATTR_RO(pendingq_count);
521 static ssize_t reset_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
524 struct ap_queue *aq = to_ap_queue(dev);
527 spin_lock_bh(&aq->lock);
528 switch (aq->sm_state) {
529 case AP_SM_STATE_RESET_START:
530 case AP_SM_STATE_RESET_WAIT:
531 rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
533 case AP_SM_STATE_WORKING:
534 case AP_SM_STATE_QUEUE_FULL:
535 rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
538 rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
540 spin_unlock_bh(&aq->lock);
544 static ssize_t reset_store(struct device *dev,
545 struct device_attribute *attr,
546 const char *buf, size_t count)
548 struct ap_queue *aq = to_ap_queue(dev);
550 spin_lock_bh(&aq->lock);
551 __ap_flush_queue(aq);
552 aq->sm_state = AP_SM_STATE_RESET_START;
553 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
554 spin_unlock_bh(&aq->lock);
556 AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
557 AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
562 static DEVICE_ATTR_RW(reset);
564 static ssize_t interrupt_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
567 struct ap_queue *aq = to_ap_queue(dev);
570 spin_lock_bh(&aq->lock);
571 if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
572 rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
573 else if (aq->interrupt)
574 rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
576 rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
577 spin_unlock_bh(&aq->lock);
581 static DEVICE_ATTR_RO(interrupt);
583 static ssize_t config_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct ap_queue *aq = to_ap_queue(dev);
589 spin_lock_bh(&aq->lock);
590 rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
591 spin_unlock_bh(&aq->lock);
595 static DEVICE_ATTR_RO(config);
597 #ifdef CONFIG_ZCRYPT_DEBUG
598 static ssize_t states_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 struct ap_queue *aq = to_ap_queue(dev);
604 spin_lock_bh(&aq->lock);
605 /* queue device state */
606 switch (aq->dev_state) {
607 case AP_DEV_STATE_UNINITIATED:
608 rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
610 case AP_DEV_STATE_OPERATING:
611 rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
613 case AP_DEV_STATE_SHUTDOWN:
614 rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
616 case AP_DEV_STATE_ERROR:
617 rc = scnprintf(buf, PAGE_SIZE, "ERROR");
620 rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
622 /* state machine state */
624 switch (aq->sm_state) {
625 case AP_SM_STATE_RESET_START:
626 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
629 case AP_SM_STATE_RESET_WAIT:
630 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
633 case AP_SM_STATE_SETIRQ_WAIT:
634 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
637 case AP_SM_STATE_IDLE:
638 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
641 case AP_SM_STATE_WORKING:
642 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
645 case AP_SM_STATE_QUEUE_FULL:
646 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
650 rc += scnprintf(buf + rc, PAGE_SIZE - rc,
654 spin_unlock_bh(&aq->lock);
658 static DEVICE_ATTR_RO(states);
660 static ssize_t last_err_rc_show(struct device *dev,
661 struct device_attribute *attr, char *buf)
663 struct ap_queue *aq = to_ap_queue(dev);
666 spin_lock_bh(&aq->lock);
667 rc = aq->last_err_rc;
668 spin_unlock_bh(&aq->lock);
671 case AP_RESPONSE_NORMAL:
672 return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
673 case AP_RESPONSE_Q_NOT_AVAIL:
674 return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
675 case AP_RESPONSE_RESET_IN_PROGRESS:
676 return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
677 case AP_RESPONSE_DECONFIGURED:
678 return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
679 case AP_RESPONSE_CHECKSTOPPED:
680 return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
681 case AP_RESPONSE_BUSY:
682 return scnprintf(buf, PAGE_SIZE, "BUSY\n");
683 case AP_RESPONSE_INVALID_ADDRESS:
684 return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
685 case AP_RESPONSE_OTHERWISE_CHANGED:
686 return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
687 case AP_RESPONSE_Q_FULL:
688 return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
689 case AP_RESPONSE_INDEX_TOO_BIG:
690 return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
691 case AP_RESPONSE_NO_FIRST_PART:
692 return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
693 case AP_RESPONSE_MESSAGE_TOO_BIG:
694 return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
695 case AP_RESPONSE_REQ_FAC_NOT_INST:
696 return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
698 return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
701 static DEVICE_ATTR_RO(last_err_rc);
704 static struct attribute *ap_queue_dev_attrs[] = {
705 &dev_attr_request_count.attr,
706 &dev_attr_requestq_count.attr,
707 &dev_attr_pendingq_count.attr,
708 &dev_attr_reset.attr,
709 &dev_attr_interrupt.attr,
710 &dev_attr_config.attr,
711 #ifdef CONFIG_ZCRYPT_DEBUG
712 &dev_attr_states.attr,
713 &dev_attr_last_err_rc.attr,
718 static struct attribute_group ap_queue_dev_attr_group = {
719 .attrs = ap_queue_dev_attrs
722 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
723 &ap_queue_dev_attr_group,
727 static struct device_type ap_queue_type = {
729 .groups = ap_queue_dev_attr_groups,
732 static void ap_queue_device_release(struct device *dev)
734 struct ap_queue *aq = to_ap_queue(dev);
736 spin_lock_bh(&ap_queues_lock);
737 hash_del(&aq->hnode);
738 spin_unlock_bh(&ap_queues_lock);
743 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
747 aq = kzalloc(sizeof(*aq), GFP_KERNEL);
750 aq->ap_dev.device.release = ap_queue_device_release;
751 aq->ap_dev.device.type = &ap_queue_type;
752 aq->ap_dev.device_type = device_type;
754 aq->interrupt = false;
755 spin_lock_init(&aq->lock);
756 INIT_LIST_HEAD(&aq->pendingq);
757 INIT_LIST_HEAD(&aq->requestq);
758 timer_setup(&aq->timeout, ap_request_timeout, 0);
763 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
767 spin_lock_bh(&aq->lock);
768 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
769 spin_unlock_bh(&aq->lock);
771 EXPORT_SYMBOL(ap_queue_init_reply);
774 * ap_queue_message(): Queue a request to an AP device.
775 * @aq: The AP device to queue the message to
776 * @ap_msg: The message that is to be added
778 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
782 /* msg needs to have a valid receive-callback */
783 BUG_ON(!ap_msg->receive);
785 spin_lock_bh(&aq->lock);
787 /* only allow to queue new messages if device state is ok */
788 if (aq->dev_state == AP_DEV_STATE_OPERATING) {
789 list_add_tail(&ap_msg->list, &aq->requestq);
790 aq->requestq_count++;
791 aq->total_request_count++;
792 atomic64_inc(&aq->card->total_request_count);
796 /* Send/receive as many request from the queue as possible. */
797 ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
799 spin_unlock_bh(&aq->lock);
803 EXPORT_SYMBOL(ap_queue_message);
806 * ap_cancel_message(): Cancel a crypto request.
807 * @aq: The AP device that has the message queued
808 * @ap_msg: The message that is to be removed
810 * Cancel a crypto request. This is done by removing the request
811 * from the device pending or request queue. Note that the
812 * request stays on the AP queue. When it finishes the message
813 * reply will be discarded because the psmid can't be found.
815 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
817 struct ap_message *tmp;
819 spin_lock_bh(&aq->lock);
820 if (!list_empty(&ap_msg->list)) {
821 list_for_each_entry(tmp, &aq->pendingq, list)
822 if (tmp->psmid == ap_msg->psmid) {
823 aq->pendingq_count--;
826 aq->requestq_count--;
828 list_del_init(&ap_msg->list);
830 spin_unlock_bh(&aq->lock);
832 EXPORT_SYMBOL(ap_cancel_message);
835 * __ap_flush_queue(): Flush requests.
836 * @aq: Pointer to the AP queue
838 * Flush all requests from the request/pending queue of an AP device.
840 static void __ap_flush_queue(struct ap_queue *aq)
842 struct ap_message *ap_msg, *next;
844 list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
845 list_del_init(&ap_msg->list);
846 aq->pendingq_count--;
847 ap_msg->rc = -EAGAIN;
848 ap_msg->receive(aq, ap_msg, NULL);
850 list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
851 list_del_init(&ap_msg->list);
852 aq->requestq_count--;
853 ap_msg->rc = -EAGAIN;
854 ap_msg->receive(aq, ap_msg, NULL);
859 void ap_flush_queue(struct ap_queue *aq)
861 spin_lock_bh(&aq->lock);
862 __ap_flush_queue(aq);
863 spin_unlock_bh(&aq->lock);
865 EXPORT_SYMBOL(ap_flush_queue);
867 void ap_queue_prepare_remove(struct ap_queue *aq)
869 spin_lock_bh(&aq->lock);
871 __ap_flush_queue(aq);
872 /* move queue device state to SHUTDOWN in progress */
873 aq->dev_state = AP_DEV_STATE_SHUTDOWN;
874 spin_unlock_bh(&aq->lock);
875 del_timer_sync(&aq->timeout);
878 void ap_queue_remove(struct ap_queue *aq)
881 * all messages have been flushed and the device state
882 * is SHUTDOWN. Now reset with zero which also clears
883 * the irq registration and move the device state
884 * to the initial value AP_DEV_STATE_UNINITIATED.
886 spin_lock_bh(&aq->lock);
888 aq->dev_state = AP_DEV_STATE_UNINITIATED;
889 spin_unlock_bh(&aq->lock);
892 void ap_queue_init_state(struct ap_queue *aq)
894 spin_lock_bh(&aq->lock);
895 aq->dev_state = AP_DEV_STATE_OPERATING;
896 aq->sm_state = AP_SM_STATE_RESET_START;
897 ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
898 spin_unlock_bh(&aq->lock);
900 EXPORT_SYMBOL(ap_queue_init_state);