63b35c8e40bde6430b2c07e8f11375e7e05f59f5
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / fnic / fnic.h
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  */
18 #ifndef _FNIC_H_
19 #define _FNIC_H_
20
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/workqueue.h>
24 #include <linux/bitops.h>
25 #include <scsi/libfc.h>
26 #include <scsi/libfcoe.h>
27 #include "fnic_io.h"
28 #include "fnic_res.h"
29 #include "vnic_dev.h"
30 #include "vnic_wq.h"
31 #include "vnic_rq.h"
32 #include "vnic_cq.h"
33 #include "vnic_wq_copy.h"
34 #include "vnic_intr.h"
35 #include "vnic_stats.h"
36 #include "vnic_scsi.h"
37
38 #define DRV_NAME                "fnic"
39 #define DRV_DESCRIPTION         "Cisco FCoE HBA Driver"
40 #define DRV_VERSION             "1.5.0.2"
41 #define PFX                     DRV_NAME ": "
42 #define DFX                     DRV_NAME "%d: "
43
44 #define DESC_CLEAN_LOW_WATERMARK 8
45 #define FNIC_MAX_IO_REQ         2048 /* scsi_cmnd tag map entries */
46 #define FNIC_IO_LOCKS           64 /* IO locks: power of 2 */
47 #define FNIC_DFLT_QUEUE_DEPTH   32
48 #define FNIC_STATS_RATE_LIMIT   4 /* limit rate at which stats are pulled up */
49
50 /*
51  * Tag bits used for special requests.
52  */
53 #define FNIC_TAG_ABORT          BIT(30)         /* tag bit indicating abort */
54 #define FNIC_TAG_DEV_RST        BIT(29)         /* indicates device reset */
55 #define FNIC_TAG_MASK           (BIT(24) - 1)   /* mask for lookup */
56 #define FNIC_NO_TAG             -1
57
58 /*
59  * Command flags to identify the type of command and for other future
60  * use.
61  */
62 #define FNIC_NO_FLAGS                   0
63 #define FNIC_CDB_REQ                    BIT(1)  /* All IOs with a valid CDB */
64 #define FNIC_BLOCKING_REQ               BIT(2)  /* All blocking Requests */
65 #define FNIC_DEVICE_RESET               BIT(3)  /* Device reset request */
66 #define FNIC_DEV_RST_PENDING            BIT(4)  /* Device reset pending */
67 #define FNIC_DEV_RST_TIMED_OUT          BIT(5)  /* Device reset timed out */
68 #define FNIC_DEV_RST_TERM_ISSUED        BIT(6)  /* Device reset terminate */
69 #define FNIC_DEV_RST_DONE               BIT(7)  /* Device reset done */
70
71 /*
72  * Usage of the scsi_cmnd scratchpad.
73  * These fields are locked by the hashed io_req_lock.
74  */
75 #define CMD_SP(Cmnd)            ((Cmnd)->SCp.ptr)
76 #define CMD_STATE(Cmnd)         ((Cmnd)->SCp.phase)
77 #define CMD_ABTS_STATUS(Cmnd)   ((Cmnd)->SCp.Message)
78 #define CMD_LR_STATUS(Cmnd)     ((Cmnd)->SCp.have_data_in)
79 #define CMD_TAG(Cmnd)           ((Cmnd)->SCp.sent_command)
80 #define CMD_FLAGS(Cmnd)         ((Cmnd)->SCp.Status)
81
82 #define FCPIO_INVALID_CODE 0x100 /* hdr_status value unused by firmware */
83
84 #define FNIC_LUN_RESET_TIMEOUT       10000      /* mSec */
85 #define FNIC_HOST_RESET_TIMEOUT      10000      /* mSec */
86 #define FNIC_RMDEVICE_TIMEOUT        1000       /* mSec */
87 #define FNIC_HOST_RESET_SETTLE_TIME  30         /* Sec */
88 #define FNIC_ABT_TERM_DELAY_TIMEOUT  500        /* mSec */
89
90 #define FNIC_MAX_FCP_TARGET     256
91
92 /**
93  * state_flags to identify host state along along with fnic's state
94  **/
95 #define __FNIC_FLAGS_FWRESET            BIT(0) /* fwreset in progress */
96 #define __FNIC_FLAGS_BLOCK_IO           BIT(1) /* IOs are blocked */
97
98 #define FNIC_FLAGS_NONE                 (0)
99 #define FNIC_FLAGS_FWRESET              (__FNIC_FLAGS_FWRESET | \
100                                         __FNIC_FLAGS_BLOCK_IO)
101
102 #define FNIC_FLAGS_IO_BLOCKED           (__FNIC_FLAGS_BLOCK_IO)
103
104 #define fnic_set_state_flags(fnicp, st_flags)   \
105         __fnic_set_state_flags(fnicp, st_flags, 0)
106
107 #define fnic_clear_state_flags(fnicp, st_flags)  \
108         __fnic_set_state_flags(fnicp, st_flags, 1)
109
110 extern unsigned int fnic_log_level;
111
112 #define FNIC_MAIN_LOGGING 0x01
113 #define FNIC_FCS_LOGGING 0x02
114 #define FNIC_SCSI_LOGGING 0x04
115 #define FNIC_ISR_LOGGING 0x08
116
117 #define FNIC_CHECK_LOGGING(LEVEL, CMD)                          \
118 do {                                                            \
119         if (unlikely(fnic_log_level & LEVEL))                   \
120                 do {                                            \
121                         CMD;                                    \
122                 } while (0);                                    \
123 } while (0)
124
125 #define FNIC_MAIN_DBG(kern_level, host, fmt, args...)           \
126         FNIC_CHECK_LOGGING(FNIC_MAIN_LOGGING,                   \
127                          shost_printk(kern_level, host, fmt, ##args);)
128
129 #define FNIC_FCS_DBG(kern_level, host, fmt, args...)            \
130         FNIC_CHECK_LOGGING(FNIC_FCS_LOGGING,                    \
131                          shost_printk(kern_level, host, fmt, ##args);)
132
133 #define FNIC_SCSI_DBG(kern_level, host, fmt, args...)           \
134         FNIC_CHECK_LOGGING(FNIC_SCSI_LOGGING,                   \
135                          shost_printk(kern_level, host, fmt, ##args);)
136
137 #define FNIC_ISR_DBG(kern_level, host, fmt, args...)            \
138         FNIC_CHECK_LOGGING(FNIC_ISR_LOGGING,                    \
139                          shost_printk(kern_level, host, fmt, ##args);)
140
141 extern const char *fnic_state_str[];
142
143 enum fnic_intx_intr_index {
144         FNIC_INTX_WQ_RQ_COPYWQ,
145         FNIC_INTX_ERR,
146         FNIC_INTX_NOTIFY,
147         FNIC_INTX_INTR_MAX,
148 };
149
150 enum fnic_msix_intr_index {
151         FNIC_MSIX_RQ,
152         FNIC_MSIX_WQ,
153         FNIC_MSIX_WQ_COPY,
154         FNIC_MSIX_ERR_NOTIFY,
155         FNIC_MSIX_INTR_MAX,
156 };
157
158 struct fnic_msix_entry {
159         int requested;
160         char devname[IFNAMSIZ];
161         irqreturn_t (*isr)(int, void *);
162         void *devid;
163 };
164
165 enum fnic_state {
166         FNIC_IN_FC_MODE = 0,
167         FNIC_IN_FC_TRANS_ETH_MODE,
168         FNIC_IN_ETH_MODE,
169         FNIC_IN_ETH_TRANS_FC_MODE,
170 };
171
172 #define FNIC_WQ_COPY_MAX 1
173 #define FNIC_WQ_MAX 1
174 #define FNIC_RQ_MAX 1
175 #define FNIC_CQ_MAX (FNIC_WQ_COPY_MAX + FNIC_WQ_MAX + FNIC_RQ_MAX)
176
177 struct mempool;
178
179 /* Per-instance private data structure */
180 struct fnic {
181         struct fc_lport *lport;
182         struct fcoe_ctlr ctlr;          /* FIP FCoE controller structure */
183         struct vnic_dev_bar bar0;
184
185         struct msix_entry msix_entry[FNIC_MSIX_INTR_MAX];
186         struct fnic_msix_entry msix[FNIC_MSIX_INTR_MAX];
187
188         struct vnic_stats *stats;
189         unsigned long stats_time;       /* time of stats update */
190         struct vnic_nic_cfg *nic_cfg;
191         char name[IFNAMSIZ];
192         struct timer_list notify_timer; /* used for MSI interrupts */
193
194         unsigned int err_intr_offset;
195         unsigned int link_intr_offset;
196
197         unsigned int wq_count;
198         unsigned int cq_count;
199
200         u32 vlan_hw_insert:1;           /* let hw insert the tag */
201         u32 in_remove:1;                /* fnic device in removal */
202         u32 stop_rx_link_events:1;      /* stop proc. rx frames, link events */
203
204         struct completion *remove_wait; /* device remove thread blocks */
205
206         atomic_t in_flight;             /* io counter */
207         u32 _reserved;                  /* fill hole */
208         unsigned long state_flags;      /* protected by host lock */
209         enum fnic_state state;
210         spinlock_t fnic_lock;
211
212         u16 vlan_id;                    /* VLAN tag including priority */
213         u8 data_src_addr[ETH_ALEN];
214         u64 fcp_input_bytes;            /* internal statistic */
215         u64 fcp_output_bytes;           /* internal statistic */
216         u32 link_down_cnt;
217         int link_status;
218
219         struct list_head list;
220         struct pci_dev *pdev;
221         struct vnic_fc_config config;
222         struct vnic_dev *vdev;
223         unsigned int raw_wq_count;
224         unsigned int wq_copy_count;
225         unsigned int rq_count;
226         int fw_ack_index[FNIC_WQ_COPY_MAX];
227         unsigned short fw_ack_recd[FNIC_WQ_COPY_MAX];
228         unsigned short wq_copy_desc_low[FNIC_WQ_COPY_MAX];
229         unsigned int intr_count;
230         u32 __iomem *legacy_pba;
231         struct fnic_host_tag *tags;
232         mempool_t *io_req_pool;
233         mempool_t *io_sgl_pool[FNIC_SGL_NUM_CACHES];
234         spinlock_t io_req_lock[FNIC_IO_LOCKS];  /* locks for scsi cmnds */
235
236         struct work_struct link_work;
237         struct work_struct frame_work;
238         struct sk_buff_head frame_queue;
239         struct sk_buff_head tx_queue;
240
241         /* copy work queue cache line section */
242         ____cacheline_aligned struct vnic_wq_copy wq_copy[FNIC_WQ_COPY_MAX];
243         /* completion queue cache line section */
244         ____cacheline_aligned struct vnic_cq cq[FNIC_CQ_MAX];
245
246         spinlock_t wq_copy_lock[FNIC_WQ_COPY_MAX];
247
248         /* work queue cache line section */
249         ____cacheline_aligned struct vnic_wq wq[FNIC_WQ_MAX];
250         spinlock_t wq_lock[FNIC_WQ_MAX];
251
252         /* receive queue cache line section */
253         ____cacheline_aligned struct vnic_rq rq[FNIC_RQ_MAX];
254
255         /* interrupt resource cache line section */
256         ____cacheline_aligned struct vnic_intr intr[FNIC_MSIX_INTR_MAX];
257 };
258
259 static inline struct fnic *fnic_from_ctlr(struct fcoe_ctlr *fip)
260 {
261         return container_of(fip, struct fnic, ctlr);
262 }
263
264 extern struct workqueue_struct *fnic_event_queue;
265 extern struct device_attribute *fnic_attrs[];
266
267 void fnic_clear_intr_mode(struct fnic *fnic);
268 int fnic_set_intr_mode(struct fnic *fnic);
269 void fnic_free_intr(struct fnic *fnic);
270 int fnic_request_intr(struct fnic *fnic);
271
272 int fnic_send(struct fc_lport *, struct fc_frame *);
273 void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf);
274 void fnic_handle_frame(struct work_struct *work);
275 void fnic_handle_link(struct work_struct *work);
276 int fnic_rq_cmpl_handler(struct fnic *fnic, int);
277 int fnic_alloc_rq_frame(struct vnic_rq *rq);
278 void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf);
279 void fnic_flush_tx(struct fnic *);
280 void fnic_eth_send(struct fcoe_ctlr *, struct sk_buff *skb);
281 void fnic_set_port_id(struct fc_lport *, u32, struct fc_frame *);
282 void fnic_update_mac(struct fc_lport *, u8 *new);
283 void fnic_update_mac_locked(struct fnic *, u8 *new);
284
285 int fnic_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
286 int fnic_abort_cmd(struct scsi_cmnd *);
287 int fnic_device_reset(struct scsi_cmnd *);
288 int fnic_host_reset(struct scsi_cmnd *);
289 int fnic_reset(struct Scsi_Host *);
290 void fnic_scsi_cleanup(struct fc_lport *);
291 void fnic_scsi_abort_io(struct fc_lport *);
292 void fnic_empty_scsi_cleanup(struct fc_lport *);
293 void fnic_exch_mgr_reset(struct fc_lport *, u32, u32);
294 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int);
295 int fnic_wq_cmpl_handler(struct fnic *fnic, int);
296 int fnic_flogi_reg_handler(struct fnic *fnic, u32);
297 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
298                                   struct fcpio_host_req *desc);
299 int fnic_fw_reset_handler(struct fnic *fnic);
300 void fnic_terminate_rport_io(struct fc_rport *);
301 const char *fnic_state_to_str(unsigned int state);
302
303 void fnic_log_q_error(struct fnic *fnic);
304 void fnic_handle_link_event(struct fnic *fnic);
305
306 static inline int
307 fnic_chk_state_flags_locked(struct fnic *fnic, unsigned long st_flags)
308 {
309         return ((fnic->state_flags & st_flags) == st_flags);
310 }
311 void __fnic_set_state_flags(struct fnic *, unsigned long, unsigned long);
312 #endif /* _FNIC_H_ */