- patches.arch/s390-fate311926-ap-type10-toleration.patch:
[opensuse:kernel.git] / drivers / s390 / crypto / ap_bus.c
1 /*
2  * linux/drivers/s390/crypto/ap_bus.c
3  *
4  * Copyright (C) 2006 IBM Corporation
5  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
6  *            Martin Schwidefsky <schwidefsky@de.ibm.com>
7  *            Ralph Wuerthner <rwuerthn@de.ibm.com>
8  *            Felix Beck <felix.beck@de.ibm.com>
9  *            Holger Dengler <hd@linux.vnet.ibm.com>
10  *
11  * Adjunct processor bus.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #define KMSG_COMPONENT "ap"
29 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
30
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/err.h>
35 #include <linux/interrupt.h>
36 #include <linux/workqueue.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <asm/reset.h>
41 #include <asm/airq.h>
42 #include <asm/atomic.h>
43 #include <asm/system.h>
44 #include <asm/isc.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
47
48 #include "ap_bus.h"
49
50 /* Some prototypes. */
51 static void ap_scan_bus(struct work_struct *);
52 static void ap_poll_all(unsigned long);
53 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *);
54 static int ap_poll_thread_start(void);
55 static void ap_poll_thread_stop(void);
56 static void ap_request_timeout(unsigned long);
57 static inline void ap_schedule_poll_timer(void);
58 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
59 static int ap_device_remove(struct device *dev);
60 static int ap_device_probe(struct device *dev);
61 static void ap_interrupt_handler(void *unused1, void *unused2);
62 static void ap_reset(struct ap_device *ap_dev);
63 static void ap_config_timeout(unsigned long ptr);
64 static int ap_select_domain(void);
65
66 /*
67  * Module description.
68  */
69 MODULE_AUTHOR("IBM Corporation");
70 MODULE_DESCRIPTION("Adjunct Processor Bus driver, "
71                    "Copyright 2006 IBM Corporation");
72 MODULE_LICENSE("GPL");
73
74 /*
75  * Module parameter
76  */
77 int ap_domain_index = -1;       /* Adjunct Processor Domain Index */
78 module_param_named(domain, ap_domain_index, int, 0000);
79 MODULE_PARM_DESC(domain, "domain index for ap devices");
80 EXPORT_SYMBOL(ap_domain_index);
81
82 static int ap_thread_flag = 0;
83 module_param_named(poll_thread, ap_thread_flag, int, 0000);
84 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
85
86 static struct device *ap_root_device = NULL;
87 static DEFINE_SPINLOCK(ap_device_list_lock);
88 static LIST_HEAD(ap_device_list);
89
90 /*
91  * Workqueue & timer for bus rescan.
92  */
93 static struct workqueue_struct *ap_work_queue;
94 static struct timer_list ap_config_timer;
95 static int ap_config_time = AP_CONFIG_TIME;
96 static DECLARE_WORK(ap_config_work, ap_scan_bus);
97
98 /*
99  * Tasklet & timer for AP request polling and interrupts
100  */
101 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
102 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
103 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
104 static struct task_struct *ap_poll_kthread = NULL;
105 static DEFINE_MUTEX(ap_poll_thread_mutex);
106 static DEFINE_SPINLOCK(ap_poll_timer_lock);
107 static void *ap_interrupt_indicator;
108 static struct hrtimer ap_poll_timer;
109 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
110  * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
111 static unsigned long long poll_timeout = 250000;
112
113 /* Suspend flag */
114 static int ap_suspend_flag;
115 /* Flag to check if domain was set through module parameter domain=. This is
116  * important when supsend and resume is done in a z/VM environment where the
117  * domain might change. */
118 static int user_set_domain = 0;
119 static struct bus_type ap_bus_type;
120
121 /**
122  * ap_using_interrupts() - Returns non-zero if interrupt support is
123  * available.
124  */
125 static inline int ap_using_interrupts(void)
126 {
127         return ap_interrupt_indicator != NULL;
128 }
129
130 /**
131  * ap_intructions_available() - Test if AP instructions are available.
132  *
133  * Returns 0 if the AP instructions are installed.
134  */
135 static inline int ap_instructions_available(void)
136 {
137         register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
138         register unsigned long reg1 asm ("1") = -ENODEV;
139         register unsigned long reg2 asm ("2") = 0UL;
140
141         asm volatile(
142                 "   .long 0xb2af0000\n"         /* PQAP(TAPQ) */
143                 "0: la    %1,0\n"
144                 "1:\n"
145                 EX_TABLE(0b, 1b)
146                 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
147         return reg1;
148 }
149
150 /**
151  * ap_interrupts_available(): Test if AP interrupts are available.
152  *
153  * Returns 1 if AP interrupts are available.
154  */
155 static int ap_interrupts_available(void)
156 {
157         unsigned long long facility_bits[2];
158
159         if (stfle(facility_bits, 2) <= 1)
160                 return 0;
161         if (!(facility_bits[0] & (1ULL << 61)) ||
162             !(facility_bits[1] & (1ULL << 62)))
163                 return 0;
164         return 1;
165 }
166
167 /**
168  * ap_test_queue(): Test adjunct processor queue.
169  * @qid: The AP queue number
170  * @queue_depth: Pointer to queue depth value
171  * @device_type: Pointer to device type value
172  *
173  * Returns AP queue status structure.
174  */
175 static inline struct ap_queue_status
176 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type)
177 {
178         register unsigned long reg0 asm ("0") = qid;
179         register struct ap_queue_status reg1 asm ("1");
180         register unsigned long reg2 asm ("2") = 0UL;
181
182         asm volatile(".long 0xb2af0000"         /* PQAP(TAPQ) */
183                      : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
184         *device_type = (int) (reg2 >> 24);
185         *queue_depth = (int) (reg2 & 0xff);
186         return reg1;
187 }
188
189 /**
190  * ap_reset_queue(): Reset adjunct processor queue.
191  * @qid: The AP queue number
192  *
193  * Returns AP queue status structure.
194  */
195 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
196 {
197         register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
198         register struct ap_queue_status reg1 asm ("1");
199         register unsigned long reg2 asm ("2") = 0UL;
200
201         asm volatile(
202                 ".long 0xb2af0000"              /* PQAP(RAPQ) */
203                 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
204         return reg1;
205 }
206
207 #ifdef CONFIG_64BIT
208 /**
209  * ap_queue_interruption_control(): Enable interruption for a specific AP.
210  * @qid: The AP queue number
211  * @ind: The notification indicator byte
212  *
213  * Returns AP queue status.
214  */
215 static inline struct ap_queue_status
216 ap_queue_interruption_control(ap_qid_t qid, void *ind)
217 {
218         register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
219         register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
220         register struct ap_queue_status reg1_out asm ("1");
221         register void *reg2 asm ("2") = ind;
222         asm volatile(
223                 ".long 0xb2af0000"              /* PQAP(RAPQ) */
224                 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
225                 :
226                 : "cc" );
227         return reg1_out;
228 }
229 #endif
230
231 #ifdef CONFIG_64BIT
232 static inline struct ap_queue_status
233 __ap_query_functions(ap_qid_t qid, unsigned int *functions)
234 {
235         register unsigned long reg0 asm ("0") = 0UL | qid | (1UL << 23);
236         register struct ap_queue_status reg1 asm ("1") = AP_QUEUE_STATUS_INVALID;
237         register unsigned long reg2 asm ("2");
238
239         asm volatile(
240                 ".long 0xb2af0000\n"
241                 "0:\n"
242                 EX_TABLE(0b, 0b)
243                 : "+d" (reg0), "+d" (reg1), "=d" (reg2)
244                 :
245                 : "cc");
246
247         *functions = (unsigned int)(reg2 >> 32);
248         return reg1;
249 }
250 #endif
251
252 /**
253  * ap_query_functions(): Query supported functions.
254  * @qid: The AP queue number
255  * @functions: Pointer to functions field.
256  *
257  * Returns
258  *   0        on success.
259  *   -ENODEV  if queue not valid.
260  *   -EBUSY   if device busy.
261  *   -EINVAL  if query function is not supported
262  */
263 static int ap_query_functions(ap_qid_t qid, unsigned int *functions)
264 {
265 #ifdef CONFIG_64BIT
266         struct ap_queue_status status;
267         int i;
268         status = __ap_query_functions(qid, functions);
269
270         for (i = 0; i < AP_MAX_RESET; i++) {
271                 if (ap_queue_status_invalid_test(&status))
272                         return -ENODEV;
273
274                 switch (status.response_code) {
275                 case AP_RESPONSE_NORMAL:
276                         return 0;
277                 case AP_RESPONSE_RESET_IN_PROGRESS:
278                 case AP_RESPONSE_BUSY:
279                         break;
280                 case AP_RESPONSE_Q_NOT_AVAIL:
281                 case AP_RESPONSE_DECONFIGURED:
282                 case AP_RESPONSE_CHECKSTOPPED:
283                 case AP_RESPONSE_INVALID_ADDRESS:
284                         return -ENODEV;
285                 case AP_RESPONSE_OTHERWISE_CHANGED:
286                         break;
287                 default:
288                         break;
289                 }
290                 if (i < AP_MAX_RESET - 1) {
291                         udelay(5);
292                         status = __ap_query_functions(qid, functions);
293                 }
294         }
295         return -EBUSY;
296 #else
297         return -EINVAL;
298 #endif
299 }
300
301 /**
302  * ap_4096_commands_availablen(): Check for availability of 4096 bit RSA
303  * support.
304  * @qid: The AP queue number
305  *
306  * Returns 1 if 4096 bit RSA keys are support fo the AP, returns 0 if not.
307  */
308 int ap_4096_commands_available(ap_qid_t qid)
309 {
310         unsigned int functions;
311
312         if (ap_query_functions(qid, &functions))
313                 return 0;
314
315         return test_ap_facility(functions, 1) &&
316                test_ap_facility(functions, 2);
317 }
318 EXPORT_SYMBOL(ap_4096_commands_available);
319
320 /**
321  * ap_queue_enable_interruption(): Enable interruption on an AP.
322  * @qid: The AP queue number
323  * @ind: the notification indicator byte
324  *
325  * Enables interruption on AP queue via ap_queue_interruption_control(). Based
326  * on the return value it waits a while and tests the AP queue if interrupts
327  * have been switched on using ap_test_queue().
328  */
329 static int ap_queue_enable_interruption(ap_qid_t qid, void *ind)
330 {
331 #ifdef CONFIG_64BIT
332         struct ap_queue_status status;
333         int t_depth, t_device_type, rc, i;
334
335         rc = -EBUSY;
336         status = ap_queue_interruption_control(qid, ind);
337
338         for (i = 0; i < AP_MAX_RESET; i++) {
339                 switch (status.response_code) {
340                 case AP_RESPONSE_NORMAL:
341                         if (status.int_enabled)
342                                 return 0;
343                         break;
344                 case AP_RESPONSE_RESET_IN_PROGRESS:
345                 case AP_RESPONSE_BUSY:
346                         break;
347                 case AP_RESPONSE_Q_NOT_AVAIL:
348                 case AP_RESPONSE_DECONFIGURED:
349                 case AP_RESPONSE_CHECKSTOPPED:
350                 case AP_RESPONSE_INVALID_ADDRESS:
351                         return -ENODEV;
352                 case AP_RESPONSE_OTHERWISE_CHANGED:
353                         if (status.int_enabled)
354                                 return 0;
355                         break;
356                 default:
357                         break;
358                 }
359                 if (i < AP_MAX_RESET - 1) {
360                         udelay(5);
361                         status = ap_test_queue(qid, &t_depth, &t_device_type);
362                 }
363         }
364         return rc;
365 #else
366         return -EINVAL;
367 #endif
368 }
369
370 /**
371  * __ap_send(): Send message to adjunct processor queue.
372  * @qid: The AP queue number
373  * @psmid: The program supplied message identifier
374  * @msg: The message text
375  * @length: The message length
376  * @special: Special Bit
377  *
378  * Returns AP queue status structure.
379  * Condition code 1 on NQAP can't happen because the L bit is 1.
380  * Condition code 2 on NQAP also means the send is incomplete,
381  * because a segment boundary was reached. The NQAP is repeated.
382  */
383 static inline struct ap_queue_status
384 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
385           unsigned int special)
386 {
387         typedef struct { char _[length]; } msgblock;
388         register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
389         register struct ap_queue_status reg1 asm ("1");
390         register unsigned long reg2 asm ("2") = (unsigned long) msg;
391         register unsigned long reg3 asm ("3") = (unsigned long) length;
392         register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
393         register unsigned long reg5 asm ("5") = (unsigned int) psmid;
394
395         if (special == 1)
396                 reg0 |= 0x400000UL;
397
398         asm volatile (
399                 "0: .long 0xb2ad0042\n"         /* DQAP */
400                 "   brc   2,0b"
401                 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
402                 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
403                 : "cc" );
404         return reg1;
405 }
406
407 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
408 {
409         struct ap_queue_status status;
410
411         status = __ap_send(qid, psmid, msg, length, 0);
412         switch (status.response_code) {
413         case AP_RESPONSE_NORMAL:
414                 return 0;
415         case AP_RESPONSE_Q_FULL:
416         case AP_RESPONSE_RESET_IN_PROGRESS:
417                 return -EBUSY;
418         case AP_RESPONSE_REQ_FAC_NOT_INST:
419                 return -EINVAL;
420         default:        /* Device is gone. */
421                 return -ENODEV;
422         }
423 }
424 EXPORT_SYMBOL(ap_send);
425
426 /**
427  * __ap_recv(): Receive message from adjunct processor queue.
428  * @qid: The AP queue number
429  * @psmid: Pointer to program supplied message identifier
430  * @msg: The message text
431  * @length: The message length
432  *
433  * Returns AP queue status structure.
434  * Condition code 1 on DQAP means the receive has taken place
435  * but only partially.  The response is incomplete, hence the
436  * DQAP is repeated.
437  * Condition code 2 on DQAP also means the receive is incomplete,
438  * this time because a segment boundary was reached. Again, the
439  * DQAP is repeated.
440  * Note that gpr2 is used by the DQAP instruction to keep track of
441  * any 'residual' length, in case the instruction gets interrupted.
442  * Hence it gets zeroed before the instruction.
443  */
444 static inline struct ap_queue_status
445 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
446 {
447         typedef struct { char _[length]; } msgblock;
448         register unsigned long reg0 asm("0") = qid | 0x80000000UL;
449         register struct ap_queue_status reg1 asm ("1");
450         register unsigned long reg2 asm("2") = 0UL;
451         register unsigned long reg4 asm("4") = (unsigned long) msg;
452         register unsigned long reg5 asm("5") = (unsigned long) length;
453         register unsigned long reg6 asm("6") = 0UL;
454         register unsigned long reg7 asm("7") = 0UL;
455
456
457         asm volatile(
458                 "0: .long 0xb2ae0064\n"
459                 "   brc   6,0b\n"
460                 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
461                 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
462                 "=m" (*(msgblock *) msg) : : "cc" );
463         *psmid = (((unsigned long long) reg6) << 32) + reg7;
464         return reg1;
465 }
466
467 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
468 {
469         struct ap_queue_status status;
470
471         status = __ap_recv(qid, psmid, msg, length);
472         switch (status.response_code) {
473         case AP_RESPONSE_NORMAL:
474                 return 0;
475         case AP_RESPONSE_NO_PENDING_REPLY:
476                 if (status.queue_empty)
477                         return -ENOENT;
478                 return -EBUSY;
479         case AP_RESPONSE_RESET_IN_PROGRESS:
480                 return -EBUSY;
481         default:
482                 return -ENODEV;
483         }
484 }
485 EXPORT_SYMBOL(ap_recv);
486
487 /**
488  * ap_query_queue(): Check if an AP queue is available.
489  * @qid: The AP queue number
490  * @queue_depth: Pointer to queue depth value
491  * @device_type: Pointer to device type value
492  *
493  * The test is repeated for AP_MAX_RESET times.
494  */
495 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type)
496 {
497         struct ap_queue_status status;
498         int t_depth, t_device_type, rc, i;
499
500         rc = -EBUSY;
501         for (i = 0; i < AP_MAX_RESET; i++) {
502                 status = ap_test_queue(qid, &t_depth, &t_device_type);
503                 switch (status.response_code) {
504                 case AP_RESPONSE_NORMAL:
505                         *queue_depth = t_depth + 1;
506                         *device_type = t_device_type;
507                         rc = 0;
508                         break;
509                 case AP_RESPONSE_Q_NOT_AVAIL:
510                         rc = -ENODEV;
511                         break;
512                 case AP_RESPONSE_RESET_IN_PROGRESS:
513                         break;
514                 case AP_RESPONSE_DECONFIGURED:
515                         rc = -ENODEV;
516                         break;
517                 case AP_RESPONSE_CHECKSTOPPED:
518                         rc = -ENODEV;
519                         break;
520                 case AP_RESPONSE_INVALID_ADDRESS:
521                         rc = -ENODEV;
522                         break;
523                 case AP_RESPONSE_OTHERWISE_CHANGED:
524                         break;
525                 case AP_RESPONSE_BUSY:
526                         break;
527                 default:
528                         BUG();
529                 }
530                 if (rc != -EBUSY)
531                         break;
532                 if (i < AP_MAX_RESET - 1)
533                         udelay(5);
534         }
535         return rc;
536 }
537
538 /**
539  * ap_init_queue(): Reset an AP queue.
540  * @qid: The AP queue number
541  *
542  * Reset an AP queue and wait for it to become available again.
543  */
544 static int ap_init_queue(ap_qid_t qid)
545 {
546         struct ap_queue_status status;
547         int rc, dummy, i;
548
549         rc = -ENODEV;
550         status = ap_reset_queue(qid);
551         for (i = 0; i < AP_MAX_RESET; i++) {
552                 switch (status.response_code) {
553                 case AP_RESPONSE_NORMAL:
554                         if (status.queue_empty)
555                                 rc = 0;
556                         break;
557                 case AP_RESPONSE_Q_NOT_AVAIL:
558                 case AP_RESPONSE_DECONFIGURED:
559                 case AP_RESPONSE_CHECKSTOPPED:
560                         i = AP_MAX_RESET;       /* return with -ENODEV */
561                         break;
562                 case AP_RESPONSE_RESET_IN_PROGRESS:
563                         rc = -EBUSY;
564                 case AP_RESPONSE_BUSY:
565                 default:
566                         break;
567                 }
568                 if (rc != -ENODEV && rc != -EBUSY)
569                         break;
570                 if (i < AP_MAX_RESET - 1) {
571                         udelay(5);
572                         status = ap_test_queue(qid, &dummy, &dummy);
573                 }
574         }
575         if (rc == 0 && ap_using_interrupts()) {
576                 rc = ap_queue_enable_interruption(qid, ap_interrupt_indicator);
577                 /* If interruption mode is supported by the machine,
578                 * but an AP can not be enabled for interruption then
579                 * the AP will be discarded.    */
580                 if (rc)
581                         pr_err("Registering adapter interrupts for "
582                                "AP %d failed\n", AP_QID_DEVICE(qid));
583         }
584         return rc;
585 }
586
587 /**
588  * ap_increase_queue_count(): Arm request timeout.
589  * @ap_dev: Pointer to an AP device.
590  *
591  * Arm request timeout if an AP device was idle and a new request is submitted.
592  */
593 static void ap_increase_queue_count(struct ap_device *ap_dev)
594 {
595         int timeout = ap_dev->drv->request_timeout;
596
597         ap_dev->queue_count++;
598         if (ap_dev->queue_count == 1) {
599                 mod_timer(&ap_dev->timeout, jiffies + timeout);
600                 ap_dev->reset = AP_RESET_ARMED;
601         }
602 }
603
604 /**
605  * ap_decrease_queue_count(): Decrease queue count.
606  * @ap_dev: Pointer to an AP device.
607  *
608  * If AP device is still alive, re-schedule request timeout if there are still
609  * pending requests.
610  */
611 static void ap_decrease_queue_count(struct ap_device *ap_dev)
612 {
613         int timeout = ap_dev->drv->request_timeout;
614
615         ap_dev->queue_count--;
616         if (ap_dev->queue_count > 0)
617                 mod_timer(&ap_dev->timeout, jiffies + timeout);
618         else
619                 /*
620                  * The timeout timer should to be disabled now - since
621                  * del_timer_sync() is very expensive, we just tell via the
622                  * reset flag to ignore the pending timeout timer.
623                  */
624                 ap_dev->reset = AP_RESET_IGNORE;
625 }
626
627 /*
628  * AP device related attributes.
629  */
630 static ssize_t ap_hwtype_show(struct device *dev,
631                               struct device_attribute *attr, char *buf)
632 {
633         struct ap_device *ap_dev = to_ap_dev(dev);
634         return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
635 }
636
637 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
638 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
639                              char *buf)
640 {
641         struct ap_device *ap_dev = to_ap_dev(dev);
642         return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
643 }
644
645 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
646 static ssize_t ap_request_count_show(struct device *dev,
647                                      struct device_attribute *attr,
648                                      char *buf)
649 {
650         struct ap_device *ap_dev = to_ap_dev(dev);
651         int rc;
652
653         spin_lock_bh(&ap_dev->lock);
654         rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
655         spin_unlock_bh(&ap_dev->lock);
656         return rc;
657 }
658
659 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
660
661 static ssize_t ap_modalias_show(struct device *dev,
662                                 struct device_attribute *attr, char *buf)
663 {
664         return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type);
665 }
666
667 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
668
669 static struct attribute *ap_dev_attrs[] = {
670         &dev_attr_hwtype.attr,
671         &dev_attr_depth.attr,
672         &dev_attr_request_count.attr,
673         &dev_attr_modalias.attr,
674         NULL
675 };
676 static struct attribute_group ap_dev_attr_group = {
677         .attrs = ap_dev_attrs
678 };
679
680 /**
681  * ap_bus_match()
682  * @dev: Pointer to device
683  * @drv: Pointer to device_driver
684  *
685  * AP bus driver registration/unregistration.
686  */
687 static int ap_bus_match(struct device *dev, struct device_driver *drv)
688 {
689         struct ap_device *ap_dev = to_ap_dev(dev);
690         struct ap_driver *ap_drv = to_ap_drv(drv);
691         struct ap_device_id *id;
692
693         /*
694          * Compare device type of the device with the list of
695          * supported types of the device_driver.
696          */
697         for (id = ap_drv->ids; id->match_flags; id++) {
698                 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
699                     (id->dev_type != ap_dev->device_type))
700                         continue;
701                 return 1;
702         }
703         return 0;
704 }
705
706 /**
707  * ap_uevent(): Uevent function for AP devices.
708  * @dev: Pointer to device
709  * @env: Pointer to kobj_uevent_env
710  *
711  * It sets up a single environment variable DEV_TYPE which contains the
712  * hardware device type.
713  */
714 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
715 {
716         struct ap_device *ap_dev = to_ap_dev(dev);
717         int retval = 0;
718
719         if (!ap_dev)
720                 return -ENODEV;
721
722         /* Set up DEV_TYPE environment variable. */
723         retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
724         if (retval)
725                 return retval;
726
727         /* Add MODALIAS= */
728         retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
729
730         return retval;
731 }
732
733 static int ap_bus_suspend(struct device *dev, pm_message_t state)
734 {
735         struct ap_device *ap_dev = to_ap_dev(dev);
736         unsigned long flags;
737
738         if (!ap_suspend_flag) {
739                 ap_suspend_flag = 1;
740
741                 /* Disable scanning for devices, thus we do not want to scan
742                  * for them after removing.
743                  */
744                 del_timer_sync(&ap_config_timer);
745                 if (ap_work_queue != NULL) {
746                         destroy_workqueue(ap_work_queue);
747                         ap_work_queue = NULL;
748                 }
749
750                 tasklet_disable(&ap_tasklet);
751         }
752         /* Poll on the device until all requests are finished. */
753         do {
754                 flags = 0;
755                 spin_lock_bh(&ap_dev->lock);
756                 __ap_poll_device(ap_dev, &flags);
757                 spin_unlock_bh(&ap_dev->lock);
758         } while ((flags & 1) || (flags & 2));
759
760         spin_lock_bh(&ap_dev->lock);
761         ap_dev->unregistered = 1;
762         spin_unlock_bh(&ap_dev->lock);
763
764         return 0;
765 }
766
767 static int ap_bus_resume(struct device *dev)
768 {
769         int rc = 0;
770         struct ap_device *ap_dev = to_ap_dev(dev);
771
772         if (ap_suspend_flag) {
773                 ap_suspend_flag = 0;
774                 if (!ap_interrupts_available())
775                         ap_interrupt_indicator = NULL;
776                 if (!user_set_domain) {
777                         ap_domain_index = -1;
778                         ap_select_domain();
779                 }
780                 init_timer(&ap_config_timer);
781                 ap_config_timer.function = ap_config_timeout;
782                 ap_config_timer.data = 0;
783                 ap_config_timer.expires = jiffies + ap_config_time * HZ;
784                 add_timer(&ap_config_timer);
785                 ap_work_queue = create_singlethread_workqueue("kapwork");
786                 if (!ap_work_queue)
787                         return -ENOMEM;
788                 tasklet_enable(&ap_tasklet);
789                 if (!ap_using_interrupts())
790                         ap_schedule_poll_timer();
791                 else
792                         tasklet_schedule(&ap_tasklet);
793                 if (ap_thread_flag)
794                         rc = ap_poll_thread_start();
795         }
796         if (AP_QID_QUEUE(ap_dev->qid) != ap_domain_index) {
797                 spin_lock_bh(&ap_dev->lock);
798                 ap_dev->qid = AP_MKQID(AP_QID_DEVICE(ap_dev->qid),
799                                        ap_domain_index);
800                 spin_unlock_bh(&ap_dev->lock);
801         }
802         queue_work(ap_work_queue, &ap_config_work);
803
804         return rc;
805 }
806
807 static struct bus_type ap_bus_type = {
808         .name = "ap",
809         .match = &ap_bus_match,
810         .uevent = &ap_uevent,
811         .suspend = ap_bus_suspend,
812         .resume = ap_bus_resume
813 };
814
815 static int ap_device_probe(struct device *dev)
816 {
817         struct ap_device *ap_dev = to_ap_dev(dev);
818         struct ap_driver *ap_drv = to_ap_drv(dev->driver);
819         int rc;
820
821         ap_dev->drv = ap_drv;
822         rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
823         if (!rc) {
824                 spin_lock_bh(&ap_device_list_lock);
825                 list_add(&ap_dev->list, &ap_device_list);
826                 spin_unlock_bh(&ap_device_list_lock);
827         }
828         return rc;
829 }
830
831 /**
832  * __ap_flush_queue(): Flush requests.
833  * @ap_dev: Pointer to the AP device
834  *
835  * Flush all requests from the request/pending queue of an AP device.
836  */
837 static void __ap_flush_queue(struct ap_device *ap_dev)
838 {
839         struct ap_message *ap_msg, *next;
840
841         list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
842                 list_del_init(&ap_msg->list);
843                 ap_dev->pendingq_count--;
844                 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
845         }
846         list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
847                 list_del_init(&ap_msg->list);
848                 ap_dev->requestq_count--;
849                 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
850         }
851 }
852
853 void ap_flush_queue(struct ap_device *ap_dev)
854 {
855         spin_lock_bh(&ap_dev->lock);
856         __ap_flush_queue(ap_dev);
857         spin_unlock_bh(&ap_dev->lock);
858 }
859 EXPORT_SYMBOL(ap_flush_queue);
860
861 static int ap_device_remove(struct device *dev)
862 {
863         struct ap_device *ap_dev = to_ap_dev(dev);
864         struct ap_driver *ap_drv = ap_dev->drv;
865
866         ap_flush_queue(ap_dev);
867         del_timer_sync(&ap_dev->timeout);
868         spin_lock_bh(&ap_device_list_lock);
869         list_del_init(&ap_dev->list);
870         spin_unlock_bh(&ap_device_list_lock);
871         if (ap_drv->remove)
872                 ap_drv->remove(ap_dev);
873         spin_lock_bh(&ap_dev->lock);
874         atomic_sub(ap_dev->queue_count, &ap_poll_requests);
875         spin_unlock_bh(&ap_dev->lock);
876         return 0;
877 }
878
879 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
880                        char *name)
881 {
882         struct device_driver *drv = &ap_drv->driver;
883
884         drv->bus = &ap_bus_type;
885         drv->probe = ap_device_probe;
886         drv->remove = ap_device_remove;
887         drv->owner = owner;
888         drv->name = name;
889         return driver_register(drv);
890 }
891 EXPORT_SYMBOL(ap_driver_register);
892
893 void ap_driver_unregister(struct ap_driver *ap_drv)
894 {
895         driver_unregister(&ap_drv->driver);
896 }
897 EXPORT_SYMBOL(ap_driver_unregister);
898
899 /*
900  * AP bus attributes.
901  */
902 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
903 {
904         return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
905 }
906
907 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
908
909 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
910 {
911         return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
912 }
913
914 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
915 {
916         return snprintf(buf, PAGE_SIZE, "%d\n",
917                         ap_using_interrupts() ? 1 : 0);
918 }
919
920 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
921
922 static ssize_t ap_config_time_store(struct bus_type *bus,
923                                     const char *buf, size_t count)
924 {
925         int time;
926
927         if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
928                 return -EINVAL;
929         ap_config_time = time;
930         if (!timer_pending(&ap_config_timer) ||
931             !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) {
932                 ap_config_timer.expires = jiffies + ap_config_time * HZ;
933                 add_timer(&ap_config_timer);
934         }
935         return count;
936 }
937
938 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
939
940 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
941 {
942         return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
943 }
944
945 static ssize_t ap_poll_thread_store(struct bus_type *bus,
946                                     const char *buf, size_t count)
947 {
948         int flag, rc;
949
950         if (sscanf(buf, "%d\n", &flag) != 1)
951                 return -EINVAL;
952         if (flag) {
953                 rc = ap_poll_thread_start();
954                 if (rc)
955                         return rc;
956         }
957         else
958                 ap_poll_thread_stop();
959         return count;
960 }
961
962 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
963
964 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
965 {
966         return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
967 }
968
969 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
970                                   size_t count)
971 {
972         unsigned long long time;
973         ktime_t hr_time;
974
975         /* 120 seconds = maximum poll interval */
976         if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
977             time > 120000000000ULL)
978                 return -EINVAL;
979         poll_timeout = time;
980         hr_time = ktime_set(0, poll_timeout);
981
982         if (!hrtimer_is_queued(&ap_poll_timer) ||
983             !hrtimer_forward(&ap_poll_timer, hrtimer_get_expires(&ap_poll_timer), hr_time)) {
984                 hrtimer_set_expires(&ap_poll_timer, hr_time);
985                 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
986         }
987         return count;
988 }
989
990 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
991
992 static struct bus_attribute *const ap_bus_attrs[] = {
993         &bus_attr_ap_domain,
994         &bus_attr_config_time,
995         &bus_attr_poll_thread,
996         &bus_attr_ap_interrupts,
997         &bus_attr_poll_timeout,
998         NULL,
999 };
1000
1001 /**
1002  * ap_select_domain(): Select an AP domain.
1003  *
1004  * Pick one of the 16 AP domains.
1005  */
1006 static int ap_select_domain(void)
1007 {
1008         int queue_depth, device_type, count, max_count, best_domain;
1009         int rc, i, j;
1010
1011         /*
1012          * We want to use a single domain. Either the one specified with
1013          * the "domain=" parameter or the domain with the maximum number
1014          * of devices.
1015          */
1016         if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS)
1017                 /* Domain has already been selected. */
1018                 return 0;
1019         best_domain = -1;
1020         max_count = 0;
1021         for (i = 0; i < AP_DOMAINS; i++) {
1022                 count = 0;
1023                 for (j = 0; j < AP_DEVICES; j++) {
1024                         ap_qid_t qid = AP_MKQID(j, i);
1025                         rc = ap_query_queue(qid, &queue_depth, &device_type);
1026                         if (rc)
1027                                 continue;
1028                         count++;
1029                 }
1030                 if (count > max_count) {
1031                         max_count = count;
1032                         best_domain = i;
1033                 }
1034         }
1035         if (best_domain >= 0){
1036                 ap_domain_index = best_domain;
1037                 return 0;
1038         }
1039         return -ENODEV;
1040 }
1041
1042 /**
1043  * ap_probe_device_type(): Find the device type of an AP.
1044  * @ap_dev: pointer to the AP device.
1045  *
1046  * Find the device type if query queue returned a device type of 0.
1047  */
1048 static int ap_probe_device_type(struct ap_device *ap_dev)
1049 {
1050         static unsigned char msg[] = {
1051                 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1052                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1053                 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1054                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1055                 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1056                 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1057                 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1058                 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1059                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1060                 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1061                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1062                 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1063                 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1064                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1065                 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1066                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1067                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1068                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1069                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1070                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1071                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1072                 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1073                 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1074                 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1075                 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1076                 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1077                 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1078                 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1079                 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1080                 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1081                 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1082                 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1083                 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1084                 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1085                 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1086                 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1087                 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1088                 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1089                 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1090                 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1091                 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1092                 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1093                 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1094                 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1095                 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1096         };
1097         struct ap_queue_status status;
1098         unsigned long long psmid;
1099         char *reply;
1100         int rc, i;
1101
1102         reply = (void *) get_zeroed_page(GFP_KERNEL);
1103         if (!reply) {
1104                 rc = -ENOMEM;
1105                 goto out;
1106         }
1107
1108         status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1109                            msg, sizeof(msg), 0);
1110         if (status.response_code != AP_RESPONSE_NORMAL) {
1111                 rc = -ENODEV;
1112                 goto out_free;
1113         }
1114
1115         /* Wait for the test message to complete. */
1116         for (i = 0; i < 6; i++) {
1117                 mdelay(300);
1118                 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1119                 if (status.response_code == AP_RESPONSE_NORMAL &&
1120                     psmid == 0x0102030405060708ULL)
1121                         break;
1122         }
1123         if (i < 6) {
1124                 /* Got an answer. */
1125                 if (reply[0] == 0x00 && reply[1] == 0x86)
1126                         ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1127                 else
1128                         ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1129                 rc = 0;
1130         } else
1131                 rc = -ENODEV;
1132
1133 out_free:
1134         free_page((unsigned long) reply);
1135 out:
1136         return rc;
1137 }
1138
1139 static void ap_interrupt_handler(void *unused1, void *unused2)
1140 {
1141         tasklet_schedule(&ap_tasklet);
1142 }
1143
1144 /**
1145  * __ap_scan_bus(): Scan the AP bus.
1146  * @dev: Pointer to device
1147  * @data: Pointer to data
1148  *
1149  * Scan the AP bus for new devices.
1150  */
1151 static int __ap_scan_bus(struct device *dev, void *data)
1152 {
1153         return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1154 }
1155
1156 static void ap_device_release(struct device *dev)
1157 {
1158         struct ap_device *ap_dev = to_ap_dev(dev);
1159
1160         kfree(ap_dev);
1161 }
1162
1163 static void ap_scan_bus(struct work_struct *unused)
1164 {
1165         struct ap_device *ap_dev;
1166         struct device *dev;
1167         ap_qid_t qid;
1168         int queue_depth, device_type;
1169         unsigned int device_functions;
1170         int rc, i;
1171
1172         if (ap_select_domain() != 0)
1173                 return;
1174         for (i = 0; i < AP_DEVICES; i++) {
1175                 qid = AP_MKQID(i, ap_domain_index);
1176                 dev = bus_find_device(&ap_bus_type, NULL,
1177                                       (void *)(unsigned long)qid,
1178                                       __ap_scan_bus);
1179                 rc = ap_query_queue(qid, &queue_depth, &device_type);
1180                 if (dev) {
1181                         if (rc == -EBUSY) {
1182                                 set_current_state(TASK_UNINTERRUPTIBLE);
1183                                 schedule_timeout(AP_RESET_TIMEOUT);
1184                                 rc = ap_query_queue(qid, &queue_depth,
1185                                                     &device_type);
1186                         }
1187                         ap_dev = to_ap_dev(dev);
1188                         spin_lock_bh(&ap_dev->lock);
1189                         if (rc || ap_dev->unregistered) {
1190                                 spin_unlock_bh(&ap_dev->lock);
1191                                 if (ap_dev->unregistered)
1192                                         i--;
1193                                 device_unregister(dev);
1194                                 put_device(dev);
1195                                 continue;
1196                         }
1197                         spin_unlock_bh(&ap_dev->lock);
1198                         put_device(dev);
1199                         continue;
1200                 }
1201                 if (rc)
1202                         continue;
1203                 rc = ap_init_queue(qid);
1204                 if (rc)
1205                         continue;
1206                 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1207                 if (!ap_dev)
1208                         break;
1209                 ap_dev->qid = qid;
1210                 ap_dev->queue_depth = queue_depth;
1211                 ap_dev->unregistered = 1;
1212                 spin_lock_init(&ap_dev->lock);
1213                 INIT_LIST_HEAD(&ap_dev->pendingq);
1214                 INIT_LIST_HEAD(&ap_dev->requestq);
1215                 INIT_LIST_HEAD(&ap_dev->list);
1216                 setup_timer(&ap_dev->timeout, ap_request_timeout,
1217                             (unsigned long) ap_dev);
1218                 switch (device_type) {
1219                 case 0:
1220                         if (ap_probe_device_type(ap_dev)) {
1221                                 kfree(ap_dev);
1222                                 continue;
1223                         }
1224                         break;
1225                 case 10:
1226                         if (ap_query_functions(qid, &device_functions)) {
1227                                 kfree(ap_dev);
1228                                 continue;
1229                         }
1230                         if (test_ap_facility(device_functions, 3))
1231                                 ap_dev->device_type = AP_DEVICE_TYPE_CEX3C;
1232                         else if (test_ap_facility(device_functions, 4))
1233                                 ap_dev->device_type = AP_DEVICE_TYPE_CEX3A;
1234                         else {
1235                                 kfree(ap_dev);
1236                                 continue;
1237                         }
1238                         break;
1239                 default:
1240                         ap_dev->device_type = device_type;
1241                 }
1242
1243                 ap_dev->device.bus = &ap_bus_type;
1244                 ap_dev->device.parent = ap_root_device;
1245                 if (dev_set_name(&ap_dev->device, "card%02x",
1246                                  AP_QID_DEVICE(ap_dev->qid))) {
1247                         kfree(ap_dev);
1248                         continue;
1249                 }
1250                 ap_dev->device.release = ap_device_release;
1251                 rc = device_register(&ap_dev->device);
1252                 if (rc) {
1253                         put_device(&ap_dev->device);
1254                         continue;
1255                 }
1256                 /* Add device attributes. */
1257                 rc = sysfs_create_group(&ap_dev->device.kobj,
1258                                         &ap_dev_attr_group);
1259                 if (!rc) {
1260                         spin_lock_bh(&ap_dev->lock);
1261                         ap_dev->unregistered = 0;
1262                         spin_unlock_bh(&ap_dev->lock);
1263                 }
1264                 else
1265                         device_unregister(&ap_dev->device);
1266         }
1267 }
1268
1269 static void
1270 ap_config_timeout(unsigned long ptr)
1271 {
1272         queue_work(ap_work_queue, &ap_config_work);
1273         ap_config_timer.expires = jiffies + ap_config_time * HZ;
1274         add_timer(&ap_config_timer);
1275 }
1276
1277 /**
1278  * ap_schedule_poll_timer(): Schedule poll timer.
1279  *
1280  * Set up the timer to run the poll tasklet
1281  */
1282 static inline void ap_schedule_poll_timer(void)
1283 {
1284         ktime_t hr_time;
1285
1286         spin_lock_bh(&ap_poll_timer_lock);
1287         if (ap_using_interrupts() || ap_suspend_flag)
1288                 goto out;
1289         if (hrtimer_is_queued(&ap_poll_timer))
1290                 goto out;
1291         if (ktime_to_ns(hrtimer_expires_remaining(&ap_poll_timer)) <= 0) {
1292                 hr_time = ktime_set(0, poll_timeout);
1293                 hrtimer_forward_now(&ap_poll_timer, hr_time);
1294                 hrtimer_restart(&ap_poll_timer);
1295         }
1296 out:
1297         spin_unlock_bh(&ap_poll_timer_lock);
1298 }
1299
1300 /**
1301  * ap_poll_read(): Receive pending reply messages from an AP device.
1302  * @ap_dev: pointer to the AP device
1303  * @flags: pointer to control flags, bit 2^0 is set if another poll is
1304  *         required, bit 2^1 is set if the poll timer needs to get armed
1305  *
1306  * Returns 0 if the device is still present, -ENODEV if not.
1307  */
1308 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1309 {
1310         struct ap_queue_status status;
1311         struct ap_message *ap_msg;
1312
1313         if (ap_dev->queue_count <= 0)
1314                 return 0;
1315         status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1316                            ap_dev->reply->message, ap_dev->reply->length);
1317         switch (status.response_code) {
1318         case AP_RESPONSE_NORMAL:
1319                 atomic_dec(&ap_poll_requests);
1320                 ap_decrease_queue_count(ap_dev);
1321                 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1322                         if (ap_msg->psmid != ap_dev->reply->psmid)
1323                                 continue;
1324                         list_del_init(&ap_msg->list);
1325                         ap_dev->pendingq_count--;
1326                         ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply);
1327                         break;
1328                 }
1329                 if (ap_dev->queue_count > 0)
1330                         *flags |= 1;
1331                 break;
1332         case AP_RESPONSE_NO_PENDING_REPLY:
1333                 if (status.queue_empty) {
1334                         /* The card shouldn't forget requests but who knows. */
1335                         atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1336                         ap_dev->queue_count = 0;
1337                         list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1338                         ap_dev->requestq_count += ap_dev->pendingq_count;
1339                         ap_dev->pendingq_count = 0;
1340                 } else
1341                         *flags |= 2;
1342                 break;
1343         default:
1344                 return -ENODEV;
1345         }
1346         return 0;
1347 }
1348
1349 /**
1350  * ap_poll_write(): Send messages from the request queue to an AP device.
1351  * @ap_dev: pointer to the AP device
1352  * @flags: pointer to control flags, bit 2^0 is set if another poll is
1353  *         required, bit 2^1 is set if the poll timer needs to get armed
1354  *
1355  * Returns 0 if the device is still present, -ENODEV if not.
1356  */
1357 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1358 {
1359         struct ap_queue_status status;
1360         struct ap_message *ap_msg;
1361
1362         if (ap_dev->requestq_count <= 0 ||
1363             ap_dev->queue_count >= ap_dev->queue_depth)
1364                 return 0;
1365         /* Start the next request on the queue. */
1366         ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1367         status = __ap_send(ap_dev->qid, ap_msg->psmid,
1368                            ap_msg->message, ap_msg->length, ap_msg->special);
1369         switch (status.response_code) {
1370         case AP_RESPONSE_NORMAL:
1371                 atomic_inc(&ap_poll_requests);
1372                 ap_increase_queue_count(ap_dev);
1373                 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1374                 ap_dev->requestq_count--;
1375                 ap_dev->pendingq_count++;
1376                 if (ap_dev->queue_count < ap_dev->queue_depth &&
1377                     ap_dev->requestq_count > 0)
1378                         *flags |= 1;
1379                 *flags |= 2;
1380                 break;
1381         case AP_RESPONSE_Q_FULL:
1382         case AP_RESPONSE_RESET_IN_PROGRESS:
1383                 *flags |= 2;
1384                 break;
1385         case AP_RESPONSE_MESSAGE_TOO_BIG:
1386         case AP_RESPONSE_REQ_FAC_NOT_INST:
1387                 return -EINVAL;
1388         default:
1389                 return -ENODEV;
1390         }
1391         return 0;
1392 }
1393
1394 /**
1395  * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1396  * @ap_dev: pointer to the bus device
1397  * @flags: pointer to control flags, bit 2^0 is set if another poll is
1398  *         required, bit 2^1 is set if the poll timer needs to get armed
1399  *
1400  * Poll AP device for pending replies and send new messages. If either
1401  * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1402  * Returns 0.
1403  */
1404 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1405 {
1406         int rc;
1407
1408         rc = ap_poll_read(ap_dev, flags);
1409         if (rc)
1410                 return rc;
1411         return ap_poll_write(ap_dev, flags);
1412 }
1413
1414 /**
1415  * __ap_queue_message(): Queue a message to a device.
1416  * @ap_dev: pointer to the AP device
1417  * @ap_msg: the message to be queued
1418  *
1419  * Queue a message to a device. Returns 0 if successful.
1420  */
1421 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1422 {
1423         struct ap_queue_status status;
1424
1425         if (list_empty(&ap_dev->requestq) &&
1426             ap_dev->queue_count < ap_dev->queue_depth) {
1427                 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1428                                    ap_msg->message, ap_msg->length,
1429                                    ap_msg->special);
1430                 switch (status.response_code) {
1431                 case AP_RESPONSE_NORMAL:
1432                         list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1433                         atomic_inc(&ap_poll_requests);
1434                         ap_dev->pendingq_count++;
1435                         ap_increase_queue_count(ap_dev);
1436                         ap_dev->total_request_count++;
1437                         break;
1438                 case AP_RESPONSE_Q_FULL:
1439                 case AP_RESPONSE_RESET_IN_PROGRESS:
1440                         list_add_tail(&ap_msg->list, &ap_dev->requestq);
1441                         ap_dev->requestq_count++;
1442                         ap_dev->total_request_count++;
1443                         return -EBUSY;
1444                 case AP_RESPONSE_REQ_FAC_NOT_INST:
1445                 case AP_RESPONSE_MESSAGE_TOO_BIG:
1446                         ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL));
1447                         return -EINVAL;
1448                 default:        /* Device is gone. */
1449                         ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1450                         return -ENODEV;
1451                 }
1452         } else {
1453                 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1454                 ap_dev->requestq_count++;
1455                 ap_dev->total_request_count++;
1456                 return -EBUSY;
1457         }
1458         ap_schedule_poll_timer();
1459         return 0;
1460 }
1461
1462 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1463 {
1464         unsigned long flags;
1465         int rc;
1466
1467         spin_lock_bh(&ap_dev->lock);
1468         if (!ap_dev->unregistered) {
1469                 /* Make room on the queue by polling for finished requests. */
1470                 rc = ap_poll_queue(ap_dev, &flags);
1471                 if (!rc)
1472                         rc = __ap_queue_message(ap_dev, ap_msg);
1473                 if (!rc)
1474                         wake_up(&ap_poll_wait);
1475                 if (rc == -ENODEV)
1476                         ap_dev->unregistered = 1;
1477         } else {
1478                 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV));
1479                 rc = -ENODEV;
1480         }
1481         spin_unlock_bh(&ap_dev->lock);
1482         if (rc == -ENODEV)
1483                 device_unregister(&ap_dev->device);
1484 }
1485 EXPORT_SYMBOL(ap_queue_message);
1486
1487 /**
1488  * ap_cancel_message(): Cancel a crypto request.
1489  * @ap_dev: The AP device that has the message queued
1490  * @ap_msg: The message that is to be removed
1491  *
1492  * Cancel a crypto request. This is done by removing the request
1493  * from the device pending or request queue. Note that the
1494  * request stays on the AP queue. When it finishes the message
1495  * reply will be discarded because the psmid can't be found.
1496  */
1497 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1498 {
1499         struct ap_message *tmp;
1500
1501         spin_lock_bh(&ap_dev->lock);
1502         if (!list_empty(&ap_msg->list)) {
1503                 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1504                         if (tmp->psmid == ap_msg->psmid) {
1505                                 ap_dev->pendingq_count--;
1506                                 goto found;
1507                         }
1508                 ap_dev->requestq_count--;
1509         found:
1510                 list_del_init(&ap_msg->list);
1511         }
1512         spin_unlock_bh(&ap_dev->lock);
1513 }
1514 EXPORT_SYMBOL(ap_cancel_message);
1515
1516 /**
1517  * ap_poll_timeout(): AP receive polling for finished AP requests.
1518  * @unused: Unused pointer.
1519  *
1520  * Schedules the AP tasklet using a high resolution timer.
1521  */
1522 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1523 {
1524         tasklet_schedule(&ap_tasklet);
1525         return HRTIMER_NORESTART;
1526 }
1527
1528 /**
1529  * ap_reset(): Reset a not responding AP device.
1530  * @ap_dev: Pointer to the AP device
1531  *
1532  * Reset a not responding AP device and move all requests from the
1533  * pending queue to the request queue.
1534  */
1535 static void ap_reset(struct ap_device *ap_dev)
1536 {
1537         int rc;
1538
1539         ap_dev->reset = AP_RESET_IGNORE;
1540         atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1541         ap_dev->queue_count = 0;
1542         list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1543         ap_dev->requestq_count += ap_dev->pendingq_count;
1544         ap_dev->pendingq_count = 0;
1545         rc = ap_init_queue(ap_dev->qid);
1546         if (rc == -ENODEV)
1547                 ap_dev->unregistered = 1;
1548 }
1549
1550 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1551 {
1552         if (!ap_dev->unregistered) {
1553                 if (ap_poll_queue(ap_dev, flags))
1554                         ap_dev->unregistered = 1;
1555                 if (ap_dev->reset == AP_RESET_DO)
1556                         ap_reset(ap_dev);
1557         }
1558         return 0;
1559 }
1560
1561 /**
1562  * ap_poll_all(): Poll all AP devices.
1563  * @dummy: Unused variable
1564  *
1565  * Poll all AP devices on the bus in a round robin fashion. Continue
1566  * polling until bit 2^0 of the control flags is not set. If bit 2^1
1567  * of the control flags has been set arm the poll timer.
1568  */
1569 static void ap_poll_all(unsigned long dummy)
1570 {
1571         unsigned long flags;
1572         struct ap_device *ap_dev;
1573
1574         /* Reset the indicator if interrupts are used. Thus new interrupts can
1575          * be received. Doing it in the beginning of the tasklet is therefor
1576          * important that no requests on any AP get lost.
1577          */
1578         if (ap_using_interrupts())
1579                 xchg((u8 *)ap_interrupt_indicator, 0);
1580         do {
1581                 flags = 0;
1582                 spin_lock(&ap_device_list_lock);
1583                 list_for_each_entry(ap_dev, &ap_device_list, list) {
1584                         spin_lock(&ap_dev->lock);
1585                         __ap_poll_device(ap_dev, &flags);
1586                         spin_unlock(&ap_dev->lock);
1587                 }
1588                 spin_unlock(&ap_device_list_lock);
1589         } while (flags & 1);
1590         if (flags & 2)
1591                 ap_schedule_poll_timer();
1592 }
1593
1594 /**
1595  * ap_poll_thread(): Thread that polls for finished requests.
1596  * @data: Unused pointer
1597  *
1598  * AP bus poll thread. The purpose of this thread is to poll for
1599  * finished requests in a loop if there is a "free" cpu - that is
1600  * a cpu that doesn't have anything better to do. The polling stops
1601  * as soon as there is another task or if all messages have been
1602  * delivered.
1603  */
1604 static int ap_poll_thread(void *data)
1605 {
1606         DECLARE_WAITQUEUE(wait, current);
1607         unsigned long flags;
1608         int requests;
1609         struct ap_device *ap_dev;
1610
1611         set_user_nice(current, 19);
1612         while (1) {
1613                 if (ap_suspend_flag)
1614                         return 0;
1615                 if (need_resched()) {
1616                         schedule();
1617                         continue;
1618                 }
1619                 add_wait_queue(&ap_poll_wait, &wait);
1620                 set_current_state(TASK_INTERRUPTIBLE);
1621                 if (kthread_should_stop())
1622                         break;
1623                 requests = atomic_read(&ap_poll_requests);
1624                 if (requests <= 0)
1625                         schedule();
1626                 set_current_state(TASK_RUNNING);
1627                 remove_wait_queue(&ap_poll_wait, &wait);
1628
1629                 flags = 0;
1630                 spin_lock_bh(&ap_device_list_lock);
1631                 list_for_each_entry(ap_dev, &ap_device_list, list) {
1632                         spin_lock(&ap_dev->lock);
1633                         __ap_poll_device(ap_dev, &flags);
1634                         spin_unlock(&ap_dev->lock);
1635                 }
1636                 spin_unlock_bh(&ap_device_list_lock);
1637         }
1638         set_current_state(TASK_RUNNING);
1639         remove_wait_queue(&ap_poll_wait, &wait);
1640         return 0;
1641 }
1642
1643 static int ap_poll_thread_start(void)
1644 {
1645         int rc;
1646
1647         if (ap_using_interrupts() || ap_suspend_flag)
1648                 return 0;
1649         mutex_lock(&ap_poll_thread_mutex);
1650         if (!ap_poll_kthread) {
1651                 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
1652                 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0;
1653                 if (rc)
1654                         ap_poll_kthread = NULL;
1655         }
1656         else
1657                 rc = 0;
1658         mutex_unlock(&ap_poll_thread_mutex);
1659         return rc;
1660 }
1661
1662 static void ap_poll_thread_stop(void)
1663 {
1664         mutex_lock(&ap_poll_thread_mutex);
1665         if (ap_poll_kthread) {
1666                 kthread_stop(ap_poll_kthread);
1667                 ap_poll_kthread = NULL;
1668         }
1669         mutex_unlock(&ap_poll_thread_mutex);
1670 }
1671
1672 /**
1673  * ap_request_timeout(): Handling of request timeouts
1674  * @data: Holds the AP device.
1675  *
1676  * Handles request timeouts.
1677  */
1678 static void ap_request_timeout(unsigned long data)
1679 {
1680         struct ap_device *ap_dev = (struct ap_device *) data;
1681
1682         if (ap_dev->reset == AP_RESET_ARMED) {
1683                 ap_dev->reset = AP_RESET_DO;
1684
1685                 if (ap_using_interrupts())
1686                         tasklet_schedule(&ap_tasklet);
1687         }
1688 }
1689
1690 static void ap_reset_domain(void)
1691 {
1692         int i;
1693
1694         if (ap_domain_index != -1)
1695                 for (i = 0; i < AP_DEVICES; i++)
1696                         ap_reset_queue(AP_MKQID(i, ap_domain_index));
1697 }
1698
1699 static void ap_reset_all(void)
1700 {
1701         int i, j;
1702
1703         for (i = 0; i < AP_DOMAINS; i++)
1704                 for (j = 0; j < AP_DEVICES; j++)
1705                         ap_reset_queue(AP_MKQID(j, i));
1706 }
1707
1708 static struct reset_call ap_reset_call = {
1709         .fn = ap_reset_all,
1710 };
1711
1712 /**
1713  * ap_module_init(): The module initialization code.
1714  *
1715  * Initializes the module.
1716  */
1717 int __init ap_module_init(void)
1718 {
1719         int rc, i;
1720
1721         if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) {
1722                 pr_warning("%d is not a valid cryptographic domain\n",
1723                            ap_domain_index);
1724                 return -EINVAL;
1725         }
1726         /* In resume callback we need to know if the user had set the domain.
1727          * If so, we can not just reset it.
1728          */
1729         if (ap_domain_index >= 0)
1730                 user_set_domain = 1;
1731
1732         if (ap_instructions_available() != 0) {
1733                 pr_warning("The hardware system does not support "
1734                            "AP instructions\n");
1735                 return -ENODEV;
1736         }
1737         if (ap_interrupts_available()) {
1738                 isc_register(AP_ISC);
1739                 ap_interrupt_indicator = s390_register_adapter_interrupt(
1740                         &ap_interrupt_handler, NULL, AP_ISC);
1741                 if (IS_ERR(ap_interrupt_indicator)) {
1742                         ap_interrupt_indicator = NULL;
1743                         isc_unregister(AP_ISC);
1744                 }
1745         }
1746
1747         register_reset_call(&ap_reset_call);
1748
1749         /* Create /sys/bus/ap. */
1750         rc = bus_register(&ap_bus_type);
1751         if (rc)
1752                 goto out;
1753         for (i = 0; ap_bus_attrs[i]; i++) {
1754                 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1755                 if (rc)
1756                         goto out_bus;
1757         }
1758
1759         /* Create /sys/devices/ap. */
1760         ap_root_device = root_device_register("ap");
1761         rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0;
1762         if (rc)
1763                 goto out_bus;
1764
1765         ap_work_queue = create_singlethread_workqueue("kapwork");
1766         if (!ap_work_queue) {
1767                 rc = -ENOMEM;
1768                 goto out_root;
1769         }
1770
1771         if (ap_select_domain() == 0)
1772                 ap_scan_bus(NULL);
1773
1774         /* Setup the AP bus rescan timer. */
1775         init_timer(&ap_config_timer);
1776         ap_config_timer.function = ap_config_timeout;
1777         ap_config_timer.data = 0;
1778         ap_config_timer.expires = jiffies + ap_config_time * HZ;
1779         add_timer(&ap_config_timer);
1780
1781         /* Setup the high resultion poll timer.
1782          * If we are running under z/VM adjust polling to z/VM polling rate.
1783          */
1784         if (MACHINE_IS_VM)
1785                 poll_timeout = 1500000;
1786         spin_lock_init(&ap_poll_timer_lock);
1787         hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1788         ap_poll_timer.function = ap_poll_timeout;
1789
1790         /* Start the low priority AP bus poll thread. */
1791         if (ap_thread_flag) {
1792                 rc = ap_poll_thread_start();
1793                 if (rc)
1794                         goto out_work;
1795         }
1796
1797         return 0;
1798
1799 out_work:
1800         del_timer_sync(&ap_config_timer);
1801         hrtimer_cancel(&ap_poll_timer);
1802         destroy_workqueue(ap_work_queue);
1803 out_root:
1804         root_device_unregister(ap_root_device);
1805 out_bus:
1806         while (i--)
1807                 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1808         bus_unregister(&ap_bus_type);
1809 out:
1810         unregister_reset_call(&ap_reset_call);
1811         if (ap_using_interrupts()) {
1812                 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1813                 isc_unregister(AP_ISC);
1814         }
1815         return rc;
1816 }
1817
1818 static int __ap_match_all(struct device *dev, void *data)
1819 {
1820         return 1;
1821 }
1822
1823 /**
1824  * ap_modules_exit(): The module termination code
1825  *
1826  * Terminates the module.
1827  */
1828 void ap_module_exit(void)
1829 {
1830         int i;
1831         struct device *dev;
1832
1833         ap_reset_domain();
1834         ap_poll_thread_stop();
1835         del_timer_sync(&ap_config_timer);
1836         hrtimer_cancel(&ap_poll_timer);
1837         destroy_workqueue(ap_work_queue);
1838         tasklet_kill(&ap_tasklet);
1839         root_device_unregister(ap_root_device);
1840         while ((dev = bus_find_device(&ap_bus_type, NULL, NULL,
1841                     __ap_match_all)))
1842         {
1843                 device_unregister(dev);
1844                 put_device(dev);
1845         }
1846         for (i = 0; ap_bus_attrs[i]; i++)
1847                 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1848         bus_unregister(&ap_bus_type);
1849         unregister_reset_call(&ap_reset_call);
1850         if (ap_using_interrupts()) {
1851                 s390_unregister_adapter_interrupt(ap_interrupt_indicator, AP_ISC);
1852                 isc_unregister(AP_ISC);
1853         }
1854 }
1855
1856 #ifndef CONFIG_ZCRYPT_MONOLITHIC
1857 module_init(ap_module_init);
1858 module_exit(ap_module_exit);
1859 #endif