ap_bus.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright IBM Corp. 2006, 2023
  4. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  5. * Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  7. * Felix Beck <felix.beck@de.ibm.com>
  8. * Holger Dengler <hd@linux.vnet.ibm.com>
  9. *
  10. * Adjunct processor bus header file.
  11. */
  12. #ifndef _AP_BUS_H_
  13. #define _AP_BUS_H_
  14. #include <linux/device.h>
  15. #include <linux/types.h>
  16. #include <linux/hashtable.h>
  17. #include <asm/isc.h>
  18. #include <asm/ap.h>
  19. #define AP_DEVICES 256 /* Number of AP devices. */
  20. #define AP_DOMAINS 256 /* Number of AP domains. */
  21. #define AP_IOCTLS 256 /* Number of ioctls. */
  22. #define AP_RESET_TIMEOUT (HZ*0.7) /* Time in ticks for reset timeouts. */
  23. #define AP_CONFIG_TIME 30 /* Time in seconds between AP bus rescans. */
  24. #define AP_POLL_TIME 1 /* Time in ticks between receive polls. */
  25. #define AP_DEFAULT_MAX_MSG_SIZE (12 * 1024)
  26. #define AP_TAPQ_ML_FIELD_CHUNK_SIZE (4096)
  27. extern int ap_domain_index;
  28. extern atomic_t ap_max_msg_size;
  29. extern DECLARE_HASHTABLE(ap_queues, 8);
  30. extern spinlock_t ap_queues_lock;
  31. static inline int ap_test_bit(unsigned int *ptr, unsigned int nr)
  32. {
  33. return (*ptr & (0x80000000u >> nr)) != 0;
  34. }
  35. #define AP_RESPONSE_NORMAL 0x00
  36. #define AP_RESPONSE_Q_NOT_AVAIL 0x01
  37. #define AP_RESPONSE_RESET_IN_PROGRESS 0x02
  38. #define AP_RESPONSE_DECONFIGURED 0x03
  39. #define AP_RESPONSE_CHECKSTOPPED 0x04
  40. #define AP_RESPONSE_BUSY 0x05
  41. #define AP_RESPONSE_INVALID_ADDRESS 0x06
  42. #define AP_RESPONSE_OTHERWISE_CHANGED 0x07
  43. #define AP_RESPONSE_INVALID_GISA 0x08
  44. #define AP_RESPONSE_Q_BOUND_TO_ANOTHER 0x09
  45. #define AP_RESPONSE_STATE_CHANGE_IN_PROGRESS 0x0A
  46. #define AP_RESPONSE_Q_NOT_BOUND 0x0B
  47. #define AP_RESPONSE_Q_FULL 0x10
  48. #define AP_RESPONSE_NO_PENDING_REPLY 0x10
  49. #define AP_RESPONSE_INDEX_TOO_BIG 0x11
  50. #define AP_RESPONSE_NO_FIRST_PART 0x13
  51. #define AP_RESPONSE_MESSAGE_TOO_BIG 0x15
  52. #define AP_RESPONSE_REQ_FAC_NOT_INST 0x16
  53. #define AP_RESPONSE_Q_BIND_ERROR 0x30
  54. #define AP_RESPONSE_Q_NOT_AVAIL_FOR_ASSOC 0x31
  55. #define AP_RESPONSE_Q_NOT_EMPTY 0x32
  56. #define AP_RESPONSE_BIND_LIMIT_EXCEEDED 0x33
  57. #define AP_RESPONSE_INVALID_ASSOC_SECRET 0x34
  58. #define AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE 0x35
  59. #define AP_RESPONSE_ASSOC_FAILED 0x36
  60. #define AP_RESPONSE_INVALID_DOMAIN 0x42
  61. /*
  62. * Supported AP device types
  63. */
  64. #define AP_DEVICE_TYPE_CEX4 10
  65. #define AP_DEVICE_TYPE_CEX5 11
  66. #define AP_DEVICE_TYPE_CEX6 12
  67. #define AP_DEVICE_TYPE_CEX7 13
  68. #define AP_DEVICE_TYPE_CEX8 14
  69. /*
  70. * AP queue state machine states
  71. */
  72. enum ap_sm_state {
  73. AP_SM_STATE_RESET_START = 0,
  74. AP_SM_STATE_RESET_WAIT,
  75. AP_SM_STATE_SETIRQ_WAIT,
  76. AP_SM_STATE_IDLE,
  77. AP_SM_STATE_WORKING,
  78. AP_SM_STATE_QUEUE_FULL,
  79. AP_SM_STATE_ASSOC_WAIT,
  80. NR_AP_SM_STATES
  81. };
  82. /*
  83. * AP queue state machine events
  84. */
  85. enum ap_sm_event {
  86. AP_SM_EVENT_POLL,
  87. AP_SM_EVENT_TIMEOUT,
  88. NR_AP_SM_EVENTS
  89. };
  90. /*
  91. * AP queue state wait behaviour
  92. */
  93. enum ap_sm_wait {
  94. AP_SM_WAIT_AGAIN = 0, /* retry immediately */
  95. AP_SM_WAIT_HIGH_TIMEOUT, /* poll high freq, wait for timeout */
  96. AP_SM_WAIT_LOW_TIMEOUT, /* poll low freq, wait for timeout */
  97. AP_SM_WAIT_INTERRUPT, /* wait for thin interrupt (if available) */
  98. AP_SM_WAIT_NONE, /* no wait */
  99. NR_AP_SM_WAIT
  100. };
  101. /*
  102. * AP queue device states
  103. */
  104. enum ap_dev_state {
  105. AP_DEV_STATE_UNINITIATED = 0, /* fresh and virgin, not touched */
  106. AP_DEV_STATE_OPERATING, /* queue dev is working normal */
  107. AP_DEV_STATE_SHUTDOWN, /* remove/unbind/shutdown in progress */
  108. AP_DEV_STATE_ERROR, /* device is in error state */
  109. NR_AP_DEV_STATES
  110. };
  111. struct ap_device;
  112. struct ap_message;
  113. /*
  114. * The ap driver struct includes a flags field which holds some info for
  115. * the ap bus about the driver. Currently only one flag is supported and
  116. * used: The DEFAULT flag marks an ap driver as a default driver which is
  117. * used together with the apmask and aqmask whitelisting of the ap bus.
  118. */
  119. #define AP_DRIVER_FLAG_DEFAULT 0x0001
  120. struct ap_driver {
  121. struct device_driver driver;
  122. struct ap_device_id *ids;
  123. unsigned int flags;
  124. int (*probe)(struct ap_device *);
  125. void (*remove)(struct ap_device *);
  126. int (*in_use)(unsigned long *apm, unsigned long *aqm);
  127. /*
  128. * Called at the start of the ap bus scan function when
  129. * the crypto config information (qci) has changed.
  130. * This callback is not invoked if there is no AP
  131. * QCI support available.
  132. */
  133. void (*on_config_changed)(struct ap_config_info *new_config_info,
  134. struct ap_config_info *old_config_info);
  135. /*
  136. * Called at the end of the ap bus scan function when
  137. * the crypto config information (qci) has changed.
  138. * This callback is not invoked if there is no AP
  139. * QCI support available.
  140. */
  141. void (*on_scan_complete)(struct ap_config_info *new_config_info,
  142. struct ap_config_info *old_config_info);
  143. };
  144. #define to_ap_drv(x) container_of_const((x), struct ap_driver, driver)
  145. int ap_driver_register(struct ap_driver *, struct module *, char *);
  146. void ap_driver_unregister(struct ap_driver *);
  147. struct ap_device {
  148. struct device device;
  149. int device_type; /* AP device type. */
  150. const char *driver_override;
  151. };
  152. #define to_ap_dev(x) container_of((x), struct ap_device, device)
  153. struct ap_card {
  154. struct ap_device ap_dev;
  155. struct ap_tapq_hwinfo hwinfo; /* TAPQ GR2 content */
  156. int id; /* AP card number. */
  157. unsigned int maxmsgsize; /* AP msg limit for this card */
  158. bool config; /* configured state */
  159. bool chkstop; /* checkstop state */
  160. atomic64_t total_request_count; /* # requests ever for this AP device.*/
  161. };
  162. #define TAPQ_CARD_HWINFO_MASK 0xFFFF0000FFFF0F0FUL
  163. #define ASSOC_IDX_INVALID 0x10000
  164. #define to_ap_card(x) container_of((x), struct ap_card, ap_dev.device)
  165. struct ap_queue {
  166. struct ap_device ap_dev;
  167. struct hlist_node hnode; /* Node for the ap_queues hashtable */
  168. struct ap_card *card; /* Ptr to assoc. AP card. */
  169. spinlock_t lock; /* Per device lock. */
  170. enum ap_dev_state dev_state; /* queue device state */
  171. bool config; /* configured state */
  172. bool chkstop; /* checkstop state */
  173. ap_qid_t qid; /* AP queue id. */
  174. unsigned int se_bstate; /* SE bind state (BS) */
  175. unsigned int assoc_idx; /* SE association index */
  176. int queue_count; /* # messages currently on AP queue. */
  177. int pendingq_count; /* # requests on pendingq list. */
  178. int requestq_count; /* # requests on requestq list. */
  179. u64 total_request_count; /* # requests ever for this AP device.*/
  180. int request_timeout; /* Request timeout in jiffies. */
  181. struct timer_list timeout; /* Timer for request timeouts. */
  182. struct list_head pendingq; /* List of message sent to AP queue. */
  183. struct list_head requestq; /* List of message yet to be sent. */
  184. struct ap_message *reply; /* Per device reply message. */
  185. enum ap_sm_state sm_state; /* ap queue state machine state */
  186. int rapq_fbit; /* fbit arg for next rapq invocation */
  187. int last_err_rc; /* last error state response code */
  188. };
  189. #define to_ap_queue(x) container_of((x), struct ap_queue, ap_dev.device)
  190. typedef enum ap_sm_wait (ap_func_t)(struct ap_queue *queue);
  191. struct ap_response_type {
  192. struct completion work;
  193. int type;
  194. };
  195. struct ap_message {
  196. struct list_head list; /* Request queueing. */
  197. unsigned long psmid; /* Message id. */
  198. void *msg; /* Pointer to message buffer. */
  199. size_t len; /* actual msg len in msg buffer */
  200. size_t bufsize; /* allocated msg buffer size */
  201. u16 flags; /* Flags, see AP_MSG_FLAG_xxx */
  202. int rc; /* Return code for this message */
  203. struct ap_response_type response;
  204. /* receive is called from tasklet context */
  205. void (*receive)(struct ap_queue *, struct ap_message *,
  206. struct ap_message *);
  207. };
  208. #define AP_MSG_FLAG_SPECIAL 0x0001 /* flag msg as 'special' with NQAP */
  209. #define AP_MSG_FLAG_USAGE 0x0002 /* CCA, EP11: usage (no admin) msg */
  210. #define AP_MSG_FLAG_ADMIN 0x0004 /* CCA, EP11: admin (=control) msg */
  211. #define AP_MSG_FLAG_MEMPOOL 0x0008 /* ap msg buffer allocated via mempool */
  212. int ap_init_apmsg(struct ap_message *ap_msg, u32 flags);
  213. void ap_release_apmsg(struct ap_message *ap_msg);
  214. enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
  215. enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);
  216. int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg);
  217. void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg);
  218. void ap_flush_queue(struct ap_queue *aq);
  219. bool ap_queue_usable(struct ap_queue *aq);
  220. void *ap_airq_ptr(void);
  221. int ap_sb_available(void);
  222. bool ap_is_se_guest(void);
  223. void ap_wait(enum ap_sm_wait wait);
  224. void ap_request_timeout(struct timer_list *t);
  225. bool ap_bus_force_rescan(void);
  226. int ap_test_config_usage_domain(unsigned int domain);
  227. int ap_test_config_ctrl_domain(unsigned int domain);
  228. void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *ap_msg);
  229. struct ap_queue *ap_queue_create(ap_qid_t qid, struct ap_card *ac);
  230. void ap_queue_prepare_remove(struct ap_queue *aq);
  231. void ap_queue_remove(struct ap_queue *aq);
  232. void ap_queue_init_state(struct ap_queue *aq);
  233. void _ap_queue_init_state(struct ap_queue *aq);
  234. struct ap_card *ap_card_create(int id, struct ap_tapq_hwinfo info,
  235. int comp_type);
  236. #define APMASKSIZE (BITS_TO_LONGS(AP_DEVICES) * sizeof(unsigned long))
  237. #define AQMASKSIZE (BITS_TO_LONGS(AP_DOMAINS) * sizeof(unsigned long))
  238. struct ap_perms {
  239. unsigned long ioctlm[BITS_TO_LONGS(AP_IOCTLS)];
  240. unsigned long apm[BITS_TO_LONGS(AP_DEVICES)];
  241. unsigned long aqm[BITS_TO_LONGS(AP_DOMAINS)];
  242. unsigned long adm[BITS_TO_LONGS(AP_DOMAINS)];
  243. };
  244. extern struct ap_perms ap_perms;
  245. extern bool ap_apmask_aqmask_in_use;
  246. extern int ap_driver_override_ctr;
  247. extern struct mutex ap_attr_mutex;
  248. /*
  249. * Get ap_queue device for this qid.
  250. * Returns ptr to the struct ap_queue device or NULL if there
  251. * was no ap_queue device with this qid found. When something is
  252. * found, the reference count of the embedded device is increased.
  253. * So the caller has to decrease the reference count after use
  254. * with a call to put_device(&aq->ap_dev.device).
  255. */
  256. struct ap_queue *ap_get_qdev(ap_qid_t qid);
  257. /*
  258. * check APQN for owned/reserved by ap bus and default driver(s).
  259. * Checks if this APQN is or will be in use by the ap bus
  260. * and the default set of drivers.
  261. * If yes, returns 1, if not returns 0. On error a negative
  262. * errno value is returned.
  263. */
  264. int ap_owned_by_def_drv(int card, int queue);
  265. /*
  266. * check 'matrix' of APQNs for owned/reserved by ap bus and
  267. * default driver(s).
  268. * Checks if there is at least one APQN in the given 'matrix'
  269. * marked as owned/reserved by the ap bus and default driver(s).
  270. * If such an APQN is found the return value is 1, otherwise
  271. * 0 is returned. On error a negative errno value is returned.
  272. * The parameter apm is a bitmask which should be declared
  273. * as DECLARE_BITMAP(apm, AP_DEVICES), the aqm parameter is
  274. * similar, should be declared as DECLARE_BITMAP(aqm, AP_DOMAINS).
  275. */
  276. int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
  277. unsigned long *aqm);
  278. /*
  279. * ap_parse_mask_str() - helper function to parse a bitmap string
  280. * and clear/set the bits in the bitmap accordingly. The string may be
  281. * given as absolute value, a hex string like 0x1F2E3D4C5B6A" simple
  282. * overwriting the current content of the bitmap. Or as relative string
  283. * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
  284. * bits are cleared or set. Distinction is done based on the very
  285. * first character which may be '+' or '-' for the relative string
  286. * and otherwise assume to be an absolute value string. If parsing fails
  287. * a negative errno value is returned. All arguments and bitmaps are
  288. * big endian order.
  289. */
  290. int ap_parse_mask_str(const char *str,
  291. unsigned long *bitmap, int bits,
  292. struct mutex *lock);
  293. /*
  294. * ap_hex2bitmap() - Convert a string containing a hexadecimal number (str)
  295. * into a bitmap (bitmap) with bits set that correspond to the bits represented
  296. * by the hex string. Input and output data is in big endian order.
  297. *
  298. * str - Input hex string of format "0x1234abcd". The leading "0x" is optional.
  299. * At least one digit is required. Must be large enough to hold the number of
  300. * bits represented by the bits parameter.
  301. *
  302. * bitmap - Pointer to a bitmap. Upon successful completion of this function,
  303. * this bitmap will have bits set to match the value of str. If bitmap is longer
  304. * than str, then the rightmost bits of bitmap are padded with zeros. Must be
  305. * large enough to hold the number of bits represented by the bits parameter.
  306. *
  307. * bits - Length, in bits, of the bitmap represented by str. Must be a multiple
  308. * of 8.
  309. *
  310. * Returns: 0 On success
  311. * -EINVAL If str format is invalid or bits is not a multiple of 8.
  312. */
  313. int ap_hex2bitmap(const char *str, unsigned long *bitmap, int bits);
  314. /*
  315. * Interface to wait for the AP bus to have done one initial ap bus
  316. * scan and all detected APQNs have been bound to device drivers.
  317. * If these both conditions are not fulfilled, this function blocks
  318. * on a condition with wait_for_completion_killable_timeout().
  319. * If these both conditions are fulfilled (before the timeout hits)
  320. * the return value is 0. If the timeout (in jiffies) hits instead
  321. * -ETIME is returned. On failures negative return values are
  322. * returned to the caller.
  323. * It may be that the AP bus scan finds new devices. Then the
  324. * condition that all APQNs are bound to their device drivers
  325. * is reset to false and this call again blocks until either all
  326. * APQNs are bound to a device driver or the timeout hits again.
  327. */
  328. int ap_wait_apqn_bindings_complete(unsigned long timeout);
  329. void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg);
  330. void ap_send_online_uevent(struct ap_device *ap_dev, int online);
  331. #endif /* _AP_BUS_H_ */