]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - drivers/s390/crypto/ap_bus.c
ktime: Cleanup ktime_set() usage
[mirror_ubuntu-artful-kernel.git] / drivers / s390 / crypto / ap_bus.c
index f407b4f9d0ba5ce10389ab7fa790184090be607e..5fa6991928647971ef52047dad1294455219ada8 100644 (file)
 #include <linux/ktime.h>
 #include <asm/facility.h>
 #include <linux/crypto.h>
+#include <linux/mod_devicetable.h>
+#include <linux/debugfs.h>
 
 #include "ap_bus.h"
+#include "ap_asm.h"
+#include "ap_debug.h"
 
 /*
  * Module description.
@@ -62,6 +66,7 @@ MODULE_ALIAS_CRYPTO("z90crypt");
  * Module parameter
  */
 int ap_domain_index = -1;      /* Adjunct Processor Domain Index */
+static DEFINE_SPINLOCK(ap_domain_lock);
 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
 MODULE_PARM_DESC(domain, "domain index for ap devices");
 EXPORT_SYMBOL(ap_domain_index);
@@ -70,12 +75,20 @@ static int ap_thread_flag = 0;
 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
 
-static struct device *ap_root_device = NULL;
+static struct device *ap_root_device;
+
+DEFINE_SPINLOCK(ap_list_lock);
+LIST_HEAD(ap_card_list);
+
 static struct ap_config_info *ap_configuration;
-static DEFINE_SPINLOCK(ap_device_list_lock);
-static LIST_HEAD(ap_device_list);
 static bool initialised;
 
+/*
+ * AP bus related debug feature things.
+ */
+static struct dentry *ap_dbf_root;
+debug_info_t *ap_dbf_info;
+
 /*
  * Workqueue timer for bus rescan.
  */
@@ -89,7 +102,6 @@ static DECLARE_WORK(ap_scan_work, ap_scan_bus);
  */
 static void ap_tasklet_fn(unsigned long);
 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
-static atomic_t ap_poll_requests = ATOMIC_INIT(0);
 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
 static struct task_struct *ap_poll_kthread = NULL;
 static DEFINE_MUTEX(ap_poll_thread_mutex);
@@ -129,23 +141,17 @@ static inline int ap_using_interrupts(void)
 }
 
 /**
- * ap_intructions_available() - Test if AP instructions are available.
+ * ap_airq_ptr() - Get the address of the adapter interrupt indicator
  *
- * Returns 0 if the AP instructions are installed.
+ * Returns the address of the local-summary-indicator of the adapter
+ * interrupt handler for AP, or NULL if adapter interrupts are not
+ * available.
  */
-static inline int ap_instructions_available(void)
+void *ap_airq_ptr(void)
 {
-       register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
-       register unsigned long reg1 asm ("1") = -ENODEV;
-       register unsigned long reg2 asm ("2") = 0UL;
-
-       asm volatile(
-               "   .long 0xb2af0000\n"         /* PQAP(TAPQ) */
-               "0: la    %1,0\n"
-               "1:\n"
-               EX_TABLE(0b, 1b)
-               : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
-       return reg1;
+       if (ap_using_interrupts())
+               return ap_airq.lsi_ptr;
+       return NULL;
 }
 
 /**
@@ -169,19 +175,6 @@ static int ap_configuration_available(void)
        return test_facility(12);
 }
 
-static inline struct ap_queue_status
-__pqap_tapq(ap_qid_t qid, unsigned long *info)
-{
-       register unsigned long reg0 asm ("0") = qid;
-       register struct ap_queue_status reg1 asm ("1");
-       register unsigned long reg2 asm ("2") = 0UL;
-
-       asm volatile(".long 0xb2af0000"         /* PQAP(TAPQ) */
-                    : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
-       *info = reg2;
-       return reg1;
-}
-
 /**
  * ap_test_queue(): Test adjunct processor queue.
  * @qid: The AP queue number
@@ -192,85 +185,16 @@ __pqap_tapq(ap_qid_t qid, unsigned long *info)
 static inline struct ap_queue_status
 ap_test_queue(ap_qid_t qid, unsigned long *info)
 {
-       struct ap_queue_status aqs;
-       unsigned long _info;
-
        if (test_facility(15))
                qid |= 1UL << 23;               /* set APFT T bit*/
