1 /*
2  * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17 
18 #ifndef __BFA_FCPIM_H__
19 #define __BFA_FCPIM_H__
20 
21 #include "bfa.h"
22 #include "bfa_svc.h"
23 #include "bfi_ms.h"
24 #include "bfa_defs_svc.h"
25 #include "bfa_cs.h"
26 
27 
28 #define BFA_ITNIM_MIN   32
29 #define BFA_ITNIM_MAX   1024
30 
31 #define BFA_IOIM_MIN	8
32 #define BFA_IOIM_MAX	2000
33 
34 #define BFA_TSKIM_MIN   4
35 #define BFA_TSKIM_MAX   512
36 #define BFA_FCPIM_PATHTOV_DEF	(30 * 1000)	/* in millisecs */
37 #define BFA_FCPIM_PATHTOV_MAX	(90 * 1000)	/* in millisecs */
38 
39 
40 #define bfa_itnim_ioprofile_update(__itnim, __index)			\
41 	(__itnim->ioprofile.iocomps[__index]++)
42 
43 #define BFA_IOIM_RETRY_TAG_OFFSET 11
44 #define BFA_IOIM_IOTAG_MASK 0x07ff /* 2K IOs */
45 #define BFA_IOIM_RETRY_MAX 7
46 
47 /* Buckets are are 512 bytes to 2MB */
48 static inline u32
bfa_ioim_get_index(u32 n)49 bfa_ioim_get_index(u32 n) {
50 	int pos = 0;
51 	if (n >= (1UL)<<22)
52 		return BFA_IOBUCKET_MAX - 1;
53 	n >>= 8;
54 	if (n >= (1UL)<<16)
55 		n >>= 16; pos += 16;
56 	if (n >= 1 << 8)
57 		n >>= 8; pos += 8;
58 	if (n >= 1 << 4)
59 		n >>= 4; pos += 4;
60 	if (n >= 1 << 2)
61 		n >>= 2; pos += 2;
62 	if (n >= 1 << 1)
63 		pos += 1;
64 
65 	return (n == 0) ? (0) : pos;
66 }
67 
68 /*
69  * forward declarations
70  */
71 struct bfa_ioim_s;
72 struct bfa_tskim_s;
73 struct bfad_ioim_s;
74 struct bfad_tskim_s;
75 
76 typedef void    (*bfa_fcpim_profile_t) (struct bfa_ioim_s *ioim);
77 
78 struct bfa_fcpim_mod_s {
79 	struct bfa_s		*bfa;
80 	struct bfa_itnim_s	*itnim_arr;
81 	struct bfa_ioim_s	*ioim_arr;
82 	struct bfa_ioim_sp_s	*ioim_sp_arr;
83 	struct bfa_tskim_s	*tskim_arr;
84 	struct bfa_dma_s	snsbase;
85 	int			num_itnims;
86 	int			num_ioim_reqs;
87 	int			num_tskim_reqs;
88 	u32			path_tov;
89 	u16			q_depth;
90 	u8			reqq;		/*  Request queue to be used */
91 	u8			rsvd;
92 	struct list_head	itnim_q;	/*  queue of active itnim */
93 	struct list_head	ioim_free_q;	/*  free IO resources	*/
94 	struct list_head	ioim_resfree_q; /*  IOs waiting for f/w */
95 	struct list_head	ioim_comp_q;	/*  IO global comp Q	*/
96 	struct list_head	tskim_free_q;
97 	u32			ios_active;	/*  current active IOs	*/
98 	u32			delay_comp;
99 	struct bfa_fcpim_del_itn_stats_s del_itn_stats;
100 	bfa_boolean_t		ioredirect;
101 	bfa_boolean_t		io_profile;
102 	u32			io_profile_start_time;
103 	bfa_fcpim_profile_t     profile_comp;
104 	bfa_fcpim_profile_t     profile_start;
105 };
106 
107 /*
108  * BFA IO (initiator mode)
109  */
110 struct bfa_ioim_s {
111 	struct list_head	qe;		/*  queue elememt	*/
112 	bfa_sm_t		sm;		/*  BFA ioim state machine */
113 	struct bfa_s		*bfa;		/*  BFA module	*/
114 	struct bfa_fcpim_mod_s	*fcpim;		/*  parent fcpim module */
115 	struct bfa_itnim_s	*itnim;		/*  i-t-n nexus for this IO  */
116 	struct bfad_ioim_s	*dio;		/*  driver IO handle	*/
117 	u16			iotag;		/*  FWI IO tag	*/
118 	u16			abort_tag;	/*  unqiue abort request tag */
119 	u16			nsges;		/*  number of SG elements */
120 	u16			nsgpgs;		/*  number of SG pages	*/
121 	struct bfa_sgpg_s	*sgpg;		/*  first SG page	*/
122 	struct list_head	sgpg_q;		/*  allocated SG pages	*/
123 	struct bfa_cb_qe_s	hcb_qe;		/*  bfa callback qelem	*/
124 	bfa_cb_cbfn_t		io_cbfn;	/*  IO completion handler */
125 	struct bfa_ioim_sp_s	*iosp;		/*  slow-path IO handling */
126 	u8			reqq;		/*  Request queue for I/O */
127 	u64			start_time;	/*  IO's Profile start val */
128 };
129 
130 struct bfa_ioim_sp_s {
131 	struct bfi_msg_s	comp_rspmsg;	/*  IO comp f/w response */
132 	u8			*snsinfo;	/*  sense info for this IO   */
133 	struct bfa_sgpg_wqe_s	sgpg_wqe;	/*  waitq elem for sgpg	*/
134 	struct bfa_reqq_wait_s	reqq_wait;	/*  to wait for room in reqq */
135 	bfa_boolean_t		abort_explicit;	/*  aborted by OS	*/
136 	struct bfa_tskim_s	*tskim;		/*  Relevant TM cmd	*/
137 };
138 
139 /*
140  * BFA Task management command (initiator mode)
141  */
142 struct bfa_tskim_s {
143 	struct list_head	qe;
144 	bfa_sm_t		sm;
145 	struct bfa_s		*bfa;	/*  BFA module  */
146 	struct bfa_fcpim_mod_s  *fcpim;	/*  parent fcpim module	*/
147 	struct bfa_itnim_s	*itnim;	/*  i-t-n nexus for this IO  */
148 	struct bfad_tskim_s	*dtsk;  /*  driver task mgmt cmnd	*/
149 	bfa_boolean_t		notify;	/*  notify itnim on TM comp  */
150 	struct scsi_lun		lun;	/*  lun if applicable	*/
151 	enum fcp_tm_cmnd	tm_cmnd; /*  task management command  */
152 	u16			tsk_tag; /*  FWI IO tag	*/
153 	u8			tsecs;	/*  timeout in seconds	*/
154 	struct bfa_reqq_wait_s  reqq_wait;   /*  to wait for room in reqq */
155 	struct list_head	io_q;	/*  queue of affected IOs	*/
156 	struct bfa_wc_s		wc;	/*  waiting counter	*/
157 	struct bfa_cb_qe_s	hcb_qe;	/*  bfa callback qelem	*/
158 	enum bfi_tskim_status   tsk_status;  /*  TM status	*/
159 };
160 
161 /*
162  * BFA i-t-n (initiator mode)
163  */
164 struct bfa_itnim_s {
165 	struct list_head	qe;	/*  queue element	*/
166 	bfa_sm_t		sm;	/*  i-t-n im BFA state machine  */
167 	struct bfa_s		*bfa;	/*  bfa instance	*/
168 	struct bfa_rport_s	*rport;	/*  bfa rport	*/
169 	void			*ditn;	/*  driver i-t-n structure	*/
170 	struct bfi_mhdr_s	mhdr;	/*  pre-built mhdr	*/
171 	u8			msg_no;	/*  itnim/rport firmware handle */
172 	u8			reqq;	/*  CQ for requests	*/
173 	struct bfa_cb_qe_s	hcb_qe;	/*  bfa callback qelem	*/
174 	struct list_head pending_q;	/*  queue of pending IO requests */
175 	struct list_head io_q;		/*  queue of active IO requests */
176 	struct list_head io_cleanup_q;	/*  IO being cleaned up	*/
177 	struct list_head tsk_q;		/*  queue of active TM commands */
178 	struct list_head  delay_comp_q; /*  queue of failed inflight cmds */
179 	bfa_boolean_t   seq_rec;	/*  SQER supported	*/
180 	bfa_boolean_t   is_online;	/*  itnim is ONLINE for IO	*/
181 	bfa_boolean_t   iotov_active;	/*  IO TOV timer is active	 */
182 	struct bfa_wc_s	wc;		/*  waiting counter	*/
183 	struct bfa_timer_s timer;	/*  pending IO TOV	 */
184 	struct bfa_reqq_wait_s reqq_wait; /*  to wait for room in reqq */
185 	struct bfa_fcpim_mod_s *fcpim;	/*  fcpim module	*/
186 	struct bfa_itnim_iostats_s	stats;
187 	struct bfa_itnim_ioprofile_s  ioprofile;
188 };
189 
190 #define bfa_itnim_is_online(_itnim) ((_itnim)->is_online)
191 #define BFA_FCPIM_MOD(_hal) (&(_hal)->modules.fcpim_mod)
192 #define BFA_IOIM_TAG_2_ID(_iotag)	((_iotag) & BFA_IOIM_IOTAG_MASK)
193 #define BFA_IOIM_FROM_TAG(_fcpim, _iotag)	\
194 	(&fcpim->ioim_arr[(_iotag & BFA_IOIM_IOTAG_MASK)])
195 #define BFA_TSKIM_FROM_TAG(_fcpim, _tmtag)	\
196 	(&fcpim->tskim_arr[_tmtag & (fcpim->num_tskim_reqs - 1)])
197 
198 #define bfa_io_profile_start_time(_bfa)	\
199 	(_bfa->modules.fcpim_mod.io_profile_start_time)
200 #define bfa_fcpim_get_io_profile(_bfa)	\
201 	(_bfa->modules.fcpim_mod.io_profile)
202 #define bfa_ioim_update_iotag(__ioim) do {				\
203 	uint16_t k = (__ioim)->iotag >> BFA_IOIM_RETRY_TAG_OFFSET;	\
204 	k++; (__ioim)->iotag &= BFA_IOIM_IOTAG_MASK;			\
205 	(__ioim)->iotag |= k << BFA_IOIM_RETRY_TAG_OFFSET;		\
206 } while (0)
207 
208 static inline bfa_boolean_t
bfa_ioim_maxretry_reached(struct bfa_ioim_s * ioim)209 bfa_ioim_maxretry_reached(struct bfa_ioim_s *ioim)
210 {
211 	uint16_t k = ioim->iotag >> BFA_IOIM_RETRY_TAG_OFFSET;
212 	if (k < BFA_IOIM_RETRY_MAX)
213 		return BFA_FALSE;
214 	return BFA_TRUE;
215 }
216 
217 /*
218  * function prototypes
219  */
220 void	bfa_ioim_attach(struct bfa_fcpim_mod_s *fcpim,
221 					struct bfa_meminfo_s *minfo);
222 void	bfa_ioim_isr(struct bfa_s *bfa, struct bfi_msg_s *msg);
223 void	bfa_ioim_good_comp_isr(struct bfa_s *bfa,
224 					struct bfi_msg_s *msg);
225 void	bfa_ioim_cleanup(struct bfa_ioim_s *ioim);
226 void	bfa_ioim_cleanup_tm(struct bfa_ioim_s *ioim,
227 					struct bfa_tskim_s *tskim);
228 void	bfa_ioim_iocdisable(struct bfa_ioim_s *ioim);
229 void	bfa_ioim_tov(struct bfa_ioim_s *ioim);
230 
231 void	bfa_tskim_attach(struct bfa_fcpim_mod_s *fcpim,
232 					struct bfa_meminfo_s *minfo);
233 void	bfa_tskim_isr(struct bfa_s *bfa, struct bfi_msg_s *msg);
234 void	bfa_tskim_iodone(struct bfa_tskim_s *tskim);
235 void	bfa_tskim_iocdisable(struct bfa_tskim_s *tskim);
236 void	bfa_tskim_cleanup(struct bfa_tskim_s *tskim);
237 
238 void	bfa_itnim_meminfo(struct bfa_iocfc_cfg_s *cfg, u32 *km_len,
239 					u32 *dm_len);
240 void	bfa_itnim_attach(struct bfa_fcpim_mod_s *fcpim,
241 					struct bfa_meminfo_s *minfo);
242 void	bfa_itnim_detach(struct bfa_fcpim_mod_s *fcpim);
243 void	bfa_itnim_iocdisable(struct bfa_itnim_s *itnim);
244 void	bfa_itnim_isr(struct bfa_s *bfa, struct bfi_msg_s *msg);
245 void	bfa_itnim_iodone(struct bfa_itnim_s *itnim);
246 void	bfa_itnim_tskdone(struct bfa_itnim_s *itnim);
247 bfa_boolean_t   bfa_itnim_hold_io(struct bfa_itnim_s *itnim);
248 
249 /*
250  * bfa fcpim module API functions
251  */
252 void	bfa_fcpim_path_tov_set(struct bfa_s *bfa, u16 path_tov);
253 u16	bfa_fcpim_path_tov_get(struct bfa_s *bfa);
254 u16	bfa_fcpim_qdepth_get(struct bfa_s *bfa);
255 
256 #define bfa_fcpim_ioredirect_enabled(__bfa)				\
257 	(((struct bfa_fcpim_mod_s *)(BFA_FCPIM_MOD(__bfa)))->ioredirect)
258 
259 #define bfa_fcpim_get_next_reqq(__bfa, __qid)				\
260 {									\
261 	struct bfa_fcpim_mod_s *__fcpim = BFA_FCPIM_MOD(__bfa);      \
262 	__fcpim->reqq++;						\
263 	__fcpim->reqq &= (BFI_IOC_MAX_CQS - 1);      \
264 	*(__qid) = __fcpim->reqq;					\
265 }
266 
267 #define bfa_iocfc_map_msg_to_qid(__msg, __qid)				\
268 	*(__qid) = (u8)((__msg) & (BFI_IOC_MAX_CQS - 1));
269 /*
270  * bfa itnim API functions
271  */
272 struct bfa_itnim_s *bfa_itnim_create(struct bfa_s *bfa,
273 		struct bfa_rport_s *rport, void *itnim);
274 void bfa_itnim_delete(struct bfa_itnim_s *itnim);
275 void bfa_itnim_online(struct bfa_itnim_s *itnim, bfa_boolean_t seq_rec);
276 void bfa_itnim_offline(struct bfa_itnim_s *itnim);
277 void bfa_itnim_clear_stats(struct bfa_itnim_s *itnim);
278 bfa_status_t bfa_itnim_get_ioprofile(struct bfa_itnim_s *itnim,
279 			struct bfa_itnim_ioprofile_s *ioprofile);
280 
281 #define bfa_itnim_get_reqq(__ioim) (((struct bfa_ioim_s *)__ioim)->itnim->reqq)
282 
283 /*
284  * BFA completion callback for bfa_itnim_online().
285  */
286 void	bfa_cb_itnim_online(void *itnim);
287 
288 /*
289  * BFA completion callback for bfa_itnim_offline().
290  */
291 void	bfa_cb_itnim_offline(void *itnim);
292 void	bfa_cb_itnim_tov_begin(void *itnim);
293 void	bfa_cb_itnim_tov(void *itnim);
294 
295 /*
296  * BFA notification to FCS/driver for second level error recovery.
297  * Atleast one I/O request has timedout and target is unresponsive to
298  * repeated abort requests. Second level error recovery should be initiated
299  * by starting implicit logout and recovery procedures.
300  */
301 void	bfa_cb_itnim_sler(void *itnim);
302 
303 /*
304  * bfa ioim API functions
305  */
306 struct bfa_ioim_s	*bfa_ioim_alloc(struct bfa_s *bfa,
307 					struct bfad_ioim_s *dio,
308 					struct bfa_itnim_s *itnim,
309 					u16 nsgles);
310 
311 void		bfa_ioim_free(struct bfa_ioim_s *ioim);
312 void		bfa_ioim_start(struct bfa_ioim_s *ioim);
313 bfa_status_t	bfa_ioim_abort(struct bfa_ioim_s *ioim);
314 void		bfa_ioim_delayed_comp(struct bfa_ioim_s *ioim,
315 				      bfa_boolean_t iotov);
316 /*
317  * I/O completion notification.
318  *
319  * @param[in]		dio			driver IO structure
320  * @param[in]		io_status		IO completion status
321  * @param[in]		scsi_status		SCSI status returned by target
322  * @param[in]		sns_len			SCSI sense length, 0 if none
323  * @param[in]		sns_info		SCSI sense data, if any
324  * @param[in]		residue			Residual length
325  *
326  * @return None
327  */
328 void bfa_cb_ioim_done(void *bfad, struct bfad_ioim_s *dio,
329 			enum bfi_ioim_status io_status,
330 			u8 scsi_status, int sns_len,
331 			u8 *sns_info, s32 residue);
332 
333 /*
334  * I/O good completion notification.
335  */
336 void bfa_cb_ioim_good_comp(void *bfad, struct bfad_ioim_s *dio);
337 
338 /*
339  * I/O abort completion notification
340  */
341 void bfa_cb_ioim_abort(void *bfad, struct bfad_ioim_s *dio);
342 
343 /*
344  * bfa tskim API functions
345  */
346 struct bfa_tskim_s *bfa_tskim_alloc(struct bfa_s *bfa,
347 			struct bfad_tskim_s *dtsk);
348 void bfa_tskim_free(struct bfa_tskim_s *tskim);
349 void bfa_tskim_start(struct bfa_tskim_s *tskim,
350 			struct bfa_itnim_s *itnim, struct scsi_lun lun,
351 			enum fcp_tm_cmnd tm, u8 t_secs);
352 void bfa_cb_tskim_done(void *bfad, struct bfad_tskim_s *dtsk,
353 			enum bfi_tskim_status tsk_status);
354 
355 #endif /* __BFA_FCPIM_H__ */
356