common.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * CXL Flash Device Driver
  3. *
  4. * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
  5. * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
  6. *
  7. * Copyright (C) 2015 IBM Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #ifndef _CXLFLASH_COMMON_H
  15. #define _CXLFLASH_COMMON_H
  16. #include <linux/list.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/types.h>
  19. #include <scsi/scsi.h>
  20. #include <scsi/scsi_device.h>
  21. extern const struct file_operations cxlflash_cxl_fops;
  22. #define MAX_CONTEXT CXLFLASH_MAX_CONTEXT /* num contexts per afu */
  23. #define CXLFLASH_BLOCK_SIZE 4096 /* 4K blocks */
  24. #define CXLFLASH_MAX_XFER_SIZE 16777216 /* 16MB transfer */
  25. #define CXLFLASH_MAX_SECTORS (CXLFLASH_MAX_XFER_SIZE/512) /* SCSI wants
  26. max_sectors
  27. in units of
  28. 512 byte
  29. sectors
  30. */
  31. #define MAX_RHT_PER_CONTEXT (PAGE_SIZE / sizeof(struct sisl_rht_entry))
  32. /* AFU command retry limit */
  33. #define MC_RETRY_CNT 5 /* sufficient for SCSI check and
  34. certain AFU errors */
  35. /* Command management definitions */
  36. #define CXLFLASH_NUM_CMDS (2 * CXLFLASH_MAX_CMDS) /* Must be a pow2 for
  37. alignment and more
  38. efficient array
  39. index derivation
  40. */
  41. #define CXLFLASH_MAX_CMDS 256
  42. #define CXLFLASH_MAX_CMDS_PER_LUN CXLFLASH_MAX_CMDS
  43. /* RRQ for master issued cmds */
  44. #define NUM_RRQ_ENTRY CXLFLASH_MAX_CMDS
  45. static inline void check_sizes(void)
  46. {
  47. BUILD_BUG_ON_NOT_POWER_OF_2(CXLFLASH_NUM_CMDS);
  48. }
  49. /* AFU defines a fixed size of 4K for command buffers (borrow 4K page define) */
  50. #define CMD_BUFSIZE SIZE_4K
  51. /* flags in IOA status area for host use */
  52. #define B_DONE 0x01
  53. #define B_ERROR 0x02 /* set with B_DONE */
  54. #define B_TIMEOUT 0x04 /* set with B_DONE & B_ERROR */
  55. enum cxlflash_lr_state {
  56. LINK_RESET_INVALID,
  57. LINK_RESET_REQUIRED,
  58. LINK_RESET_COMPLETE
  59. };
  60. enum cxlflash_init_state {
  61. INIT_STATE_NONE,
  62. INIT_STATE_PCI,
  63. INIT_STATE_AFU,
  64. INIT_STATE_SCSI
  65. };
  66. enum cxlflash_state {
  67. STATE_NORMAL, /* Normal running state, everything good */
  68. STATE_RESET, /* Reset state, trying to reset/recover */
  69. STATE_FAILTERM /* Failed/terminating state, error out users/threads */
  70. };
  71. /*
  72. * Each context has its own set of resource handles that is visible
  73. * only from that context.
  74. */
  75. struct cxlflash_cfg {
  76. struct afu *afu;
  77. struct cxl_context *mcctx;
  78. struct pci_dev *dev;
  79. struct pci_device_id *dev_id;
  80. struct Scsi_Host *host;
  81. ulong cxlflash_regs_pci;
  82. struct work_struct work_q;
  83. enum cxlflash_init_state init_state;
  84. enum cxlflash_lr_state lr_state;
  85. int lr_port;
  86. atomic_t scan_host_needed;
  87. struct cxl_afu *cxl_afu;
  88. struct pci_dev *parent_dev;
  89. atomic_t recovery_threads;
  90. struct mutex ctx_recovery_mutex;
  91. struct mutex ctx_tbl_list_mutex;
  92. struct rw_semaphore ioctl_rwsem;
  93. struct ctx_info *ctx_tbl[MAX_CONTEXT];
  94. struct list_head ctx_err_recovery; /* contexts w/ recovery pending */
  95. struct file_operations cxl_fops;
  96. /* Parameters that are LUN table related */
  97. int last_lun_index[CXLFLASH_NUM_FC_PORTS];
  98. int promote_lun_index;
  99. struct list_head lluns; /* list of llun_info structs */
  100. wait_queue_head_t tmf_waitq;
  101. spinlock_t tmf_slock;
  102. bool tmf_active;
  103. wait_queue_head_t reset_waitq;
  104. enum cxlflash_state state;
  105. };
  106. struct afu_cmd {
  107. struct sisl_ioarcb rcb; /* IOARCB (cache line aligned) */
  108. struct sisl_ioasa sa; /* IOASA must follow IOARCB */
  109. spinlock_t slock;
  110. struct completion cevent;
  111. char *buf; /* per command buffer */
  112. struct afu *parent;
  113. int slot;
  114. atomic_t free;
  115. u8 cmd_tmf:1;
  116. /* As per the SISLITE spec the IOARCB EA has to be 16-byte aligned.
  117. * However for performance reasons the IOARCB/IOASA should be
  118. * cache line aligned.
  119. */
  120. } __aligned(cache_line_size());
  121. struct afu {
  122. /* Stuff requiring alignment go first. */
  123. u64 rrq_entry[NUM_RRQ_ENTRY]; /* 2K RRQ */
  124. /*
  125. * Command & data for AFU commands.
  126. */
  127. struct afu_cmd cmd[CXLFLASH_NUM_CMDS];
  128. /* Beware of alignment till here. Preferably introduce new
  129. * fields after this point
  130. */
  131. /* AFU HW */
  132. struct cxl_ioctl_start_work work;
  133. struct cxlflash_afu_map __iomem *afu_map; /* entire MMIO map */
  134. struct sisl_host_map __iomem *host_map; /* MC host map */
  135. struct sisl_ctrl_map __iomem *ctrl_map; /* MC control map */
  136. struct kref mapcount;
  137. ctx_hndl_t ctx_hndl; /* master's context handle */
  138. u64 *hrrq_start;
  139. u64 *hrrq_end;
  140. u64 *hrrq_curr;
  141. bool toggle;
  142. bool read_room;
  143. atomic64_t room;
  144. u64 hb;
  145. u32 cmd_couts; /* Number of command checkouts */
  146. u32 internal_lun; /* User-desired LUN mode for this AFU */
  147. char version[16];
  148. u64 interface_version;
  149. struct cxlflash_cfg *parent; /* Pointer back to parent cxlflash_cfg */
  150. };
  151. static inline u64 lun_to_lunid(u64 lun)
  152. {
  153. __be64 lun_id;
  154. int_to_scsilun(lun, (struct scsi_lun *)&lun_id);
  155. return be64_to_cpu(lun_id);
  156. }
  157. int cxlflash_afu_sync(struct afu *, ctx_hndl_t, res_hndl_t, u8);
  158. void cxlflash_list_init(void);
  159. void cxlflash_term_global_luns(void);
  160. void cxlflash_free_errpage(void);
  161. int cxlflash_ioctl(struct scsi_device *, int, void __user *);
  162. void cxlflash_stop_term_user_contexts(struct cxlflash_cfg *);
  163. int cxlflash_mark_contexts_error(struct cxlflash_cfg *);
  164. void cxlflash_term_local_luns(struct cxlflash_cfg *);
  165. void cxlflash_restore_luntable(struct cxlflash_cfg *);
  166. #endif /* ifndef _CXLFLASH_COMMON_H */