Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 /*
0003  * Copyright IBM Corp. 2006, 2019
0004  * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
0005  *        Martin Schwidefsky <schwidefsky@de.ibm.com>
0006  *        Ralph Wuerthner <rwuerthn@de.ibm.com>
0007  *        Felix Beck <felix.beck@de.ibm.com>
0008  *        Holger Dengler <hd@linux.vnet.ibm.com>
0009  *
0010  * Adjunct processor bus header file.
0011  */
0012 
0013 #ifndef _AP_BUS_H_
0014 #define _AP_BUS_H_
0015 
0016 #include <linux/device.h>
0017 #include <linux/types.h>
0018 #include <linux/hashtable.h>
0019 #include <asm/isc.h>
0020 #include <asm/ap.h>
0021 
0022 #define AP_DEVICES 256      /* Number of AP devices. */
0023 #define AP_DOMAINS 256      /* Number of AP domains. */
0024 #define AP_IOCTLS  256      /* Number of ioctls. */
0025 #define AP_RESET_TIMEOUT (HZ*0.7)   /* Time in ticks for reset timeouts. */
0026 #define AP_CONFIG_TIME 30   /* Time in seconds between AP bus rescans. */
0027 #define AP_POLL_TIME 1      /* Time in ticks between receive polls. */
0028 #define AP_DEFAULT_MAX_MSG_SIZE (12 * 1024)
0029 #define AP_TAPQ_ML_FIELD_CHUNK_SIZE (4096)
0030 
0031 extern int ap_domain_index;
0032 extern atomic_t ap_max_msg_size;
0033 
0034 extern DECLARE_HASHTABLE(ap_queues, 8);
0035 extern spinlock_t ap_queues_lock;
0036 
0037 static inline int ap_test_bit(unsigned int *ptr, unsigned int nr)
0038 {
0039     return (*ptr & (0x80000000u >> nr)) != 0;
0040 }
0041 
0042 #define AP_RESPONSE_NORMAL      0x00
0043 #define AP_RESPONSE_Q_NOT_AVAIL     0x01
0044 #define AP_RESPONSE_RESET_IN_PROGRESS   0x02
0045 #define AP_RESPONSE_DECONFIGURED    0x03
0046 #define AP_RESPONSE_CHECKSTOPPED    0x04
0047 #define AP_RESPONSE_BUSY        0x05
0048 #define AP_RESPONSE_INVALID_ADDRESS 0x06
0049 #define AP_RESPONSE_OTHERWISE_CHANGED   0x07
0050 #define AP_RESPONSE_INVALID_GISA    0x08
0051 #define AP_RESPONSE_Q_FULL      0x10
0052 #define AP_RESPONSE_NO_PENDING_REPLY    0x10
0053 #define AP_RESPONSE_INDEX_TOO_BIG   0x11
0054 #define AP_RESPONSE_NO_FIRST_PART   0x13
0055 #define AP_RESPONSE_MESSAGE_TOO_BIG 0x15
0056 #define AP_RESPONSE_REQ_FAC_NOT_INST    0x16
0057 #define AP_RESPONSE_INVALID_DOMAIN  0x42
0058 
0059 /*
0060  * Known device types
0061  */
0062 #define AP_DEVICE_TYPE_PCICC    3
0063 #define AP_DEVICE_TYPE_PCICA    4
0064 #define AP_DEVICE_TYPE_PCIXCC   5
0065 #define AP_DEVICE_TYPE_CEX2A    6
0066 #define AP_DEVICE_TYPE_CEX2C    7
0067 #define AP_DEVICE_TYPE_CEX3A    8
0068 #define AP_DEVICE_TYPE_CEX3C    9
0069 #define AP_DEVICE_TYPE_CEX4 10
0070 #define AP_DEVICE_TYPE_CEX5 11
0071 #define AP_DEVICE_TYPE_CEX6 12
0072 #define AP_DEVICE_TYPE_CEX7 13
0073 #define AP_DEVICE_TYPE_CEX8 14
0074 
0075 /*
0076  * Known function facilities
0077  */
0078 #define AP_FUNC_MEX4K 1
0079 #define AP_FUNC_CRT4K 2
0080 #define AP_FUNC_COPRO 3
0081 #define AP_FUNC_ACCEL 4
0082 #define AP_FUNC_EP11  5
0083 #define AP_FUNC_APXA  6
0084 
0085 /*
0086  * AP queue state machine states
0087  */
0088 enum ap_sm_state {
0089     AP_SM_STATE_RESET_START = 0,
0090     AP_SM_STATE_RESET_WAIT,
0091     AP_SM_STATE_SETIRQ_WAIT,
0092     AP_SM_STATE_IDLE,
0093     AP_SM_STATE_WORKING,
0094     AP_SM_STATE_QUEUE_FULL,
0095     NR_AP_SM_STATES
0096 };
0097 
0098 /*
0099  * AP queue state machine events
0100  */
0101 enum ap_sm_event {
0102     AP_SM_EVENT_POLL,
0103     AP_SM_EVENT_TIMEOUT,
0104     NR_AP_SM_EVENTS
0105 };
0106 
0107 /*
0108  * AP queue state wait behaviour
0109  */
0110 enum ap_sm_wait {
0111     AP_SM_WAIT_AGAIN = 0,   /* retry immediately */
0112     AP_SM_WAIT_TIMEOUT, /* wait for timeout */
0113     AP_SM_WAIT_INTERRUPT,   /* wait for thin interrupt (if available) */
0114     AP_SM_WAIT_NONE,    /* no wait */
0115     NR_AP_SM_WAIT
0116 };
0117 
0118 /*
0119  * AP queue device states
0120  */
0121 enum ap_dev_state {
0122     AP_DEV_STATE_UNINITIATED = 0,   /* fresh and virgin, not touched */
0123     AP_DEV_STATE_OPERATING,     /* queue dev is working normal */
0124     AP_DEV_STATE_SHUTDOWN,      /* remove/unbind/shutdown in progress */
0125     AP_DEV_STATE_ERROR,     /* device is in error state */
0126     NR_AP_DEV_STATES
0127 };
0128 
0129 struct ap_device;
0130 struct ap_message;
0131 
0132 /*
0133  * The ap driver struct includes a flags field which holds some info for
0134  * the ap bus about the driver. Currently only one flag is supported and
0135  * used: The DEFAULT flag marks an ap driver as a default driver which is
0136  * used together with the apmask and aqmask whitelisting of the ap bus.
0137  */
0138 #define AP_DRIVER_FLAG_DEFAULT 0x0001
0139 
0140 struct ap_driver {
0141     struct device_driver driver;
0142     struct ap_device_id *ids;
0143     unsigned int flags;
0144 
0145     int (*probe)(struct ap_device *);
0146     void (*remove)(struct ap_device *);
0147     int (*in_use)(unsigned long *apm, unsigned long *aqm);
0148     /*
0149      * Called at the start of the ap bus scan function when
0150      * the crypto config information (qci) has changed.
0151      * This callback is not invoked if there is no AP
0152      * QCI support available.
0153      */
0154     void (*on_config_changed)(struct ap_config_info *new_config_info,
0155                   struct ap_config_info *old_config_info);
0156     /*
0157      * Called at the end of the ap bus scan function when
0158      * the crypto config information (qci) has changed.
0159      * This callback is not invoked if there is no AP
0160      * QCI support available.
0161      */
0162     void (*on_scan_complete)(struct ap_config_info *new_config_info,
0163                  struct ap_config_info *old_config_info);
0164 };
0165 
0166 #define to_ap_drv(x) container_of((x), struct ap_driver, driver)
0167 
0168 int ap_driver_register(struct ap_driver *, struct module *, char *);
0169 void ap_driver_unregister(struct ap_driver *);
0170 
0171 struct ap_device {
0172     struct device device;
0173     int device_type;        /* AP device type. */
0174 };
0175 
0176 #define to_ap_dev(x) container_of((x), struct ap_device, device)
0177 
0178 struct ap_card {
0179     struct ap_device ap_dev;
0180     int raw_hwtype;         /* AP raw hardware type. */
0181     unsigned int functions;     /* AP device function bitfield. */
0182     int queue_depth;        /* AP queue depth.*/
0183     int id;             /* AP card number. */
0184     unsigned int maxmsgsize;    /* AP msg limit for this card */
0185     bool config;            /* configured state */
0186     bool chkstop;           /* checkstop state */
0187     atomic64_t total_request_count; /* # requests ever for this AP device.*/
0188 };
0189 
0190 #define to_ap_card(x) container_of((x), struct ap_card, ap_dev.device)
0191 
0192 struct ap_queue {
0193     struct ap_device ap_dev;
0194     struct hlist_node hnode;    /* Node for the ap_queues hashtable */
0195     struct ap_card *card;       /* Ptr to assoc. AP card. */
0196     spinlock_t lock;        /* Per device lock. */
0197     enum ap_dev_state dev_state;    /* queue device state */
0198     bool config;            /* configured state */
0199     bool chkstop;           /* checkstop state */
0200     ap_qid_t qid;           /* AP queue id. */
0201     bool interrupt;         /* indicate if interrupts are enabled */
0202     int queue_count;        /* # messages currently on AP queue. */
0203     int pendingq_count;     /* # requests on pendingq list. */
0204     int requestq_count;     /* # requests on requestq list. */
0205     u64 total_request_count;    /* # requests ever for this AP device.*/
0206     int request_timeout;        /* Request timeout in jiffies. */
0207     struct timer_list timeout;  /* Timer for request timeouts. */
0208     struct list_head pendingq;  /* List of message sent to AP queue. */
0209     struct list_head requestq;  /* List of message yet to be sent. */
0210     struct ap_message *reply;   /* Per device reply message. */
0211     enum ap_sm_state sm_state;  /* ap queue state machine state */
0212     int last_err_rc;        /* last error state response code */
0213 };
0214 
0215 #define to_ap_queue(x) container_of((x), struct ap_queue, ap_dev.device)
0216 
0217 typedef enum ap_sm_wait (ap_func_t)(struct ap_queue *queue);
0218 
0219 /* failure injection cmd struct */
0220 struct ap_fi {
0221     union {
0222         u16 cmd;        /* fi flags + action */
0223         struct {
0224             u8 flags;   /* fi flags only */
0225             u8 action;  /* fi action only */
0226         };
0227     };
0228 };
0229 
0230 /* all currently known fi actions */
0231 enum ap_fi_actions {
0232     AP_FI_ACTION_CCA_AGENT_FF   = 0x01,
0233     AP_FI_ACTION_CCA_DOM_INVAL  = 0x02,
0234     AP_FI_ACTION_NQAP_QID_INVAL = 0x03,
0235 };
0236 
0237 /* all currently known fi flags */
0238 enum ap_fi_flags {
0239     AP_FI_FLAG_NO_RETRY   = 0x01,
0240     AP_FI_FLAG_TOGGLE_SPECIAL = 0x02,
0241 };
0242 
0243 struct ap_message {
0244     struct list_head list;      /* Request queueing. */
0245     unsigned long long psmid;   /* Message id. */
0246     void *msg;          /* Pointer to message buffer. */
0247     unsigned int len;       /* actual msg len in msg buffer */
0248     unsigned int bufsize;       /* allocated msg buffer size */
0249     u16 flags;          /* Flags, see AP_MSG_FLAG_xxx */
0250     struct ap_fi fi;        /* Failure Injection cmd */
0251     int rc;             /* Return code for this message */
0252     void *private;          /* ap driver private pointer. */
0253     /* receive is called from tasklet context */
0254     void (*receive)(struct ap_queue *, struct ap_message *,
0255             struct ap_message *);
0256 };
0257 
0258 #define AP_MSG_FLAG_SPECIAL  0x0001 /* flag msg as 'special' with NQAP */
0259 #define AP_MSG_FLAG_USAGE    0x0002 /* CCA, EP11: usage (no admin) msg */
0260 #define AP_MSG_FLAG_ADMIN    0x0004 /* CCA, EP11: admin (=control) msg */
0261 
0262 /**
0263  * ap_init_message() - Initialize ap_message.
0264  * Initialize a message before using. Otherwise this might result in
0265  * unexpected behaviour.
0266  */
0267 static inline void ap_init_message(struct ap_message *ap_msg)
0268 {
0269     memset(ap_msg, 0, sizeof(*ap_msg));
0270 }
0271 
0272 /**
0273  * ap_release_message() - Release ap_message.
0274  * Releases all memory used internal within the ap_message struct
0275  * Currently this is the message and private field.
0276  */
0277 static inline void ap_release_message(struct ap_message *ap_msg)
0278 {
0279     kfree_sensitive(ap_msg->msg);
0280     kfree_sensitive(ap_msg->private);
0281 }
0282 
0283 /*
0284  * Note: don't use ap_send/ap_recv after using ap_queue_message
0285  * for the first time. Otherwise the ap message queue will get
0286  * confused.
0287  */
0288 int ap_send(ap_qid_t, unsigned long long, void *, size_t);
0289 int ap_recv(ap_qid_t, unsigned long long *, void *, size_t);
0290 
0291 enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
0292 enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);
0293 
0294 int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg);
0295 void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg);
0296 void ap_flush_queue(struct ap_queue *aq);
0297 
0298 void *ap_airq_ptr(void);
0299 void ap_wait(enum ap_sm_wait wait);
0300 void ap_request_timeout(struct timer_list *t);
0301 void ap_bus_force_rescan(void);
0302 
0303 int ap_test_config_usage_domain(unsigned int domain);
0304 int ap_test_config_ctrl_domain(unsigned int domain);
0305 
0306 void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *ap_msg);
0307 struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type);
0308 void ap_queue_prepare_remove(struct ap_queue *aq);
0309 void ap_queue_remove(struct ap_queue *aq);
0310 void ap_queue_init_state(struct ap_queue *aq);
0311 
0312 struct ap_card *ap_card_create(int id, int queue_depth, int raw_type,
0313                    int comp_type, unsigned int functions, int ml);
0314 
0315 #define APMASKSIZE (BITS_TO_LONGS(AP_DEVICES) * sizeof(unsigned long))
0316 #define AQMASKSIZE (BITS_TO_LONGS(AP_DOMAINS) * sizeof(unsigned long))
0317 
0318 struct ap_perms {
0319     unsigned long ioctlm[BITS_TO_LONGS(AP_IOCTLS)];
0320     unsigned long apm[BITS_TO_LONGS(AP_DEVICES)];
0321     unsigned long aqm[BITS_TO_LONGS(AP_DOMAINS)];
0322     unsigned long adm[BITS_TO_LONGS(AP_DOMAINS)];
0323 };
0324 
0325 extern struct ap_perms ap_perms;
0326 extern struct mutex ap_perms_mutex;
0327 
0328 /*
0329  * Get ap_queue device for this qid.
0330  * Returns ptr to the struct ap_queue device or NULL if there
0331  * was no ap_queue device with this qid found. When something is
0332  * found, the reference count of the embedded device is increased.
0333  * So the caller has to decrease the reference count after use
0334  * with a call to put_device(&aq->ap_dev.device).
0335  */
0336 struct ap_queue *ap_get_qdev(ap_qid_t qid);
0337 
0338 /*
0339  * check APQN for owned/reserved by ap bus and default driver(s).
0340  * Checks if this APQN is or will be in use by the ap bus
0341  * and the default set of drivers.
0342  * If yes, returns 1, if not returns 0. On error a negative
0343  * errno value is returned.
0344  */
0345 int ap_owned_by_def_drv(int card, int queue);
0346 
0347 /*
0348  * check 'matrix' of APQNs for owned/reserved by ap bus and
0349  * default driver(s).
0350  * Checks if there is at least one APQN in the given 'matrix'
0351  * marked as owned/reserved by the ap bus and default driver(s).
0352  * If such an APQN is found the return value is 1, otherwise
0353  * 0 is returned. On error a negative errno value is returned.
0354  * The parameter apm is a bitmask which should be declared
0355  * as DECLARE_BITMAP(apm, AP_DEVICES), the aqm parameter is
0356  * similar, should be declared as DECLARE_BITMAP(aqm, AP_DOMAINS).
0357  */
0358 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
0359                        unsigned long *aqm);
0360 
0361 /*
0362  * ap_parse_mask_str() - helper function to parse a bitmap string
0363  * and clear/set the bits in the bitmap accordingly. The string may be
0364  * given as absolute value, a hex string like 0x1F2E3D4C5B6A" simple
0365  * overwriting the current content of the bitmap. Or as relative string
0366  * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
0367  * bits are cleared or set. Distinction is done based on the very
0368  * first character which may be '+' or '-' for the relative string
0369  * and othewise assume to be an absolute value string. If parsing fails
0370  * a negative errno value is returned. All arguments and bitmaps are
0371  * big endian order.
0372  */
0373 int ap_parse_mask_str(const char *str,
0374               unsigned long *bitmap, int bits,
0375               struct mutex *lock);
0376 
0377 /*
0378  * Interface to wait for the AP bus to have done one initial ap bus
0379  * scan and all detected APQNs have been bound to device drivers.
0380  * If these both conditions are not fulfilled, this function blocks
0381  * on a condition with wait_for_completion_killable_timeout().
0382  * If these both conditions are fulfilled (before the timeout hits)
0383  * the return value is 0. If the timeout (in jiffies) hits instead
0384  * -ETIME is returned. On failures negative return values are
0385  * returned to the caller.
0386  */
0387 int ap_wait_init_apqn_bindings_complete(unsigned long timeout);
0388 
0389 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg);
0390 void ap_send_online_uevent(struct ap_device *ap_dev, int online);
0391 
0392 #endif /* _AP_BUS_H_ */