ap_bus.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright IBM Corp. 2006, 2019
  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. extern int ap_domain_index;
  26. extern DECLARE_HASHTABLE(ap_queues, 8);
  27. extern spinlock_t ap_queues_lock;
  28. static inline int ap_test_bit(unsigned int *ptr, unsigned int nr)
  29. {
  30. return (*ptr & (0x80000000u >> nr)) != 0;
  31. }
  32. #define AP_RESPONSE_NORMAL 0x00
  33. #define AP_RESPONSE_Q_NOT_AVAIL 0x01
  34. #define AP_RESPONSE_RESET_IN_PROGRESS 0x02
  35. #define AP_RESPONSE_DECONFIGURED 0x03
  36. #define AP_RESPONSE_CHECKSTOPPED 0x04
  37. #define AP_RESPONSE_BUSY 0x05
  38. #define AP_RESPONSE_INVALID_ADDRESS 0x06
  39. #define AP_RESPONSE_OTHERWISE_CHANGED 0x07
  40. #define AP_RESPONSE_Q_FULL 0x10
  41. #define AP_RESPONSE_NO_PENDING_REPLY 0x10
  42. #define AP_RESPONSE_INDEX_TOO_BIG 0x11
  43. #define AP_RESPONSE_NO_FIRST_PART 0x13
  44. #define AP_RESPONSE_MESSAGE_TOO_BIG 0x15
  45. #define AP_RESPONSE_REQ_FAC_NOT_INST 0x16
  46. #define AP_RESPONSE_INVALID_DOMAIN 0x42
  47. /*
  48. * Known device types
  49. */
  50. #define AP_DEVICE_TYPE_PCICC 3
  51. #define AP_DEVICE_TYPE_PCICA 4
  52. #define AP_DEVICE_TYPE_PCIXCC 5
  53. #define AP_DEVICE_TYPE_CEX2A 6
  54. #define AP_DEVICE_TYPE_CEX2C 7
  55. #define AP_DEVICE_TYPE_CEX3A 8
  56. #define AP_DEVICE_TYPE_CEX3C 9
  57. #define AP_DEVICE_TYPE_CEX4 10
  58. #define AP_DEVICE_TYPE_CEX5 11
  59. #define AP_DEVICE_TYPE_CEX6 12
  60. #define AP_DEVICE_TYPE_CEX7 13
  61. /*
  62. * Known function facilities
  63. */
  64. #define AP_FUNC_MEX4K 1
  65. #define AP_FUNC_CRT4K 2
  66. #define AP_FUNC_COPRO 3
  67. #define AP_FUNC_ACCEL 4
  68. #define AP_FUNC_EP11 5
  69. #define AP_FUNC_APXA 6
  70. /*
  71. * AP queue state machine states
  72. */
  73. enum ap_sm_state {
  74. AP_SM_STATE_RESET_START = 0,
  75. AP_SM_STATE_RESET_WAIT,
  76. AP_SM_STATE_SETIRQ_WAIT,
  77. AP_SM_STATE_IDLE,
  78. AP_SM_STATE_WORKING,
  79. AP_SM_STATE_QUEUE_FULL,
  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_TIMEOUT, /* wait for timeout */
  96. AP_SM_WAIT_INTERRUPT, /* wait for thin interrupt (if available) */
  97. AP_SM_WAIT_NONE, /* no wait */
  98. NR_AP_SM_WAIT
  99. };
  100. /*
  101. * AP queue device states
  102. */
  103. enum ap_dev_state {
  104. AP_DEV_STATE_UNINITIATED = 0, /* fresh and virgin, not touched */
  105. AP_DEV_STATE_OPERATING, /* queue dev is working normal */
  106. AP_DEV_STATE_SHUTDOWN, /* remove/unbind/shutdown in progress */
  107. AP_DEV_STATE_ERROR, /* device is in error state */
  108. NR_AP_DEV_STATES
  109. };
  110. struct ap_device;
  111. struct ap_message;
  112. /*
  113. * The ap driver struct includes a flags field which holds some info for
  114. * the ap bus about the driver. Currently only one flag is supported and
  115. * used: The DEFAULT flag marks an ap driver as a default driver which is
  116. * used together with the apmask and aqmask whitelisting of the ap bus.
  117. */
  118. #define AP_DRIVER_FLAG_DEFAULT 0x0001
  119. struct ap_driver {
  120. struct device_driver driver;
  121. struct ap_device_id *ids;
  122. unsigned int flags;
  123. int (*probe)(struct ap_device *);
  124. void (*remove)(struct ap_device *);
  125. };
  126. #define to_ap_drv(x) container_of((x), struct ap_driver, driver)
  127. int ap_driver_register(struct ap_driver *, struct module *, char *);
  128. void ap_driver_unregister(struct ap_driver *);
  129. struct ap_device {
  130. struct device device;
  131. struct ap_driver *drv; /* Pointer to AP device driver. */
  132. int device_type; /* AP device type. */
  133. };
  134. #define to_ap_dev(x) container_of((x), struct ap_device, device)
  135. struct ap_card {
  136. struct ap_device ap_dev;
  137. void *private; /* ap driver private pointer. */
  138. int raw_hwtype; /* AP raw hardware type. */
  139. unsigned int functions; /* AP device function bitfield. */
  140. int queue_depth; /* AP queue depth.*/
  141. int id; /* AP card number. */
  142. bool config; /* configured state */
  143. atomic64_t total_request_count; /* # requests ever for this AP device.*/
  144. };
  145. #define to_ap_card(x) container_of((x), struct ap_card, ap_dev.device)
  146. struct ap_queue {
  147. struct ap_device ap_dev;
  148. struct hlist_node hnode; /* Node for the ap_queues hashtable */
  149. struct ap_card *card; /* Ptr to assoc. AP card. */
  150. spinlock_t lock; /* Per device lock. */
  151. void *private; /* ap driver private pointer. */
  152. enum ap_dev_state dev_state; /* queue device state */
  153. bool config; /* configured state */
  154. ap_qid_t qid; /* AP queue id. */
  155. bool interrupt; /* indicate if interrupts are enabled */
  156. int queue_count; /* # messages currently on AP queue. */
  157. int pendingq_count; /* # requests on pendingq list. */
  158. int requestq_count; /* # requests on requestq list. */
  159. u64 total_request_count; /* # requests ever for this AP device.*/
  160. int request_timeout; /* Request timeout in jiffies. */
  161. struct timer_list timeout; /* Timer for request timeouts. */
  162. struct list_head pendingq; /* List of message sent to AP queue. */
  163. struct list_head requestq; /* List of message yet to be sent. */
  164. struct ap_message *reply; /* Per device reply message. */
  165. enum ap_sm_state sm_state; /* ap queue state machine state */
  166. int last_err_rc; /* last error state response code */
  167. };
  168. #define to_ap_queue(x) container_of((x), struct ap_queue, ap_dev.device)
  169. typedef enum ap_sm_wait (ap_func_t)(struct ap_queue *queue);
  170. /* failure injection cmd struct */
  171. struct ap_fi {
  172. union {
  173. u16 cmd; /* fi flags + action */
  174. struct {
  175. u8 flags; /* fi flags only */
  176. u8 action; /* fi action only */
  177. };
  178. };
  179. };
  180. /* all currently known fi actions */
  181. enum ap_fi_actions {
  182. AP_FI_ACTION_CCA_AGENT_FF = 0x01,
  183. AP_FI_ACTION_CCA_DOM_INVAL = 0x02,
  184. AP_FI_ACTION_NQAP_QID_INVAL = 0x03,
  185. };
  186. /* all currently known fi flags */
  187. enum ap_fi_flags {
  188. AP_FI_FLAG_NO_RETRY = 0x01,
  189. AP_FI_FLAG_TOGGLE_SPECIAL = 0x02,
  190. };
  191. struct ap_message {
  192. struct list_head list; /* Request queueing. */
  193. unsigned long long psmid; /* Message id. */
  194. void *msg; /* Pointer to message buffer. */
  195. unsigned int len; /* Message length. */
  196. u16 flags; /* Flags, see AP_MSG_FLAG_xxx */
  197. struct ap_fi fi; /* Failure Injection cmd */
  198. int rc; /* Return code for this message */
  199. void *private; /* ap driver private pointer. */
  200. /* receive is called from tasklet context */
  201. void (*receive)(struct ap_queue *, struct ap_message *,
  202. struct ap_message *);
  203. };
  204. #define AP_MSG_FLAG_SPECIAL 1 /* flag msg as 'special' with NQAP */
  205. /**
  206. * ap_init_message() - Initialize ap_message.
  207. * Initialize a message before using. Otherwise this might result in
  208. * unexpected behaviour.
  209. */
  210. static inline void ap_init_message(struct ap_message *ap_msg)
  211. {
  212. memset(ap_msg, 0, sizeof(*ap_msg));
  213. }
  214. /**
  215. * ap_release_message() - Release ap_message.
  216. * Releases all memory used internal within the ap_message struct
  217. * Currently this is the message and private field.
  218. */
  219. static inline void ap_release_message(struct ap_message *ap_msg)
  220. {
  221. kfree_sensitive(ap_msg->msg);
  222. kfree_sensitive(ap_msg->private);
  223. }
  224. /*
  225. * Note: don't use ap_send/ap_recv after using ap_queue_message
  226. * for the first time. Otherwise the ap message queue will get
  227. * confused.
  228. */
  229. int ap_send(ap_qid_t, unsigned long long, void *, size_t);
  230. int ap_recv(ap_qid_t, unsigned long long *, void *, size_t);
  231. enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
  232. enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);
  233. int ap_queue_message(struct ap_queue *aq, struct ap_message *ap_msg);
  234. void ap_cancel_message(struct ap_queue *aq, struct ap_message *ap_msg);
  235. void ap_flush_queue(struct ap_queue *aq);
  236. void *ap_airq_ptr(void);
  237. void ap_wait(enum ap_sm_wait wait);
  238. void ap_request_timeout(struct timer_list *t);
  239. void ap_bus_force_rescan(void);
  240. int ap_test_config_usage_domain(unsigned int domain);
  241. int ap_test_config_ctrl_domain(unsigned int domain);
  242. void ap_queue_init_reply(struct ap_queue *aq, struct ap_message *ap_msg);
  243. struct ap_queue *ap_queue_create(ap_qid_t qid, int device_type);
  244. void ap_queue_prepare_remove(struct ap_queue *aq);
  245. void ap_queue_remove(struct ap_queue *aq);
  246. void ap_queue_init_state(struct ap_queue *aq);
  247. struct ap_card *ap_card_create(int id, int queue_depth, int raw_device_type,
  248. int comp_device_type, unsigned int functions);
  249. struct ap_perms {
  250. unsigned long ioctlm[BITS_TO_LONGS(AP_IOCTLS)];
  251. unsigned long apm[BITS_TO_LONGS(AP_DEVICES)];
  252. unsigned long aqm[BITS_TO_LONGS(AP_DOMAINS)];
  253. };
  254. extern struct ap_perms ap_perms;
  255. extern struct mutex ap_perms_mutex;
  256. /*
  257. * Get ap_queue device for this qid.
  258. * Returns ptr to the struct ap_queue device or NULL if there
  259. * was no ap_queue device with this qid found. When something is
  260. * found, the reference count of the embedded device is increased.
  261. * So the caller has to decrease the reference count after use
  262. * with a call to put_device(&aq->ap_dev.device).
  263. */
  264. struct ap_queue *ap_get_qdev(ap_qid_t qid);
  265. /*
  266. * check APQN for owned/reserved by ap bus and default driver(s).
  267. * Checks if this APQN is or will be in use by the ap bus
  268. * and the default set of drivers.
  269. * If yes, returns 1, if not returns 0. On error a negative
  270. * errno value is returned.
  271. */
  272. int ap_owned_by_def_drv(int card, int queue);
  273. /*
  274. * check 'matrix' of APQNs for owned/reserved by ap bus and
  275. * default driver(s).
  276. * Checks if there is at least one APQN in the given 'matrix'
  277. * marked as owned/reserved by the ap bus and default driver(s).
  278. * If such an APQN is found the return value is 1, otherwise
  279. * 0 is returned. On error a negative errno value is returned.
  280. * The parameter apm is a bitmask which should be declared
  281. * as DECLARE_BITMAP(apm, AP_DEVICES), the aqm parameter is
  282. * similar, should be declared as DECLARE_BITMAP(aqm, AP_DOMAINS).
  283. */
  284. int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
  285. unsigned long *aqm);
  286. /*
  287. * ap_parse_mask_str() - helper function to parse a bitmap string
  288. * and clear/set the bits in the bitmap accordingly. The string may be
  289. * given as absolute value, a hex string like 0x1F2E3D4C5B6A" simple
  290. * overwriting the current content of the bitmap. Or as relative string
  291. * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
  292. * bits are cleared or set. Distinction is done based on the very
  293. * first character which may be '+' or '-' for the relative string
  294. * and othewise assume to be an absolute value string. If parsing fails
  295. * a negative errno value is returned. All arguments and bitmaps are
  296. * big endian order.
  297. */
  298. int ap_parse_mask_str(const char *str,
  299. unsigned long *bitmap, int bits,
  300. struct mutex *lock);
  301. #endif /* _AP_BUS_H_ */