2
0

css.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Channel subsystem structures and definitions.
  3. *
  4. * Copyright 2012 IBM Corp.
  5. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or (at
  8. * your option) any later version. See the COPYING file in the top-level
  9. * directory.
  10. */
  11. #ifndef CSS_H
  12. #define CSS_H
  13. #include "hw/s390x/adapter.h"
  14. #include "hw/s390x/s390_flic.h"
  15. #include "hw/s390x/ioinst.h"
  16. #include "system/kvm.h"
  17. #include "target/s390x/cpu-qom.h"
  18. /* Channel subsystem constants. */
  19. #define MAX_DEVNO 65535
  20. #define MAX_SCHID 65535
  21. #define MAX_SSID 3
  22. #define MAX_CSSID 255
  23. #define MAX_CHPID 255
  24. #define MAX_ISC 7
  25. #define MAX_CIWS 62
  26. #define VIRTUAL_CSSID 0xfe
  27. #define VIRTIO_CCW_CHPID 0 /* used by convention */
  28. typedef struct CIW {
  29. uint8_t type;
  30. uint8_t command;
  31. uint16_t count;
  32. } QEMU_PACKED CIW;
  33. typedef struct SenseId {
  34. /* common part */
  35. uint8_t reserved; /* always 0x'FF' */
  36. uint16_t cu_type; /* control unit type */
  37. uint8_t cu_model; /* control unit model */
  38. uint16_t dev_type; /* device type */
  39. uint8_t dev_model; /* device model */
  40. uint8_t unused; /* padding byte */
  41. /* extended part */
  42. CIW ciw[MAX_CIWS]; /* variable # of CIWs */
  43. } SenseId; /* Note: No QEMU_PACKED due to unaligned members */
  44. /* Channel measurements, from linux/drivers/s390/cio/cmf.c. */
  45. typedef struct CMB {
  46. uint16_t ssch_rsch_count;
  47. uint16_t sample_count;
  48. uint32_t device_connect_time;
  49. uint32_t function_pending_time;
  50. uint32_t device_disconnect_time;
  51. uint32_t control_unit_queuing_time;
  52. uint32_t device_active_only_time;
  53. uint32_t reserved[2];
  54. } QEMU_PACKED CMB;
  55. typedef struct CMBE {
  56. uint32_t ssch_rsch_count;
  57. uint32_t sample_count;
  58. uint32_t device_connect_time;
  59. uint32_t function_pending_time;
  60. uint32_t device_disconnect_time;
  61. uint32_t control_unit_queuing_time;
  62. uint32_t device_active_only_time;
  63. uint32_t device_busy_time;
  64. uint32_t initial_command_response_time;
  65. uint32_t reserved[7];
  66. } QEMU_PACKED CMBE;
  67. typedef enum CcwDataStreamOp {
  68. CDS_OP_R = 0, /* read, false when used as is_write */
  69. CDS_OP_W = 1, /* write, true when used as is_write */
  70. CDS_OP_A = 2 /* advance, should not be used as is_write */
  71. } CcwDataStreamOp;
  72. /* normal usage is via SuchchDev.cds instead of instantiating */
  73. typedef struct CcwDataStream {
  74. #define CDS_F_IDA 0x01
  75. #define CDS_F_MIDA 0x02
  76. #define CDS_F_I2K 0x04
  77. #define CDS_F_C64 0x08
  78. #define CDS_F_FMT 0x10 /* CCW format-1 */
  79. #define CDS_F_STREAM_BROKEN 0x80
  80. uint8_t flags;
  81. uint8_t at_idaw;
  82. uint16_t at_byte;
  83. uint16_t count;
  84. uint32_t cda_orig;
  85. int (*op_handler)(struct CcwDataStream *cds, void *buff, int len,
  86. CcwDataStreamOp op);
  87. hwaddr cda;
  88. bool do_skip;
  89. } CcwDataStream;
  90. /*
  91. * IO instructions conclude according to this. Currently we have only
  92. * cc codes. Valid values are 0, 1, 2, 3 and the generic semantic for
  93. * IO instructions is described briefly. For more details consult the PoP.
  94. */
  95. typedef enum IOInstEnding {
  96. /* produced expected result */
  97. IOINST_CC_EXPECTED = 0,
  98. /* status conditions were present or produced alternate result */
  99. IOINST_CC_STATUS_PRESENT = 1,
  100. /* inst. ineffective because busy with previously initiated function */
  101. IOINST_CC_BUSY = 2,
  102. /* inst. ineffective because not operational */
  103. IOINST_CC_NOT_OPERATIONAL = 3
  104. } IOInstEnding;
  105. typedef struct SubchDev SubchDev;
  106. struct SubchDev {
  107. /* channel-subsystem related things: */
  108. SCHIB curr_status; /* Needs alignment and thus must come first */
  109. ORB orb;
  110. uint8_t cssid;
  111. uint8_t ssid;
  112. uint16_t schid;
  113. uint16_t devno;
  114. uint8_t sense_data[32];
  115. hwaddr channel_prog;
  116. CCW1 last_cmd;
  117. bool last_cmd_valid;
  118. bool ccw_fmt_1;
  119. bool thinint_active;
  120. uint8_t ccw_no_data_cnt;
  121. uint16_t migrated_schid; /* used for mismatch detection */
  122. CcwDataStream cds;
  123. /* transport-provided data: */
  124. int (*ccw_cb) (SubchDev *, CCW1);
  125. void (*disable_cb)(SubchDev *);
  126. IOInstEnding (*do_subchannel_work) (SubchDev *);
  127. void (*irb_cb)(SubchDev *, IRB *);
  128. SenseId id;
  129. void *driver_data;
  130. ESW esw;
  131. };
  132. static inline void sch_gen_unit_exception(SubchDev *sch)
  133. {
  134. sch->curr_status.scsw.ctrl &= ~(SCSW_ACTL_DEVICE_ACTIVE |
  135. SCSW_ACTL_SUBCH_ACTIVE);
  136. sch->curr_status.scsw.ctrl |= SCSW_STCTL_PRIMARY |
  137. SCSW_STCTL_SECONDARY |
  138. SCSW_STCTL_ALERT |
  139. SCSW_STCTL_STATUS_PEND;
  140. sch->curr_status.scsw.cpa = sch->channel_prog + 8;
  141. sch->curr_status.scsw.dstat = SCSW_DSTAT_UNIT_EXCEP;
  142. }
  143. extern const VMStateDescription vmstate_subch_dev;
  144. /*
  145. * Identify a device within the channel subsystem.
  146. * Note that this can be used to identify either the subchannel or
  147. * the attached I/O device, as there's always one I/O device per
  148. * subchannel.
  149. */
  150. typedef struct CssDevId {
  151. uint8_t cssid;
  152. uint8_t ssid;
  153. uint16_t devid;
  154. bool valid;
  155. } CssDevId;
  156. extern const PropertyInfo css_devid_propinfo;
  157. #define DEFINE_PROP_CSS_DEV_ID(_n, _s, _f) \
  158. DEFINE_PROP(_n, _s, _f, css_devid_propinfo, CssDevId)
  159. typedef struct IndAddr {
  160. hwaddr addr;
  161. uint64_t map;
  162. unsigned long refcnt;
  163. int32_t len;
  164. QTAILQ_ENTRY(IndAddr) sibling;
  165. } IndAddr;
  166. extern const VMStateDescription vmstate_ind_addr;
  167. #define VMSTATE_PTR_TO_IND_ADDR(_f, _s) \
  168. VMSTATE_STRUCT(_f, _s, 1, vmstate_ind_addr, IndAddr*)
  169. IndAddr *get_indicator(hwaddr ind_addr, int len);
  170. void release_indicator(AdapterInfo *adapter, IndAddr *indicator);
  171. int map_indicator(AdapterInfo *adapter, IndAddr *indicator);
  172. typedef SubchDev *(*css_subch_cb_func)(uint8_t m, uint8_t cssid, uint8_t ssid,
  173. uint16_t schid);
  174. int css_create_css_image(uint8_t cssid, bool default_image);
  175. bool css_devno_used(uint8_t cssid, uint8_t ssid, uint16_t devno);
  176. void css_subch_assign(uint8_t cssid, uint8_t ssid, uint16_t schid,
  177. uint16_t devno, SubchDev *sch);
  178. void css_sch_build_virtual_schib(SubchDev *sch, uint8_t chpid, uint8_t type);
  179. int css_sch_build_schib(SubchDev *sch, CssDevId *dev_id);
  180. unsigned int css_find_free_chpid(uint8_t cssid);
  181. uint16_t css_build_subchannel_id(SubchDev *sch);
  182. void copy_scsw_to_guest(SCSW *dest, const SCSW *src);
  183. void copy_esw_to_guest(ESW *dest, const ESW *src);
  184. void css_inject_io_interrupt(SubchDev *sch);
  185. void css_reset(void);
  186. void css_reset_sch(SubchDev *sch);
  187. void css_crw_add_to_queue(CRW crw);
  188. void css_queue_crw(uint8_t rsc, uint8_t erc, int solicited,
  189. int chain, uint16_t rsid);
  190. void css_generate_sch_crws(uint8_t cssid, uint8_t ssid, uint16_t schid,
  191. int hotplugged, int add);
  192. void css_generate_chp_crws(uint8_t cssid, uint8_t chpid);
  193. void css_generate_css_crws(uint8_t cssid);
  194. void css_clear_sei_pending(void);
  195. IOInstEnding s390_ccw_cmd_request(SubchDev *sch);
  196. IOInstEnding do_subchannel_work_virtual(SubchDev *sub);
  197. IOInstEnding do_subchannel_work_passthrough(SubchDev *sub);
  198. void build_irb_passthrough(SubchDev *sch, IRB *irb);
  199. void build_irb_virtual(SubchDev *sch, IRB *irb);
  200. int s390_ccw_halt(SubchDev *sch);
  201. int s390_ccw_clear(SubchDev *sch);
  202. IOInstEnding s390_ccw_store(SubchDev *sch);
  203. typedef enum {
  204. CSS_IO_ADAPTER_VIRTIO = 0,
  205. CSS_IO_ADAPTER_PCI = 1,
  206. CSS_IO_ADAPTER_TYPE_NUMS,
  207. } CssIoAdapterType;
  208. void css_adapter_interrupt(CssIoAdapterType type, uint8_t isc);
  209. int css_do_sic(S390CPU *cpu, uint8_t isc, uint16_t mode);
  210. uint32_t css_get_adapter_id(CssIoAdapterType type, uint8_t isc);
  211. void css_register_io_adapters(CssIoAdapterType type, bool swap, bool maskable,
  212. uint8_t flags, Error **errp);
  213. #ifndef CONFIG_USER_ONLY
  214. SubchDev *css_find_subch(uint8_t m, uint8_t cssid, uint8_t ssid,
  215. uint16_t schid);
  216. bool css_subch_visible(SubchDev *sch);
  217. void css_conditional_io_interrupt(SubchDev *sch);
  218. IOInstEnding css_do_stsch(SubchDev *sch, SCHIB *schib);
  219. bool css_schid_final(int m, uint8_t cssid, uint8_t ssid, uint16_t schid);
  220. IOInstEnding css_do_msch(SubchDev *sch, const SCHIB *schib);
  221. IOInstEnding css_do_xsch(SubchDev *sch);
  222. IOInstEnding css_do_csch(SubchDev *sch);
  223. IOInstEnding css_do_hsch(SubchDev *sch);
  224. IOInstEnding css_do_ssch(SubchDev *sch, ORB *orb);
  225. int css_do_tsch_get_irb(SubchDev *sch, IRB *irb, int *irb_len);
  226. void css_do_tsch_update_subch(SubchDev *sch);
  227. int css_do_stcrw(CRW *crw);
  228. void css_undo_stcrw(CRW *crw);
  229. int css_collect_chp_desc(int m, uint8_t cssid, uint8_t f_chpid, uint8_t l_chpid,
  230. int rfmt, void *buf);
  231. void css_do_schm(uint8_t mbk, int update, int dct, uint64_t mbo);
  232. int css_enable_mcsse(void);
  233. int css_enable_mss(void);
  234. IOInstEnding css_do_rsch(SubchDev *sch);
  235. int css_do_rchp(uint8_t cssid, uint8_t chpid);
  236. bool css_present(uint8_t cssid);
  237. #endif
  238. extern const PropertyInfo css_devid_ro_propinfo;
  239. #define DEFINE_PROP_CSS_DEV_ID_RO(_n, _s, _f) \
  240. DEFINE_PROP(_n, _s, _f, css_devid_ro_propinfo, CssDevId)
  241. /**
  242. * Create a subchannel for the given bus id.
  243. *
  244. * If @p bus_id is valid, verify that it is not already in use, and find a
  245. * free devno for it.
  246. * If @p bus_id is not valid find a free subchannel id and device number
  247. * across all subchannel sets and all css images starting from the default
  248. * css image.
  249. *
  250. * If either of the former actions succeed, allocate a subchannel structure,
  251. * initialise it with the bus id, subchannel id and device number, register
  252. * it with the CSS and return it. Otherwise return NULL.
  253. *
  254. * The caller becomes owner of the returned subchannel structure and
  255. * is responsible for unregistering and freeing it.
  256. */
  257. SubchDev *css_create_sch(CssDevId bus_id, Error **errp);
  258. /** Turn on css migration */
  259. void css_register_vmstate(void);
  260. void ccw_dstream_init(CcwDataStream *cds, CCW1 const *ccw, ORB const *orb);
  261. static inline void ccw_dstream_rewind(CcwDataStream *cds)
  262. {
  263. cds->at_byte = 0;
  264. cds->at_idaw = 0;
  265. cds->cda = cds->cda_orig;
  266. }
  267. static inline bool ccw_dstream_good(CcwDataStream *cds)
  268. {
  269. return !(cds->flags & CDS_F_STREAM_BROKEN);
  270. }
  271. static inline uint16_t ccw_dstream_residual_count(CcwDataStream *cds)
  272. {
  273. return cds->count - cds->at_byte;
  274. }
  275. static inline uint16_t ccw_dstream_avail(CcwDataStream *cds)
  276. {
  277. return ccw_dstream_good(cds) ? ccw_dstream_residual_count(cds) : 0;
  278. }
  279. static inline int ccw_dstream_advance(CcwDataStream *cds, int len)
  280. {
  281. return cds->op_handler(cds, NULL, len, CDS_OP_A);
  282. }
  283. static inline int ccw_dstream_write_buf(CcwDataStream *cds, void *buff, int len)
  284. {
  285. return cds->op_handler(cds, buff, len, CDS_OP_W);
  286. }
  287. static inline int ccw_dstream_read_buf(CcwDataStream *cds, void *buff, int len)
  288. {
  289. return cds->op_handler(cds, buff, len, CDS_OP_R);
  290. }
  291. #define ccw_dstream_read(cds, v) ccw_dstream_read_buf((cds), &(v), sizeof(v))
  292. #define ccw_dstream_write(cds, v) ccw_dstream_write_buf((cds), &(v), sizeof(v))
  293. /**
  294. * true if (vmstate based) migration of the channel subsystem
  295. * is enabled, false if it is disabled.
  296. */
  297. extern bool css_migration_enabled;
  298. #endif