1 /*
2  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3  * Copyright (c) 2014- QLogic Corporation.
4  * All rights reserved
5  * www.qlogic.com
6  *
7  * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License (GPL) Version 2 as
11  * published by the Free Software Foundation
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18 
19 #ifndef __BFAD_IM_H__
20 #define __BFAD_IM_H__
21 
22 #include "bfa_fcs.h"
23 
24 #define FCPI_NAME " fcpim"
25 
26 #ifndef KOBJ_NAME_LEN
27 #define KOBJ_NAME_LEN           20
28 #endif
29 
30 bfa_status_t bfad_im_module_init(void);
31 void bfad_im_module_exit(void);
32 bfa_status_t bfad_im_probe(struct bfad_s *bfad);
33 void bfad_im_probe_undo(struct bfad_s *bfad);
34 bfa_status_t bfad_im_port_new(struct bfad_s *bfad, struct bfad_port_s *port);
35 void bfad_im_port_delete(struct bfad_s *bfad, struct bfad_port_s *port);
36 void bfad_im_port_clean(struct bfad_im_port_s *im_port);
37 int  bfad_im_scsi_host_alloc(struct bfad_s *bfad,
38 		struct bfad_im_port_s *im_port, struct device *dev);
39 void bfad_im_scsi_host_free(struct bfad_s *bfad,
40 				struct bfad_im_port_s *im_port);
41 u32 bfad_im_supported_speeds(struct bfa_s *bfa);
42 
43 #define MAX_FCP_TARGET 1024
44 #define MAX_FCP_LUN 16384
45 #define BFAD_TARGET_RESET_TMO 60
46 #define BFAD_LUN_RESET_TMO 60
47 #define BFA_QUEUE_FULL_RAMP_UP_TIME 120
48 
49 /*
50  * itnim flags
51  */
52 #define IO_DONE_BIT			0
53 
54 struct bfad_itnim_data_s {
55 	struct bfad_itnim_s *itnim;
56 };
57 
58 struct bfad_im_port_s {
59 	struct bfad_s         *bfad;
60 	struct bfad_port_s    *port;
61 	struct work_struct port_delete_work;
62 	int             idr_id;
63 	u16        cur_scsi_id;
64 	u16	flags;
65 	struct list_head binding_list;
66 	struct Scsi_Host *shost;
67 	struct list_head itnim_mapped_list;
68 	struct fc_vport *fc_vport;
69 };
70 
71 struct bfad_im_port_pointer {
72 	struct bfad_im_port_s *p;
73 };
74 
bfad_get_im_port(struct Scsi_Host * host)75 static inline struct bfad_im_port_s *bfad_get_im_port(struct Scsi_Host *host)
76 {
77 	struct bfad_im_port_pointer *im_portp = shost_priv(host);
78 	return im_portp->p;
79 }
80 
81 enum bfad_itnim_state {
82 	ITNIM_STATE_NONE,
83 	ITNIM_STATE_ONLINE,
84 	ITNIM_STATE_OFFLINE_PENDING,
85 	ITNIM_STATE_OFFLINE,
86 	ITNIM_STATE_TIMEOUT,
87 	ITNIM_STATE_FREE,
88 };
89 
90 /*
91  * Per itnim data structure
92  */
93 struct bfad_itnim_s {
94 	struct list_head list_entry;
95 	struct bfa_fcs_itnim_s fcs_itnim;
96 	struct work_struct itnim_work;
97 	u32        flags;
98 	enum bfad_itnim_state state;
99 	struct bfad_im_s *im;
100 	struct bfad_im_port_s *im_port;
101 	struct bfad_rport_s *drv_rport;
102 	struct fc_rport *fc_rport;
103 	struct bfa_itnim_s *bfa_itnim;
104 	u16        scsi_tgt_id;
105 	u16	   channel;
106 	u16        queue_work;
107 	unsigned long	last_ramp_up_time;
108 	unsigned long	last_queue_full_time;
109 };
110 
111 enum bfad_binding_type {
112 	FCP_PWWN_BINDING = 0x1,
113 	FCP_NWWN_BINDING = 0x2,
114 	FCP_FCID_BINDING = 0x3,
115 };
116 
117 struct bfad_fcp_binding {
118 	struct list_head list_entry;
119 	enum bfad_binding_type binding_type;
120 	u16        scsi_target_id;
121 	u32        fc_id;
122 	wwn_t           nwwn;
123 	wwn_t           pwwn;
124 };
125 
126 struct bfad_im_s {
127 	struct bfad_s         *bfad;
128 	struct workqueue_struct *drv_workq;
129 	char            drv_workq_name[KOBJ_NAME_LEN];
130 	struct work_struct	aen_im_notify_work;
131 };
132 
133 #define bfad_get_aen_entry(_drv, _entry) do {				\
134 	unsigned long	_flags;						\
135 	spin_lock_irqsave(&(_drv)->bfad_aen_spinlock, _flags);		\
136 	bfa_q_deq(&(_drv)->free_aen_q, &(_entry));			\
137 	if (_entry)							\
138 		list_add_tail(&(_entry)->qe, &(_drv)->active_aen_q);	\
139 	spin_unlock_irqrestore(&(_drv)->bfad_aen_spinlock, _flags);	\
140 } while (0)
141 
142 /* post fc_host vendor event */
bfad_im_post_vendor_event(struct bfa_aen_entry_s * entry,struct bfad_s * drv,int cnt,enum bfa_aen_category cat,enum bfa_ioc_aen_event evt)143 static inline void bfad_im_post_vendor_event(struct bfa_aen_entry_s *entry,
144 					     struct bfad_s *drv, int cnt,
145 					     enum bfa_aen_category cat,
146 					     enum bfa_ioc_aen_event evt)
147 {
148 	struct timespec64 ts;
149 
150 	ktime_get_real_ts64(&ts);
151 	/*
152 	 * 'unsigned long aen_tv_sec' overflows in y2106 on 32-bit
153 	 * architectures, or in 2038 if user space interprets it
154 	 * as 'signed'.
155 	 */
156 	entry->aen_tv_sec = ts.tv_sec;
157 	entry->aen_tv_usec = ts.tv_nsec / NSEC_PER_USEC;
158 	entry->bfad_num = drv->inst_no;
159 	entry->seq_num = cnt;
160 	entry->aen_category = cat;
161 	entry->aen_type = evt;
162 	if (drv->bfad_flags & BFAD_FC4_PROBE_DONE)
163 		queue_work(drv->im->drv_workq, &drv->im->aen_im_notify_work);
164 }
165 
166 struct Scsi_Host *bfad_scsi_host_alloc(struct bfad_im_port_s *im_port,
167 				struct bfad_s *);
168 bfa_status_t bfad_thread_workq(struct bfad_s *bfad);
169 void bfad_destroy_workq(struct bfad_im_s *im);
170 void bfad_fc_host_init(struct bfad_im_port_s *im_port);
171 void bfad_scsi_host_free(struct bfad_s *bfad,
172 				 struct bfad_im_port_s *im_port);
173 void bfad_ramp_up_qdepth(struct bfad_itnim_s *itnim,
174 				 struct scsi_device *sdev);
175 void bfad_handle_qfull(struct bfad_itnim_s *itnim, struct scsi_device *sdev);
176 struct bfad_itnim_s *bfad_get_itnim(struct bfad_im_port_s *im_port, int id);
177 
178 extern struct scsi_host_template bfad_im_scsi_host_template;
179 extern struct scsi_host_template bfad_im_vport_template;
180 extern struct fc_function_template bfad_im_fc_function_template;
181 extern struct fc_function_template bfad_im_vport_fc_function_template;
182 extern struct scsi_transport_template *bfad_im_scsi_transport_template;
183 extern struct scsi_transport_template *bfad_im_scsi_vport_transport_template;
184 
185 extern struct device_attribute *bfad_im_host_attrs[];
186 extern struct device_attribute *bfad_im_vport_attrs[];
187 
188 irqreturn_t bfad_intx(int irq, void *dev_id);
189 
190 int bfad_im_bsg_request(struct bsg_job *job);
191 int bfad_im_bsg_timeout(struct bsg_job *job);
192 
193 /*
194  * Macro to set the SCSI device sdev_bflags - sdev_bflags are used by the
195  * SCSI mid-layer to choose LUN Scanning mode REPORT_LUNS vs. Sequential Scan
196  *
197  * Internally iterate's over all the ITNIM's part of the im_port & set's the
198  * sdev_bflags for the scsi_device associated with LUN #0.
199  */
200 #define bfad_reset_sdev_bflags(__im_port, __lunmask_cfg) do {		\
201 	struct scsi_device *__sdev = NULL;				\
202 	struct bfad_itnim_s *__itnim = NULL;				\
203 	u32 scan_flags = BLIST_NOREPORTLUN | BLIST_SPARSELUN;		\
204 	list_for_each_entry(__itnim, &((__im_port)->itnim_mapped_list),	\
205 			    list_entry) {				\
206 		__sdev = scsi_device_lookup((__im_port)->shost,		\
207 					    __itnim->channel,		\
208 					    __itnim->scsi_tgt_id, 0);	\
209 		if (__sdev) {						\
210 			if ((__lunmask_cfg) == BFA_TRUE)		\
211 				__sdev->sdev_bflags |= scan_flags;	\
212 			else						\
213 				__sdev->sdev_bflags &= ~scan_flags;	\
214 			scsi_device_put(__sdev);			\
215 		}							\
216 	}								\
217 } while (0)
218 
219 #endif
220