s390/ap: Fix hanging ioctl caused by wrong msg counter
[linux-2.6-microblaze.git] / drivers / s390 / crypto / ap_queue.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2016
4  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
5  *
6  * Adjunct processor bus, queue related code.
7  */
8
9 #define KMSG_COMPONENT "ap"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <asm/facility.h>
15
16 #include "ap_bus.h"
17 #include "ap_debug.h"
18
19 static void __ap_flush_queue(struct ap_queue *aq);
20
21 /**
22  * ap_queue_enable_interruption(): Enable interruption on an AP queue.
23  * @qid: The AP queue number
24  * @ind: the notification indicator byte
25  *
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().
29  */
30 static int ap_queue_enable_interruption(struct ap_queue *aq, void *ind)
31 {
32         struct ap_queue_status status;
33         struct ap_qirq_ctrl qirqctrl = { 0 };
34
35         qirqctrl.ir = 1;
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:
41                 return 0;
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",
47                        AP_QID_CARD(aq->qid),
48                        AP_QID_QUEUE(aq->qid));
49                 return -EOPNOTSUPP;
50         case AP_RESPONSE_RESET_IN_PROGRESS:
51         case AP_RESPONSE_BUSY:
52         default:
53                 return -EBUSY;
54         }
55 }
56
57 /**
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
64  *
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.
69  */
70 static inline struct ap_queue_status
71 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
72           int special)
73 {
74         if (special)
75                 qid |= 0x400000UL;
76         return ap_nqap(qid, psmid, msg, length);
77 }
78
79 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
80 {
81         struct ap_queue_status status;
82
83         status = __ap_send(qid, psmid, msg, length, 0);
84         switch (status.response_code) {
85         case AP_RESPONSE_NORMAL:
86                 return 0;
87         case AP_RESPONSE_Q_FULL:
88         case AP_RESPONSE_RESET_IN_PROGRESS:
89                 return -EBUSY;
90         case AP_RESPONSE_REQ_FAC_NOT_INST:
91                 return -EINVAL;
92         default:        /* Device is gone. */
93                 return -ENODEV;
94         }
95 }
96 EXPORT_SYMBOL(ap_send);
97
98 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
99 {
100         struct ap_queue_status status;
101
102         if (msg == NULL)
103                 return -EINVAL;
104         status = ap_dqap(qid, psmid, msg, length);
105         switch (status.response_code) {
106         case AP_RESPONSE_NORMAL:
107                 return 0;
108         case AP_RESPONSE_NO_PENDING_REPLY:
109                 if (status.queue_empty)
110                         return -ENOENT;
111                 return -EBUSY;
112         case AP_RESPONSE_RESET_IN_PROGRESS:
113                 return -EBUSY;
114         default:
115                 return -ENODEV;
116         }
117 }
118 EXPORT_SYMBOL(ap_recv);
119
120 /* State machine definitions and helpers */
121
122 static enum ap_sm_wait ap_sm_nop(struct ap_queue *aq)
123 {
124         return AP_SM_WAIT_NONE;
125 }
126
127 /**
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
131  *
132  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
133  */
134 static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
135 {
136         struct ap_queue_status status;
137         struct ap_message *ap_msg;
138         bool found = false;
139
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 (aq->queue_count > 0)
146                         mod_timer(&aq->timeout,
147                                   jiffies + aq->request_timeout);
148                 list_for_each_entry(ap_msg, &aq->pendingq, list) {
149                         if (ap_msg->psmid != aq->reply->psmid)
150                                 continue;
151                         list_del_init(&ap_msg->list);
152                         aq->pendingq_count--;
153                         ap_msg->receive(aq, ap_msg, aq->reply);
154                         found = true;
155                         break;
156                 }
157                 if (!found) {
158                         AP_DBF_WARN("%s unassociated reply psmid=0x%016llx on 0x%02x.%04x\n",
159                                     __func__, aq->reply->psmid,
160                                     AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
161                 }
162                 fallthrough;
163         case AP_RESPONSE_NO_PENDING_REPLY:
164                 if (!status.queue_empty || aq->queue_count <= 0)
165                         break;
166                 /* The card shouldn't forget requests but who knows. */
167                 aq->queue_count = 0;
168                 list_splice_init(&aq->pendingq, &aq->requestq);
169                 aq->requestq_count += aq->pendingq_count;
170                 aq->pendingq_count = 0;
171                 break;
172         default:
173                 break;
174         }
175         return status;
176 }
177
178 /**
179  * ap_sm_read(): Receive pending reply messages from an AP queue.
180  * @aq: pointer to the AP queue
181  *
182  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
183  */
184 static enum ap_sm_wait ap_sm_read(struct ap_queue *aq)
185 {
186         struct ap_queue_status status;
187
188         if (!aq->reply)
189                 return AP_SM_WAIT_NONE;
190         status = ap_sm_recv(aq);
191         switch (status.response_code) {
192         case AP_RESPONSE_NORMAL:
193                 if (aq->queue_count > 0) {
194                         aq->sm_state = AP_SM_STATE_WORKING;
195                         return AP_SM_WAIT_AGAIN;
196                 }
197                 aq->sm_state = AP_SM_STATE_IDLE;
198                 return AP_SM_WAIT_NONE;
199         case AP_RESPONSE_NO_PENDING_REPLY:
200                 if (aq->queue_count > 0)
201                         return AP_SM_WAIT_INTERRUPT;
202                 aq->sm_state = AP_SM_STATE_IDLE;
203                 return AP_SM_WAIT_NONE;
204         default:
205                 aq->dev_state = AP_DEV_STATE_ERROR;
206                 aq->last_err_rc = status.response_code;
207                 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
208                             __func__, status.response_code,
209                             AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
210                 return AP_SM_WAIT_NONE;
211         }
212 }
213
214 /**
215  * ap_sm_write(): Send messages from the request queue to an AP queue.
216  * @aq: pointer to the AP queue
217  *
218  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
219  */
220 static enum ap_sm_wait ap_sm_write(struct ap_queue *aq)
221 {
222         struct ap_queue_status status;
223         struct ap_message *ap_msg;
224         ap_qid_t qid = aq->qid;
225
226         if (aq->requestq_count <= 0)
227                 return AP_SM_WAIT_NONE;
228         /* Start the next request on the queue. */
229         ap_msg = list_entry(aq->requestq.next, struct ap_message, list);
230 #ifdef CONFIG_ZCRYPT_DEBUG
231         if (ap_msg->fi.action == AP_FI_ACTION_NQAP_QID_INVAL) {
232                 AP_DBF_WARN("%s fi cmd 0x%04x: forcing invalid qid 0xFF00\n",
233                             __func__, ap_msg->fi.cmd);
234                 qid = 0xFF00;
235         }
236 #endif
237         status = __ap_send(qid, ap_msg->psmid,
238                            ap_msg->msg, ap_msg->len,
239                            ap_msg->flags & AP_MSG_FLAG_SPECIAL);
240         switch (status.response_code) {
241         case AP_RESPONSE_NORMAL:
242                 aq->queue_count = max_t(int, 1, aq->queue_count + 1);
243                 if (aq->queue_count == 1)
244                         mod_timer(&aq->timeout, jiffies + aq->request_timeout);
245                 list_move_tail(&ap_msg->list, &aq->pendingq);
246                 aq->requestq_count--;
247                 aq->pendingq_count++;
248                 if (aq->queue_count < aq->card->queue_depth) {
249                         aq->sm_state = AP_SM_STATE_WORKING;
250                         return AP_SM_WAIT_AGAIN;
251                 }
252                 fallthrough;
253         case AP_RESPONSE_Q_FULL:
254                 aq->sm_state = AP_SM_STATE_QUEUE_FULL;
255                 return AP_SM_WAIT_INTERRUPT;
256         case AP_RESPONSE_RESET_IN_PROGRESS:
257                 aq->sm_state = AP_SM_STATE_RESET_WAIT;
258                 return AP_SM_WAIT_TIMEOUT;
259         case AP_RESPONSE_INVALID_DOMAIN:
260                 AP_DBF(DBF_WARN, "AP_RESPONSE_INVALID_DOMAIN on NQAP\n");
261                 fallthrough;
262         case AP_RESPONSE_MESSAGE_TOO_BIG:
263         case AP_RESPONSE_REQ_FAC_NOT_INST:
264                 list_del_init(&ap_msg->list);
265                 aq->requestq_count--;
266                 ap_msg->rc = -EINVAL;
267                 ap_msg->receive(aq, ap_msg, NULL);
268                 return AP_SM_WAIT_AGAIN;
269         default:
270                 aq->dev_state = AP_DEV_STATE_ERROR;
271                 aq->last_err_rc = status.response_code;
272                 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
273                             __func__, status.response_code,
274                             AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
275                 return AP_SM_WAIT_NONE;
276         }
277 }
278
279 /**
280  * ap_sm_read_write(): Send and receive messages to/from an AP queue.
281  * @aq: pointer to the AP queue
282  *
283  * Returns AP_SM_WAIT_NONE, AP_SM_WAIT_AGAIN, or AP_SM_WAIT_INTERRUPT
284  */
285 static enum ap_sm_wait ap_sm_read_write(struct ap_queue *aq)
286 {
287         return min(ap_sm_read(aq), ap_sm_write(aq));
288 }
289
290 /**
291  * ap_sm_reset(): Reset an AP queue.
292  * @qid: The AP queue number
293  *
294  * Submit the Reset command to an AP queue.
295  */
296 static enum ap_sm_wait ap_sm_reset(struct ap_queue *aq)
297 {
298         struct ap_queue_status status;
299
300         status = ap_rapq(aq->qid);
301         switch (status.response_code) {
302         case AP_RESPONSE_NORMAL:
303         case AP_RESPONSE_RESET_IN_PROGRESS:
304                 aq->sm_state = AP_SM_STATE_RESET_WAIT;
305                 aq->interrupt = AP_INTR_DISABLED;
306                 return AP_SM_WAIT_TIMEOUT;
307         default:
308                 aq->dev_state = AP_DEV_STATE_ERROR;
309                 aq->last_err_rc = status.response_code;
310                 AP_DBF_WARN("%s RC 0x%02x on 0x%02x.%04x -> AP_DEV_STATE_ERROR\n",
311                             __func__, status.response_code,
312                             AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
313                 return AP_SM_WAIT_NONE;
314         }
315 }
316
317 /**
318  * ap_sm_reset_wait(): Test queue for completion of the reset operation
319  * @aq: pointer to the AP queue
320  *
321  * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
322  */
323 static enum ap_sm_wait ap_sm_reset_wait(struct ap_queue *aq)
324 {
325         struct ap_queue_status status;
326         void *lsi_ptr;
327
328         if (aq->queue_count > 0 && aq->reply)
329                 /* Try to read a completed message and get the status */
330                 status = ap_sm_recv(aq);
331         else
332                 /* Get the status with TAPQ */
333                 status = ap_tapq(aq->qid, NULL);
334
335         switch (status.response_code) {
336         case AP_RESPONSE_NORMAL:
337                 lsi_ptr = ap_airq_ptr();
338                 if (lsi_ptr && ap_queue_enable_interruption(aq, lsi_ptr) == 0)
339                         aq->sm_state = AP_SM_STATE_SETIRQ_WAIT;
340                 else
341                         aq->sm_state = (aq->queue_count > 0) ?
342                                 AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
343                 return AP_SM_WAIT_AGAIN;
344         case AP_RESPONSE_BUSY:
345         case AP_RESPONSE_RESET_IN_PROGRESS:
346                 return AP_SM_WAIT_TIMEOUT;
347         case AP_RESPONSE_Q_NOT_AVAIL:
348         case AP_RESPONSE_DECONFIGURED:
349         case AP_RESPONSE_CHECKSTOPPED:
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_setirq_wait(): Test queue for completion of the irq enablement
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_setirq_wait(struct ap_queue *aq)
367 {
368         struct ap_queue_status status;
369
370         if (aq->queue_count > 0 && aq->reply)
371                 /* Try to read a completed message and get the status */
372                 status = ap_sm_recv(aq);
373         else
374                 /* Get the status with TAPQ */
375                 status = ap_tapq(aq->qid, NULL);
376
377         if (status.irq_enabled == 1) {
378                 /* Irqs are now enabled */
379                 aq->interrupt = AP_INTR_ENABLED;
380                 aq->sm_state = (aq->queue_count > 0) ?
381                         AP_SM_STATE_WORKING : AP_SM_STATE_IDLE;
382         }
383
384         switch (status.response_code) {
385         case AP_RESPONSE_NORMAL:
386                 if (aq->queue_count > 0)
387                         return AP_SM_WAIT_AGAIN;
388                 fallthrough;
389         case AP_RESPONSE_NO_PENDING_REPLY:
390                 return AP_SM_WAIT_TIMEOUT;
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 state machine jump table
403  */
404 static ap_func_t *ap_jumptable[NR_AP_SM_STATES][NR_AP_SM_EVENTS] = {
405         [AP_SM_STATE_RESET_START] = {
406                 [AP_SM_EVENT_POLL] = ap_sm_reset,
407                 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
408         },
409         [AP_SM_STATE_RESET_WAIT] = {
410                 [AP_SM_EVENT_POLL] = ap_sm_reset_wait,
411                 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
412         },
413         [AP_SM_STATE_SETIRQ_WAIT] = {
414                 [AP_SM_EVENT_POLL] = ap_sm_setirq_wait,
415                 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
416         },
417         [AP_SM_STATE_IDLE] = {
418                 [AP_SM_EVENT_POLL] = ap_sm_write,
419                 [AP_SM_EVENT_TIMEOUT] = ap_sm_nop,
420         },
421         [AP_SM_STATE_WORKING] = {
422                 [AP_SM_EVENT_POLL] = ap_sm_read_write,
423                 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
424         },
425         [AP_SM_STATE_QUEUE_FULL] = {
426                 [AP_SM_EVENT_POLL] = ap_sm_read,
427                 [AP_SM_EVENT_TIMEOUT] = ap_sm_reset,
428         },
429 };
430
431 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event)
432 {
433         if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
434                 return ap_jumptable[aq->sm_state][event](aq);
435         else
436                 return AP_SM_WAIT_NONE;
437 }
438
439 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event)
440 {
441         enum ap_sm_wait wait;
442
443         while ((wait = ap_sm_event(aq, event)) == AP_SM_WAIT_AGAIN)
444                 ;
445         return wait;
446 }
447
448 /*
449  * AP queue related attributes.
450  */
451 static ssize_t request_count_show(struct device *dev,
452                                   struct device_attribute *attr,
453                                   char *buf)
454 {
455         struct ap_queue *aq = to_ap_queue(dev);
456         bool valid = false;
457         u64 req_cnt;
458
459         spin_lock_bh(&aq->lock);
460         if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
461                 req_cnt = aq->total_request_count;
462                 valid = true;
463         }
464         spin_unlock_bh(&aq->lock);
465
466         if (valid)
467                 return scnprintf(buf, PAGE_SIZE, "%llu\n", req_cnt);
468         else
469                 return scnprintf(buf, PAGE_SIZE, "-\n");
470 }
471
472 static ssize_t request_count_store(struct device *dev,
473                                    struct device_attribute *attr,
474                                    const char *buf, size_t count)
475 {
476         struct ap_queue *aq = to_ap_queue(dev);
477
478         spin_lock_bh(&aq->lock);
479         aq->total_request_count = 0;
480         spin_unlock_bh(&aq->lock);
481
482         return count;
483 }
484
485 static DEVICE_ATTR_RW(request_count);
486
487 static ssize_t requestq_count_show(struct device *dev,
488                                    struct device_attribute *attr, char *buf)
489 {
490         struct ap_queue *aq = to_ap_queue(dev);
491         unsigned int reqq_cnt = 0;
492
493         spin_lock_bh(&aq->lock);
494         if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
495                 reqq_cnt = aq->requestq_count;
496         spin_unlock_bh(&aq->lock);
497         return scnprintf(buf, PAGE_SIZE, "%d\n", reqq_cnt);
498 }
499
500 static DEVICE_ATTR_RO(requestq_count);
501
502 static ssize_t pendingq_count_show(struct device *dev,
503                                    struct device_attribute *attr, char *buf)
504 {
505         struct ap_queue *aq = to_ap_queue(dev);
506         unsigned int penq_cnt = 0;
507
508         spin_lock_bh(&aq->lock);
509         if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
510                 penq_cnt = aq->pendingq_count;
511         spin_unlock_bh(&aq->lock);
512         return scnprintf(buf, PAGE_SIZE, "%d\n", penq_cnt);
513 }
514
515 static DEVICE_ATTR_RO(pendingq_count);
516
517 static ssize_t reset_show(struct device *dev,
518                           struct device_attribute *attr, char *buf)
519 {
520         struct ap_queue *aq = to_ap_queue(dev);
521         int rc = 0;
522
523         spin_lock_bh(&aq->lock);
524         switch (aq->sm_state) {
525         case AP_SM_STATE_RESET_START:
526         case AP_SM_STATE_RESET_WAIT:
527                 rc = scnprintf(buf, PAGE_SIZE, "Reset in progress.\n");
528                 break;
529         case AP_SM_STATE_WORKING:
530         case AP_SM_STATE_QUEUE_FULL:
531                 rc = scnprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
532                 break;
533         default:
534                 rc = scnprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
535         }
536         spin_unlock_bh(&aq->lock);
537         return rc;
538 }
539
540 static ssize_t reset_store(struct device *dev,
541                            struct device_attribute *attr,
542                            const char *buf, size_t count)
543 {
544         struct ap_queue *aq = to_ap_queue(dev);
545
546         spin_lock_bh(&aq->lock);
547         __ap_flush_queue(aq);
548         aq->sm_state = AP_SM_STATE_RESET_START;
549         ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
550         spin_unlock_bh(&aq->lock);
551
552         AP_DBF(DBF_INFO, "reset queue=%02x.%04x triggered by user\n",
553                AP_QID_CARD(aq->qid), AP_QID_QUEUE(aq->qid));
554
555         return count;
556 }
557
558 static DEVICE_ATTR_RW(reset);
559
560 static ssize_t interrupt_show(struct device *dev,
561                               struct device_attribute *attr, char *buf)
562 {
563         struct ap_queue *aq = to_ap_queue(dev);
564         int rc = 0;
565
566         spin_lock_bh(&aq->lock);
567         if (aq->sm_state == AP_SM_STATE_SETIRQ_WAIT)
568                 rc = scnprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
569         else if (aq->interrupt == AP_INTR_ENABLED)
570                 rc = scnprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
571         else
572                 rc = scnprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
573         spin_unlock_bh(&aq->lock);
574         return rc;
575 }
576
577 static DEVICE_ATTR_RO(interrupt);
578
579 static ssize_t config_show(struct device *dev,
580                              struct device_attribute *attr, char *buf)
581 {
582         struct ap_queue *aq = to_ap_queue(dev);
583         int rc;
584
585         spin_lock_bh(&aq->lock);
586         rc = scnprintf(buf, PAGE_SIZE, "%d\n", aq->config ? 1 : 0);
587         spin_unlock_bh(&aq->lock);
588         return rc;
589 }
590
591 static DEVICE_ATTR_RO(config);
592
593 #ifdef CONFIG_ZCRYPT_DEBUG
594 static ssize_t states_show(struct device *dev,
595                            struct device_attribute *attr, char *buf)
596 {
597         struct ap_queue *aq = to_ap_queue(dev);
598         int rc = 0;
599
600         spin_lock_bh(&aq->lock);
601         /* queue device state */
602         switch (aq->dev_state) {
603         case AP_DEV_STATE_UNINITIATED:
604                 rc = scnprintf(buf, PAGE_SIZE, "UNINITIATED\n");
605                 break;
606         case AP_DEV_STATE_OPERATING:
607                 rc = scnprintf(buf, PAGE_SIZE, "OPERATING");
608                 break;
609         case AP_DEV_STATE_SHUTDOWN:
610                 rc = scnprintf(buf, PAGE_SIZE, "SHUTDOWN");
611                 break;
612         case AP_DEV_STATE_ERROR:
613                 rc = scnprintf(buf, PAGE_SIZE, "ERROR");
614                 break;
615         default:
616                 rc = scnprintf(buf, PAGE_SIZE, "UNKNOWN");
617         }
618         /* state machine state */
619         if (aq->dev_state) {
620                 switch (aq->sm_state) {
621                 case AP_SM_STATE_RESET_START:
622                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
623                                         " [RESET_START]\n");
624                         break;
625                 case AP_SM_STATE_RESET_WAIT:
626                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
627                                         " [RESET_WAIT]\n");
628                         break;
629                 case AP_SM_STATE_SETIRQ_WAIT:
630                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
631                                         " [SETIRQ_WAIT]\n");
632                         break;
633                 case AP_SM_STATE_IDLE:
634                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
635                                         " [IDLE]\n");
636                         break;
637                 case AP_SM_STATE_WORKING:
638                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
639                                         " [WORKING]\n");
640                         break;
641                 case AP_SM_STATE_QUEUE_FULL:
642                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
643                                         " [FULL]\n");
644                         break;
645                 default:
646                         rc += scnprintf(buf + rc, PAGE_SIZE - rc,
647                                         " [UNKNOWN]\n");
648                 }
649         }
650         spin_unlock_bh(&aq->lock);
651
652         return rc;
653 }
654 static DEVICE_ATTR_RO(states);
655
656 static ssize_t last_err_rc_show(struct device *dev,
657                                 struct device_attribute *attr, char *buf)
658 {
659         struct ap_queue *aq = to_ap_queue(dev);
660         int rc;
661
662         spin_lock_bh(&aq->lock);
663         rc = aq->last_err_rc;
664         spin_unlock_bh(&aq->lock);
665
666         switch (rc) {
667         case AP_RESPONSE_NORMAL:
668                 return scnprintf(buf, PAGE_SIZE, "NORMAL\n");
669         case AP_RESPONSE_Q_NOT_AVAIL:
670                 return scnprintf(buf, PAGE_SIZE, "Q_NOT_AVAIL\n");
671         case AP_RESPONSE_RESET_IN_PROGRESS:
672                 return scnprintf(buf, PAGE_SIZE, "RESET_IN_PROGRESS\n");
673         case AP_RESPONSE_DECONFIGURED:
674                 return scnprintf(buf, PAGE_SIZE, "DECONFIGURED\n");
675         case AP_RESPONSE_CHECKSTOPPED:
676                 return scnprintf(buf, PAGE_SIZE, "CHECKSTOPPED\n");
677         case AP_RESPONSE_BUSY:
678                 return scnprintf(buf, PAGE_SIZE, "BUSY\n");
679         case AP_RESPONSE_INVALID_ADDRESS:
680                 return scnprintf(buf, PAGE_SIZE, "INVALID_ADDRESS\n");
681         case AP_RESPONSE_OTHERWISE_CHANGED:
682                 return scnprintf(buf, PAGE_SIZE, "OTHERWISE_CHANGED\n");
683         case AP_RESPONSE_Q_FULL:
684                 return scnprintf(buf, PAGE_SIZE, "Q_FULL/NO_PENDING_REPLY\n");
685         case AP_RESPONSE_INDEX_TOO_BIG:
686                 return scnprintf(buf, PAGE_SIZE, "INDEX_TOO_BIG\n");
687         case AP_RESPONSE_NO_FIRST_PART:
688                 return scnprintf(buf, PAGE_SIZE, "NO_FIRST_PART\n");
689         case AP_RESPONSE_MESSAGE_TOO_BIG:
690                 return scnprintf(buf, PAGE_SIZE, "MESSAGE_TOO_BIG\n");
691         case AP_RESPONSE_REQ_FAC_NOT_INST:
692                 return scnprintf(buf, PAGE_SIZE, "REQ_FAC_NOT_INST\n");
693         default:
694                 return scnprintf(buf, PAGE_SIZE, "response code %d\n", rc);
695         }
696 }
697 static DEVICE_ATTR_RO(last_err_rc);
698 #endif
699
700 static struct attribute *ap_queue_dev_attrs[] = {
701         &dev_attr_request_count.attr,
702         &dev_attr_requestq_count.attr,
703         &dev_attr_pendingq_count.attr,
704         &dev_attr_reset.attr,
705         &dev_attr_interrupt.attr,
706         &dev_attr_config.attr,
707 #ifdef CONFIG_ZCRYPT_DEBUG
708         &dev_attr_states.attr,
709         &dev_attr_last_err_rc.attr,
710 #endif
711         NULL
712 };
713
714 static struct attribute_group ap_queue_dev_attr_group = {
715         .attrs = ap_queue_dev_attrs
716 };
717
718 static const struct attribute_group *ap_queue_dev_attr_groups[] = {
719         &ap_queue_dev_attr_group,
720         NULL
721 };
722
723 static struct device_type ap_queue_type = {
724         .name = "ap_queue",
725         .groups = ap_queue_dev_attr_groups,
726 };
727
728 static void ap_queue_device_release(struct device *dev)
729 {
730         struct ap_queue *aq = to_ap_queue(dev);
731
732         spin_lock_bh(&ap_queues_lock);
733         hash_del(&aq->hnode);
734         spin_unlock_bh(&ap_queues_lock);
735
736         kfree(aq);
737 }
738
739 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type)
740 {
741         struct ap_queue *aq;
742
743         aq = kzalloc(sizeof(*aq), GFP_KERNEL);
744         if (!aq)
745                 return NULL;
746         aq->ap_dev.device.release = ap_queue_device_release;
747         aq->ap_dev.device.type = &ap_queue_type;
748         aq->ap_dev.device_type = device_type;
749         aq->qid = qid;
750         aq->interrupt = AP_INTR_DISABLED;
751         spin_lock_init(&aq->lock);
752         INIT_LIST_HEAD(&aq->pendingq);
753         INIT_LIST_HEAD(&aq->requestq);
754         timer_setup(&aq->timeout, ap_request_timeout, 0);
755
756         return aq;
757 }
758
759 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *reply)
760 {
761         aq->reply = reply;
762
763         spin_lock_bh(&aq->lock);
764         ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
765         spin_unlock_bh(&aq->lock);
766 }
767 EXPORT_SYMBOL(ap_queue_init_reply);
768
769 /**
770  * ap_queue_message(): Queue a request to an AP device.
771  * @aq: The AP device to queue the message to
772  * @ap_msg: The message that is to be added
773  */
774 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg)
775 {
776         int rc = 0;
777
778         /* msg needs to have a valid receive-callback */
779         BUG_ON(!ap_msg->receive);
780
781         spin_lock_bh(&aq->lock);
782
783         /* only allow to queue new messages if device state is ok */
784         if (aq->dev_state == AP_DEV_STATE_OPERATING) {
785                 list_add_tail(&ap_msg->list, &aq->requestq);
786                 aq->requestq_count++;
787                 aq->total_request_count++;
788                 atomic64_inc(&aq->card->total_request_count);
789         } else
790                 rc = -ENODEV;
791
792         /* Send/receive as many request from the queue as possible. */
793         ap_wait(ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
794
795         spin_unlock_bh(&aq->lock);
796
797         return rc;
798 }
799 EXPORT_SYMBOL(ap_queue_message);
800
801 /**
802  * ap_cancel_message(): Cancel a crypto request.
803  * @aq: The AP device that has the message queued
804  * @ap_msg: The message that is to be removed
805  *
806  * Cancel a crypto request. This is done by removing the request
807  * from the device pending or request queue. Note that the
808  * request stays on the AP queue. When it finishes the message
809  * reply will be discarded because the psmid can't be found.
810  */
811 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg)
812 {
813         struct ap_message *tmp;
814
815         spin_lock_bh(&aq->lock);
816         if (!list_empty(&ap_msg->list)) {
817                 list_for_each_entry(tmp, &aq->pendingq, list)
818                         if (tmp->psmid == ap_msg->psmid) {
819                                 aq->pendingq_count--;
820                                 goto found;
821                         }
822                 aq->requestq_count--;
823 found:
824                 list_del_init(&ap_msg->list);
825         }
826         spin_unlock_bh(&aq->lock);
827 }
828 EXPORT_SYMBOL(ap_cancel_message);
829
830 /**
831  * __ap_flush_queue(): Flush requests.
832  * @aq: Pointer to the AP queue
833  *
834  * Flush all requests from the request/pending queue of an AP device.
835  */
836 static void __ap_flush_queue(struct ap_queue *aq)
837 {
838         struct ap_message *ap_msg, *next;
839
840         list_for_each_entry_safe(ap_msg, next, &aq->pendingq, list) {
841                 list_del_init(&ap_msg->list);
842                 aq->pendingq_count--;
843                 ap_msg->rc = -EAGAIN;
844                 ap_msg->receive(aq, ap_msg, NULL);
845         }
846         list_for_each_entry_safe(ap_msg, next, &aq->requestq, list) {
847                 list_del_init(&ap_msg->list);
848                 aq->requestq_count--;
849                 ap_msg->rc = -EAGAIN;
850                 ap_msg->receive(aq, ap_msg, NULL);
851         }
852         aq->queue_count = 0;
853 }
854
855 void ap_flush_queue(struct ap_queue *aq)
856 {
857         spin_lock_bh(&aq->lock);
858         __ap_flush_queue(aq);
859         spin_unlock_bh(&aq->lock);
860 }
861 EXPORT_SYMBOL(ap_flush_queue);
862
863 void ap_queue_prepare_remove(struct ap_queue *aq)
864 {
865         spin_lock_bh(&aq->lock);
866         /* flush queue */
867         __ap_flush_queue(aq);
868         /* move queue device state to SHUTDOWN in progress */
869         aq->dev_state = AP_DEV_STATE_SHUTDOWN;
870         spin_unlock_bh(&aq->lock);
871         del_timer_sync(&aq->timeout);
872 }
873
874 void ap_queue_remove(struct ap_queue *aq)
875 {
876         /*
877          * all messages have been flushed and the device state
878          * is SHUTDOWN. Now reset with zero which also clears
879          * the irq registration and move the device state
880          * to the initial value AP_DEV_STATE_UNINITIATED.
881          */
882         spin_lock_bh(&aq->lock);
883         ap_zapq(aq->qid);
884         aq->dev_state = AP_DEV_STATE_UNINITIATED;
885         spin_unlock_bh(&aq->lock);
886 }
887
888 void ap_queue_init_state(struct ap_queue *aq)
889 {
890         spin_lock_bh(&aq->lock);
891         aq->dev_state = AP_DEV_STATE_OPERATING;
892         aq->sm_state = AP_SM_STATE_RESET_START;
893         ap_wait(ap_sm_event(aq, AP_SM_EVENT_POLL));
894         spin_unlock_bh(&aq->lock);
895 }
896 EXPORT_SYMBOL(ap_queue_init_state);