-       aqs = __pqap_tapq(qid, &_info);
-       if (info)
-               *info = _info;
-       return aqs;
-}
-
-/**
- * ap_reset_queue(): Reset adjunct processor queue.
- * @qid: The AP queue number
- *
- * Returns AP queue status structure.
- */
-static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
-{
-       register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
-       register struct ap_queue_status reg1 asm ("1");
-       register unsigned long reg2 asm ("2") = 0UL;
-
-       asm volatile(
-               ".long 0xb2af0000"              /* PQAP(RAPQ) */
-               : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
-       return reg1;
-}
-
-/**
- * ap_queue_interruption_control(): Enable interruption for a specific AP.
- * @qid: The AP queue number
- * @ind: The notification indicator byte
- *
- * Returns AP queue status.
- */
-static inline struct ap_queue_status
-ap_queue_interruption_control(ap_qid_t qid, void *ind)
-{
-       register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
-       register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
-       register struct ap_queue_status reg1_out asm ("1");
-       register void *reg2 asm ("2") = ind;
-       asm volatile(
-               ".long 0xb2af0000"              /* PQAP(AQIC) */
-               : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
-               :
-               : "cc" );
-       return reg1_out;
-}
-
-/**
- * ap_query_configuration(): Get AP configuration data
- *
- * Returns 0 on success, or -EOPNOTSUPP.
- */
-static inline int __ap_query_configuration(void)
-{
-       register unsigned long reg0 asm ("0") = 0x04000000UL;
-       register unsigned long reg1 asm ("1") = -EINVAL;
-       register void *reg2 asm ("2") = (void *) ap_configuration;
-
-       asm volatile(
-               ".long 0xb2af0000\n"            /* PQAP(QCI) */
-               "0: la    %1,0\n"
-               "1:\n"
-               EX_TABLE(0b, 1b)
-               : "+d" (reg0), "+d" (reg1), "+d" (reg2)
-               :
-               : "cc");
-
-       return reg1;
+       return ap_tapq(qid, info);
 }
 
 static inline int ap_query_configuration(void)
 {
        if (!ap_configuration)
                return -EOPNOTSUPP;
-       return __ap_query_configuration();
+       return ap_qci(ap_configuration);
 }
 
 /**
@@ -330,162 +254,6 @@ static inline int ap_test_config_domain(unsigned int domain)
        return ap_test_config(ap_configuration->aqm, domain);
 }
 
-/**
- * ap_queue_enable_interruption(): Enable interruption on an AP.
- * @qid: The AP queue number
- * @ind: the notification indicator byte
- *
- * Enables interruption on AP queue via ap_queue_interruption_control(). Based
- * on the return value it waits a while and tests the AP queue if interrupts
- * have been switched on using ap_test_queue().
- */
-static int ap_queue_enable_interruption(struct ap_device *ap_dev, void *ind)
-{
-       struct ap_queue_status status;
-
-       status = ap_queue_interruption_control(ap_dev->qid, ind);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-       case AP_RESPONSE_OTHERWISE_CHANGED:
-               return 0;
-       case AP_RESPONSE_Q_NOT_AVAIL:
-       case AP_RESPONSE_DECONFIGURED:
-       case AP_RESPONSE_CHECKSTOPPED:
-       case AP_RESPONSE_INVALID_ADDRESS:
-               pr_err("Registering adapter interrupts for AP %d failed\n",
-                      AP_QID_DEVICE(ap_dev->qid));
-               return -EOPNOTSUPP;
-       case AP_RESPONSE_RESET_IN_PROGRESS:
-       case AP_RESPONSE_BUSY:
-       default:
-               return -EBUSY;
-       }
-}
-
-static inline struct ap_queue_status
-__nqap(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
-{
-       typedef struct { char _[length]; } msgblock;
-       register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
-       register struct ap_queue_status reg1 asm ("1");
-       register unsigned long reg2 asm ("2") = (unsigned long) msg;
-       register unsigned long reg3 asm ("3") = (unsigned long) length;
-       register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
-       register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
-
-       asm volatile (
-               "0: .long 0xb2ad0042\n"         /* NQAP */
-               "   brc   2,0b"
-               : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
-               : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
-               : "cc");
-       return reg1;
-}
-
-/**
- * __ap_send(): Send message to adjunct processor queue.
- * @qid: The AP queue number
- * @psmid: The program supplied message identifier
- * @msg: The message text
- * @length: The message length
- * @special: Special Bit
- *
- * Returns AP queue status structure.
- * Condition code 1 on NQAP can't happen because the L bit is 1.
- * Condition code 2 on NQAP also means the send is incomplete,
- * because a segment boundary was reached. The NQAP is repeated.
- */
-static inline struct ap_queue_status
-__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
-         unsigned int special)
-{
-       if (special == 1)
-               qid |= 0x400000UL;
-       return __nqap(qid, psmid, msg, length);
-}
-
-int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
-{
-       struct ap_queue_status status;
-
-       status = __ap_send(qid, psmid, msg, length, 0);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               return 0;
-       case AP_RESPONSE_Q_FULL:
-       case AP_RESPONSE_RESET_IN_PROGRESS:
-               return -EBUSY;
-       case AP_RESPONSE_REQ_FAC_NOT_INST:
-               return -EINVAL;
-       default:        /* Device is gone. */
-               return -ENODEV;
-       }
-}
-EXPORT_SYMBOL(ap_send);
-
-/**
- * __ap_recv(): Receive message from adjunct processor queue.
- * @qid: The AP queue number
- * @psmid: Pointer to program supplied message identifier
- * @msg: The message text
- * @length: The message length
- *
- * Returns AP queue status structure.
- * Condition code 1 on DQAP means the receive has taken place
- * but only partially. The response is incomplete, hence the
- * DQAP is repeated.
- * Condition code 2 on DQAP also means the receive is incomplete,
- * this time because a segment boundary was reached. Again, the
- * DQAP is repeated.
- * Note that gpr2 is used by the DQAP instruction to keep track of
- * any 'residual' length, in case the instruction gets interrupted.
- * Hence it gets zeroed before the instruction.
- */
-static inline struct ap_queue_status
-__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
-{
-       typedef struct { char _[length]; } msgblock;
-       register unsigned long reg0 asm("0") = qid | 0x80000000UL;
-       register struct ap_queue_status reg1 asm ("1");
-       register unsigned long reg2 asm("2") = 0UL;
-       register unsigned long reg4 asm("4") = (unsigned long) msg;
-       register unsigned long reg5 asm("5") = (unsigned long) length;
-       register unsigned long reg6 asm("6") = 0UL;
-       register unsigned long reg7 asm("7") = 0UL;
-
-
-       asm volatile(
-               "0: .long 0xb2ae0064\n"         /* DQAP */
-               "   brc   6,0b\n"
-               : "+d" (reg0), "=d" (reg1), "+d" (reg2),
-               "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
-               "=m" (*(msgblock *) msg) : : "cc" );
-       *psmid = (((unsigned long long) reg6) << 32) + reg7;
-       return reg1;
-}
-
-int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
-{
-       struct ap_queue_status status;
-
-       if (msg == NULL)
-               return -EINVAL;
-       status = __ap_recv(qid, psmid, msg, length);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               return 0;
-       case AP_RESPONSE_NO_PENDING_REPLY:
-               if (status.queue_empty)
-                       return -ENOENT;
-               return -EBUSY;
-       case AP_RESPONSE_RESET_IN_PROGRESS:
-               return -EBUSY;
-       default:
-               return -ENODEV;
-       }
-}
-EXPORT_SYMBOL(ap_recv);
-
 /**
  * ap_query_queue(): Check if an AP queue is available.
  * @qid: The AP queue number
@@ -500,7 +268,7 @@ static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
        unsigned long info;
        int nd;
 
-       if (!ap_test_config_card_id(AP_QID_DEVICE(qid)))
+       if (!ap_test_config_card_id(AP_QID_CARD(qid)))
                return -ENODEV;
 
        status = ap_test_queue(qid, &info);
@@ -511,8 +279,28 @@ static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
                *facilities = (unsigned int)(info >> 32);
                /* Update maximum domain id */
                nd = (info >> 16) & 0xff;
+               /* if N bit is available, z13 and newer */
                if ((info & (1UL << 57)) && nd > 0)
                        ap_max_domain_id = nd;
+               else /* older machine types */
+                       ap_max_domain_id = 15;
+               switch (*device_type) {
+                       /* For CEX2 and CEX3 the available functions
+                        * are not refrected by the facilities bits.
+                        * Instead it is coded into the type. So here
+                        * modify the function bits based on the type.
+                        */
+               case AP_DEVICE_TYPE_CEX2A:
+               case AP_DEVICE_TYPE_CEX3A:
+                       *facilities |= 0x08000000;
+                       break;
+               case AP_DEVICE_TYPE_CEX2C:
+               case AP_DEVICE_TYPE_CEX3C:
+                       *facilities |= 0x10000000;
+                       break;
+               default:
+                       break;
+               }
                return 0;
        case AP_RESPONSE_Q_NOT_AVAIL:
        case AP_RESPONSE_DECONFIGURED:
@@ -528,9 +316,7 @@ static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
        }
 }
 
-/* State machine definitions and helpers */
-
-static void ap_sm_wait(enum ap_wait wait)
+void ap_wait(enum ap_wait wait)
 {
        ktime_t hr_time;
 
@@ -547,7 +333,7 @@ static void ap_sm_wait(enum ap_wait wait)
        case AP_WAIT_TIMEOUT:
                spin_lock_bh(&ap_poll_timer_lock);
                if (!hrtimer_is_queued(&ap_poll_timer)) {
-                       hr_time = ktime_set(0, poll_timeout);
+                       hr_time = poll_timeout;
                        hrtimer_forward_now(&ap_poll_timer, hr_time);
                        hrtimer_restart(&ap_poll_timer);
                }
@@ -559,350 +345,21 @@ static void ap_sm_wait(enum ap_wait wait)
        }
 }
 
