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. atomic_t recovery_threads;
  89. struct mutex ctx_recovery_mutex;
  90. struct mutex ctx_tbl_list_mutex;
  91. struct rw_semaphore ioctl_rwsem;
  92. struct ctx_info *ctx_tbl[MAX_CONTEXT];
  93. struct list_head ctx_err_recovery; /* contexts w/ recovery pending */
  94. struct file_operations cxl_fops;
  95. /* Parameters that are LUN table related */
  96. int last_lun_index[CXLFLASH_NUM_FC_PORTS];
  97. int promote_lun_index;
  98. struct list_head lluns; /* list of llun_info structs */
  99. wait_queue_head_t tmf_waitq;
  100. spinlock_t tmf_slock;
  101. bool tmf_active;
  102. wait_queue_head_t reset_waitq;
  103. enum cxlflash_state state;
  104. };
  105. struct afu_cmd {
  106. struct sisl_ioarcb rcb; /* IOARCB (cache line aligned) */
  107. struct sisl_ioasa sa; /* IOASA must follow IOARCB */
  108. spinlock_t slock;
  109. struct completion cevent;
  110. char *buf; /* per command buffer */
  111. struct afu *parent;
  112. int slot;
  113. atomic_t free;
  114. u8 cmd_tmf:1;
  115. /* As per the SISLITE spec the IOARCB EA has to be 16-byte aligned.
  116. * However for performance reasons the IOARCB/IOASA should be
  117. * cache line aligned.
  118. */
  119. } __aligned(cache_line_size());
  120. struct afu {
  121. /* Stuff requiring alignment go first. */
  122. u64 rrq_entry[NUM_RRQ_ENTRY]; /* 2K RRQ */
  123. /*
  124. * Command & data for AFU commands.
  125. */
  126. struct afu_cmd cmd[CXLFLASH_NUM_CMDS];
  127. /* Beware of alignment till here. Preferably introduce new
  128. * fields after this point
  129. */
  130. /* AFU HW */
  131. struct cxl_ioctl_start_work work;
  132. struct cxlflash_afu_map __iomem *afu_map; /* entire MMIO map */
  133. struct sisl_host_map __iomem *host_map; /* MC host map */
  134. struct sisl_ctrl_map __iomem *ctrl_map; /* MC control map */
  135. struct kref mapcount;
  136. ctx_hndl_t ctx_hndl; /* master's context handle */
  137. u64 *hrrq_start;
  138. u64 *hrrq_end;
  139. u64 *hrrq_curr;
  140. bool toggle;
  141. bool read_room;
  142. atomic64_t room;
  143. u64 hb;
  144. u32 cmd_couts; /* Number of command checkouts */
  145. u32 internal_lun; /* User-desired LUN mode for this AFU */
  146. char version[16];
  147. u64 interface_version;
  148. struct cxlflash_cfg *parent; /* Pointer back to parent cxlflash_cfg */
  149. };
  150. static inline u64 lun_to_lunid(u64 lun)
  151. {
  152. __be64 lun_id;
  153. int_to_scsilun(lun, (struct scsi_lun *)&lun_id);
  154. return be64_to_cpu(lun_id);
  155. }
  156. int cxlflash_afu_sync(struct afu *, ctx_hndl_t, res_hndl_t, u8);
  157. void cxlflash_list_init(void);
  158. void cxlflash_term_global_luns(void);
  159. void cxlflash_free_errpage(void);
  160. int cxlflash_ioctl(struct scsi_device *, int, void __user *);
  161. void cxlflash_stop_term_user_contexts(struct cxlflash_cfg *);
  162. int cxlflash_mark_contexts_error(struct cxlflash_cfg *);
  163. void cxlflash_term_local_luns(struct cxlflash_cfg *);
  164. void cxlflash_restore_luntable(struct cxlflash_cfg *);
  165. #endif /* ifndef _CXLFLASH_COMMON_H */