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