-static enum ap_wait ap_sm_nop(struct ap_device *ap_dev)
-{
-       return AP_WAIT_NONE;
-}
-
-/**
- * ap_sm_recv(): Receive pending reply messages from an AP device but do
- *     not change the state of the device.
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
- */
-static struct ap_queue_status ap_sm_recv(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-       struct ap_message *ap_msg;
-
-       status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
-                          ap_dev->reply->message, ap_dev->reply->length);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               atomic_dec(&ap_poll_requests);
-               ap_dev->queue_count--;
-               if (ap_dev->queue_count > 0)
-                       mod_timer(&ap_dev->timeout,
-                                 jiffies + ap_dev->drv->request_timeout);
-               list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
-                       if (ap_msg->psmid != ap_dev->reply->psmid)
-                               continue;
-                       list_del_init(&ap_msg->list);
-                       ap_dev->pendingq_count--;
-                       ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
-                       break;
-               }
-       case AP_RESPONSE_NO_PENDING_REPLY:
-               if (!status.queue_empty || ap_dev->queue_count <= 0)
-                       break;
-               /* The card shouldn't forget requests but who knows. */
-               atomic_sub(ap_dev->queue_count, &ap_poll_requests);
-               ap_dev->queue_count = 0;
-               list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
-               ap_dev->requestq_count += ap_dev->pendingq_count;
-               ap_dev->pendingq_count = 0;
-               break;
-       default:
-               break;
-       }
-       return status;
-}
-
-/**
- * ap_sm_read(): Receive pending reply messages from an AP device.
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
- */
-static enum ap_wait ap_sm_read(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-
-       if (!ap_dev->reply)
-               return AP_WAIT_NONE;
-       status = ap_sm_recv(ap_dev);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               if (ap_dev->queue_count > 0) {
-                       ap_dev->state = AP_STATE_WORKING;
-                       return AP_WAIT_AGAIN;
-               }
-               ap_dev->state = AP_STATE_IDLE;
-               return AP_WAIT_NONE;
-       case AP_RESPONSE_NO_PENDING_REPLY:
-               if (ap_dev->queue_count > 0)
-                       return AP_WAIT_INTERRUPT;
-               ap_dev->state = AP_STATE_IDLE;
-               return AP_WAIT_NONE;
-       default:
-               ap_dev->state = AP_STATE_BORKED;
-               return AP_WAIT_NONE;
-       }
-}
-
-/**
- * ap_sm_suspend_read(): Receive pending reply messages from an AP device
- * without changing the device state in between. In suspend mode we don't
- * allow sending new requests, therefore just fetch pending replies.
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_WAIT_NONE or AP_WAIT_AGAIN
- */
-static enum ap_wait ap_sm_suspend_read(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-
-       if (!ap_dev->reply)
-               return AP_WAIT_NONE;
-       status = ap_sm_recv(ap_dev);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               if (ap_dev->queue_count > 0)
-                       return AP_WAIT_AGAIN;
-               /* fall through */
-       default:
-               return AP_WAIT_NONE;
-       }
-}
-
-/**
- * ap_sm_write(): Send messages from the request queue to an AP device.
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
- */
-static enum ap_wait ap_sm_write(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-       struct ap_message *ap_msg;
-
-       if (ap_dev->requestq_count <= 0)
-               return AP_WAIT_NONE;
-       /* Start the next request on the queue. */
-       ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
-       status = __ap_send(ap_dev->qid, ap_msg->psmid,
-                          ap_msg->message, ap_msg->length, ap_msg->special);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               atomic_inc(&ap_poll_requests);
-               ap_dev->queue_count++;
-               if (ap_dev->queue_count == 1)
-                       mod_timer(&ap_dev->timeout,
-                                 jiffies + ap_dev->drv->request_timeout);
-               list_move_tail(&ap_msg->list, &ap_dev->pendingq);
-               ap_dev->requestq_count--;
-               ap_dev->pendingq_count++;
-               if (ap_dev->queue_count < ap_dev->queue_depth) {
-                       ap_dev->state = AP_STATE_WORKING;
-                       return AP_WAIT_AGAIN;
-               }
-               /* fall through */
-       case AP_RESPONSE_Q_FULL:
-               ap_dev->state = AP_STATE_QUEUE_FULL;
-               return AP_WAIT_INTERRUPT;
-       case AP_RESPONSE_RESET_IN_PROGRESS:
-               ap_dev->state = AP_STATE_RESET_WAIT;
-               return AP_WAIT_TIMEOUT;
-       case AP_RESPONSE_MESSAGE_TOO_BIG:
-       case AP_RESPONSE_REQ_FAC_NOT_INST:
-               list_del_init(&ap_msg->list);
-               ap_dev->requestq_count--;
-               ap_msg->rc = -EINVAL;
-               ap_msg->receive(ap_dev, ap_msg, NULL);
-               return AP_WAIT_AGAIN;
-       default:
-               ap_dev->state = AP_STATE_BORKED;
-               return AP_WAIT_NONE;
-       }
-}
-
-/**
- * ap_sm_read_write(): Send and receive messages to/from an AP device.
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_WAIT_NONE, AP_WAIT_AGAIN, or AP_WAIT_INTERRUPT
- */
-static enum ap_wait ap_sm_read_write(struct ap_device *ap_dev)
-{
-       return min(ap_sm_read(ap_dev), ap_sm_write(ap_dev));
-}
-
-/**
- * ap_sm_reset(): Reset an AP queue.
- * @qid: The AP queue number
- *
- * Submit the Reset command to an AP queue.
- */
-static enum ap_wait ap_sm_reset(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-
-       status = ap_reset_queue(ap_dev->qid);
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-       case AP_RESPONSE_RESET_IN_PROGRESS:
-               ap_dev->state = AP_STATE_RESET_WAIT;
-               ap_dev->interrupt = AP_INTR_DISABLED;
-               return AP_WAIT_TIMEOUT;
-       case AP_RESPONSE_BUSY:
-               return AP_WAIT_TIMEOUT;
-       case AP_RESPONSE_Q_NOT_AVAIL:
-       case AP_RESPONSE_DECONFIGURED:
-       case AP_RESPONSE_CHECKSTOPPED:
-       default:
-               ap_dev->state = AP_STATE_BORKED;
-               return AP_WAIT_NONE;
-       }
-}
-
-/**
- * ap_sm_reset_wait(): Test queue for completion of the reset operation
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
- */
-static enum ap_wait ap_sm_reset_wait(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-       unsigned long info;
-
-       if (ap_dev->queue_count > 0 && ap_dev->reply)
-               /* Try to read a completed message and get the status */
-               status = ap_sm_recv(ap_dev);
-       else
-               /* Get the status with TAPQ */
-               status = ap_test_queue(ap_dev->qid, &info);
-
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               if (ap_using_interrupts() &&
-                   ap_queue_enable_interruption(ap_dev,
-                                                ap_airq.lsi_ptr) == 0)
-                       ap_dev->state = AP_STATE_SETIRQ_WAIT;
-               else
-                       ap_dev->state = (ap_dev->queue_count > 0) ?
-                               AP_STATE_WORKING : AP_STATE_IDLE;
-               return AP_WAIT_AGAIN;
-       case AP_RESPONSE_BUSY:
-       case AP_RESPONSE_RESET_IN_PROGRESS:
-               return AP_WAIT_TIMEOUT;
-       case AP_RESPONSE_Q_NOT_AVAIL:
-       case AP_RESPONSE_DECONFIGURED:
-       case AP_RESPONSE_CHECKSTOPPED:
-       default:
-               ap_dev->state = AP_STATE_BORKED;
-               return AP_WAIT_NONE;
-       }
-}
-
-/**
- * ap_sm_setirq_wait(): Test queue for completion of the irq enablement
- * @ap_dev: pointer to the AP device
- *
- * Returns AP_POLL_IMMEDIATELY, AP_POLL_AFTER_TIMEROUT or 0.
- */
-static enum ap_wait ap_sm_setirq_wait(struct ap_device *ap_dev)
-{
-       struct ap_queue_status status;
-       unsigned long info;
-
-       if (ap_dev->queue_count > 0 && ap_dev->reply)
-               /* Try to read a completed message and get the status */
-               status = ap_sm_recv(ap_dev);
-       else
-               /* Get the status with TAPQ */
-               status = ap_test_queue(ap_dev->qid, &info);
-
-       if (status.int_enabled == 1) {
-               /* Irqs are now enabled */
-               ap_dev->interrupt = AP_INTR_ENABLED;
-               ap_dev->state = (ap_dev->queue_count > 0) ?
-                       AP_STATE_WORKING : AP_STATE_IDLE;
-       }
-
-       switch (status.response_code) {
-       case AP_RESPONSE_NORMAL:
-               if (ap_dev->queue_count > 0)
-                       return AP_WAIT_AGAIN;
-               /* fallthrough */
-       case AP_RESPONSE_NO_PENDING_REPLY:
-               return AP_WAIT_TIMEOUT;
-       default:
-               ap_dev->state = AP_STATE_BORKED;
-               return AP_WAIT_NONE;
-       }
-}
-
-/*
- * AP state machine jump table
- */
-static ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
-       [AP_STATE_RESET_START] = {
-               [AP_EVENT_POLL] = ap_sm_reset,
-               [AP_EVENT_TIMEOUT] = ap_sm_nop,
-       },
-       [AP_STATE_RESET_WAIT] = {
-               [AP_EVENT_POLL] = ap_sm_reset_wait,
-               [AP_EVENT_TIMEOUT] = ap_sm_nop,
-       },
-       [AP_STATE_SETIRQ_WAIT] = {
-               [AP_EVENT_POLL] = ap_sm_setirq_wait,
-               [AP_EVENT_TIMEOUT] = ap_sm_nop,
-       },
-       [AP_STATE_IDLE] = {
-               [AP_EVENT_POLL] = ap_sm_write,
-               [AP_EVENT_TIMEOUT] = ap_sm_nop,
-       },
-       [AP_STATE_WORKING] = {
-               [AP_EVENT_POLL] = ap_sm_read_write,
-               [AP_EVENT_TIMEOUT] = ap_sm_reset,
-       },
-       [AP_STATE_QUEUE_FULL] = {
-               [AP_EVENT_POLL] = ap_sm_read,
-               [AP_EVENT_TIMEOUT] = ap_sm_reset,
-       },
-       [AP_STATE_SUSPEND_WAIT] = {
-               [AP_EVENT_POLL] = ap_sm_suspend_read,
-               [AP_EVENT_TIMEOUT] = ap_sm_nop,
-       },
-       [AP_STATE_BORKED] = {
-               [AP_EVENT_POLL] = ap_sm_nop,
-               [AP_EVENT_TIMEOUT] = ap_sm_nop,
-       },
-};
-
-static inline enum ap_wait ap_sm_event(struct ap_device *ap_dev,
-                                      enum ap_event event)
-{
-       return ap_jumptable[ap_dev->state][event](ap_dev);
-}
-
-static inline enum ap_wait ap_sm_event_loop(struct ap_device *ap_dev,
-                                           enum ap_event event)
-{
-       enum ap_wait wait;
-
-       while ((wait = ap_sm_event(ap_dev, event)) == AP_WAIT_AGAIN)
-               ;
-       return wait;
-}
-
 /**
  * ap_request_timeout(): Handling of request timeouts
  * @data: Holds the AP device.
  *
  * Handles request timeouts.
  */
