snic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright 2014 Cisco Systems, Inc. All rights reserved. */
  3. #ifndef _SNIC_H_
  4. #define _SNIC_H_
  5. #include <linux/module.h>
  6. #include <linux/netdevice.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/bitops.h>
  9. #include <linux/mempool.h>
  10. #include <scsi/scsi_cmnd.h>
  11. #include <scsi/scsi.h>
  12. #include <scsi/scsi_host.h>
  13. #include "snic_disc.h"
  14. #include "snic_io.h"
  15. #include "snic_res.h"
  16. #include "snic_trc.h"
  17. #include "snic_stats.h"
  18. #include "vnic_dev.h"
  19. #include "vnic_wq.h"
  20. #include "vnic_cq.h"
  21. #include "vnic_intr.h"
  22. #include "vnic_stats.h"
  23. #include "vnic_snic.h"
  24. #define SNIC_DRV_NAME "snic"
  25. #define SNIC_DRV_DESCRIPTION "Cisco SCSI NIC Driver"
  26. #define SNIC_DRV_VERSION "0.0.1.18"
  27. #define PFX SNIC_DRV_NAME ":"
  28. #define DFX SNIC_DRV_NAME "%d: "
  29. #define DESC_CLEAN_LOW_WATERMARK 8
  30. #define SNIC_UCSM_DFLT_THROTTLE_CNT_BLD 16 /* UCSM default throttle count */
  31. #define SNIC_MAX_IO_REQ 50 /* scsi_cmnd tag map entries */
  32. #define SNIC_MIN_IO_REQ 8 /* Min IO throttle count */
  33. #define SNIC_IO_LOCKS 64 /* IO locks: power of 2 */
  34. #define SNIC_DFLT_QUEUE_DEPTH 32 /* Default Queue Depth */
  35. #define SNIC_MAX_QUEUE_DEPTH 64 /* Max Queue Depth */
  36. #define SNIC_DFLT_CMD_TIMEOUT 90 /* Extended tmo for FW */
  37. /*
  38. * Tag bits used for special requests.
  39. */
  40. #define SNIC_TAG_ABORT BIT(30) /* Tag indicating abort */
  41. #define SNIC_TAG_DEV_RST BIT(29) /* Tag for device reset */
  42. #define SNIC_TAG_IOCTL_DEV_RST BIT(28) /* Tag for User Device Reset */
  43. #define SNIC_TAG_MASK (BIT(24) - 1) /* Mask for lookup */
  44. #define SNIC_NO_TAG -1
  45. /*
  46. * Command flags to identify the type of command and for other future use
  47. */
  48. #define SNIC_NO_FLAGS 0
  49. #define SNIC_IO_INITIALIZED BIT(0)
  50. #define SNIC_IO_ISSUED BIT(1)
  51. #define SNIC_IO_DONE BIT(2)
  52. #define SNIC_IO_REQ_NULL BIT(3)
  53. #define SNIC_IO_ABTS_PENDING BIT(4)
  54. #define SNIC_IO_ABORTED BIT(5)
  55. #define SNIC_IO_ABTS_ISSUED BIT(6)
  56. #define SNIC_IO_TERM_ISSUED BIT(7)
  57. #define SNIC_IO_ABTS_TIMEDOUT BIT(8)
  58. #define SNIC_IO_ABTS_TERM_DONE BIT(9)
  59. #define SNIC_IO_ABTS_TERM_REQ_NULL BIT(10)
  60. #define SNIC_IO_ABTS_TERM_TIMEDOUT BIT(11)
  61. #define SNIC_IO_INTERNAL_TERM_PENDING BIT(12)
  62. #define SNIC_IO_INTERNAL_TERM_ISSUED BIT(13)
  63. #define SNIC_DEVICE_RESET BIT(14)
  64. #define SNIC_DEV_RST_ISSUED BIT(15)
  65. #define SNIC_DEV_RST_TIMEDOUT BIT(16)
  66. #define SNIC_DEV_RST_ABTS_ISSUED BIT(17)
  67. #define SNIC_DEV_RST_TERM_ISSUED BIT(18)
  68. #define SNIC_DEV_RST_DONE BIT(19)
  69. #define SNIC_DEV_RST_REQ_NULL BIT(20)
  70. #define SNIC_DEV_RST_ABTS_DONE BIT(21)
  71. #define SNIC_DEV_RST_TERM_DONE BIT(22)
  72. #define SNIC_DEV_RST_ABTS_PENDING BIT(23)
  73. #define SNIC_DEV_RST_PENDING BIT(24)
  74. #define SNIC_DEV_RST_NOTSUP BIT(25)
  75. #define SNIC_SCSI_CLEANUP BIT(26)
  76. #define SNIC_HOST_RESET_ISSUED BIT(27)
  77. #define SNIC_HOST_RESET_CMD_TERM \
  78. (SNIC_DEV_RST_NOTSUP | SNIC_SCSI_CLEANUP | SNIC_HOST_RESET_ISSUED)
  79. #define SNIC_ABTS_TIMEOUT 30000 /* msec */
  80. #define SNIC_LUN_RESET_TIMEOUT 30000 /* msec */
  81. #define SNIC_HOST_RESET_TIMEOUT 30000 /* msec */
  82. /*
  83. * These are protected by the hashed req_lock.
  84. */
  85. #define CMD_SP(Cmnd) \
  86. (((struct snic_internal_io_state *)scsi_cmd_priv(Cmnd))->rqi)
  87. #define CMD_STATE(Cmnd) \
  88. (((struct snic_internal_io_state *)scsi_cmd_priv(Cmnd))->state)
  89. #define CMD_ABTS_STATUS(Cmnd) \
  90. (((struct snic_internal_io_state *)scsi_cmd_priv(Cmnd))->abts_status)
  91. #define CMD_LR_STATUS(Cmnd) \
  92. (((struct snic_internal_io_state *)scsi_cmd_priv(Cmnd))->lr_status)
  93. #define CMD_FLAGS(Cmnd) \
  94. (((struct snic_internal_io_state *)scsi_cmd_priv(Cmnd))->flags)
  95. #define SNIC_INVALID_CODE 0x100 /* Hdr Status val unused by firmware */
  96. #define SNIC_MAX_TARGET 256
  97. #define SNIC_FLAGS_NONE (0)
  98. /* snic module params */
  99. extern unsigned int snic_max_qdepth;
  100. /* snic debugging */
  101. extern unsigned int snic_log_level;
  102. #define SNIC_MAIN_LOGGING 0x1
  103. #define SNIC_SCSI_LOGGING 0x2
  104. #define SNIC_ISR_LOGGING 0x8
  105. #define SNIC_DESC_LOGGING 0x10
  106. #define SNIC_CHECK_LOGGING(LEVEL, CMD) \
  107. do { \
  108. if (unlikely(snic_log_level & LEVEL)) \
  109. do { \
  110. CMD; \
  111. } while (0); \
  112. } while (0)
  113. #define SNIC_MAIN_DBG(host, fmt, args...) \
  114. SNIC_CHECK_LOGGING(SNIC_MAIN_LOGGING, \
  115. shost_printk(KERN_INFO, host, fmt, ## args);)
  116. #define SNIC_SCSI_DBG(host, fmt, args...) \
  117. SNIC_CHECK_LOGGING(SNIC_SCSI_LOGGING, \
  118. shost_printk(KERN_INFO, host, fmt, ##args);)
  119. #define SNIC_DISC_DBG(host, fmt, args...) \
  120. SNIC_CHECK_LOGGING(SNIC_SCSI_LOGGING, \
  121. shost_printk(KERN_INFO, host, fmt, ##args);)
  122. #define SNIC_ISR_DBG(host, fmt, args...) \
  123. SNIC_CHECK_LOGGING(SNIC_ISR_LOGGING, \
  124. shost_printk(KERN_INFO, host, fmt, ##args);)
  125. #define SNIC_HOST_ERR(host, fmt, args...) \
  126. shost_printk(KERN_ERR, host, fmt, ##args)
  127. #define SNIC_HOST_INFO(host, fmt, args...) \
  128. shost_printk(KERN_INFO, host, fmt, ##args)
  129. #define SNIC_INFO(fmt, args...) \
  130. pr_info(PFX fmt, ## args)
  131. #define SNIC_DBG(fmt, args...) \
  132. pr_info(PFX fmt, ## args)
  133. #define SNIC_ERR(fmt, args...) \
  134. pr_err(PFX fmt, ## args)
  135. #ifdef DEBUG
  136. #define SNIC_BUG_ON(EXPR) \
  137. ({ \
  138. if (EXPR) { \
  139. SNIC_ERR("SNIC BUG(%s)\n", #EXPR); \
  140. BUG_ON(EXPR); \
  141. } \
  142. })
  143. #else
  144. #define SNIC_BUG_ON(EXPR) \
  145. ({ \
  146. if (EXPR) { \
  147. SNIC_ERR("SNIC BUG(%s) at %s : %d\n", \
  148. #EXPR, __func__, __LINE__); \
  149. WARN_ON_ONCE(EXPR); \
  150. } \
  151. })
  152. #endif
  153. /* Soft assert */
  154. #define SNIC_ASSERT_NOT_IMPL(EXPR) \
  155. ({ \
  156. if (EXPR) {\
  157. SNIC_INFO("Functionality not impl'ed at %s:%d\n", \
  158. __func__, __LINE__); \
  159. WARN_ON_ONCE(EXPR); \
  160. } \
  161. })
  162. extern const char *snic_state_str[];
  163. enum snic_intx_intr_index {
  164. SNIC_INTX_WQ_RQ_COPYWQ,
  165. SNIC_INTX_ERR,
  166. SNIC_INTX_NOTIFY,
  167. SNIC_INTX_INTR_MAX,
  168. };
  169. enum snic_msix_intr_index {
  170. SNIC_MSIX_WQ,
  171. SNIC_MSIX_IO_CMPL,
  172. SNIC_MSIX_ERR_NOTIFY,
  173. SNIC_MSIX_INTR_MAX,
  174. };
  175. #define SNIC_INTRHDLR_NAMSZ (2 * IFNAMSIZ)
  176. struct snic_msix_entry {
  177. int requested;
  178. char devname[SNIC_INTRHDLR_NAMSZ];
  179. irqreturn_t (*isr)(int, void *);
  180. void *devid;
  181. };
  182. enum snic_state {
  183. SNIC_INIT = 0,
  184. SNIC_ERROR,
  185. SNIC_ONLINE,
  186. SNIC_OFFLINE,
  187. SNIC_FWRESET,
  188. };
  189. #define SNIC_WQ_MAX 1
  190. #define SNIC_CQ_IO_CMPL_MAX 1
  191. #define SNIC_CQ_MAX (SNIC_WQ_MAX + SNIC_CQ_IO_CMPL_MAX)
  192. /* firmware version information */
  193. struct snic_fw_info {
  194. u32 fw_ver;
  195. u32 hid; /* u16 hid | u16 vnic id */
  196. u32 max_concur_ios; /* max concurrent ios */
  197. u32 max_sgs_per_cmd; /* max sgls per IO */
  198. u32 max_io_sz; /* max io size supported */
  199. u32 hba_cap; /* hba capabilities */
  200. u32 max_tgts; /* max tgts supported */
  201. u16 io_tmo; /* FW Extended timeout */
  202. struct completion *wait; /* protected by snic lock*/
  203. };
  204. /*
  205. * snic_work item : defined to process asynchronous events
  206. */
  207. struct snic_work {
  208. struct work_struct work;
  209. u16 ev_id;
  210. u64 *ev_data;
  211. };
  212. /*
  213. * snic structure to represent SCSI vNIC
  214. */
  215. struct snic {
  216. /* snic specific members */
  217. struct list_head list;
  218. char name[IFNAMSIZ];
  219. atomic_t state;
  220. spinlock_t snic_lock;
  221. struct completion *remove_wait;
  222. bool in_remove;
  223. bool stop_link_events; /* stop processing link events */
  224. /* discovery related */
  225. struct snic_disc disc;
  226. /* Scsi Host info */
  227. struct Scsi_Host *shost;
  228. /* vnic related structures */
  229. struct vnic_dev_bar bar0;
  230. struct vnic_stats *stats;
  231. unsigned long stats_time;
  232. unsigned long stats_reset_time;
  233. struct vnic_dev *vdev;
  234. /* hw resource info */
  235. unsigned int wq_count;
  236. unsigned int cq_count;
  237. unsigned int intr_count;
  238. unsigned int err_intr_offset;
  239. int link_status; /* retrieved from svnic_dev_link_status() */
  240. u32 link_down_cnt;
  241. /* pci related */
  242. struct pci_dev *pdev;
  243. struct snic_msix_entry msix[SNIC_MSIX_INTR_MAX];
  244. /* io related info */
  245. mempool_t *req_pool[SNIC_REQ_MAX_CACHES]; /* (??) */
  246. ____cacheline_aligned spinlock_t io_req_lock[SNIC_IO_LOCKS];
  247. /* Maintain snic specific commands, cmds with no tag in spl_cmd_list */
  248. ____cacheline_aligned spinlock_t spl_cmd_lock;
  249. struct list_head spl_cmd_list;
  250. unsigned int max_tag_id;
  251. atomic_t ios_inflight; /* io in flight counter */
  252. struct vnic_snic_config config;
  253. struct work_struct link_work;
  254. /* firmware information */
  255. struct snic_fw_info fwinfo;
  256. /* Work for processing Target related work */
  257. struct work_struct tgt_work;
  258. /* Work for processing Discovery */
  259. struct work_struct disc_work;
  260. /* stats related */
  261. unsigned int reset_stats;
  262. atomic64_t io_cmpl_skip;
  263. struct snic_stats s_stats; /* Per SNIC driver stats */
  264. /* platform specific */
  265. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  266. struct dentry *stats_host; /* Per snic debugfs root */
  267. struct dentry *stats_file; /* Per snic debugfs file */
  268. struct dentry *reset_stats_file;/* Per snic reset stats file */
  269. #endif
  270. /* completion queue cache line section */
  271. ____cacheline_aligned struct vnic_cq cq[SNIC_CQ_MAX];
  272. /* work queue cache line section */
  273. ____cacheline_aligned struct vnic_wq wq[SNIC_WQ_MAX];
  274. spinlock_t wq_lock[SNIC_WQ_MAX];
  275. /* interrupt resource cache line section */
  276. ____cacheline_aligned struct vnic_intr intr[SNIC_MSIX_INTR_MAX];
  277. }; /* end of snic structure */
  278. /*
  279. * SNIC Driver's Global Data
  280. */
  281. struct snic_global {
  282. struct list_head snic_list;
  283. spinlock_t snic_list_lock;
  284. struct kmem_cache *req_cache[SNIC_REQ_MAX_CACHES];
  285. struct workqueue_struct *event_q;
  286. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  287. /* debugfs related global data */
  288. struct dentry *trc_root;
  289. struct dentry *stats_root;
  290. struct snic_trc trc ____cacheline_aligned;
  291. #endif
  292. };
  293. extern struct snic_global *snic_glob;
  294. int snic_glob_init(void);
  295. void snic_glob_cleanup(void);
  296. extern struct workqueue_struct *snic_event_queue;
  297. extern const struct attribute_group *snic_host_groups[];
  298. int snic_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
  299. int snic_abort_cmd(struct scsi_cmnd *);
  300. int snic_device_reset(struct scsi_cmnd *);
  301. int snic_host_reset(struct scsi_cmnd *);
  302. int snic_reset(struct Scsi_Host *, struct scsi_cmnd *);
  303. void snic_shutdown_scsi_cleanup(struct snic *);
  304. int snic_request_intr(struct snic *);
  305. void snic_free_intr(struct snic *);
  306. int snic_set_intr_mode(struct snic *);
  307. void snic_clear_intr_mode(struct snic *);
  308. int snic_fwcq_cmpl_handler(struct snic *, int);
  309. int snic_wq_cmpl_handler(struct snic *, int);
  310. void snic_free_wq_buf(struct vnic_wq *, struct vnic_wq_buf *);
  311. void snic_log_q_error(struct snic *);
  312. void snic_handle_link_event(struct snic *);
  313. void snic_handle_link(struct work_struct *);
  314. int snic_queue_exch_ver_req(struct snic *);
  315. void snic_io_exch_ver_cmpl_handler(struct snic *, struct snic_fw_req *);
  316. int snic_queue_wq_desc(struct snic *, void *os_buf, u16 len);
  317. void snic_handle_untagged_req(struct snic *, struct snic_req_info *);
  318. void snic_release_untagged_req(struct snic *, struct snic_req_info *);
  319. void snic_free_all_untagged_reqs(struct snic *);
  320. int snic_get_conf(struct snic *);
  321. void snic_set_state(struct snic *, enum snic_state);
  322. int snic_get_state(struct snic *);
  323. const char *snic_state_to_str(unsigned int);
  324. void snic_hex_dump(char *, char *, int);
  325. void snic_print_desc(const char *fn, char *os_buf, int len);
  326. const char *show_opcode_name(int val);
  327. #endif /* _SNIC_H */