-static void ap_request_timeout(unsigned long data)
+void ap_request_timeout(unsigned long data)
 {
-       struct ap_device *ap_dev = (struct ap_device *) data;
+       struct ap_queue *aq = (struct ap_queue *) data;
 
        if (ap_suspend_flag)
                return;
-       spin_lock_bh(&ap_dev->lock);
-       ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_TIMEOUT));
-       spin_unlock_bh(&ap_dev->lock);
+       spin_lock_bh(&aq->lock);
+       ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
+       spin_unlock_bh(&aq->lock);
 }
 
 /**
@@ -937,7 +394,8 @@ static void ap_interrupt_handler(struct airq_struct *airq)
  */
 static void ap_tasklet_fn(unsigned long dummy)
 {
-       struct ap_device *ap_dev;
+       struct ap_card *ac;
+       struct ap_queue *aq;
        enum ap_wait wait = AP_WAIT_NONE;
 
        /* Reset the indicator if interrupts are used. Thus new interrupts can
@@ -947,14 +405,35 @@ static void ap_tasklet_fn(unsigned long dummy)
        if (ap_using_interrupts())
                xchg(ap_airq.lsi_ptr, 0);
 
-       spin_lock(&ap_device_list_lock);
-       list_for_each_entry(ap_dev, &ap_device_list, list) {
-               spin_lock_bh(&ap_dev->lock);
-               wait = min(wait, ap_sm_event_loop(ap_dev, AP_EVENT_POLL));
-               spin_unlock_bh(&ap_dev->lock);
+       spin_lock_bh(&ap_list_lock);
+       for_each_ap_card(ac) {
+               for_each_ap_queue(aq, ac) {
+                       spin_lock_bh(&aq->lock);
+                       wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
+                       spin_unlock_bh(&aq->lock);
+               }
        }
-       spin_unlock(&ap_device_list_lock);
-       ap_sm_wait(wait);
+       spin_unlock_bh(&ap_list_lock);
+
+       ap_wait(wait);
+}
+
+static int ap_pending_requests(void)
+{
+       struct ap_card *ac;
+       struct ap_queue *aq;
+
+       spin_lock_bh(&ap_list_lock);
+       for_each_ap_card(ac) {
+               for_each_ap_queue(aq, ac) {
+                       if (aq->queue_count == 0)
+                               continue;
+                       spin_unlock_bh(&ap_list_lock);
+                       return 1;
+               }
+       }
+       spin_unlock_bh(&ap_list_lock);
+       return 0;
 }
 
 /**
@@ -976,8 +455,7 @@ static int ap_poll_thread(void *data)
        while (!kthread_should_stop()) {
                add_wait_queue(&ap_poll_wait, &wait);
                set_current_state(TASK_INTERRUPTIBLE);
-               if (ap_suspend_flag ||
-                   atomic_read(&ap_poll_requests) <= 0) {
+               if (ap_suspend_flag || !ap_pending_requests()) {
                        schedule();
                        try_to_freeze();
                }
@@ -989,7 +467,8 @@ static int ap_poll_thread(void *data)
                        continue;
                }
                ap_tasklet_fn(0);
-       } while (!kthread_should_stop());
+       }
+
        return 0;
 }
 
@@ -1018,207 +497,8 @@ static void ap_poll_thread_stop(void)
        mutex_unlock(&ap_poll_thread_mutex);
 }
 
-/**
- * ap_queue_message(): Queue a request to an AP device.
- * @ap_dev: The AP device to queue the message to
- * @ap_msg: The message that is to be added
- */
-void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
-{
-       /* For asynchronous message handling a valid receive-callback
-        * is required. */
-       BUG_ON(!ap_msg->receive);
-
-       spin_lock_bh(&ap_dev->lock);
-       /* Queue the message. */
-       list_add_tail(&ap_msg->list, &ap_dev->requestq);
-       ap_dev->requestq_count++;
-       ap_dev->total_request_count++;
-       /* Send/receive as many request from the queue as possible. */
-       ap_sm_wait(ap_sm_event_loop(ap_dev, AP_EVENT_POLL));
-       spin_unlock_bh(&ap_dev->lock);
-}
-EXPORT_SYMBOL(ap_queue_message);
-
-/**
- * ap_cancel_message(): Cancel a crypto request.
- * @ap_dev: The AP device that has the message queued
- * @ap_msg: The message that is to be removed
- *
- * Cancel a crypto request. This is done by removing the request
- * from the device pending or request queue. Note that the
- * request stays on the AP queue. When it finishes the message
- * reply will be discarded because the psmid can't be found.
- */
-void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
-{
-       struct ap_message *tmp;
-
-       spin_lock_bh(&ap_dev->lock);
-       if (!list_empty(&ap_msg->list)) {
-               list_for_each_entry(tmp, &ap_dev->pendingq, list)
-                       if (tmp->psmid == ap_msg->psmid) {
-                               ap_dev->pendingq_count--;
-                               goto found;
-                       }
-               ap_dev->requestq_count--;
-found:
-               list_del_init(&ap_msg->list);
-       }
-       spin_unlock_bh(&ap_dev->lock);
-}
-EXPORT_SYMBOL(ap_cancel_message);
-
-/*
- * AP device related attributes.
- */
-static ssize_t ap_hwtype_show(struct device *dev,
-                             struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
-}
-
-static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
-
-static ssize_t ap_raw_hwtype_show(struct device *dev,
-                             struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-
-       return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype);
-}
-
-static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL);
-
-static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
-                            char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
-}
-
-static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
-static ssize_t ap_request_count_show(struct device *dev,
-                                    struct device_attribute *attr,
-                                    char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       int rc;
-
-       spin_lock_bh(&ap_dev->lock);
-       rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
-       spin_unlock_bh(&ap_dev->lock);
-       return rc;
-}
-
-static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
-
-static ssize_t ap_requestq_count_show(struct device *dev,
-                                     struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       int rc;
-
-       spin_lock_bh(&ap_dev->lock);
-       rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
-       spin_unlock_bh(&ap_dev->lock);
-       return rc;
-}
-
-static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
-
-static ssize_t ap_pendingq_count_show(struct device *dev,
-                                     struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       int rc;
-
-       spin_lock_bh(&ap_dev->lock);
-       rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
-       spin_unlock_bh(&ap_dev->lock);
-       return rc;
-}
-
-static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
-
-static ssize_t ap_reset_show(struct device *dev,
-                                     struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       int rc = 0;
-
-       spin_lock_bh(&ap_dev->lock);
-       switch (ap_dev->state) {
-       case AP_STATE_RESET_START:
-       case AP_STATE_RESET_WAIT:
-               rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
-               break;
-       case AP_STATE_WORKING:
-       case AP_STATE_QUEUE_FULL:
-               rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
-               break;
-       default:
-               rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
-       }
-       spin_unlock_bh(&ap_dev->lock);
-       return rc;
-}
-
-static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
-
-static ssize_t ap_interrupt_show(struct device *dev,
-                                     struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       int rc = 0;
-
-       spin_lock_bh(&ap_dev->lock);
-       if (ap_dev->state == AP_STATE_SETIRQ_WAIT)
-               rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
-       else if (ap_dev->interrupt == AP_INTR_ENABLED)
-               rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
-       else
-               rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
-       spin_unlock_bh(&ap_dev->lock);
-       return rc;
-}
-
-static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
-
-static ssize_t ap_modalias_show(struct device *dev,
-                               struct device_attribute *attr, char *buf)
-{
-       return sprintf(buf, "ap:t%02X\n", to_ap_dev(dev)->device_type);
-}
-
-static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
-
-static ssize_t ap_functions_show(struct device *dev,
-                                struct device_attribute *attr, char *buf)
-{
-       struct ap_device *ap_dev = to_ap_dev(dev);
-       return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
-}
-
-static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
-
-static struct attribute *ap_dev_attrs[] = {
-       &dev_attr_hwtype.attr,
-       &dev_attr_raw_hwtype.attr,
-       &dev_attr_depth.attr,
-       &dev_attr_request_count.attr,
-       &dev_attr_requestq_count.attr,
-       &dev_attr_pendingq_count.attr,
-       &dev_attr_reset.attr,
-       &dev_attr_interrupt.attr,
-       &dev_attr_modalias.attr,
-       &dev_attr_ap_functions.attr,
-       NULL
-};
-static struct attribute_group ap_dev_attr_group = {
-       .attrs = ap_dev_attrs
-};
+#define is_card_dev(x) ((x)->parent == ap_root_device)
+#define is_queue_dev(x) ((x)->parent != ap_root_device)
 
 /**
  * ap_bus_match()
@@ -1229,7 +509,6 @@ static struct attribute_group ap_dev_attr_group = {
  */
 static int ap_bus_match(struct device *dev, struct device_driver *drv)
 {
-       struct ap_device *ap_dev = to_ap_dev(dev);
        struct ap_driver *ap_drv = to_ap_drv(drv);
        struct ap_device_id *id;
 
@@ -1238,10 +517,14 @@ static int ap_bus_match(struct device *dev, struct device_driver *drv)
         * supported types of the device_driver.
         */
        for (id = ap_drv->ids; id->match_flags; id++) {
-               if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
-                   (id->dev_type != ap_dev->device_type))
-                       continue;
-               return 1;
+               if (is_card_dev(dev) &&
+                   id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
+                   id->dev_type == to_ap_dev(dev)->device_type)
+                       return 1;
+               if (is_queue_dev(dev) &&
+                   id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
+                   id->dev_type == to_ap_dev(dev)->device_type)
+                       return 1;
        }
        return 0;
 }
@@ -1277,18 +560,24 @@ static int ap_dev_suspend(struct device *dev)
 {
        struct ap_device *ap_dev = to_ap_dev(dev);
 
-       /* Poll on the device until all requests are finished. */
-       spin_lock_bh(&ap_dev->lock);
-       ap_dev->state = AP_STATE_SUSPEND_WAIT;
-       while (ap_sm_event(ap_dev, AP_EVENT_POLL) != AP_WAIT_NONE)
-               ;
-       ap_dev->state = AP_STATE_BORKED;
-       spin_unlock_bh(&ap_dev->lock);
+       if (ap_dev->drv && ap_dev->drv->suspend)
+               ap_dev->drv->suspend(ap_dev);
+       return 0;
+}
+
+static int ap_dev_resume(struct device *dev)
+{
+       struct ap_device *ap_dev = to_ap_dev(dev);
+
+       if (ap_dev->drv && ap_dev->drv->resume)
+               ap_dev->drv->resume(ap_dev);
        return 0;
 }
 
 static void ap_bus_suspend(void)
 {
+       AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
+
        ap_suspend_flag = 1;
        /*
         * Disable scanning for devices, thus we do not want to scan
@@ -1298,9 +587,25 @@ static void ap_bus_suspend(void)
        tasklet_disable(&ap_tasklet);
 }
 
-static int __ap_devices_unregister(struct device *dev, void *dummy)
+static int __ap_card_devices_unregister(struct device *dev, void *dummy)
+{
+       if (is_card_dev(dev))
+               device_unregister(dev);
+       return 0;
+}
+
+static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
 {
-       device_unregister(dev);
+       if (is_queue_dev(dev))
+               device_unregister(dev);
+       return 0;
+}
+
+static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
+{
+       if (is_queue_dev(dev) &&
+           AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
+               device_unregister(dev);
        return 0;
 }
 
@@ -1308,8 +613,15 @@ static void ap_bus_resume(void)
 {
        int rc;
 
-       /* Unconditionally remove all AP devices */
-       bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
+       AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
+
+       /* remove all queue devices */
+       bus_for_each_dev(&ap_bus_type, NULL, NULL,
+                        __ap_queue_devices_unregister);
+       /* remove all card devices */
+       bus_for_each_dev(&ap_bus_type, NULL, NULL,
+                        __ap_card_devices_unregister);
+
        /* Reset thin interrupt setting */
        if (ap_interrupts_available() && !ap_using_interrupts()) {
                rc = register_adapter_interrupt(&ap_airq);
@@ -1351,7 +663,7 @@ static struct notifier_block ap_power_notifier = {
        .notifier_call = ap_power_event,
 };
 
-static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, NULL);
+static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
 
 static struct bus_type ap_bus_type = {
        .name = "ap",
@@ -1360,17 +672,6 @@ static struct bus_type ap_bus_type = {
        .pm = &ap_bus_pm_ops,
 };
 
-void ap_device_init_reply(struct ap_device *ap_dev,
-                         struct ap_message *reply)
-{
-       ap_dev->reply = reply;
-
-       spin_lock_bh(&ap_dev->lock);
-       ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_POLL));
-       spin_unlock_bh(&ap_dev->lock);
-}
-EXPORT_SYMBOL(ap_device_init_reply);
-
 static int ap_device_probe(struct device *dev)
 {
        struct ap_device *ap_dev = to_ap_dev(dev);
@@ -1384,61 +685,22 @@ static int ap_device_probe(struct device *dev)
        return rc;
 }
 
-/**
- * __ap_flush_queue(): Flush requests.
- * @ap_dev: Pointer to the AP device
- *
- * Flush all requests from the request/pending queue of an AP device.
- */
-static void __ap_flush_queue(struct ap_device *ap_dev)
-{
-       struct ap_message *ap_msg, *next;
-
-       list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
-               list_del_init(&ap_msg->list);
-               ap_dev->pendingq_count--;
-               ap_msg->rc = -EAGAIN;
-               ap_msg->receive(ap_dev, ap_msg, NULL);
-       }
-       list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
-               list_del_init(&ap_msg->list);
-               ap_dev->requestq_count--;
-               ap_msg->rc = -EAGAIN;
-               ap_msg->receive(ap_dev, ap_msg, NULL);
-       }
-}
-
-void ap_flush_queue(struct ap_device *ap_dev)
-{
-       spin_lock_bh(&ap_dev->lock);
-       __ap_flush_queue(ap_dev);
-       spin_unlock_bh(&ap_dev->lock);
-}
-EXPORT_SYMBOL(ap_flush_queue);
-
 static int ap_device_remove(struct device *dev)
 {
        struct ap_device *ap_dev = to_ap_dev(dev);
        struct ap_driver *ap_drv = ap_dev->drv;
 
-       ap_flush_queue(ap_dev);
-       del_timer_sync(&ap_dev->timeout);
-       spin_lock_bh(&ap_device_list_lock);
-       list_del_init(&ap_dev->list);
-       spin_unlock_bh(&ap_device_list_lock);
+       spin_lock_bh(&ap_list_lock);
+       if (is_card_dev(dev))
+               list_del_init(&to_ap_card(dev)->list);
+       else
+               list_del_init(&to_ap_queue(dev)->list);
+       spin_unlock_bh(&ap_list_lock);
        if (ap_drv->remove)
                ap_drv->remove(ap_dev);
-       spin_lock_bh(&ap_dev->lock);
-       atomic_sub(ap_dev->queue_count, &ap_poll_requests);
-       spin_unlock_bh(&ap_dev->lock);
        return 0;
 }
 
-static void ap_device_release(struct device *dev)
-{
-       kfree(to_ap_dev(dev));
-}
-
 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
                       char *name)
 {
@@ -1481,18 +743,30 @@ static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
        return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
 }
 
-static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
+static ssize_t ap_domain_store(struct bus_type *bus,
+                              const char *buf, size_t count)
+{
+       int domain;
+
+       if (sscanf(buf, "%i\n", &domain) != 1 ||
+           domain < 0 || domain > ap_max_domain_id)
+               return -EINVAL;
+       spin_lock_bh(&ap_domain_lock);
+       ap_domain_index = domain;
+       spin_unlock_bh(&ap_domain_lock);
+
+       AP_DBF(DBF_DEBUG, "store new default domain=%d\n", domain);
+
+       return count;
+}
+
+static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
 
 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
 {
        if (!ap_configuration)  /* QCI not supported */
                return snprintf(buf, PAGE_SIZE, "not supported\n");
-       if (!test_facility(76))
-               /* format 0 - 16 bit domain field */
-               return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
-                               ap_configuration->adm[0],
-                               ap_configuration->adm[1]);
-       /* format 1 - 256 bit domain field */
+
        return snprintf(buf, PAGE_SIZE,
                        "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
                        ap_configuration->adm[0], ap_configuration->adm[1],
@@ -1504,6 +778,22 @@ static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
 static BUS_ATTR(ap_control_domain_mask, 0444,
                ap_control_domain_mask_show, NULL);
 
+static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
+{
+       if (!ap_configuration)  /* QCI not supported */
+               return snprintf(buf, PAGE_SIZE, "not supported\n");
+
+       return snprintf(buf, PAGE_SIZE,
+                       "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
+                       ap_configuration->aqm[0], ap_configuration->aqm[1],
+                       ap_configuration->aqm[2], ap_configuration->aqm[3],
+                       ap_configuration->aqm[4], ap_configuration->aqm[5],
+                       ap_configuration->aqm[6], ap_configuration->aqm[7]);
+}
+
+static BUS_ATTR(ap_usage_domain_mask, 0444,
+               ap_usage_domain_mask_show, NULL);
+
 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
 {
        return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
@@ -1570,7 +860,7 @@ static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
            time > 120000000000ULL)
                return -EINVAL;
        poll_timeout = time;
-       hr_time = ktime_set(0, poll_timeout);
+       hr_time = poll_timeout;
 
        spin_lock_bh(&ap_poll_timer_lock);
        hrtimer_cancel(&ap_poll_timer);
@@ -1599,6 +889,7 @@ static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
 static struct bus_attribute *const ap_bus_attrs[] = {
        &bus_attr_ap_domain,
        &bus_attr_ap_control_domain_mask,
+       &bus_attr_ap_usage_domain_mask,
        &bus_attr_config_time,
        &bus_attr_poll_thread,
        &bus_attr_ap_interrupts,
@@ -1623,9 +914,12 @@ static int ap_select_domain(void)
         * the "domain=" parameter or the domain with the maximum number
         * of devices.
         */
-       if (ap_domain_index >= 0)
+       spin_lock_bh(&ap_domain_lock);
+       if (ap_domain_index >= 0) {
                /* Domain has already been selected. */
+               spin_unlock_bh(&ap_domain_lock);
                return 0;
+       }
        best_domain = -1;
        max_count = 0;
        for (i = 0; i < AP_DOMAINS; i++) {
@@ -1647,109 +941,171 @@ static int ap_select_domain(void)
        }
        if (best_domain >= 0){
                ap_domain_index = best_domain;
+               spin_unlock_bh(&ap_domain_lock);
                return 0;
        }
+       spin_unlock_bh(&ap_domain_lock);
        return -ENODEV;
 }
 
-/**
- * __ap_scan_bus(): Scan the AP bus.
- * @dev: Pointer to device
- * @data: Pointer to data
- *
- * Scan the AP bus for new devices.
+/*
+ * helper function to be used with bus_find_dev
+ * matches for the card device with the given id
  */
-static int __ap_scan_bus(struct device *dev, void *data)
+static int __match_card_device_with_id(struct device *dev, void *data)
 {
-       return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
+       return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
 }
 
+/* helper function to be used with bus_find_dev
+ * matches for the queue device with a given qid
+ */
+static int __match_queue_device_with_qid(struct device *dev, void *data)
+{
+       return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
+}
+
+/**
+ * ap_scan_bus(): Scan the AP bus for new devices
+ * Runs periodically, workqueue timer (ap_config_time)
+ */
 static void ap_scan_bus(struct work_struct *unused)
 {
-       struct ap_device *ap_dev;
+       struct ap_queue *aq;
+       struct ap_card *ac;
        struct device *dev;
        ap_qid_t qid;
-       int queue_depth = 0, device_type = 0;
-       unsigned int device_functions = 0;
-       int rc, i, borked;
+       int depth = 0, type = 0;
+       unsigned int functions = 0;
+       int rc, id, dom, borked, domains;
+
+       AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
 
        ap_query_configuration();
        if (ap_select_domain() != 0)
                goto out;
 
-       for (i = 0; i < AP_DEVICES; i++) {
-               qid = AP_MKQID(i, ap_domain_index);
+       for (id = 0; id < AP_DEVICES; id++) {
+               /* check if device is registered */
                dev = bus_find_device(&ap_bus_type, NULL,
-                                     (void *)(unsigned long)qid,
-                                     __ap_scan_bus);
-               rc = ap_query_queue(qid, &queue_depth, &device_type,
-                                   &device_functions);
-               if (dev) {
-                       ap_dev = to_ap_dev(dev);
-                       spin_lock_bh(&ap_dev->lock);
-                       if (rc == -ENODEV)
-                               ap_dev->state = AP_STATE_BORKED;
-                       borked = ap_dev->state == AP_STATE_BORKED;
-                       spin_unlock_bh(&ap_dev->lock);
-                       if (borked)     /* Remove broken device */
+                                     (void *)(long) id,
+                                     __match_card_device_with_id);
+               ac = dev ? to_ap_card(dev) : NULL;
+               if (!ap_test_config_card_id(id)) {
+                       if (dev) {
+                               /* Card device has been removed from
+                                * configuration, remove the belonging
+                                * queue devices.
+                                */
+                               bus_for_each_dev(&ap_bus_type, NULL,
+                                       (void *)(long) id,
+                                       __ap_queue_devices_with_id_unregister);
+                               /* now remove the card device */
                                device_unregister(dev);
-                       put_device(dev);
-                       if (!borked)
-                               continue;
-               }
-               if (rc)
-                       continue;
-               ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
-               if (!ap_dev)
-                       break;
-               ap_dev->qid = qid;
-               ap_dev->state = AP_STATE_RESET_START;
-               ap_dev->interrupt = AP_INTR_DISABLED;
-               ap_dev->queue_depth = queue_depth;
-               ap_dev->raw_hwtype = device_type;
-               ap_dev->device_type = device_type;
-               ap_dev->functions = device_functions;
-               spin_lock_init(&ap_dev->lock);
-               INIT_LIST_HEAD(&ap_dev->pendingq);
-               INIT_LIST_HEAD(&ap_dev->requestq);
-               INIT_LIST_HEAD(&ap_dev->list);
-               setup_timer(&ap_dev->timeout, ap_request_timeout,
-                           (unsigned long) ap_dev);
-
-               ap_dev->device.bus = &ap_bus_type;
-               ap_dev->device.parent = ap_root_device;
-               rc = dev_set_name(&ap_dev->device, "card%02x",
-                                 AP_QID_DEVICE(ap_dev->qid));
-               if (rc) {
-                       kfree(ap_dev);
-                       continue;
-               }
-               /* Add to list of devices */
-               spin_lock_bh(&ap_device_list_lock);
-               list_add(&ap_dev->list, &ap_device_list);
-               spin_unlock_bh(&ap_device_list_lock);
-               /* Start with a device reset */
-               spin_lock_bh(&ap_dev->lock);
-               ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_POLL));
-               spin_unlock_bh(&ap_dev->lock);
-               /* Register device */
-               ap_dev->device.release = ap_device_release;
-               rc = device_register(&ap_dev->device);
-               if (rc) {
-                       spin_lock_bh(&ap_dev->lock);
-                       list_del_init(&ap_dev->list);
-                       spin_unlock_bh(&ap_dev->lock);
-                       put_device(&ap_dev->device);
+                               put_device(dev);
+                       }
                        continue;
                }
-               /* Add device attributes. */
-               rc = sysfs_create_group(&ap_dev->device.kobj,
-                                       &ap_dev_attr_group);
-               if (rc) {
-                       device_unregister(&ap_dev->device);
-                       continue;
+               /* According to the configuration there should be a card
+                * device, so check if there is at least one valid queue
+                * and maybe create queue devices and the card device.
+                */
+               domains = 0;
+               for (dom = 0; dom < AP_DOMAINS; dom++) {
+                       qid = AP_MKQID(id, dom);
+                       dev = bus_find_device(&ap_bus_type, NULL,
+                                             (void *)(long) qid,
+                                             __match_queue_device_with_qid);
+                       aq = dev ? to_ap_queue(dev) : NULL;
+                       if (!ap_test_config_domain(dom)) {
+                               if (dev) {
+                                       /* Queue device exists but has been
+                                        * removed from configuration.
+                                        */
+                                       device_unregister(dev);
+                                       put_device(dev);
+                               }
+                               continue;
+                       }
+                       rc = ap_query_queue(qid, &depth, &type, &functions);
+                       if (dev) {
+                               spin_lock_bh(&aq->lock);
+                               if (rc == -ENODEV ||
+                                   /* adapter reconfiguration */
+                                   (ac && ac->functions != functions))
+                                       aq->state = AP_STATE_BORKED;
+                               borked = aq->state == AP_STATE_BORKED;
+                               spin_unlock_bh(&aq->lock);
+                               if (borked)     /* Remove broken device */
+                                       device_unregister(dev);
+                               put_device(dev);
+                               if (!borked) {
+                                       domains++;
+                                       continue;
+                               }
+                       }
+                       if (rc)
+                               continue;
+                       /* new queue device needed */
+                       if (!ac) {
+                               /* but first create the card device */
+                               ac = ap_card_create(id, depth,
+                                                   type, functions);
+                               if (!ac)
+                                       continue;
+                               ac->ap_dev.device.bus = &ap_bus_type;
+                               ac->ap_dev.device.parent = ap_root_device;
+                               dev_set_name(&ac->ap_dev.device,
+                                            "card%02x", id);
+                               /* Register card with AP bus */
+                               rc = device_register(&ac->ap_dev.device);
+                               if (rc) {
+                                       put_device(&ac->ap_dev.device);
+                                       ac = NULL;
+                                       break;
+                               }
+                               /* get it and thus adjust reference counter */
+                               get_device(&ac->ap_dev.device);
+                               /* Add card device to card list */
+                               spin_lock_bh(&ap_list_lock);
+                               list_add(&ac->list, &ap_card_list);
+                               spin_unlock_bh(&ap_list_lock);
+                       }
+                       /* now create the new queue device */
+                       aq = ap_queue_create(qid, type);
+                       if (!aq)
+                               continue;
+                       aq->card = ac;
+                       aq->ap_dev.device.bus = &ap_bus_type;
+                       aq->ap_dev.device.parent = &ac->ap_dev.device;
+                       dev_set_name(&aq->ap_dev.device,
+                                    "%02x.%04x", id, dom);
+                       /* Add queue device to card queue list */
+                       spin_lock_bh(&ap_list_lock);
+                       list_add(&aq->list, &ac->queues);
+                       spin_unlock_bh(&ap_list_lock);
+                       /* Start with a device reset */
+                       spin_lock_bh(&aq->lock);
+                       ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
+                       spin_unlock_bh(&aq->lock);
+                       /* Register device */
+                       rc = device_register(&aq->ap_dev.device);
+                       if (rc) {
+                               spin_lock_bh(&ap_list_lock);
+                               list_del_init(&aq->list);
+                               spin_unlock_bh(&ap_list_lock);
+                               put_device(&aq->ap_dev.device);
+                               continue;
+                       }
+                       domains++;
+               } /* end domain loop */
+               if (ac) {
+                       /* remove card dev if there are no queue devices */
+                       if (!domains)
+                               device_unregister(&ac->ap_dev.device);
+                       put_device(&ac->ap_dev.device);
                }
-       }
+       } /* end device loop */
 out:
        mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
 }
@@ -1768,7 +1124,7 @@ static void ap_reset_domain(void)
        if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
                return;
        for (i = 0; i < AP_DEVICES; i++)
-               ap_reset_queue(AP_MKQID(i, ap_domain_index));
+               ap_rapq(AP_MKQID(i, ap_domain_index));
 }
 
 static void ap_reset_all(void)
@@ -1781,7 +1137,7 @@ static void ap_reset_all(void)
                for (j = 0; j < AP_DEVICES; j++) {
                        if (!ap_test_config_card_id(j))
                                continue;
-                       ap_reset_queue(AP_MKQID(j, i));
+                       ap_rapq(AP_MKQID(j, i));
                }
        }
 }
@@ -1790,6 +1146,23 @@ static struct reset_call ap_reset_call = {
        .fn = ap_reset_all,
 };
 
+int __init ap_debug_init(void)
+{
+       ap_dbf_root = debugfs_create_dir("ap", NULL);
+       ap_dbf_info = debug_register("ap", 1, 1,
+                                    DBF_MAX_SPRINTF_ARGS * sizeof(long));
+       debug_register_view(ap_dbf_info, &debug_sprintf_view);
+       debug_set_level(ap_dbf_info, DBF_ERR);
+
+       return 0;
+}
+
+void ap_debug_exit(void)
+{
+       debugfs_remove(ap_dbf_root);
+       debug_unregister(ap_dbf_info);
+}
+
 /**
  * ap_module_init(): The module initialization code.
  *
@@ -1800,6 +1173,10 @@ int __init ap_module_init(void)
        int max_domain_id;
        int rc, i;
 
+       rc = ap_debug_init();
+       if (rc)
+               return rc;
+
        if (ap_instructions_available() != 0) {
                pr_warn("The hardware system does not support AP instructions\n");
                return -ENODEV;
@@ -1909,7 +1286,15 @@ void ap_module_exit(void)
        del_timer_sync(&ap_config_timer);
        hrtimer_cancel(&ap_poll_timer);
        tasklet_kill(&ap_tasklet);
-       bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
+
+       /* first remove queue devices */
+       bus_for_each_dev(&ap_bus_type, NULL, NULL,
+                        __ap_queue_devices_unregister);
+       /* now remove the card devices */
+       bus_for_each_dev(&ap_bus_type, NULL, NULL,
+                        __ap_card_devices_unregister);
+
+       /* remove bus attributes */
        for (i = 0; ap_bus_attrs[i]; i++)
                bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
        unregister_pm_notifier(&ap_power_notifier);
@@ -1919,6 +1304,8 @@ void ap_module_exit(void)
        unregister_reset_call(&ap_reset_call);
        if (ap_using_interrupts())
                unregister_adapter_interrupt(&ap_airq);
+
+       ap_debug_exit();
 }
 
 module_init(ap_module_init);