Blame view

drivers/scsi/qla4xxx/ql4_os.c 148 KB
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1
2
  /*
   * QLogic iSCSI HBA Driver
7d01d0698   Vikas Chaudhary   [SCSI] qla4xxx: U...
3
   * Copyright (c)  2003-2010 QLogic Corporation
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4
5
6
7
   *
   * See LICENSE.qla4xxx for copyright and licensing details.
   */
  #include <linux/moduleparam.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
8
  #include <linux/slab.h>
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
9
10
  #include <linux/blkdev.h>
  #include <linux/iscsi_boot_sysfs.h>
13483730a   Mike Christie   [SCSI] qla4xxx: f...
11
  #include <linux/inet.h>
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
12
13
14
15
16
  
  #include <scsi/scsi_tcq.h>
  #include <scsi/scsicam.h>
  
  #include "ql4_def.h"
bee4fe8e6   David C Somayajulu   [SCSI] qla4xxx: q...
17
18
19
20
  #include "ql4_version.h"
  #include "ql4_glbl.h"
  #include "ql4_dbg.h"
  #include "ql4_inline.h"
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
21
22
23
24
  
  /*
   * Driver version
   */
479754777   Adrian Bunk   [SCSI] qla4xxx: p...
25
  static char qla4xxx_version_str[40];
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
26
27
28
29
  
  /*
   * SRB allocation cache
   */
e18b890bb   Christoph Lameter   [PATCH] slab: rem...
30
  static struct kmem_cache *srb_cachep;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
31
32
33
34
  
  /*
   * Module parameter information and variables
   */
13483730a   Mike Christie   [SCSI] qla4xxx: f...
35
36
37
38
39
40
41
42
  int ql4xdisablesysfsboot = 1;
  module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
  MODULE_PARM_DESC(ql4xdisablesysfsboot,
  		"Set to disable exporting boot targets to sysfs
  "
  		" 0 - Export boot targets
  "
  		" 1 - Do not export boot targets (Default)");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
43
  int ql4xdontresethba = 0;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
44
  module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
45
  MODULE_PARM_DESC(ql4xdontresethba,
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
46
47
48
49
50
  		"Don't reset the HBA for driver recovery 
  "
  		" 0 - It will reset HBA (Default)
  "
  		" 1 - It will NOT reset HBA");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
51

11010fecd   Andrew Vasquez   [SCSI] Maintain m...
52
  int ql4xextended_error_logging = 0; /* 0 = off, 1 = log errors */
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
53
  module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
11010fecd   Andrew Vasquez   [SCSI] Maintain m...
54
  MODULE_PARM_DESC(ql4xextended_error_logging,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
55
56
  		 "Option to enable extended error logging, "
  		 "Default is 0 - no logging, 1 - debug logging");
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
57
58
59
60
61
62
63
64
65
66
  int ql4xenablemsix = 1;
  module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
  MODULE_PARM_DESC(ql4xenablemsix,
  		"Set to enable MSI or MSI-X interrupt mechanism.
  "
  		" 0 = enable INTx interrupt mechanism.
  "
  		" 1 = enable MSI-X interrupt mechanism (Default).
  "
  		" 2 = enable MSI interrupt mechanism.");
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
67

d510d965e   Mike Christie   [SCSI] qla4xxx: f...
68
  #define QL4_DEF_QDEPTH 32
8bb4033d2   Vikas Chaudhary   [SCSI] qla4xxx: A...
69
70
71
72
73
74
  static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
  module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
  MODULE_PARM_DESC(ql4xmaxqdepth,
  		"Maximum queue depth to report for target devices.
  "
  		" Default: 32.");
d510d965e   Mike Christie   [SCSI] qla4xxx: f...
75

3038727c8   Vikas Chaudhary   [SCSI] qla4xxx: a...
76
77
78
79
80
  static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
  module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
  MODULE_PARM_DESC(ql4xsess_recovery_tmo,
  		"Target Session Recovery Timeout.
  "
13483730a   Mike Christie   [SCSI] qla4xxx: f...
81
  		" Default: 120 sec.");
3038727c8   Vikas Chaudhary   [SCSI] qla4xxx: a...
82

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
83
  static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
84
85
86
  /*
   * SCSI host template entry points
   */
479754777   Adrian Bunk   [SCSI] qla4xxx: p...
87
  static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
88
89
90
91
  
  /*
   * iSCSI template entry points
   */
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
92
93
  static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
  				  enum iscsi_param param, char *buf);
aa1e93a2f   Mike Christie   [SCSI] qla4xxx: e...
94
95
  static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  				  enum iscsi_host_param param, char *buf);
00c31889f   Mike Christie   [SCSI] qla4xxx: f...
96
97
  static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
  				   uint32_t len);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
98
99
100
  static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  				   enum iscsi_param_type param_type,
  				   int param, char *buf);
5c656af7e   Mike Christie   [SCSI] qla4xxx: a...
101
  static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
  						 struct sockaddr *dst_addr,
  						 int non_blocking);
  static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
  static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
  static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  				enum iscsi_param param, char *buf);
  static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
  static struct iscsi_cls_conn *
  qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
  static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  			     struct iscsi_cls_conn *cls_conn,
  			     uint64_t transport_fd, int is_leading);
  static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
  static struct iscsi_cls_session *
  qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  			uint16_t qdepth, uint32_t initial_cmdsn);
  static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
  static void qla4xxx_task_work(struct work_struct *wdata);
  static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
  static int qla4xxx_task_xmit(struct iscsi_task *);
  static void qla4xxx_task_cleanup(struct iscsi_task *);
  static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
  static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  				   struct iscsi_stats *stats);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
127
128
129
  /*
   * SCSI host template entry points
   */
f281233d3   Jeff Garzik   SCSI host lock pu...
130
  static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
131
  static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
132
  static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
133
  static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
134
135
136
137
  static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
  static int qla4xxx_slave_alloc(struct scsi_device *device);
  static int qla4xxx_slave_configure(struct scsi_device *device);
  static void qla4xxx_slave_destroy(struct scsi_device *sdev);
587a1f165   Al Viro   switch ->is_visib...
138
  static umode_t ql4_attr_is_visible(int param_type, int param);
95d31262b   Vikas Chaudhary   [SCSI] qla4xxx: A...
139
  static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
140

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
141
142
  static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
      QLA82XX_LEGACY_INTR_CONFIG;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
143
144
145
146
147
  static struct scsi_host_template qla4xxx_driver_template = {
  	.module			= THIS_MODULE,
  	.name			= DRIVER_NAME,
  	.proc_name		= DRIVER_NAME,
  	.queuecommand		= qla4xxx_queuecommand,
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
148
  	.eh_abort_handler	= qla4xxx_eh_abort,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
149
  	.eh_device_reset_handler = qla4xxx_eh_device_reset,
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
150
  	.eh_target_reset_handler = qla4xxx_eh_target_reset,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
151
  	.eh_host_reset_handler	= qla4xxx_eh_host_reset,
5c656af7e   Mike Christie   [SCSI] qla4xxx: a...
152
  	.eh_timed_out		= qla4xxx_eh_cmd_timed_out,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
153
154
155
156
157
158
159
160
161
162
163
  
  	.slave_configure	= qla4xxx_slave_configure,
  	.slave_alloc		= qla4xxx_slave_alloc,
  	.slave_destroy		= qla4xxx_slave_destroy,
  
  	.this_id		= -1,
  	.cmd_per_lun		= 3,
  	.use_clustering		= ENABLE_CLUSTERING,
  	.sg_tablesize		= SG_ALL,
  
  	.max_sectors		= 0xFFFF,
7ad633c06   Harish Zunjarrao   [SCSI] qla4xxx: A...
164
  	.shost_attrs		= qla4xxx_host_attrs,
95d31262b   Vikas Chaudhary   [SCSI] qla4xxx: A...
165
  	.host_reset		= qla4xxx_host_reset,
a355943ca   Vikas Chaudhary   [SCSI] qla4xxx: a...
166
  	.vendor_id		= SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
167
168
169
170
171
  };
  
  static struct iscsi_transport qla4xxx_iscsi_transport = {
  	.owner			= THIS_MODULE,
  	.name			= DRIVER_NAME,
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
172
173
174
175
  	.caps			= CAP_TEXT_NEGO |
  				  CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
  				  CAP_DATADGST | CAP_LOGIN_OFFLOAD |
  				  CAP_MULTI_R2T,
3128c6c73   Mike Christie   [SCSI] iscsi cls:...
176
  	.attr_is_visible	= ql4_attr_is_visible,
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
177
178
179
180
181
182
183
184
  	.create_session         = qla4xxx_session_create,
  	.destroy_session        = qla4xxx_session_destroy,
  	.start_conn             = qla4xxx_conn_start,
  	.create_conn            = qla4xxx_conn_create,
  	.bind_conn              = qla4xxx_conn_bind,
  	.stop_conn              = iscsi_conn_stop,
  	.destroy_conn           = qla4xxx_conn_destroy,
  	.set_param              = iscsi_set_param,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
185
  	.get_conn_param		= qla4xxx_conn_get_param,
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
186
187
188
189
190
191
192
193
194
195
  	.get_session_param	= iscsi_session_get_param,
  	.get_ep_param           = qla4xxx_get_ep_param,
  	.ep_connect		= qla4xxx_ep_connect,
  	.ep_poll		= qla4xxx_ep_poll,
  	.ep_disconnect		= qla4xxx_ep_disconnect,
  	.get_stats		= qla4xxx_conn_get_stats,
  	.send_pdu		= iscsi_conn_send_pdu,
  	.xmit_task		= qla4xxx_task_xmit,
  	.cleanup_task		= qla4xxx_task_cleanup,
  	.alloc_pdu		= qla4xxx_alloc_pdu,
aa1e93a2f   Mike Christie   [SCSI] qla4xxx: e...
196
  	.get_host_param		= qla4xxx_host_get_param,
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
197
  	.set_iface_param	= qla4xxx_iface_set_param,
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
198
  	.get_iface_param	= qla4xxx_get_iface_param,
a355943ca   Vikas Chaudhary   [SCSI] qla4xxx: a...
199
  	.bsg_request		= qla4xxx_bsg_request,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
200
201
202
  };
  
  static struct scsi_transport_template *qla4xxx_scsi_transport;
587a1f165   Al Viro   switch ->is_visib...
203
  static umode_t ql4_attr_is_visible(int param_type, int param)
3128c6c73   Mike Christie   [SCSI] iscsi cls:...
204
205
  {
  	switch (param_type) {
f27fb2ef7   Mike Christie   [SCSI] iscsi clas...
206
207
208
209
210
211
212
213
214
  	case ISCSI_HOST_PARAM:
  		switch (param) {
  		case ISCSI_HOST_PARAM_HWADDRESS:
  		case ISCSI_HOST_PARAM_IPADDRESS:
  		case ISCSI_HOST_PARAM_INITIATOR_NAME:
  			return S_IRUGO;
  		default:
  			return 0;
  		}
3128c6c73   Mike Christie   [SCSI] iscsi cls:...
215
216
  	case ISCSI_PARAM:
  		switch (param) {
590134fa7   Mike Christie   [SCSI] qla4xxx: e...
217
218
  		case ISCSI_PARAM_PERSISTENT_ADDRESS:
  		case ISCSI_PARAM_PERSISTENT_PORT:
3128c6c73   Mike Christie   [SCSI] iscsi cls:...
219
220
  		case ISCSI_PARAM_CONN_ADDRESS:
  		case ISCSI_PARAM_CONN_PORT:
1d063c172   Mike Christie   [SCSI] iscsi clas...
221
222
223
  		case ISCSI_PARAM_TARGET_NAME:
  		case ISCSI_PARAM_TPGT:
  		case ISCSI_PARAM_TARGET_ALIAS:
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
224
225
226
227
228
  		case ISCSI_PARAM_MAX_BURST:
  		case ISCSI_PARAM_MAX_R2T:
  		case ISCSI_PARAM_FIRST_BURST:
  		case ISCSI_PARAM_MAX_RECV_DLENGTH:
  		case ISCSI_PARAM_MAX_XMIT_DLENGTH:
de37920b8   Mike Christie   [SCSI] qla4xxx: e...
229
  		case ISCSI_PARAM_IFACE_NAME:
3128c6c73   Mike Christie   [SCSI] iscsi cls:...
230
231
232
233
  			return S_IRUGO;
  		default:
  			return 0;
  		}
b78dbba00   Mike Christie   [SCSI] iscsi clas...
234
235
236
237
238
239
240
241
242
243
244
245
  	case ISCSI_NET_PARAM:
  		switch (param) {
  		case ISCSI_NET_PARAM_IPV4_ADDR:
  		case ISCSI_NET_PARAM_IPV4_SUBNET:
  		case ISCSI_NET_PARAM_IPV4_GW:
  		case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  		case ISCSI_NET_PARAM_IFACE_ENABLE:
  		case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  		case ISCSI_NET_PARAM_IPV6_ADDR:
  		case ISCSI_NET_PARAM_IPV6_ROUTER:
  		case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  		case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
6ac73e8cb   Vikas Chaudhary   [SCSI] qla4xxx: A...
246
247
248
  		case ISCSI_NET_PARAM_VLAN_ID:
  		case ISCSI_NET_PARAM_VLAN_PRIORITY:
  		case ISCSI_NET_PARAM_VLAN_ENABLED:
943c157b7   Vikas Chaudhary   [SCSI] qla4xxx: A...
249
  		case ISCSI_NET_PARAM_MTU:
2ada7fc5d   Vikas Chaudhary   [SCSI] qla4xxx: a...
250
  		case ISCSI_NET_PARAM_PORT:
b78dbba00   Mike Christie   [SCSI] iscsi clas...
251
252
253
254
  			return S_IRUGO;
  		default:
  			return 0;
  		}
3128c6c73   Mike Christie   [SCSI] iscsi cls:...
255
256
257
258
  	}
  
  	return 0;
  }
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
  				   enum iscsi_param_type param_type,
  				   int param, char *buf)
  {
  	struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
  	struct scsi_qla_host *ha = to_qla_host(shost);
  	int len = -ENOSYS;
  
  	if (param_type != ISCSI_NET_PARAM)
  		return -ENOSYS;
  
  	switch (param) {
  	case ISCSI_NET_PARAM_IPV4_ADDR:
  		len = sprintf(buf, "%pI4
  ", &ha->ip_config.ip_address);
  		break;
  	case ISCSI_NET_PARAM_IPV4_SUBNET:
  		len = sprintf(buf, "%pI4
  ", &ha->ip_config.subnet_mask);
  		break;
  	case ISCSI_NET_PARAM_IPV4_GW:
  		len = sprintf(buf, "%pI4
  ", &ha->ip_config.gateway);
  		break;
  	case ISCSI_NET_PARAM_IFACE_ENABLE:
  		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  			len = sprintf(buf, "%s
  ",
  				      (ha->ip_config.ipv4_options &
  				       IPOPT_IPV4_PROTOCOL_ENABLE) ?
  				      "enabled" : "disabled");
  		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  			len = sprintf(buf, "%s
  ",
  				      (ha->ip_config.ipv6_options &
  				       IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
  				       "enabled" : "disabled");
  		break;
  	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  		len = sprintf(buf, "%s
  ",
  			      (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
  			      "dhcp" : "static");
  		break;
  	case ISCSI_NET_PARAM_IPV6_ADDR:
  		if (iface->iface_num == 0)
  			len = sprintf(buf, "%pI6
  ", &ha->ip_config.ipv6_addr0);
  		if (iface->iface_num == 1)
  			len = sprintf(buf, "%pI6
  ", &ha->ip_config.ipv6_addr1);
  		break;
  	case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  		len = sprintf(buf, "%pI6
  ",
  			      &ha->ip_config.ipv6_link_local_addr);
  		break;
  	case ISCSI_NET_PARAM_IPV6_ROUTER:
  		len = sprintf(buf, "%pI6
  ",
  			      &ha->ip_config.ipv6_default_router_addr);
  		break;
  	case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  		len = sprintf(buf, "%s
  ",
  			      (ha->ip_config.ipv6_addl_options &
  			       IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
  			       "nd" : "static");
  		break;
  	case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  		len = sprintf(buf, "%s
  ",
  			      (ha->ip_config.ipv6_addl_options &
  			       IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
  			       "auto" : "static");
  		break;
6ac73e8cb   Vikas Chaudhary   [SCSI] qla4xxx: A...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  	case ISCSI_NET_PARAM_VLAN_ID:
  		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  			len = sprintf(buf, "%d
  ",
  				      (ha->ip_config.ipv4_vlan_tag &
  				       ISCSI_MAX_VLAN_ID));
  		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  			len = sprintf(buf, "%d
  ",
  				      (ha->ip_config.ipv6_vlan_tag &
  				       ISCSI_MAX_VLAN_ID));
  		break;
  	case ISCSI_NET_PARAM_VLAN_PRIORITY:
  		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  			len = sprintf(buf, "%d
  ",
  				      ((ha->ip_config.ipv4_vlan_tag >> 13) &
  					ISCSI_MAX_VLAN_PRIORITY));
  		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  			len = sprintf(buf, "%d
  ",
  				      ((ha->ip_config.ipv6_vlan_tag >> 13) &
  					ISCSI_MAX_VLAN_PRIORITY));
  		break;
  	case ISCSI_NET_PARAM_VLAN_ENABLED:
  		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  			len = sprintf(buf, "%s
  ",
  				      (ha->ip_config.ipv4_options &
  				       IPOPT_VLAN_TAGGING_ENABLE) ?
  				       "enabled" : "disabled");
  		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  			len = sprintf(buf, "%s
  ",
  				      (ha->ip_config.ipv6_options &
  				       IPV6_OPT_VLAN_TAGGING_ENABLE) ?
  				       "enabled" : "disabled");
  		break;
943c157b7   Vikas Chaudhary   [SCSI] qla4xxx: A...
373
374
375
376
  	case ISCSI_NET_PARAM_MTU:
  		len = sprintf(buf, "%d
  ", ha->ip_config.eth_mtu_size);
  		break;
2ada7fc5d   Vikas Chaudhary   [SCSI] qla4xxx: a...
377
378
379
380
381
382
383
384
  	case ISCSI_NET_PARAM_PORT:
  		if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
  			len = sprintf(buf, "%d
  ", ha->ip_config.ipv4_port);
  		else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
  			len = sprintf(buf, "%d
  ", ha->ip_config.ipv6_port);
  		break;
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
385
386
387
388
389
390
  	default:
  		len = -ENOSYS;
  	}
  
  	return len;
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
391
392
393
  static struct iscsi_endpoint *
  qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  		   int non_blocking)
5c656af7e   Mike Christie   [SCSI] qla4xxx: a...
394
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
395
396
397
398
399
400
  	int ret;
  	struct iscsi_endpoint *ep;
  	struct qla_endpoint *qla_ep;
  	struct scsi_qla_host *ha;
  	struct sockaddr_in *addr;
  	struct sockaddr_in6 *addr6;
5c656af7e   Mike Christie   [SCSI] qla4xxx: a...
401

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
402
403
404
405
406
407
408
409
410
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	if (!shost) {
  		ret = -ENXIO;
  		printk(KERN_ERR "%s: shost is NULL
  ",
  		       __func__);
  		return ERR_PTR(ret);
  	}
5c656af7e   Mike Christie   [SCSI] qla4xxx: a...
411

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  	ha = iscsi_host_priv(shost);
  
  	ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
  	if (!ep) {
  		ret = -ENOMEM;
  		return ERR_PTR(ret);
  	}
  
  	qla_ep = ep->dd_data;
  	memset(qla_ep, 0, sizeof(struct qla_endpoint));
  	if (dst_addr->sa_family == AF_INET) {
  		memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
  		addr = (struct sockaddr_in *)&qla_ep->dst_addr;
  		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4
  ", __func__,
  				  (char *)&addr->sin_addr));
  	} else if (dst_addr->sa_family == AF_INET6) {
  		memcpy(&qla_ep->dst_addr, dst_addr,
  		       sizeof(struct sockaddr_in6));
  		addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
  		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6
  ", __func__,
  				  (char *)&addr6->sin6_addr));
  	}
  
  	qla_ep->host = shost;
  
  	return ep;
5c656af7e   Mike Christie   [SCSI] qla4xxx: a...
440
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
441
  static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
442
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
443
444
445
  	struct qla_endpoint *qla_ep;
  	struct scsi_qla_host *ha;
  	int ret = 0;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
446

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
447
448
449
450
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	qla_ep = ep->dd_data;
  	ha = to_qla_host(qla_ep->host);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
451
  	if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  		ret = 1;
  
  	return ret;
  }
  
  static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
  {
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	iscsi_destroy_endpoint(ep);
  }
  
  static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
  				enum iscsi_param param,
  				char *buf)
  {
  	struct qla_endpoint *qla_ep = ep->dd_data;
  	struct sockaddr *dst_addr;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  
  	switch (param) {
  	case ISCSI_PARAM_CONN_PORT:
  	case ISCSI_PARAM_CONN_ADDRESS:
  		if (!qla_ep)
  			return -ENOTCONN;
  
  		dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  		if (!dst_addr)
  			return -ENOTCONN;
  
  		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  						 &qla_ep->dst_addr, param, buf);
  	default:
  		return -ENOSYS;
  	}
  }
  
  static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  				   struct iscsi_stats *stats)
  {
  	struct iscsi_session *sess;
  	struct iscsi_cls_session *cls_sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  	struct ql_iscsi_stats *ql_iscsi_stats;
  	int stats_size;
  	int ret;
  	dma_addr_t iscsi_stats_dma;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
568d303b5   Mike Christie   [SCSI] qla4xxx: f...
505

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  	cls_sess = iscsi_conn_to_session(cls_conn);
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  
  	stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
  	/* Allocate memory */
  	ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
  					    &iscsi_stats_dma, GFP_KERNEL);
  	if (!ql_iscsi_stats) {
  		ql4_printk(KERN_ERR, ha,
  			   "Unable to allocate memory for iscsi stats
  ");
  		goto exit_get_stats;
568d303b5   Mike Christie   [SCSI] qla4xxx: f...
520
  	}
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  
  	ret =  qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
  				     iscsi_stats_dma);
  	if (ret != QLA_SUCCESS) {
  		ql4_printk(KERN_ERR, ha,
  			   "Unable to retreive iscsi stats
  ");
  		goto free_stats;
  	}
  
  	/* octets */
  	stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
  	stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
  	/* xmit pdus */
  	stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
  	stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
  	stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
  	stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
  	stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
  	stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
  	stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
  	stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
  	/* recv pdus */
  	stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
  	stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
  	stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
  	stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
  	stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
  	stats->logoutrsp_pdus =
  			le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
  	stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
  	stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
  	stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
  
  free_stats:
  	dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
  			  iscsi_stats_dma);
  exit_get_stats:
  	return;
  }
  
  static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
  {
  	struct iscsi_cls_session *session;
  	struct iscsi_session *sess;
  	unsigned long flags;
  	enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
  
  	session = starget_to_session(scsi_target(sc->device));
  	sess = session->dd_data;
  
  	spin_lock_irqsave(&session->lock, flags);
  	if (session->state == ISCSI_SESSION_FAILED)
  		ret = BLK_EH_RESET_TIMER;
  	spin_unlock_irqrestore(&session->lock, flags);
  
  	return ret;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
578
  }
aa1e93a2f   Mike Christie   [SCSI] qla4xxx: e...
579
580
581
582
583
584
585
586
  static int qla4xxx_host_get_param(struct Scsi_Host *shost,
  				  enum iscsi_host_param param, char *buf)
  {
  	struct scsi_qla_host *ha = to_qla_host(shost);
  	int len;
  
  	switch (param) {
  	case ISCSI_HOST_PARAM_HWADDRESS:
7ffc49a6e   Michael Chan   [ETH]: Combine fo...
587
  		len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
8ad5781ae   Mike Christie   [SCSI] iscsi clas...
588
  		break;
222369619   Mike Christie   [SCSI] iscsi clas...
589
  	case ISCSI_HOST_PARAM_IPADDRESS:
2bab08fc7   Vikas Chaudhary   [SCSI] qla4xxx: A...
590
591
  		len = sprintf(buf, "%pI4
  ", &ha->ip_config.ip_address);
222369619   Mike Christie   [SCSI] iscsi clas...
592
  		break;
8ad5781ae   Mike Christie   [SCSI] iscsi clas...
593
  	case ISCSI_HOST_PARAM_INITIATOR_NAME:
222369619   Mike Christie   [SCSI] iscsi clas...
594
595
  		len = sprintf(buf, "%s
  ", ha->name_string);
aa1e93a2f   Mike Christie   [SCSI] qla4xxx: e...
596
597
598
599
600
601
602
  		break;
  	default:
  		return -ENOSYS;
  	}
  
  	return len;
  }
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
  static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
  {
  	if (ha->iface_ipv4)
  		return;
  
  	/* IPv4 */
  	ha->iface_ipv4 = iscsi_create_iface(ha->host,
  					    &qla4xxx_iscsi_transport,
  					    ISCSI_IFACE_TYPE_IPV4, 0, 0);
  	if (!ha->iface_ipv4)
  		ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
  			   "iface0.
  ");
  }
  
  static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
  {
  	if (!ha->iface_ipv6_0)
  		/* IPv6 iface-0 */
  		ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
  						      &qla4xxx_iscsi_transport,
  						      ISCSI_IFACE_TYPE_IPV6, 0,
  						      0);
  	if (!ha->iface_ipv6_0)
  		ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  			   "iface0.
  ");
  
  	if (!ha->iface_ipv6_1)
  		/* IPv6 iface-1 */
  		ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
  						      &qla4xxx_iscsi_transport,
  						      ISCSI_IFACE_TYPE_IPV6, 1,
  						      0);
  	if (!ha->iface_ipv6_1)
  		ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
  			   "iface1.
  ");
  }
  
  static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
  {
  	if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
  		qla4xxx_create_ipv4_iface(ha);
  
  	if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
  		qla4xxx_create_ipv6_iface(ha);
  }
  
  static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
  {
  	if (ha->iface_ipv4) {
  		iscsi_destroy_iface(ha->iface_ipv4);
  		ha->iface_ipv4 = NULL;
  	}
  }
  
  static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
  {
  	if (ha->iface_ipv6_0) {
  		iscsi_destroy_iface(ha->iface_ipv6_0);
  		ha->iface_ipv6_0 = NULL;
  	}
  	if (ha->iface_ipv6_1) {
  		iscsi_destroy_iface(ha->iface_ipv6_1);
  		ha->iface_ipv6_1 = NULL;
  	}
  }
  
  static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
  {
  	qla4xxx_destroy_ipv4_iface(ha);
  	qla4xxx_destroy_ipv6_iface(ha);
  }
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
  static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
  			     struct iscsi_iface_param_info *iface_param,
  			     struct addr_ctrl_blk *init_fw_cb)
  {
  	/*
  	 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
  	 * iface_num 1 is valid only for IPv6 Addr.
  	 */
  	switch (iface_param->param) {
  	case ISCSI_NET_PARAM_IPV6_ADDR:
  		if (iface_param->iface_num & 0x1)
  			/* IPv6 Addr 1 */
  			memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
  			       sizeof(init_fw_cb->ipv6_addr1));
  		else
  			/* IPv6 Addr 0 */
  			memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
  			       sizeof(init_fw_cb->ipv6_addr0));
  		break;
  	case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
  		if (iface_param->iface_num & 0x1)
  			break;
  		memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
  		       sizeof(init_fw_cb->ipv6_if_id));
  		break;
  	case ISCSI_NET_PARAM_IPV6_ROUTER:
  		if (iface_param->iface_num & 0x1)
  			break;
  		memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
  		       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  		break;
  	case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
  		/* Autocfg applies to even interface */
  		if (iface_param->iface_num & 0x1)
  			break;
  
  		if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
  			init_fw_cb->ipv6_addtl_opts &=
  				cpu_to_le16(
  				  ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  		else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
  			init_fw_cb->ipv6_addtl_opts |=
  				cpu_to_le16(
  				  IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
  		else
  			ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  				   "IPv6 addr
  ");
  		break;
  	case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
  		/* Autocfg applies to even interface */
  		if (iface_param->iface_num & 0x1)
  			break;
  
  		if (iface_param->value[0] ==
  		    ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
  			init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
  					IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  		else if (iface_param->value[0] ==
  			 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
  			init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
  				       ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
  		else
  			ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
  				   "IPv6 linklocal addr
  ");
  		break;
  	case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
  		/* Autocfg applies to even interface */
  		if (iface_param->iface_num & 0x1)
  			break;
  
  		if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
  			memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
  			       sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
  		break;
  	case ISCSI_NET_PARAM_IFACE_ENABLE:
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
754
  		if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
755
756
  			init_fw_cb->ipv6_opts |=
  				cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
757
758
  			qla4xxx_create_ipv6_iface(ha);
  		} else {
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
759
760
761
  			init_fw_cb->ipv6_opts &=
  				cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
  					    0xFFFF);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
762
763
  			qla4xxx_destroy_ipv6_iface(ha);
  		}
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
764
  		break;
2d63673b4   Mike Christie   [SCSI] iscsi clas...
765
  	case ISCSI_NET_PARAM_VLAN_TAG:
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
766
767
  		if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
  			break;
6ac73e8cb   Vikas Chaudhary   [SCSI] qla4xxx: A...
768
769
770
771
772
773
774
775
776
777
  		init_fw_cb->ipv6_vlan_tag =
  				cpu_to_be16(*(uint16_t *)iface_param->value);
  		break;
  	case ISCSI_NET_PARAM_VLAN_ENABLED:
  		if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  			init_fw_cb->ipv6_opts |=
  				cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
  		else
  			init_fw_cb->ipv6_opts &=
  				cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
778
  		break;
943c157b7   Vikas Chaudhary   [SCSI] qla4xxx: A...
779
780
781
782
  	case ISCSI_NET_PARAM_MTU:
  		init_fw_cb->eth_mtu_size =
  				cpu_to_le16(*(uint16_t *)iface_param->value);
  		break;
2ada7fc5d   Vikas Chaudhary   [SCSI] qla4xxx: a...
783
784
785
786
787
788
789
790
  	case ISCSI_NET_PARAM_PORT:
  		/* Autocfg applies to even interface */
  		if (iface_param->iface_num & 0x1)
  			break;
  
  		init_fw_cb->ipv6_port =
  				cpu_to_le16(*(uint16_t *)iface_param->value);
  		break;
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
  	default:
  		ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d
  ",
  			   iface_param->param);
  		break;
  	}
  }
  
  static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
  			     struct iscsi_iface_param_info *iface_param,
  			     struct addr_ctrl_blk *init_fw_cb)
  {
  	switch (iface_param->param) {
  	case ISCSI_NET_PARAM_IPV4_ADDR:
  		memcpy(init_fw_cb->ipv4_addr, iface_param->value,
  		       sizeof(init_fw_cb->ipv4_addr));
  		break;
  	case ISCSI_NET_PARAM_IPV4_SUBNET:
  		memcpy(init_fw_cb->ipv4_subnet,	iface_param->value,
  		       sizeof(init_fw_cb->ipv4_subnet));
  		break;
  	case ISCSI_NET_PARAM_IPV4_GW:
  		memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
  		       sizeof(init_fw_cb->ipv4_gw_addr));
  		break;
  	case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
  		if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
  			init_fw_cb->ipv4_tcp_opts |=
  					cpu_to_le16(TCPOPT_DHCP_ENABLE);
  		else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
  			init_fw_cb->ipv4_tcp_opts &=
  					cpu_to_le16(~TCPOPT_DHCP_ENABLE);
  		else
  			ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto
  ");
  		break;
  	case ISCSI_NET_PARAM_IFACE_ENABLE:
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
828
  		if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
829
  			init_fw_cb->ipv4_ip_opts |=
2bab08fc7   Vikas Chaudhary   [SCSI] qla4xxx: A...
830
  				cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
831
832
  			qla4xxx_create_ipv4_iface(ha);
  		} else {
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
833
  			init_fw_cb->ipv4_ip_opts &=
2bab08fc7   Vikas Chaudhary   [SCSI] qla4xxx: A...
834
  				cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
835
  					    0xFFFF);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
836
837
  			qla4xxx_destroy_ipv4_iface(ha);
  		}
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
838
  		break;
2d63673b4   Mike Christie   [SCSI] iscsi clas...
839
  	case ISCSI_NET_PARAM_VLAN_TAG:
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
840
841
  		if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
  			break;
6ac73e8cb   Vikas Chaudhary   [SCSI] qla4xxx: A...
842
843
844
845
846
847
848
849
850
851
  		init_fw_cb->ipv4_vlan_tag =
  				cpu_to_be16(*(uint16_t *)iface_param->value);
  		break;
  	case ISCSI_NET_PARAM_VLAN_ENABLED:
  		if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
  			init_fw_cb->ipv4_ip_opts |=
  					cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
  		else
  			init_fw_cb->ipv4_ip_opts &=
  					cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
852
  		break;
943c157b7   Vikas Chaudhary   [SCSI] qla4xxx: A...
853
854
855
856
  	case ISCSI_NET_PARAM_MTU:
  		init_fw_cb->eth_mtu_size =
  				cpu_to_le16(*(uint16_t *)iface_param->value);
  		break;
2ada7fc5d   Vikas Chaudhary   [SCSI] qla4xxx: a...
857
858
859
860
  	case ISCSI_NET_PARAM_PORT:
  		init_fw_cb->ipv4_port =
  				cpu_to_le16(*(uint16_t *)iface_param->value);
  		break;
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
  	default:
  		ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d
  ",
  			   iface_param->param);
  		break;
  	}
  }
  
  static void
  qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
  {
  	struct addr_ctrl_blk_def *acb;
  	acb = (struct addr_ctrl_blk_def *)init_fw_cb;
  	memset(acb->reserved1, 0, sizeof(acb->reserved1));
  	memset(acb->reserved2, 0, sizeof(acb->reserved2));
  	memset(acb->reserved3, 0, sizeof(acb->reserved3));
  	memset(acb->reserved4, 0, sizeof(acb->reserved4));
  	memset(acb->reserved5, 0, sizeof(acb->reserved5));
  	memset(acb->reserved6, 0, sizeof(acb->reserved6));
  	memset(acb->reserved7, 0, sizeof(acb->reserved7));
  	memset(acb->reserved8, 0, sizeof(acb->reserved8));
  	memset(acb->reserved9, 0, sizeof(acb->reserved9));
  	memset(acb->reserved10, 0, sizeof(acb->reserved10));
  	memset(acb->reserved11, 0, sizeof(acb->reserved11));
  	memset(acb->reserved12, 0, sizeof(acb->reserved12));
  	memset(acb->reserved13, 0, sizeof(acb->reserved13));
  	memset(acb->reserved14, 0, sizeof(acb->reserved14));
  	memset(acb->reserved15, 0, sizeof(acb->reserved15));
  }
  
  static int
00c31889f   Mike Christie   [SCSI] qla4xxx: f...
892
  qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
893
894
895
896
897
898
899
900
  {
  	struct scsi_qla_host *ha = to_qla_host(shost);
  	int rval = 0;
  	struct iscsi_iface_param_info *iface_param = NULL;
  	struct addr_ctrl_blk *init_fw_cb = NULL;
  	dma_addr_t init_fw_cb_dma;
  	uint32_t mbox_cmd[MBOX_REG_COUNT];
  	uint32_t mbox_sts[MBOX_REG_COUNT];
00c31889f   Mike Christie   [SCSI] qla4xxx: f...
901
902
  	uint32_t rem = len;
  	struct nlattr *attr;
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
  
  	init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
  					sizeof(struct addr_ctrl_blk),
  					&init_fw_cb_dma, GFP_KERNEL);
  	if (!init_fw_cb) {
  		ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb
  ",
  			   __func__);
  		return -ENOMEM;
  	}
  
  	memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  	memset(&mbox_cmd, 0, sizeof(mbox_cmd));
  	memset(&mbox_sts, 0, sizeof(mbox_sts));
  
  	if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
  		ql4_printk(KERN_ERR, ha, "%s: get ifcb failed
  ", __func__);
  		rval = -EIO;
  		goto exit_init_fw_cb;
  	}
00c31889f   Mike Christie   [SCSI] qla4xxx: f...
924
925
  	nla_for_each_attr(attr, data, len, rem) {
  		iface_param = nla_data(attr);
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
  
  		if (iface_param->param_type != ISCSI_NET_PARAM)
  			continue;
  
  		switch (iface_param->iface_type) {
  		case ISCSI_IFACE_TYPE_IPV4:
  			switch (iface_param->iface_num) {
  			case 0:
  				qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
  				break;
  			default:
  				/* Cannot have more than one IPv4 interface */
  				ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
  					   "number = %d
  ",
  					   iface_param->iface_num);
  				break;
  			}
  			break;
  		case ISCSI_IFACE_TYPE_IPV6:
  			switch (iface_param->iface_num) {
  			case 0:
  			case 1:
  				qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
  				break;
  			default:
  				/* Cannot have more than two IPv6 interface */
  				ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
  					   "number = %d
  ",
  					   iface_param->iface_num);
  				break;
  			}
  			break;
  		default:
  			ql4_printk(KERN_ERR, ha, "Invalid iface type
  ");
  			break;
  		}
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
965
966
967
968
969
970
971
972
973
974
975
976
977
978
  	}
  
  	init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
  
  	rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
  				 sizeof(struct addr_ctrl_blk),
  				 FLASH_OPT_RMW_COMMIT);
  	if (rval != QLA_SUCCESS) {
  		ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed
  ",
  			   __func__);
  		rval = -EIO;
  		goto exit_init_fw_cb;
  	}
ce505f9d1   Vikas Chaudhary   [SCSI] qla4xxx: W...
979
980
981
982
983
984
985
986
987
988
989
  	rval = qla4xxx_disable_acb(ha);
  	if (rval != QLA_SUCCESS) {
  		ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed
  ",
  			   __func__);
  		rval = -EIO;
  		goto exit_init_fw_cb;
  	}
  
  	wait_for_completion_timeout(&ha->disable_acb_comp,
  				    DISABLE_ACB_TOV * HZ);
d00efe3fa   Mike Christie   [SCSI] qla4xxx: a...
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
  
  	qla4xxx_initcb_to_acb(init_fw_cb);
  
  	rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
  	if (rval != QLA_SUCCESS) {
  		ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed
  ",
  			   __func__);
  		rval = -EIO;
  		goto exit_init_fw_cb;
  	}
  
  	memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
  	qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
  				  init_fw_cb_dma);
  
  exit_init_fw_cb:
  	dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
  			  init_fw_cb, init_fw_cb_dma);
  
  	return rval;
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1012
  static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1013
1014
  				  enum iscsi_param param, char *buf)
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1015
1016
1017
1018
  	struct iscsi_conn *conn;
  	struct qla_conn *qla_conn;
  	struct sockaddr *dst_addr;
  	int len = 0;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1019

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1020
1021
1022
  	conn = cls_conn->dd_data;
  	qla_conn = conn->dd_data;
  	dst_addr = &qla_conn->qla_ep->dst_addr;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1023
1024
1025
  
  	switch (param) {
  	case ISCSI_PARAM_CONN_PORT:
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1026
  	case ISCSI_PARAM_CONN_ADDRESS:
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1027
1028
  		return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  						 dst_addr, param, buf);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1029
  	default:
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1030
  		return iscsi_conn_get_param(cls_conn, param, buf);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1031
1032
1033
  	}
  
  	return len;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1034

afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1035
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
  int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
  {
  	uint32_t mbx_sts = 0;
  	uint16_t tmp_ddb_index;
  	int ret;
  
  get_ddb_index:
  	tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
  
  	if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  				  "Free DDB index not available
  "));
  		ret = QLA_ERROR;
  		goto exit_get_ddb_index;
  	}
  
  	if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
  		goto get_ddb_index;
  
  	DEBUG2(ql4_printk(KERN_INFO, ha,
  			  "Found a free DDB index at %d
  ", tmp_ddb_index));
  	ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
  	if (ret == QLA_ERROR) {
  		if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
  			ql4_printk(KERN_INFO, ha,
  				   "DDB index = %d not available trying next
  ",
  				   tmp_ddb_index);
  			goto get_ddb_index;
  		}
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  				  "Free FW DDB not available
  "));
  	}
  
  	*ddb_index = tmp_ddb_index;
  
  exit_get_ddb_index:
  	return ret;
  }
  
  static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
  				   struct ddb_entry *ddb_entry,
  				   char *existing_ipaddr,
  				   char *user_ipaddr)
  {
  	uint8_t dst_ipaddr[IPv6_ADDR_LEN];
  	char formatted_ipaddr[DDB_IPADDR_LEN];
  	int status = QLA_SUCCESS, ret = 0;
  
  	if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
  		ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
  			       '\0', NULL);
  		if (ret == 0) {
  			status = QLA_ERROR;
  			goto out_match;
  		}
  		ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
  	} else {
  		ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
  			       '\0', NULL);
  		if (ret == 0) {
  			status = QLA_ERROR;
  			goto out_match;
  		}
  		ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
  	}
  
  	if (strcmp(existing_ipaddr, formatted_ipaddr))
  		status = QLA_ERROR;
  
  out_match:
  	return status;
  }
  
  static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
  				      struct iscsi_cls_conn *cls_conn)
  {
  	int idx = 0, max_ddbs, rval;
  	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  	struct iscsi_session *sess, *existing_sess;
  	struct iscsi_conn *conn, *existing_conn;
  	struct ddb_entry *ddb_entry;
  
  	sess = cls_sess->dd_data;
  	conn = cls_conn->dd_data;
  
  	if (sess->targetname == NULL ||
  	    conn->persistent_address == NULL ||
  	    conn->persistent_port == 0)
  		return QLA_ERROR;
  
  	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  				     MAX_DEV_DB_ENTRIES;
  
  	for (idx = 0; idx < max_ddbs; idx++) {
  		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  		if (ddb_entry == NULL)
  			continue;
  
  		if (ddb_entry->ddb_type != FLASH_DDB)
  			continue;
  
  		existing_sess = ddb_entry->sess->dd_data;
  		existing_conn = ddb_entry->conn->dd_data;
  
  		if (existing_sess->targetname == NULL ||
  		    existing_conn->persistent_address == NULL ||
  		    existing_conn->persistent_port == 0)
  			continue;
  
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  				  "IQN = %s User IQN = %s
  ",
  				  existing_sess->targetname,
  				  sess->targetname));
  
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  				  "IP = %s User IP = %s
  ",
  				  existing_conn->persistent_address,
  				  conn->persistent_address));
  
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  				  "Port = %d User Port = %d
  ",
  				  existing_conn->persistent_port,
  				  conn->persistent_port));
  
  		if (strcmp(existing_sess->targetname, sess->targetname))
  			continue;
  		rval = qla4xxx_match_ipaddress(ha, ddb_entry,
  					existing_conn->persistent_address,
  					conn->persistent_address);
  		if (rval == QLA_ERROR)
  			continue;
  		if (existing_conn->persistent_port != conn->persistent_port)
  			continue;
  		break;
  	}
  
  	if (idx == max_ddbs)
  		return QLA_ERROR;
  
  	DEBUG2(ql4_printk(KERN_INFO, ha,
  			  "Match found in fwdb sessions
  "));
  	return QLA_SUCCESS;
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1187
1188
1189
1190
1191
1192
1193
1194
1195
  static struct iscsi_cls_session *
  qla4xxx_session_create(struct iscsi_endpoint *ep,
  			uint16_t cmds_max, uint16_t qdepth,
  			uint32_t initial_cmdsn)
  {
  	struct iscsi_cls_session *cls_sess;
  	struct scsi_qla_host *ha;
  	struct qla_endpoint *qla_ep;
  	struct ddb_entry *ddb_entry;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1196
  	uint16_t ddb_index;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
  	struct iscsi_session *sess;
  	struct sockaddr *dst_addr;
  	int ret;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	if (!ep) {
  		printk(KERN_ERR "qla4xxx: missing ep.
  ");
  		return NULL;
  	}
  
  	qla_ep = ep->dd_data;
  	dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
  	ha = to_qla_host(qla_ep->host);
736cf369c   Manish Rangankar   [SCSI] qla4xxx: C...
1212

13483730a   Mike Christie   [SCSI] qla4xxx: f...
1213
1214
  	ret = qla4xxx_get_ddb_index(ha, &ddb_index);
  	if (ret == QLA_ERROR)
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1215
  		return NULL;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
  
  	cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
  				       cmds_max, sizeof(struct ddb_entry),
  				       sizeof(struct ql4_task_data),
  				       initial_cmdsn, ddb_index);
  	if (!cls_sess)
  		return NULL;
  
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ddb_entry->fw_ddb_index = ddb_index;
  	ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  	ddb_entry->ha = ha;
  	ddb_entry->sess = cls_sess;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1230
1231
  	ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
  	ddb_entry->ddb_change = qla4xxx_ddb_change;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
  	cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  	ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
  	ha->tot_ddbs++;
  
  	return cls_sess;
  }
  
  static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
  {
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  	unsigned long flags;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
736cf369c   Manish Rangankar   [SCSI] qla4xxx: C...
1251
  	qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1252
1253
1254
1255
1256
  	spin_lock_irqsave(&ha->hardware_lock, flags);
  	qla4xxx_free_ddb(ha, ddb_entry);
  	spin_unlock_irqrestore(&ha->hardware_lock, flags);
  	iscsi_session_teardown(cls_sess);
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
  static struct iscsi_cls_conn *
  qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
  {
  	struct iscsi_cls_conn *cls_conn;
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
  				    conn_idx);
ff1d0319a   Mike Christie   [SCSI] qla4xxx: c...
1268
1269
  	if (!cls_conn)
  		return NULL;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ddb_entry->conn = cls_conn;
  
  	return cls_conn;
  }
  
  static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
  			     struct iscsi_cls_conn *cls_conn,
  			     uint64_t transport_fd, int is_leading)
  {
  	struct iscsi_conn *conn;
  	struct qla_conn *qla_conn;
  	struct iscsi_endpoint *ep;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  
  	if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
  		return -EINVAL;
  	ep = iscsi_lookup_endpoint(transport_fd);
  	conn = cls_conn->dd_data;
  	qla_conn = conn->dd_data;
  	qla_conn->qla_ep = ep->dd_data;
  	return 0;
  }
  
  static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
  {
  	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1303
  	struct dev_db_entry *fw_ddb_entry = NULL;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1304
  	dma_addr_t fw_ddb_entry_dma;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1305
1306
1307
1308
1309
1310
1311
1312
1313
  	uint32_t mbx_sts = 0;
  	int ret = 0;
  	int status = QLA_SUCCESS;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
  	/* Check if we have  matching FW DDB, if yes then do not
  	 * login to this target. This could cause target to logout previous
  	 * connection
  	 */
  	ret = qla4xxx_match_fwdb_session(ha, cls_conn);
  	if (ret == QLA_SUCCESS) {
  		ql4_printk(KERN_INFO, ha,
  			   "Session already exist in FW.
  ");
  		ret = -EEXIST;
  		goto exit_conn_start;
  	}
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1326
1327
1328
1329
1330
1331
  	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  					  &fw_ddb_entry_dma, GFP_KERNEL);
  	if (!fw_ddb_entry) {
  		ql4_printk(KERN_ERR, ha,
  			   "%s: Unable to allocate dma buffer
  ", __func__);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1332
1333
  		ret = -ENOMEM;
  		goto exit_conn_start;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
  	}
  
  	ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
  	if (ret) {
  		/* If iscsid is stopped and started then no need to do
  		* set param again since ddb state will be already
  		* active and FW does not allow set ddb to an
  		* active session.
  		*/
  		if (mbx_sts)
  			if (ddb_entry->fw_ddb_device_state ==
f922da79f   Manish Rangankar   [SCSI] qla4xxx: F...
1345
  						DDB_DS_SESSION_ACTIVE) {
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1346
  				ddb_entry->unblock_sess(ddb_entry->sess);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1347
  				goto exit_set_param;
f922da79f   Manish Rangankar   [SCSI] qla4xxx: F...
1348
  			}
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1349
1350
1351
1352
1353
1354
1355
1356
1357
  
  		ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]
  ",
  			   __func__, ddb_entry->fw_ddb_index);
  		goto exit_conn_start;
  	}
  
  	status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
  	if (status == QLA_ERROR) {
0e7e85019   Manish Rangankar   [SCSI] qla4xxx: R...
1358
1359
1360
  		ql4_printk(KERN_ERR, ha, "%s: Login failed: %s
  ", __func__,
  			   sess->targetname);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1361
1362
1363
  		ret = -EINVAL;
  		goto exit_conn_start;
  	}
98270ab45   Manish Rangankar   [SCSI] qla4xxx: F...
1364
1365
1366
1367
1368
1369
  	if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
  		ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
  
  	DEBUG2(printk(KERN_INFO "%s: DDB state [%d]
  ", __func__,
  		      ddb_entry->fw_ddb_device_state));
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1370
1371
  
  exit_set_param:
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1372
1373
1374
  	ret = 0;
  
  exit_conn_start:
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1375
1376
1377
  	if (fw_ddb_entry)
  		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  				  fw_ddb_entry, fw_ddb_entry_dma);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
  	return ret;
  }
  
  static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
  {
  	struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
  	struct iscsi_session *sess;
  	struct scsi_qla_host *ha;
  	struct ddb_entry *ddb_entry;
  	int options;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  
  	options = LOGOUT_OPTION_CLOSE_SESSION;
  	if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
  		ql4_printk(KERN_ERR, ha, "%s: Logout failed
  ", __func__);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
  }
  
  static void qla4xxx_task_work(struct work_struct *wdata)
  {
  	struct ql4_task_data *task_data;
  	struct scsi_qla_host *ha;
  	struct passthru_status *sts;
  	struct iscsi_task *task;
  	struct iscsi_hdr *hdr;
  	uint8_t *data;
  	uint32_t data_len;
  	struct iscsi_conn *conn;
  	int hdr_len;
  	itt_t itt;
  
  	task_data = container_of(wdata, struct ql4_task_data, task_work);
  	ha = task_data->ha;
  	task = task_data->task;
  	sts = &task_data->sts;
  	hdr_len = sizeof(struct iscsi_hdr);
  
  	DEBUG3(printk(KERN_INFO "Status returned
  "));
  	DEBUG3(qla4xxx_dump_buffer(sts, 64));
  	DEBUG3(printk(KERN_INFO "Response buffer"));
  	DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
  
  	conn = task->conn;
  
  	switch (sts->completionStatus) {
  	case PASSTHRU_STATUS_COMPLETE:
  		hdr = (struct iscsi_hdr *)task_data->resp_buffer;
  		/* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
  		itt = sts->handle;
  		hdr->itt = itt;
  		data = task_data->resp_buffer + hdr_len;
  		data_len = task_data->resp_len - hdr_len;
  		iscsi_complete_pdu(conn, hdr, data, data_len);
  		break;
  	default:
  		ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x
  ",
  			   sts->completionStatus);
  		break;
  	}
  	return;
  }
  
  static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
  {
  	struct ql4_task_data *task_data;
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  	int hdr_len;
  
  	sess = task->conn->session;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  	task_data = task->dd_data;
  	memset(task_data, 0, sizeof(struct ql4_task_data));
  
  	if (task->sc) {
  		ql4_printk(KERN_INFO, ha,
  			   "%s: SCSI Commands not implemented
  ", __func__);
  		return -EINVAL;
  	}
  
  	hdr_len = sizeof(struct iscsi_hdr);
  	task_data->ha = ha;
  	task_data->task = task;
  
  	if (task->data_count) {
  		task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
  						     task->data_count,
  						     PCI_DMA_TODEVICE);
  	}
  
  	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d
  ",
  		      __func__, task->conn->max_recv_dlength, hdr_len));
69ca216e9   Manish Rangankar   [SCSI] qla4xxx: F...
1481
  	task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1482
1483
1484
1485
1486
1487
  	task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
  						    task_data->resp_len,
  						    &task_data->resp_dma,
  						    GFP_ATOMIC);
  	if (!task_data->resp_buffer)
  		goto exit_alloc_pdu;
69ca216e9   Manish Rangankar   [SCSI] qla4xxx: F...
1488
  	task_data->req_len = task->data_count + hdr_len;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1489
  	task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
69ca216e9   Manish Rangankar   [SCSI] qla4xxx: F...
1490
  						   task_data->req_len,
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
  						   &task_data->req_dma,
  						   GFP_ATOMIC);
  	if (!task_data->req_buffer)
  		goto exit_alloc_pdu;
  
  	task->hdr = task_data->req_buffer;
  
  	INIT_WORK(&task_data->task_work, qla4xxx_task_work);
  
  	return 0;
  
  exit_alloc_pdu:
  	if (task_data->resp_buffer)
  		dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  				  task_data->resp_buffer, task_data->resp_dma);
  
  	if (task_data->req_buffer)
69ca216e9   Manish Rangankar   [SCSI] qla4xxx: F...
1508
  		dma_free_coherent(&ha->pdev->dev, task_data->req_len,
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
  				  task_data->req_buffer, task_data->req_dma);
  	return -ENOMEM;
  }
  
  static void qla4xxx_task_cleanup(struct iscsi_task *task)
  {
  	struct ql4_task_data *task_data;
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  	int hdr_len;
  
  	hdr_len = sizeof(struct iscsi_hdr);
  	sess = task->conn->session;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  	task_data = task->dd_data;
  
  	if (task->data_count) {
  		dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
  				 task->data_count, PCI_DMA_TODEVICE);
  	}
  
  	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d
  ",
  		      __func__, task->conn->max_recv_dlength, hdr_len));
  
  	dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
  			  task_data->resp_buffer, task_data->resp_dma);
69ca216e9   Manish Rangankar   [SCSI] qla4xxx: F...
1538
  	dma_free_coherent(&ha->pdev->dev, task_data->req_len,
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
  			  task_data->req_buffer, task_data->req_dma);
  	return;
  }
  
  static int qla4xxx_task_xmit(struct iscsi_task *task)
  {
  	struct scsi_cmnd *sc = task->sc;
  	struct iscsi_session *sess = task->conn->session;
  	struct ddb_entry *ddb_entry = sess->dd_data;
  	struct scsi_qla_host *ha = ddb_entry->ha;
  
  	if (!sc)
  		return qla4xxx_send_passthru0(task);
  
  	ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented
  ",
  		   __func__);
  	return -ENOSYS;
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
  static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
  				     struct dev_db_entry *fw_ddb_entry,
  				     struct iscsi_cls_session *cls_sess,
  				     struct iscsi_cls_conn *cls_conn)
  {
  	int buflen = 0;
  	struct iscsi_session *sess;
  	struct iscsi_conn *conn;
  	char ip_addr[DDB_IPADDR_LEN];
  	uint16_t options = 0;
  
  	sess = cls_sess->dd_data;
  	conn = cls_conn->dd_data;
  
  	conn->max_recv_dlength = BYTE_UNITS *
  			  le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  
  	conn->max_xmit_dlength = BYTE_UNITS *
  			  le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  
  	sess->initial_r2t_en =
  			    (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  
  	sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  
  	sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  
  	sess->first_burst = BYTE_UNITS *
  			       le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  
  	sess->max_burst = BYTE_UNITS *
  				 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  
  	sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  
  	sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  
  	conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
  
  	sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  
  	options = le16_to_cpu(fw_ddb_entry->options);
  	if (options & DDB_OPT_IPV6_DEVICE)
  		sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  	else
  		sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  
  	iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
  			(char *)fw_ddb_entry->iscsi_name, buflen);
  	iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
  			(char *)ha->name_string, buflen);
  	iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
  			(char *)ip_addr, buflen);
  }
  
  void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
  					     struct ddb_entry *ddb_entry)
  {
  	struct iscsi_cls_session *cls_sess;
  	struct iscsi_cls_conn *cls_conn;
  	uint32_t ddb_state;
  	dma_addr_t fw_ddb_entry_dma;
  	struct dev_db_entry *fw_ddb_entry;
  
  	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  					  &fw_ddb_entry_dma, GFP_KERNEL);
  	if (!fw_ddb_entry) {
  		ql4_printk(KERN_ERR, ha,
  			   "%s: Unable to allocate dma buffer
  ", __func__);
  		goto exit_session_conn_fwddb_param;
  	}
  
  	if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  				    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  				    NULL, NULL, NULL) == QLA_ERROR) {
  		DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  				  "get_ddb_entry for fw_ddb_index %d
  ",
  				  ha->host_no, __func__,
  				  ddb_entry->fw_ddb_index));
  		goto exit_session_conn_fwddb_param;
  	}
  
  	cls_sess = ddb_entry->sess;
  
  	cls_conn = ddb_entry->conn;
  
  	/* Update params */
  	qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
  
  exit_session_conn_fwddb_param:
  	if (fw_ddb_entry)
  		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  				  fw_ddb_entry, fw_ddb_entry_dma);
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
  void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
  				       struct ddb_entry *ddb_entry)
  {
  	struct iscsi_cls_session *cls_sess;
  	struct iscsi_cls_conn *cls_conn;
  	struct iscsi_session *sess;
  	struct iscsi_conn *conn;
  	uint32_t ddb_state;
  	dma_addr_t fw_ddb_entry_dma;
  	struct dev_db_entry *fw_ddb_entry;
  
  	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  					  &fw_ddb_entry_dma, GFP_KERNEL);
  	if (!fw_ddb_entry) {
  		ql4_printk(KERN_ERR, ha,
  			   "%s: Unable to allocate dma buffer
  ", __func__);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1671
  		goto exit_session_conn_param;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
  	}
  
  	if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
  				    fw_ddb_entry_dma, NULL, NULL, &ddb_state,
  				    NULL, NULL, NULL) == QLA_ERROR) {
  		DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
  				  "get_ddb_entry for fw_ddb_index %d
  ",
  				  ha->host_no, __func__,
  				  ddb_entry->fw_ddb_index));
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1682
  		goto exit_session_conn_param;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1683
1684
1685
1686
1687
1688
1689
  	}
  
  	cls_sess = ddb_entry->sess;
  	sess = cls_sess->dd_data;
  
  	cls_conn = ddb_entry->conn;
  	conn = cls_conn->dd_data;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1690
1691
1692
1693
1694
  	/* Update timers after login */
  	ddb_entry->default_relogin_timeout =
  				le16_to_cpu(fw_ddb_entry->def_timeout);
  	ddb_entry->default_time2wait =
  				le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
  	/* Update params */
  	conn->max_recv_dlength = BYTE_UNITS *
  			  le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
  
  	conn->max_xmit_dlength = BYTE_UNITS *
  			  le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
  
  	sess->initial_r2t_en =
  			    (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  
  	sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
  
  	sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
  
  	sess->first_burst = BYTE_UNITS *
  			       le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
  
  	sess->max_burst = BYTE_UNITS *
  				 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
  
  	sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
  
  	sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
  
  	sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  
  	memcpy(sess->initiatorname, ha->name_string,
  	       min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1723
1724
1725
1726
1727
  
  exit_session_conn_param:
  	if (fw_ddb_entry)
  		dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  				  fw_ddb_entry, fw_ddb_entry_dma);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1728
  }
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
  /*
   * Timer routines
   */
  
  static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
  				unsigned long interval)
  {
  	DEBUG(printk("scsi: %s: Starting timer thread for adapter %d
  ",
  		     __func__, ha->host->host_no));
  	init_timer(&ha->timer);
  	ha->timer.expires = jiffies + interval * HZ;
  	ha->timer.data = (unsigned long)ha;
  	ha->timer.function = (void (*)(unsigned long))func;
  	add_timer(&ha->timer);
  	ha->timer_active = 1;
  }
  
  static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
  {
  	del_timer_sync(&ha->timer);
  	ha->timer_active = 0;
  }
  
  /***
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1754
1755
   * qla4xxx_mark_device_missing - blocks the session
   * @cls_session: Pointer to the session to be blocked
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1756
1757
   * @ddb_entry: Pointer to device database entry
   *
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1758
   * This routine marks a device missing and close connection.
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1759
   **/
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1760
  void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1761
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1762
  	iscsi_block_session(cls_session);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1763
  }
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1764
1765
1766
1767
1768
1769
1770
1771
  /**
   * qla4xxx_mark_all_devices_missing - mark all devices as missing.
   * @ha: Pointer to host adapter structure.
   *
   * This routine marks a device missing and resets the relogin retry count.
   **/
  void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1772
  	iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1773
  }
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1774
1775
  static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
  				       struct ddb_entry *ddb_entry,
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1776
  				       struct scsi_cmnd *cmd)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1777
1778
1779
1780
1781
1782
  {
  	struct srb *srb;
  
  	srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
  	if (!srb)
  		return srb;
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
1783
  	kref_init(&srb->srb_ref);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1784
1785
1786
1787
  	srb->ha = ha;
  	srb->ddb = ddb_entry;
  	srb->cmd = cmd;
  	srb->flags = 0;
5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
1788
  	CMD_SP(cmd) = (void *)srb;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1789
1790
1791
1792
1793
1794
1795
1796
1797
  
  	return srb;
  }
  
  static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
  {
  	struct scsi_cmnd *cmd = srb->cmd;
  
  	if (srb->flags & SRB_DMA_VALID) {
5f7186c84   FUJITA Tomonori   [SCSI] qla4xxx: c...
1798
  		scsi_dma_unmap(cmd);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1799
1800
  		srb->flags &= ~SRB_DMA_VALID;
  	}
5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
1801
  	CMD_SP(cmd) = NULL;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1802
  }
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
1803
  void qla4xxx_srb_compl(struct kref *ref)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1804
  {
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
1805
  	struct srb *srb = container_of(ref, struct srb, srb_ref);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1806
  	struct scsi_cmnd *cmd = srb->cmd;
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
1807
  	struct scsi_qla_host *ha = srb->ha;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
  
  	qla4xxx_srb_free_dma(ha, srb);
  
  	mempool_free(srb, ha->srb_mempool);
  
  	cmd->scsi_done(cmd);
  }
  
  /**
   * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1818
   * @host: scsi host
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1819
   * @cmd: Pointer to Linux's SCSI command structure
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1820
1821
1822
1823
1824
1825
1826
1827
1828
   *
   * Remarks:
   * This routine is invoked by Linux to send a SCSI command to the driver.
   * The mid-level driver tries to ensure that queuecommand never gets
   * invoked concurrently with itself or the interrupt handler (although
   * the interrupt handler may call this routine as part of request-
   * completion handling).   Unfortunely, it sometimes calls the scheduler
   * in interrupt context which is a big NO! NO!.
   **/
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1829
  static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1830
  {
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1831
  	struct scsi_qla_host *ha = to_qla_host(host);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1832
  	struct ddb_entry *ddb_entry = cmd->device->hostdata;
7fb1921b0   Mike Christie   [SCSI] qla4xxx: u...
1833
  	struct iscsi_cls_session *sess = ddb_entry->sess;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1834
1835
  	struct srb *srb;
  	int rval;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
1836
1837
1838
1839
1840
1841
1842
  	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  		if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
  			cmd->result = DID_NO_CONNECT << 16;
  		else
  			cmd->result = DID_REQUEUE << 16;
  		goto qc_fail_command;
  	}
7fb1921b0   Mike Christie   [SCSI] qla4xxx: u...
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
  	if (!sess) {
  		cmd->result = DID_IMM_RETRY << 16;
  		goto qc_fail_command;
  	}
  
  	rval = iscsi_session_chkready(sess);
  	if (rval) {
  		cmd->result = rval;
  		goto qc_fail_command;
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1853
1854
1855
1856
1857
1858
  	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  	    test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  	    test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  	    test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
  	    !test_bit(AF_ONLINE, &ha->flags) ||
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1859
  	    !test_bit(AF_LINK_UP, &ha->flags) ||
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1860
  	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
1861
  		goto qc_host_busy;
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1862
  	srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1863
  	if (!srb)
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1864
  		goto qc_host_busy;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1865
1866
1867
1868
  
  	rval = qla4xxx_send_command_to_isp(ha, srb);
  	if (rval != QLA_SUCCESS)
  		goto qc_host_busy_free_sp;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1869
1870
1871
1872
1873
  	return 0;
  
  qc_host_busy_free_sp:
  	qla4xxx_srb_free_dma(ha, srb);
  	mempool_free(srb, ha->srb_mempool);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1874
1875
1876
1877
  qc_host_busy:
  	return SCSI_MLQUEUE_HOST_BUSY;
  
  qc_fail_command:
8f0722cae   Vikas Chaudhary   [SCSI] qla4xxx: R...
1878
  	cmd->scsi_done(cmd);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
  
  	return 0;
  }
  
  /**
   * qla4xxx_mem_free - frees memory allocated to adapter
   * @ha: Pointer to host adapter structure.
   *
   * Frees memory previously allocated by qla4xxx_mem_alloc
   **/
  static void qla4xxx_mem_free(struct scsi_qla_host *ha)
  {
  	if (ha->queues)
  		dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
  				  ha->queues_dma);
  
  	ha->queues_len = 0;
  	ha->queues = NULL;
  	ha->queues_dma = 0;
  	ha->request_ring = NULL;
  	ha->request_dma = 0;
  	ha->response_ring = NULL;
  	ha->response_dma = 0;
  	ha->shadow_regs = NULL;
  	ha->shadow_regs_dma = 0;
  
  	/* Free srb pool. */
  	if (ha->srb_mempool)
  		mempool_destroy(ha->srb_mempool);
  
  	ha->srb_mempool = NULL;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1910
1911
  	if (ha->chap_dma_pool)
  		dma_pool_destroy(ha->chap_dma_pool);
4549415af   Lalit Chandivade   [SCSI] qla4xxx: D...
1912
1913
1914
  	if (ha->chap_list)
  		vfree(ha->chap_list);
  	ha->chap_list = NULL;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
1915
1916
  	if (ha->fw_ddb_dma_pool)
  		dma_pool_destroy(ha->fw_ddb_dma_pool);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1917
  	/* release io space registers  */
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1918
1919
1920
1921
  	if (is_qla8022(ha)) {
  		if (ha->nx_pcibase)
  			iounmap(
  			    (struct device_reg_82xx __iomem *)ha->nx_pcibase);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
1922
  	} else if (ha->reg)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
  		iounmap(ha->reg);
  	pci_release_regions(ha->pdev);
  }
  
  /**
   * qla4xxx_mem_alloc - allocates memory for use by adapter.
   * @ha: Pointer to host adapter structure
   *
   * Allocates DMA memory for request and response queues. Also allocates memory
   * for srbs.
   **/
  static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
  {
  	unsigned long align;
  
  	/* Allocate contiguous block of DMA memory for queues. */
  	ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  			  (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
  			  sizeof(struct shadow_regs) +
  			  MEM_ALIGN_VALUE +
  			  (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
  	ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
  					&ha->queues_dma, GFP_KERNEL);
  	if (ha->queues == NULL) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
1947
1948
1949
  		ql4_printk(KERN_WARNING, ha,
  		    "Memory Allocation failed - queues.
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
  
  		goto mem_alloc_error_exit;
  	}
  	memset(ha->queues, 0, ha->queues_len);
  
  	/*
  	 * As per RISC alignment requirements -- the bus-address must be a
  	 * multiple of the request-ring size (in bytes).
  	 */
  	align = 0;
  	if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
  		align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
  					   (MEM_ALIGN_VALUE - 1));
  
  	/* Update request and response queue pointers. */
  	ha->request_dma = ha->queues_dma + align;
  	ha->request_ring = (struct queue_entry *) (ha->queues + align);
  	ha->response_dma = ha->queues_dma + align +
  		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
  	ha->response_ring = (struct queue_entry *) (ha->queues + align +
  						    (REQUEST_QUEUE_DEPTH *
  						     QUEUE_SIZE));
  	ha->shadow_regs_dma = ha->queues_dma + align +
  		(REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
  		(RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
  	ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
  						  (REQUEST_QUEUE_DEPTH *
  						   QUEUE_SIZE) +
  						  (RESPONSE_QUEUE_DEPTH *
  						   QUEUE_SIZE));
  
  	/* Allocate memory for srb pool. */
  	ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
  					 mempool_free_slab, srb_cachep);
  	if (ha->srb_mempool == NULL) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
1985
1986
1987
  		ql4_printk(KERN_WARNING, ha,
  		    "Memory Allocation failed - SRB Pool.
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
1988
1989
1990
  
  		goto mem_alloc_error_exit;
  	}
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
1991
1992
1993
1994
1995
1996
1997
1998
1999
  	ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
  					    CHAP_DMA_BLOCK_SIZE, 8, 0);
  
  	if (ha->chap_dma_pool == NULL) {
  		ql4_printk(KERN_WARNING, ha,
  		    "%s: chap_dma_pool allocation failed..
  ", __func__);
  		goto mem_alloc_error_exit;
  	}
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
  	ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
  					      DDB_DMA_BLOCK_SIZE, 8, 0);
  
  	if (ha->fw_ddb_dma_pool == NULL) {
  		ql4_printk(KERN_WARNING, ha,
  			   "%s: fw_ddb_dma_pool allocation failed..
  ",
  			   __func__);
  		goto mem_alloc_error_exit;
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2010
2011
2012
2013
2014
2015
2016
2017
  	return QLA_SUCCESS;
  
  mem_alloc_error_exit:
  	qla4xxx_mem_free(ha);
  	return QLA_ERROR;
  }
  
  /**
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2018
2019
2020
2021
2022
   * qla4_8xxx_check_fw_alive  - Check firmware health
   * @ha: Pointer to host adapter structure.
   *
   * Context: Interrupt
   **/
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2023
  static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2024
  {
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2025
2026
  	uint32_t fw_heartbeat_counter;
  	int status = QLA_SUCCESS;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2027
2028
  
  	fw_heartbeat_counter = qla4_8xxx_rd_32(ha, QLA82XX_PEG_ALIVE_COUNTER);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
2029
2030
2031
2032
2033
2034
  	/* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
  	if (fw_heartbeat_counter == 0xffffffff) {
  		DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
  		    "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff
  ",
  		    ha->host_no, __func__));
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2035
  		return status;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
2036
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2037
2038
2039
2040
2041
2042
  
  	if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
  		ha->seconds_since_last_heartbeat++;
  		/* FW not alive after 2 seconds */
  		if (ha->seconds_since_last_heartbeat == 2) {
  			ha->seconds_since_last_heartbeat = 0;
68d92ebf5   Vikas Chaudhary   [SCSI] qla4xxx: D...
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
  
  			ql4_printk(KERN_INFO, ha,
  				   "scsi(%ld): %s, Dumping hw/fw registers:
   "
  				   " PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2:"
  				   " 0x%x,
   PEG_NET_0_PC: 0x%x, PEG_NET_1_PC:"
  				   " 0x%x,
   PEG_NET_2_PC: 0x%x, PEG_NET_3_PC:"
  				   " 0x%x,
   PEG_NET_4_PC: 0x%x
  ",
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2055
2056
2057
  				   ha->host_no, __func__,
  				   qla4_8xxx_rd_32(ha,
  						   QLA82XX_PEG_HALT_STATUS1),
68d92ebf5   Vikas Chaudhary   [SCSI] qla4xxx: D...
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
  				   qla4_8xxx_rd_32(ha,
  						   QLA82XX_PEG_HALT_STATUS2),
  				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_0 +
  						   0x3c),
  				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_1 +
  						   0x3c),
  				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_2 +
  						   0x3c),
  				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_3 +
  						   0x3c),
  				   qla4_8xxx_rd_32(ha, QLA82XX_CRB_PEG_NET_4 +
  						   0x3c));
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2070
  			status = QLA_ERROR;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2071
  		}
99457d75f   Lalit Chandivade   [SCSI] qla4xxx: R...
2072
2073
  	} else
  		ha->seconds_since_last_heartbeat = 0;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2074
  	ha->fw_heartbeat_counter = fw_heartbeat_counter;
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2075
  	return status;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
  }
  
  /**
   * qla4_8xxx_watchdog - Poll dev state
   * @ha: Pointer to host adapter structure.
   *
   * Context: Interrupt
   **/
  void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
  {
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2086
  	uint32_t dev_state, halt_status;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2087
2088
  
  	/* don't poll if reset is going on */
d56a1f7b3   Lalit Chandivade   [SCSI] qla4xxx: d...
2089
2090
  	if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
  	    test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
977f46a4b   Vikas Chaudhary   [SCSI] qla4xxx: D...
2091
  	    test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2092
  		dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2093
2094
  		if (dev_state == QLA82XX_DEV_NEED_RESET &&
  		    !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
3930b8c1f   Vikas Chaudhary   [SCSI] qla4xxx: d...
2095
2096
2097
2098
2099
2100
  			if (!ql4xdontresethba) {
  				ql4_printk(KERN_INFO, ha, "%s: HW State: "
  				    "NEED RESET!
  ", __func__);
  				set_bit(DPC_RESET_HA, &ha->dpc_flags);
  				qla4xxx_wake_dpc(ha);
3930b8c1f   Vikas Chaudhary   [SCSI] qla4xxx: d...
2101
  			}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2102
2103
  		} else if (dev_state == QLA82XX_DEV_NEED_QUIESCENT &&
  		    !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
3930b8c1f   Vikas Chaudhary   [SCSI] qla4xxx: d...
2104
2105
2106
  			ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!
  ",
  			    __func__);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2107
2108
2109
2110
  			set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
  			qla4xxx_wake_dpc(ha);
  		} else  {
  			/* Check firmware health */
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
  			if (qla4_8xxx_check_fw_alive(ha)) {
  				halt_status = qla4_8xxx_rd_32(ha,
  						QLA82XX_PEG_HALT_STATUS1);
  
  				/* Since we cannot change dev_state in interrupt
  				 * context, set appropriate DPC flag then wakeup
  				 * DPC */
  				if (halt_status & HALT_STATUS_UNRECOVERABLE)
  					set_bit(DPC_HA_UNRECOVERABLE,
  						&ha->dpc_flags);
  				else {
  					ql4_printk(KERN_INFO, ha, "%s: detect "
  						   "abort needed!
  ", __func__);
  					set_bit(DPC_RESET_HA, &ha->dpc_flags);
  				}
  				qla4xxx_mailbox_premature_completion(ha);
  				qla4xxx_wake_dpc(ha);
  			}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2130
2131
2132
  		}
  	}
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
  void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  {
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  
  	if (!(ddb_entry->ddb_type == FLASH_DDB))
  		return;
  
  	if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
  	    !iscsi_is_session_online(cls_sess)) {
  		if (atomic_read(&ddb_entry->retry_relogin_timer) !=
  		    INVALID_ENTRY) {
  			if (atomic_read(&ddb_entry->retry_relogin_timer) ==
  					0) {
  				atomic_set(&ddb_entry->retry_relogin_timer,
  					   INVALID_ENTRY);
  				set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  				set_bit(DF_RELOGIN, &ddb_entry->flags);
  				DEBUG2(ql4_printk(KERN_INFO, ha,
  				       "%s: index [%d] login device
  ",
  					__func__, ddb_entry->fw_ddb_index));
  			} else
  				atomic_dec(&ddb_entry->retry_relogin_timer);
  		}
  	}
  
  	/* Wait for relogin to timeout */
  	if (atomic_read(&ddb_entry->relogin_timer) &&
  	    (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
  		/*
  		 * If the relogin times out and the device is
  		 * still NOT ONLINE then try and relogin again.
  		 */
  		if (!iscsi_is_session_online(cls_sess)) {
  			/* Reset retry relogin timer */
  			atomic_inc(&ddb_entry->relogin_retry_count);
  			DEBUG2(ql4_printk(KERN_INFO, ha,
  				"%s: index[%d] relogin timed out-retrying"
  				" relogin (%d), retry (%d)
  ", __func__,
  				ddb_entry->fw_ddb_index,
  				atomic_read(&ddb_entry->relogin_retry_count),
  				ddb_entry->default_time2wait + 4));
  			set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  			atomic_set(&ddb_entry->retry_relogin_timer,
  				   ddb_entry->default_time2wait + 4);
  		}
  	}
  }
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2188
  /**
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2189
2190
2191
2192
2193
   * qla4xxx_timer - checks every second for work to do.
   * @ha: Pointer to host adapter structure.
   **/
  static void qla4xxx_timer(struct scsi_qla_host *ha)
  {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2194
  	int start_dpc = 0;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
2195
  	uint16_t w;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2196
  	iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
  	/* If we are in the middle of AER/EEH processing
  	 * skip any processing and reschedule the timer
  	 */
  	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  		mod_timer(&ha->timer, jiffies + HZ);
  		return;
  	}
  
  	/* Hardware read to trigger an EEH error during mailbox waits. */
  	if (!pci_channel_offline(ha->pdev))
  		pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2208

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2209
2210
2211
  	if (is_qla8022(ha)) {
  		qla4_8xxx_watchdog(ha);
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2212
2213
2214
2215
2216
2217
2218
2219
2220
  	if (!is_qla8022(ha)) {
  		/* Check for heartbeat interval. */
  		if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
  		    ha->heartbeat_interval != 0) {
  			ha->seconds_since_last_heartbeat++;
  			if (ha->seconds_since_last_heartbeat >
  			    ha->heartbeat_interval + 2)
  				set_bit(DPC_RESET_HA, &ha->dpc_flags);
  		}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2221
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2222
  	/* Wakeup the dpc routine for this adapter, if needed. */
1b46807e0   Lalit Chandivade   [SCSI] qla4xxx: R...
2223
  	if (start_dpc ||
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2224
2225
2226
  	     test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
  	     test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
  	     test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2227
  	     test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2228
2229
  	     test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
  	     test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
065aa1b4d   Vikas Chaudhary   [SCSI] qla4xxx: s...
2230
  	     test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2231
2232
  	     test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
  	     test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1b46807e0   Lalit Chandivade   [SCSI] qla4xxx: R...
2233
  	     test_bit(DPC_AEN, &ha->dpc_flags)) {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2234
2235
2236
2237
  		DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
  			      " - dpc flags = 0x%lx
  ",
  			      ha->host_no, __func__, ha->dpc_flags));
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2238
  		qla4xxx_wake_dpc(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
  	}
  
  	/* Reschedule timer thread to call us back in one second */
  	mod_timer(&ha->timer, jiffies + HZ);
  
  	DEBUG2(ha->seconds_since_last_intr++);
  }
  
  /**
   * qla4xxx_cmd_wait - waits for all outstanding commands to complete
   * @ha: Pointer to host adapter structure.
   *
   * This routine stalls the driver until all outstanding commands are returned.
   * Caller must release the Hardware Lock prior to calling this routine.
   **/
  static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
  {
  	uint32_t index = 0;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2257
2258
  	unsigned long flags;
  	struct scsi_cmnd *cmd;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2259

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2260
2261
2262
2263
2264
2265
2266
  	unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
  
  	DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
  	    "complete
  ", WAIT_CMD_TOV));
  
  	while (!time_after_eq(jiffies, wtime)) {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2267
2268
2269
2270
  		spin_lock_irqsave(&ha->hardware_lock, flags);
  		/* Find a command that hasn't completed. */
  		for (index = 0; index < ha->host->can_queue; index++) {
  			cmd = scsi_host_find_tag(ha->host, index);
a1e0063d0   Mike Christie   [SCSI] qla4xxx: F...
2271
2272
2273
2274
2275
2276
2277
  			/*
  			 * We cannot just check if the index is valid,
  			 * becase if we are run from the scsi eh, then
  			 * the scsi/block layer is going to prevent
  			 * the tag from being released.
  			 */
  			if (cmd != NULL && CMD_SP(cmd))
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2278
2279
2280
2281
2282
  				break;
  		}
  		spin_unlock_irqrestore(&ha->hardware_lock, flags);
  
  		/* If No Commands are pending, wait is complete */
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2283
2284
  		if (index == ha->host->can_queue)
  			return QLA_SUCCESS;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2285

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2286
2287
2288
2289
2290
  		msleep(1000);
  	}
  	/* If we timed out on waiting for commands to come back
  	 * return ERROR. */
  	return QLA_ERROR;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2291
  }
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2292
  int qla4xxx_hw_reset(struct scsi_qla_host *ha)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2293
  {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2294
  	uint32_t ctrl_status;
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2295
2296
2297
2298
  	unsigned long flags = 0;
  
  	DEBUG2(printk(KERN_ERR "scsi%ld: %s
  ", ha->host_no, __func__));
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2299

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2300
2301
  	if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
  		return QLA_ERROR;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
  	spin_lock_irqsave(&ha->hardware_lock, flags);
  
  	/*
  	 * If the SCSI Reset Interrupt bit is set, clear it.
  	 * Otherwise, the Soft Reset won't work.
  	 */
  	ctrl_status = readw(&ha->reg->ctrl_status);
  	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
  		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  
  	/* Issue Soft Reset */
  	writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
  	readl(&ha->reg->ctrl_status);
  
  	spin_unlock_irqrestore(&ha->hardware_lock, flags);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2317
  	return QLA_SUCCESS;
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
  }
  
  /**
   * qla4xxx_soft_reset - performs soft reset.
   * @ha: Pointer to host adapter structure.
   **/
  int qla4xxx_soft_reset(struct scsi_qla_host *ha)
  {
  	uint32_t max_wait_time;
  	unsigned long flags = 0;
f931c5343   Vikas Chaudhary   [SCSI] qla4xxx: h...
2328
  	int status;
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2329
  	uint32_t ctrl_status;
f931c5343   Vikas Chaudhary   [SCSI] qla4xxx: h...
2330
2331
2332
  	status = qla4xxx_hw_reset(ha);
  	if (status != QLA_SUCCESS)
  		return status;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2333

f931c5343   Vikas Chaudhary   [SCSI] qla4xxx: h...
2334
  	status = QLA_ERROR;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
  	/* Wait until the Network Reset Intr bit is cleared */
  	max_wait_time = RESET_INTR_TOV;
  	do {
  		spin_lock_irqsave(&ha->hardware_lock, flags);
  		ctrl_status = readw(&ha->reg->ctrl_status);
  		spin_unlock_irqrestore(&ha->hardware_lock, flags);
  
  		if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
  			break;
  
  		msleep(1000);
  	} while ((--max_wait_time));
  
  	if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
  		DEBUG2(printk(KERN_WARNING
  			      "scsi%ld: Network Reset Intr not cleared by "
  			      "Network function, clearing it now!
  ",
  			      ha->host_no));
  		spin_lock_irqsave(&ha->hardware_lock, flags);
  		writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
  		readl(&ha->reg->ctrl_status);
  		spin_unlock_irqrestore(&ha->hardware_lock, flags);
  	}
  
  	/* Wait until the firmware tells us the Soft Reset is done */
  	max_wait_time = SOFT_RESET_TOV;
  	do {
  		spin_lock_irqsave(&ha->hardware_lock, flags);
  		ctrl_status = readw(&ha->reg->ctrl_status);
  		spin_unlock_irqrestore(&ha->hardware_lock, flags);
  
  		if ((ctrl_status & CSR_SOFT_RESET) == 0) {
  			status = QLA_SUCCESS;
  			break;
  		}
  
  		msleep(1000);
  	} while ((--max_wait_time));
  
  	/*
  	 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
  	 * after the soft reset has taken place.
  	 */
  	spin_lock_irqsave(&ha->hardware_lock, flags);
  	ctrl_status = readw(&ha->reg->ctrl_status);
  	if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
  		writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
  		readl(&ha->reg->ctrl_status);
  	}
  	spin_unlock_irqrestore(&ha->hardware_lock, flags);
  
  	/* If soft reset fails then most probably the bios on other
  	 * function is also enabled.
  	 * Since the initialization is sequential the other fn
  	 * wont be able to acknowledge the soft reset.
  	 * Issue a force soft reset to workaround this scenario.
  	 */
  	if (max_wait_time == 0) {
  		/* Issue Force Soft Reset */
  		spin_lock_irqsave(&ha->hardware_lock, flags);
  		writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
  		readl(&ha->reg->ctrl_status);
  		spin_unlock_irqrestore(&ha->hardware_lock, flags);
  		/* Wait until the firmware tells us the Soft Reset is done */
  		max_wait_time = SOFT_RESET_TOV;
  		do {
  			spin_lock_irqsave(&ha->hardware_lock, flags);
  			ctrl_status = readw(&ha->reg->ctrl_status);
  			spin_unlock_irqrestore(&ha->hardware_lock, flags);
  
  			if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
  				status = QLA_SUCCESS;
  				break;
  			}
  
  			msleep(1000);
  		} while ((--max_wait_time));
  	}
  
  	return status;
  }
  
  /**
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2419
   * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2420
   * @ha: Pointer to host adapter structure.
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2421
   * @res: returned scsi status
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2422
2423
2424
2425
2426
2427
   *
   * This routine is called just prior to a HARD RESET to return all
   * outstanding commands back to the Operating System.
   * Caller should make sure that the following locks are released
   * before this calling routine: Hardware lock, and io_request_lock.
   **/
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2428
  static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2429
2430
2431
2432
2433
2434
2435
2436
2437
  {
  	struct srb *srb;
  	int i;
  	unsigned long flags;
  
  	spin_lock_irqsave(&ha->hardware_lock, flags);
  	for (i = 0; i < ha->host->can_queue; i++) {
  		srb = qla4xxx_del_from_active_array(ha, i);
  		if (srb != NULL) {
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2438
  			srb->cmd->result = res;
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
2439
  			kref_put(&srb->srb_ref, qla4xxx_srb_compl);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2440
2441
2442
  		}
  	}
  	spin_unlock_irqrestore(&ha->hardware_lock, flags);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2443
  }
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2444
2445
2446
2447
2448
2449
2450
  void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
  {
  	clear_bit(AF_ONLINE, &ha->flags);
  
  	/* Disable the board */
  	ql4_printk(KERN_INFO, ha, "Disabling the board
  ");
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2451
2452
2453
2454
2455
  
  	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  	qla4xxx_mark_all_devices_missing(ha);
  	clear_bit(AF_INIT_DONE, &ha->flags);
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2456
2457
2458
2459
2460
2461
2462
2463
  static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
  {
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  
  	sess = cls_session->dd_data;
  	ddb_entry = sess->dd_data;
  	ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2464
2465
2466
2467
2468
2469
  
  	if (ddb_entry->ddb_type == FLASH_DDB)
  		iscsi_block_session(ddb_entry->sess);
  	else
  		iscsi_session_failure(cls_session->dd_data,
  				      ISCSI_ERR_CONN_FAILED);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2470
  }
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2471
  /**
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2472
2473
   * qla4xxx_recover_adapter - recovers adapter after a fatal error
   * @ha: Pointer to host adapter structure.
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2474
   **/
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2475
  static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2476
  {
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2477
2478
  	int status = QLA_ERROR;
  	uint8_t reset_chip = 0;
8e0f3a66a   Sarang Radke   [SCSI] qla4xxx: D...
2479
  	uint32_t dev_state;
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2480
  	unsigned long wait;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2481
2482
  
  	/* Stall incoming I/O until we are done */
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2483
  	scsi_block_requests(ha->host);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2484
  	clear_bit(AF_ONLINE, &ha->flags);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2485
  	clear_bit(AF_LINK_UP, &ha->flags);
50a29aec9   Mike Christie   [SCSI] qla4xxx: f...
2486

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2487
2488
  	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE
  ", __func__));
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2489

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2490
  	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2491

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2492
  	iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2493
2494
  	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  		reset_chip = 1;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2495

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2496
2497
2498
2499
2500
2501
  	/* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
  	 * do not reset adapter, jump to initialize_adapter */
  	if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
  		status = QLA_SUCCESS;
  		goto recover_ha_init_adapter;
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2502

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
  	/* For the ISP-82xx adapter, issue a stop_firmware if invoked
  	 * from eh_host_reset or ioctl module */
  	if (is_qla8022(ha) && !reset_chip &&
  	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
  
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  		    "scsi%ld: %s - Performing stop_firmware...
  ",
  		    ha->host_no, __func__));
  		status = ha->isp_ops->reset_firmware(ha);
  		if (status == QLA_SUCCESS) {
2bd1e2be8   Nilesh Javali   [SCSI] qla4xxx: O...
2514
2515
  			if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  				qla4xxx_cmd_wait(ha);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
  			ha->isp_ops->disable_intrs(ha);
  			qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  		} else {
  			/* If the stop_firmware fails then
  			 * reset the entire chip */
  			reset_chip = 1;
  			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  			set_bit(DPC_RESET_HA, &ha->dpc_flags);
  		}
  	}
dca05c4c0   Karen Higgins   [SCSI] qla4xxx: F...
2527

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2528
2529
2530
2531
  	/* Issue full chip reset if recovering from a catastrophic error,
  	 * or if stop_firmware fails for ISP-82xx.
  	 * This is the default case for ISP-4xxx */
  	if (!is_qla8022(ha) || reset_chip) {
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
  		if (!is_qla8022(ha))
  			goto chip_reset;
  
  		/* Check if 82XX firmware is alive or not
  		 * We may have arrived here from NEED_RESET
  		 * detection only */
  		if (test_bit(AF_FW_RECOVERY, &ha->flags))
  			goto chip_reset;
  
  		wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
  		while (time_before(jiffies, wait)) {
  			if (qla4_8xxx_check_fw_alive(ha)) {
  				qla4xxx_mailbox_premature_completion(ha);
  				break;
  			}
  
  			set_current_state(TASK_UNINTERRUPTIBLE);
  			schedule_timeout(HZ);
  		}
2bd1e2be8   Nilesh Javali   [SCSI] qla4xxx: O...
2551
2552
  		if (!test_bit(AF_FW_RECOVERY, &ha->flags))
  			qla4xxx_cmd_wait(ha);
9ee91a38b   Shyam Sunder   [SCSI] qla4xxx: c...
2553
  chip_reset:
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2554
2555
2556
2557
2558
2559
2560
2561
  		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
  		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  		    "scsi%ld: %s - Performing chip reset..
  ",
  		    ha->host_no, __func__));
  		status = ha->isp_ops->reset_chip(ha);
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2562
2563
2564
  
  	/* Flush any pending ddb changed AENs */
  	qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2565
2566
  recover_ha_init_adapter:
  	/* Upon successful firmware/chip reset, re-initialize the adapter */
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2567
  	if (status == QLA_SUCCESS) {
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2568
2569
2570
2571
2572
2573
2574
2575
  		/* For ISP-4xxx, force function 1 to always initialize
  		 * before function 3 to prevent both funcions from
  		 * stepping on top of the other */
  		if (!is_qla8022(ha) && (ha->mac_index == 3))
  			ssleep(6);
  
  		/* NOTE: AF_ONLINE flag set upon successful completion of
  		 *       qla4xxx_initialize_adapter */
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2576
  		status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2577
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2578
2579
2580
2581
2582
  	/* Retry failed adapter initialization, if necessary
  	 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
  	 * case to prevent ping-pong resets between functions */
  	if (!test_bit(AF_ONLINE, &ha->flags) &&
  	    !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2583
  		/* Adapter initialization failed, see if we can retry
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2584
2585
2586
2587
  		 * resetting the ha.
  		 * Since we don't want to block the DPC for too long
  		 * with multiple resets in the same thread,
  		 * utilize DPC to retry */
8e0f3a66a   Sarang Radke   [SCSI] qla4xxx: D...
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
  		if (is_qla8022(ha)) {
  			qla4_8xxx_idc_lock(ha);
  			dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  			qla4_8xxx_idc_unlock(ha);
  			if (dev_state == QLA82XX_DEV_FAILED) {
  				ql4_printk(KERN_INFO, ha, "%s: don't retry "
  					   "recover adapter. H/W is in Failed "
  					   "state
  ", __func__);
  				qla4xxx_dead_adapter_cleanup(ha);
  				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  				clear_bit(DPC_RESET_HA_FW_CONTEXT,
  						&ha->dpc_flags);
  				status = QLA_ERROR;
  
  				goto exit_recover;
  			}
  		}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
  		if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
  			ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
  			DEBUG2(printk("scsi%ld: recover adapter - retrying "
  				      "(%d) more times
  ", ha->host_no,
  				      ha->retry_reset_ha_cnt));
  			set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  			status = QLA_ERROR;
  		} else {
  			if (ha->retry_reset_ha_cnt > 0) {
  				/* Schedule another Reset HA--DPC will retry */
  				ha->retry_reset_ha_cnt--;
  				DEBUG2(printk("scsi%ld: recover adapter - "
  					      "retry remaining %d
  ",
  					      ha->host_no,
  					      ha->retry_reset_ha_cnt));
  				status = QLA_ERROR;
  			}
  
  			if (ha->retry_reset_ha_cnt == 0) {
  				/* Recover adapter retries have been exhausted.
  				 * Adapter DEAD */
  				DEBUG2(printk("scsi%ld: recover adapter "
  					      "failed - board disabled
  ",
  					      ha->host_no));
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2634
  				qla4xxx_dead_adapter_cleanup(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2635
2636
  				clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  				clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2637
  				clear_bit(DPC_RESET_HA_FW_CONTEXT,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2638
2639
2640
2641
2642
2643
  					  &ha->dpc_flags);
  				status = QLA_ERROR;
  			}
  		}
  	} else {
  		clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2644
  		clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2645
2646
  		clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
  	}
8e0f3a66a   Sarang Radke   [SCSI] qla4xxx: D...
2647
  exit_recover:
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2648
  	ha->adapter_error_count++;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2649
2650
2651
2652
2653
2654
2655
2656
  	if (test_bit(AF_ONLINE, &ha->flags))
  		ha->isp_ops->enable_intrs(ha);
  
  	scsi_unblock_requests(ha->host);
  
  	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  	DEBUG2(printk("scsi%ld: recover adapter: %s
  ", ha->host_no,
25985edce   Lucas De Marchi   Fix common misspe...
2657
  	    status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2658

afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2659
2660
  	return status;
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2661
  static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2d7924e6b   Vikas Chaudhary   [SCSI] qla4xxx: a...
2662
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2663
2664
2665
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
2d7924e6b   Vikas Chaudhary   [SCSI] qla4xxx: a...
2666

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
  	sess = cls_session->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  	if (!iscsi_is_session_online(cls_session)) {
  		if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
  			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  				   " unblock session
  ", ha->host_no, __func__,
  				   ddb_entry->fw_ddb_index);
  			iscsi_unblock_session(ddb_entry->sess);
  		} else {
  			/* Trigger relogin */
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2679
2680
2681
2682
2683
2684
  			if (ddb_entry->ddb_type == FLASH_DDB) {
  				if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
  					qla4xxx_arm_relogin_timer(ddb_entry);
  			} else
  				iscsi_session_failure(cls_session->dd_data,
  						      ISCSI_ERR_CONN_FAILED);
2d7924e6b   Vikas Chaudhary   [SCSI] qla4xxx: a...
2685
2686
2687
  		}
  	}
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
  int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
  {
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  
  	sess = cls_session->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  	ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  		   " unblock session
  ", ha->host_no, __func__,
  		   ddb_entry->fw_ddb_index);
  
  	iscsi_unblock_session(ddb_entry->sess);
  
  	/* Start scan target */
  	if (test_bit(AF_ONLINE, &ha->flags)) {
  		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  			   " start scan
  ", ha->host_no, __func__,
  			   ddb_entry->fw_ddb_index);
  		scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
  	}
  	return QLA_SUCCESS;
  }
  
  int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
  {
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  
  	sess = cls_session->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  	ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
  		   " unblock user space session
  ", ha->host_no, __func__,
  		   ddb_entry->fw_ddb_index);
  	iscsi_conn_start(ddb_entry->conn);
  	iscsi_conn_login_event(ddb_entry->conn,
  			       ISCSI_CONN_STATE_LOGGED_IN);
  
  	return QLA_SUCCESS;
  }
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2734
2735
2736
2737
  static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
  {
  	iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
  static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
  {
  	uint16_t relogin_timer;
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  
  	relogin_timer = max(ddb_entry->default_relogin_timeout,
  			    (uint16_t)RELOGIN_TOV);
  	atomic_set(&ddb_entry->relogin_timer, relogin_timer);
  
  	DEBUG2(ql4_printk(KERN_INFO, ha,
  			  "scsi%ld: Relogin index [%d]. TOV=%d
  ", ha->host_no,
  			  ddb_entry->fw_ddb_index, relogin_timer));
  
  	qla4xxx_login_flash_ddb(cls_sess);
  }
  
  static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
  {
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb_entry;
  	struct scsi_qla_host *ha;
  
  	sess = cls_sess->dd_data;
  	ddb_entry = sess->dd_data;
  	ha = ddb_entry->ha;
  
  	if (!(ddb_entry->ddb_type == FLASH_DDB))
  		return;
  
  	if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
  	    !iscsi_is_session_online(cls_sess)) {
  		DEBUG2(ql4_printk(KERN_INFO, ha,
  				  "relogin issued
  "));
  		qla4xxx_relogin_flash_ddb(cls_sess);
  	}
  }
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2782
2783
  void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
  {
1b46807e0   Lalit Chandivade   [SCSI] qla4xxx: R...
2784
  	if (ha->dpc_thread)
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2785
  		queue_work(ha->dpc_thread, &ha->dpc_work);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2786
  }
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
  /**
   * qla4xxx_do_dpc - dpc routine
   * @data: in our case pointer to adapter structure
   *
   * This routine is a task that is schedule by the interrupt handler
   * to perform the background processing for interrupts.  We put it
   * on a task queue that is consumed whenever the scheduler runs; that's
   * so you can do anything (i.e. put the process to sleep etc).  In fact,
   * the mid-level tries to sleep when it reaches the driver threshold
   * "host->can_queue". This can cause a panic if we were in our interrupt code.
   **/
c4028958b   David Howells   WorkStruct: make ...
2798
  static void qla4xxx_do_dpc(struct work_struct *work)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2799
  {
c4028958b   David Howells   WorkStruct: make ...
2800
2801
  	struct scsi_qla_host *ha =
  		container_of(work, struct scsi_qla_host, dpc_work);
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2802
  	int status = QLA_ERROR;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2803

f26b90440   David C Somayajulu   [PATCH] qla4xxx: ...
2804
  	DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2805
2806
2807
  	    "flags = 0x%08lx, dpc_flags = 0x%08lx
  ",
  	    ha->host_no, __func__, ha->flags, ha->dpc_flags))
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2808
2809
2810
  
  	/* Initialization not yet finished. Don't do anything yet. */
  	if (!test_bit(AF_INIT_DONE, &ha->flags))
1b46807e0   Lalit Chandivade   [SCSI] qla4xxx: R...
2811
  		return;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2812

2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
2813
2814
2815
2816
  	if (test_bit(AF_EEH_BUSY, &ha->flags)) {
  		DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx
  ",
  		    ha->host_no, __func__, ha->flags));
1b46807e0   Lalit Chandivade   [SCSI] qla4xxx: R...
2817
  		return;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
2818
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
  	if (is_qla8022(ha)) {
  		if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
  			qla4_8xxx_idc_lock(ha);
  			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  			    QLA82XX_DEV_FAILED);
  			qla4_8xxx_idc_unlock(ha);
  			ql4_printk(KERN_INFO, ha, "HW State: FAILED
  ");
  			qla4_8xxx_device_state_handler(ha);
  		}
  		if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
  			qla4_8xxx_need_qsnt_handler(ha);
  		}
  	}
  
  	if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
  	    (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2836
  	    test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
  	    test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
  		if (ql4xdontresethba) {
  			DEBUG2(printk("scsi%ld: %s: Don't Reset HBA
  ",
  			    ha->host_no, __func__));
  			clear_bit(DPC_RESET_HA, &ha->dpc_flags);
  			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  			clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  			goto dpc_post_reset_ha;
  		}
  		if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
  		    test_bit(DPC_RESET_HA, &ha->dpc_flags))
  			qla4xxx_recover_adapter(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2850

477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2851
  		if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2852
  			uint8_t wait_time = RESET_INTR_TOV;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2853

afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2854
2855
2856
2857
  			while ((readw(&ha->reg->ctrl_status) &
  				(CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
  				if (--wait_time == 0)
  					break;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2858
  				msleep(1000);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2859
  			}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2860
2861
2862
2863
2864
  			if (wait_time == 0)
  				DEBUG2(printk("scsi%ld: %s: SR|FSR "
  					      "bit not cleared-- resetting
  ",
  					      ha->host_no, __func__));
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2865
  			qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2866
2867
  			if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
  				qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2868
  				status = qla4xxx_recover_adapter(ha);
477ffb9d8   David C Somayajulu   [SCSI] qla4xxx: b...
2869
2870
2871
  			}
  			clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
  			if (status == QLA_SUCCESS)
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2872
  				ha->isp_ops->enable_intrs(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2873
2874
  		}
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2875
  dpc_post_reset_ha:
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2876
2877
2878
2879
2880
2881
2882
  	/* ---- process AEN? --- */
  	if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
  		qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
  
  	/* ---- Get DHCP IP Address? --- */
  	if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
  		qla4xxx_get_dhcp_ip_address(ha);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2883
2884
2885
2886
2887
  	/* ---- relogin device? --- */
  	if (adapter_up(ha) &&
  	    test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
  		iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
  	}
065aa1b4d   Vikas Chaudhary   [SCSI] qla4xxx: s...
2888
2889
2890
2891
  	/* ---- link change? --- */
  	if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
  		if (!test_bit(AF_LINK_UP, &ha->flags)) {
  			/* ---- link down? --- */
2d7924e6b   Vikas Chaudhary   [SCSI] qla4xxx: a...
2892
  			qla4xxx_mark_all_devices_missing(ha);
065aa1b4d   Vikas Chaudhary   [SCSI] qla4xxx: s...
2893
2894
2895
2896
2897
2898
2899
  		} else {
  			/* ---- link up? --- *
  			 * F/W will auto login to all devices ONLY ONCE after
  			 * link up during driver initialization and runtime
  			 * fatal error recovery.  Therefore, the driver must
  			 * manually relogin to devices when recovering from
  			 * connection failures, logouts, expired KATO, etc. */
13483730a   Mike Christie   [SCSI] qla4xxx: f...
2900
2901
2902
2903
2904
2905
  			if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
  				qla4xxx_build_ddb_list(ha, ha->is_reset);
  				iscsi_host_for_each_session(ha->host,
  						qla4xxx_login_flash_ddb);
  			} else
  				qla4xxx_relogin_all_devices(ha);
065aa1b4d   Vikas Chaudhary   [SCSI] qla4xxx: s...
2906
2907
  		}
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2908
2909
2910
2911
2912
2913
2914
2915
  }
  
  /**
   * qla4xxx_free_adapter - release the adapter
   * @ha: pointer to adapter structure
   **/
  static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
  {
8a2889601   Sarang Radke   [SCSI] qla4xxx: f...
2916
  	qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2917
2918
2919
  
  	if (test_bit(AF_INTERRUPTS_ON, &ha->flags)) {
  		/* Turn-off interrupts on the card. */
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2920
  		ha->isp_ops->disable_intrs(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2921
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2922
2923
2924
  	/* Remove timer thread, if present */
  	if (ha->timer_active)
  		qla4xxx_stop_timer(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2925
2926
2927
  	/* Kill the kernel thread for this host */
  	if (ha->dpc_thread)
  		destroy_workqueue(ha->dpc_thread);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
2928
2929
2930
  	/* Kill the kernel thread for this host */
  	if (ha->task_wq)
  		destroy_workqueue(ha->task_wq);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2931
2932
  	/* Put firmware in known state */
  	ha->isp_ops->reset_firmware(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2933

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2934
2935
2936
2937
2938
  	if (is_qla8022(ha)) {
  		qla4_8xxx_idc_lock(ha);
  		qla4_8xxx_clear_drv_active(ha);
  		qla4_8xxx_idc_unlock(ha);
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2939

afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2940
2941
  	/* Detach interrupts */
  	if (test_and_clear_bit(AF_IRQ_ATTACHED, &ha->flags))
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2942
  		qla4xxx_free_irqs(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2943

bee4fe8e6   David C Somayajulu   [SCSI] qla4xxx: q...
2944
2945
  	/* free extra memory */
  	qla4xxx_mem_free(ha);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
  }
  
  int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
  {
  	int status = 0;
  	uint8_t revision_id;
  	unsigned long mem_base, mem_len, db_base, db_len;
  	struct pci_dev *pdev = ha->pdev;
  
  	status = pci_request_regions(pdev, DRIVER_NAME);
  	if (status) {
  		printk(KERN_WARNING
  		    "scsi(%ld) Failed to reserve PIO regions (%s) "
  		    "status=%d
  ", ha->host_no, pci_name(pdev), status);
  		goto iospace_error_exit;
  	}
  
  	pci_read_config_byte(pdev, PCI_REVISION_ID, &revision_id);
  	DEBUG2(printk(KERN_INFO "%s: revision-id=%d
  ",
  	    __func__, revision_id));
  	ha->revision_id = revision_id;
bee4fe8e6   David C Somayajulu   [SCSI] qla4xxx: q...
2969

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2970
2971
2972
2973
2974
2975
  	/* remap phys address */
  	mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  	mem_len = pci_resource_len(pdev, 0);
  	DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx
  ",
  	    __func__, mem_base, mem_len));
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
2976

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
  	/* mapping of pcibase pointer */
  	ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
  	if (!ha->nx_pcibase) {
  		printk(KERN_ERR
  		    "cannot remap MMIO (%s), aborting
  ", pci_name(pdev));
  		pci_release_regions(ha->pdev);
  		goto iospace_error_exit;
  	}
  
  	/* Mapping of IO base pointer, door bell read and write pointer */
  
  	/* mapping of IO base pointer */
  	ha->qla4_8xxx_reg =
  	    (struct device_reg_82xx  __iomem *)((uint8_t *)ha->nx_pcibase +
  	    0xbc000 + (ha->pdev->devfn << 11));
  
  	db_base = pci_resource_start(pdev, 4);  /* doorbell is on bar 4 */
  	db_len = pci_resource_len(pdev, 4);
2657c800d   Shyam Sundar   [SCSI] qla4xxx: u...
2996
2997
  	ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
  	    QLA82XX_CAM_RAM_DB2);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
2998

2657c800d   Shyam Sundar   [SCSI] qla4xxx: u...
2999
  	return 0;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
3000
3001
  iospace_error_exit:
  	return -ENOMEM;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3002
3003
3004
3005
3006
3007
3008
3009
3010
  }
  
  /***
   * qla4xxx_iospace_config - maps registers
   * @ha: pointer to adapter structure
   *
   * This routines maps HBA's registers from the pci address space
   * into the kernel virtual address space for memory mapped i/o.
   **/
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
3011
  int qla4xxx_iospace_config(struct scsi_qla_host *ha)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3012
3013
3014
3015
3016
3017
3018
3019
3020
  {
  	unsigned long pio, pio_len, pio_flags;
  	unsigned long mmio, mmio_len, mmio_flags;
  
  	pio = pci_resource_start(ha->pdev, 0);
  	pio_len = pci_resource_len(ha->pdev, 0);
  	pio_flags = pci_resource_flags(ha->pdev, 0);
  	if (pio_flags & IORESOURCE_IO) {
  		if (pio_len < MIN_IOBASE_LEN) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3021
  			ql4_printk(KERN_WARNING, ha,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3022
3023
3024
3025
3026
  				"Invalid PCI I/O region size
  ");
  			pio = 0;
  		}
  	} else {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3027
3028
  		ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3029
3030
3031
3032
3033
3034
3035
3036
3037
  		pio = 0;
  	}
  
  	/* Use MMIO operations for all accesses. */
  	mmio = pci_resource_start(ha->pdev, 1);
  	mmio_len = pci_resource_len(ha->pdev, 1);
  	mmio_flags = pci_resource_flags(ha->pdev, 1);
  
  	if (!(mmio_flags & IORESOURCE_MEM)) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3038
3039
3040
  		ql4_printk(KERN_ERR, ha,
  		    "region #0 not an MMIO resource, aborting
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3041
3042
3043
  
  		goto iospace_error_exit;
  	}
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3044

afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3045
  	if (mmio_len < MIN_IOBASE_LEN) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3046
3047
3048
  		ql4_printk(KERN_ERR, ha,
  		    "Invalid PCI mem region size, aborting
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3049
3050
3051
3052
  		goto iospace_error_exit;
  	}
  
  	if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3053
3054
3055
  		ql4_printk(KERN_WARNING, ha,
  		    "Failed to reserve PIO/MMIO regions
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3056
3057
3058
3059
3060
3061
3062
3063
  
  		goto iospace_error_exit;
  	}
  
  	ha->pio_address = pio;
  	ha->pio_length = pio_len;
  	ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
  	if (!ha->reg) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
3064
3065
3066
  		ql4_printk(KERN_ERR, ha,
  		    "cannot remap MMIO, aborting
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
3067
3068
3069
3070
3071
3072
3073
3074
3075
  
  		goto iospace_error_exit;
  	}
  
  	return 0;
  
  iospace_error_exit:
  	return -ENOMEM;
  }
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
  static struct isp_operations qla4xxx_isp_ops = {
  	.iospace_config         = qla4xxx_iospace_config,
  	.pci_config             = qla4xxx_pci_config,
  	.disable_intrs          = qla4xxx_disable_intrs,
  	.enable_intrs           = qla4xxx_enable_intrs,
  	.start_firmware         = qla4xxx_start_firmware,
  	.intr_handler           = qla4xxx_intr_handler,
  	.interrupt_service_routine = qla4xxx_interrupt_service_routine,
  	.reset_chip             = qla4xxx_soft_reset,
  	.reset_firmware         = qla4xxx_hw_reset,
  	.queue_iocb             = qla4xxx_queue_iocb,
  	.complete_iocb          = qla4xxx_complete_iocb,
  	.rd_shdw_req_q_out      = qla4xxx_rd_shdw_req_q_out,
  	.rd_shdw_rsp_q_in       = qla4xxx_rd_shdw_rsp_q_in,
  	.get_sys_info           = qla4xxx_get_sys_info,
  };
  
  static struct isp_operations qla4_8xxx_isp_ops = {
  	.iospace_config         = qla4_8xxx_iospace_config,
  	.pci_config             = qla4_8xxx_pci_config,
  	.disable_intrs          = qla4_8xxx_disable_intrs,
  	.enable_intrs           = qla4_8xxx_enable_intrs,
  	.start_firmware         = qla4_8xxx_load_risc,
  	.intr_handler           = qla4_8xxx_intr_handler,
  	.interrupt_service_routine = qla4_8xxx_interrupt_service_routine,
  	.reset_chip             = qla4_8xxx_isp_reset,
  	.reset_firmware         = qla4_8xxx_stop_firmware,
  	.queue_iocb             = qla4_8xxx_queue_iocb,
  	.complete_iocb          = qla4_8xxx_complete_iocb,
  	.rd_shdw_req_q_out      = qla4_8xxx_rd_shdw_req_q_out,
  	.rd_shdw_rsp_q_in       = qla4_8xxx_rd_shdw_rsp_q_in,
  	.get_sys_info           = qla4_8xxx_get_sys_info,
  };
  
  uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  {
  	return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
  }
  
  uint16_t qla4_8xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
  {
  	return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->req_q_out));
  }
  
  uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  {
  	return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
  }
  
  uint16_t qla4_8xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
  {
  	return (uint16_t)le32_to_cpu(readl(&ha->qla4_8xxx_reg->rsp_q_in));
  }
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
  static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
  {
  	struct scsi_qla_host *ha = data;
  	char *str = buf;
  	int rc;
  
  	switch (type) {
  	case ISCSI_BOOT_ETH_FLAGS:
  		rc = sprintf(str, "%d
  ", SYSFS_FLAG_FW_SEL_BOOT);
  		break;
  	case ISCSI_BOOT_ETH_INDEX:
  		rc = sprintf(str, "0
  ");
  		break;
  	case ISCSI_BOOT_ETH_MAC:
  		rc = sysfs_format_mac(str, ha->my_mac,
  				      MAC_ADDR_LEN);
  		break;
  	default:
  		rc = -ENOSYS;
  		break;
  	}
  	return rc;
  }
587a1f165   Al Viro   switch ->is_visib...
3154
  static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
  {
  	int rc;
  
  	switch (type) {
  	case ISCSI_BOOT_ETH_FLAGS:
  	case ISCSI_BOOT_ETH_MAC:
  	case ISCSI_BOOT_ETH_INDEX:
  		rc = S_IRUGO;
  		break;
  	default:
  		rc = 0;
  		break;
  	}
  	return rc;
  }
  
  static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
  {
  	struct scsi_qla_host *ha = data;
  	char *str = buf;
  	int rc;
  
  	switch (type) {
  	case ISCSI_BOOT_INI_INITIATOR_NAME:
  		rc = sprintf(str, "%s
  ", ha->name_string);
  		break;
  	default:
  		rc = -ENOSYS;
  		break;
  	}
  	return rc;
  }
587a1f165   Al Viro   switch ->is_visib...
3188
  static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
  {
  	int rc;
  
  	switch (type) {
  	case ISCSI_BOOT_INI_INITIATOR_NAME:
  		rc = S_IRUGO;
  		break;
  	default:
  		rc = 0;
  		break;
  	}
  	return rc;
  }
  
  static ssize_t
  qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
  			   char *buf)
  {
  	struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  	char *str = buf;
  	int rc;
  
  	switch (type) {
  	case ISCSI_BOOT_TGT_NAME:
  		rc = sprintf(buf, "%s
  ", (char *)&boot_sess->target_name);
  		break;
  	case ISCSI_BOOT_TGT_IP_ADDR:
  		if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
  			rc = sprintf(buf, "%pI4
  ",
  				     &boot_conn->dest_ipaddr.ip_address);
  		else
  			rc = sprintf(str, "%pI6
  ",
  				     &boot_conn->dest_ipaddr.ip_address);
  		break;
  	case ISCSI_BOOT_TGT_PORT:
  			rc = sprintf(str, "%d
  ", boot_conn->dest_port);
  		break;
  	case ISCSI_BOOT_TGT_CHAP_NAME:
  		rc = sprintf(str,  "%.*s
  ",
  			     boot_conn->chap.target_chap_name_length,
  			     (char *)&boot_conn->chap.target_chap_name);
  		break;
  	case ISCSI_BOOT_TGT_CHAP_SECRET:
  		rc = sprintf(str,  "%.*s
  ",
  			     boot_conn->chap.target_secret_length,
  			     (char *)&boot_conn->chap.target_secret);
  		break;
  	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  		rc = sprintf(str,  "%.*s
  ",
  			     boot_conn->chap.intr_chap_name_length,
  			     (char *)&boot_conn->chap.intr_chap_name);
  		break;
  	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  		rc = sprintf(str,  "%.*s
  ",
  			     boot_conn->chap.intr_secret_length,
  			     (char *)&boot_conn->chap.intr_secret);
  		break;
  	case ISCSI_BOOT_TGT_FLAGS:
  		rc = sprintf(str, "%d
  ", SYSFS_FLAG_FW_SEL_BOOT);
  		break;
  	case ISCSI_BOOT_TGT_NIC_ASSOC:
  		rc = sprintf(str, "0
  ");
  		break;
  	default:
  		rc = -ENOSYS;
  		break;
  	}
  	return rc;
  }
  
  static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
  {
  	struct scsi_qla_host *ha = data;
  	struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
  
  	return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  }
  
  static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
  {
  	struct scsi_qla_host *ha = data;
  	struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
  
  	return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
  }
587a1f165   Al Viro   switch ->is_visib...
3284
  static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
  {
  	int rc;
  
  	switch (type) {
  	case ISCSI_BOOT_TGT_NAME:
  	case ISCSI_BOOT_TGT_IP_ADDR:
  	case ISCSI_BOOT_TGT_PORT:
  	case ISCSI_BOOT_TGT_CHAP_NAME:
  	case ISCSI_BOOT_TGT_CHAP_SECRET:
  	case ISCSI_BOOT_TGT_REV_CHAP_NAME:
  	case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
  	case ISCSI_BOOT_TGT_NIC_ASSOC:
  	case ISCSI_BOOT_TGT_FLAGS:
  		rc = S_IRUGO;
  		break;
  	default:
  		rc = 0;
  		break;
  	}
  	return rc;
  }
  
  static void qla4xxx_boot_release(void *data)
  {
  	struct scsi_qla_host *ha = data;
  
  	scsi_host_put(ha->host);
  }
  
  static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
  {
  	dma_addr_t buf_dma;
  	uint32_t addr, pri_addr, sec_addr;
  	uint32_t offset;
  	uint16_t func_num;
  	uint8_t val;
  	uint8_t *buf = NULL;
  	size_t size = 13 * sizeof(uint8_t);
  	int ret = QLA_SUCCESS;
  
  	func_num = PCI_FUNC(ha->pdev->devfn);
0d5b36b8b   Manish Rangankar   [SCSI] qla4xxx: u...
3326
3327
3328
  	ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d
  ",
  		   __func__, ha->pdev->device, func_num);
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3329

0d5b36b8b   Manish Rangankar   [SCSI] qla4xxx: u...
3330
  	if (is_qla40XX(ha)) {
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
  		if (func_num == 1) {
  			addr = NVRAM_PORT0_BOOT_MODE;
  			pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
  			sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
  		} else if (func_num == 3) {
  			addr = NVRAM_PORT1_BOOT_MODE;
  			pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
  			sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
  		} else {
  			ret = QLA_ERROR;
  			goto exit_boot_info;
  		}
  
  		/* Check Boot Mode */
  		val = rd_nvram_byte(ha, addr);
  		if (!(val & 0x07)) {
  			DEBUG2(ql4_printk(KERN_ERR, ha,
  					  "%s: Failed Boot options : 0x%x
  ",
  					  __func__, val));
  			ret = QLA_ERROR;
  			goto exit_boot_info;
  		}
  
  		/* get primary valid target index */
  		val = rd_nvram_byte(ha, pri_addr);
  		if (val & BIT_7)
  			ddb_index[0] = (val & 0x7f);
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3359
3360
3361
3362
3363
  
  		/* get secondary valid target index */
  		val = rd_nvram_byte(ha, sec_addr);
  		if (val & BIT_7)
  			ddb_index[1] = (val & 0x7f);
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
  
  	} else if (is_qla8022(ha)) {
  		buf = dma_alloc_coherent(&ha->pdev->dev, size,
  					 &buf_dma, GFP_KERNEL);
  		if (!buf) {
  			DEBUG2(ql4_printk(KERN_ERR, ha,
  					  "%s: Unable to allocate dma buffer
  ",
  					   __func__));
  			ret = QLA_ERROR;
  			goto exit_boot_info;
  		}
  
  		if (ha->port_num == 0)
  			offset = BOOT_PARAM_OFFSET_PORT0;
  		else if (ha->port_num == 1)
  			offset = BOOT_PARAM_OFFSET_PORT1;
  		else {
  			ret = QLA_ERROR;
  			goto exit_boot_info_free;
  		}
  		addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
  		       offset;
  		if (qla4xxx_get_flash(ha, buf_dma, addr,
  				      13 * sizeof(uint8_t)) != QLA_SUCCESS) {
  			DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
  					  "failed
  ", ha->host_no, __func__));
  			ret = QLA_ERROR;
  			goto exit_boot_info_free;
  		}
  		/* Check Boot Mode */
  		if (!(buf[1] & 0x07)) {
  			DEBUG2(ql4_printk(KERN_INFO, ha,
  					  "Failed: Boot options : 0x%x
  ",
  					  buf[1]));
  			ret = QLA_ERROR;
  			goto exit_boot_info_free;
  		}
  
  		/* get primary valid target index */
  		if (buf[2] & BIT_7)
  			ddb_index[0] = buf[2] & 0x7f;
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3408
3409
3410
3411
  
  		/* get secondary valid target index */
  		if (buf[11] & BIT_7)
  			ddb_index[1] = buf[11] & 0x7f;
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3412
3413
3414
3415
3416
3417
3418
3419
3420
  	} else {
  		ret = QLA_ERROR;
  		goto exit_boot_info;
  	}
  
  	DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
  			  " target ID %d
  ", __func__, ddb_index[0],
  			  ddb_index[1]));
13483730a   Mike Christie   [SCSI] qla4xxx: f...
3421
3422
  	ha->pri_ddb_idx = ddb_index[0];
  	ha->sec_ddb_idx = ddb_index[1];
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3423
3424
3425
3426
3427
  exit_boot_info_free:
  	dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
  exit_boot_info:
  	return ret;
  }
28deb45ce   Lalit Chandivade   [SCSI] qla4xxx: F...
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
  /**
   * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
   * @ha: pointer to adapter structure
   * @username: CHAP username to be returned
   * @password: CHAP password to be returned
   *
   * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
   * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
   * So from the CHAP cache find the first BIDI CHAP entry and set it
   * to the boot record in sysfs.
   **/
  static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
  			    char *password)
  {
  	int i, ret = -EINVAL;
  	int max_chap_entries = 0;
  	struct ql4_chap_table *chap_table;
  
  	if (is_qla8022(ha))
  		max_chap_entries = (ha->hw.flt_chap_size / 2) /
  						sizeof(struct ql4_chap_table);
  	else
  		max_chap_entries = MAX_CHAP_ENTRIES_40XX;
  
  	if (!ha->chap_list) {
  		ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache
  ");
  		return ret;
  	}
  
  	mutex_lock(&ha->chap_sem);
  	for (i = 0; i < max_chap_entries; i++) {
  		chap_table = (struct ql4_chap_table *)ha->chap_list + i;
  		if (chap_table->cookie !=
  		    __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
  			continue;
  		}
  
  		if (chap_table->flags & BIT_7) /* local */
  			continue;
  
  		if (!(chap_table->flags & BIT_6)) /* Not BIDI */
  			continue;
  
  		strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
  		strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
  		ret = 0;
  		break;
  	}
  	mutex_unlock(&ha->chap_sem);
  
  	return ret;
  }
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
  static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
  				   struct ql4_boot_session_info *boot_sess,
  				   uint16_t ddb_index)
  {
  	struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
  	struct dev_db_entry *fw_ddb_entry;
  	dma_addr_t fw_ddb_entry_dma;
  	uint16_t idx;
  	uint16_t options;
  	int ret = QLA_SUCCESS;
  
  	fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  					  &fw_ddb_entry_dma, GFP_KERNEL);
  	if (!fw_ddb_entry) {
  		DEBUG2(ql4_printk(KERN_ERR, ha,
  				  "%s: Unable to allocate dma buffer.
  ",
  				  __func__));
  		ret = QLA_ERROR;
  		return ret;
  	}
  
  	if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
  				   fw_ddb_entry_dma, ddb_index)) {
  		DEBUG2(ql4_printk(KERN_ERR, ha,
  				  "%s: Flash DDB read Failed
  ", __func__));
  		ret = QLA_ERROR;
  		goto exit_boot_target;
  	}
  
  	/* Update target name and IP from DDB */
  	memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
  	       min(sizeof(boot_sess->target_name),
  		   sizeof(fw_ddb_entry->iscsi_name)));
  
  	options = le16_to_cpu(fw_ddb_entry->options);
  	if (options & DDB_OPT_IPV6_DEVICE) {
  		memcpy(&boot_conn->dest_ipaddr.ip_address,
  		       &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
  	} else {
  		boot_conn->dest_ipaddr.ip_type = 0x1;
  		memcpy(&boot_conn->dest_ipaddr.ip_address,
  		       &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
  	}
  
  	boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
  
  	/* update chap information */
  	idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
  
  	if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options))	{
  
  		DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap
  "));
  
  		ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
  				       target_chap_name,
  				       (char *)&boot_conn->chap.target_secret,
  				       idx);
  		if (ret) {
  			ql4_printk(KERN_ERR, ha, "Failed to set chap
  ");
  			ret = QLA_ERROR;
  			goto exit_boot_target;
  		}
  
  		boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  		boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  	}
  
  	if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
  
  		DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap
  "));
28deb45ce   Lalit Chandivade   [SCSI] qla4xxx: F...
3556
3557
3558
  		ret = qla4xxx_get_bidi_chap(ha,
  				    (char *)&boot_conn->chap.intr_chap_name,
  				    (char *)&boot_conn->chap.intr_secret);
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
  		if (ret) {
  			ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap
  ");
  			ret = QLA_ERROR;
  			goto exit_boot_target;
  		}
  
  		boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
  		boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
  	}
  
  exit_boot_target:
  	dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
  			  fw_ddb_entry, fw_ddb_entry_dma);
  	return ret;
  }
  
  static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
  {
  	uint16_t ddb_index[2];
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3579
3580
  	int ret = QLA_ERROR;
  	int rval;
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3581
3582
  
  	memset(ddb_index, 0, sizeof(ddb_index));
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3583
3584
  	ddb_index[0] = 0xffff;
  	ddb_index[1] = 0xffff;
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3585
3586
3587
3588
3589
3590
3591
  	ret = get_fw_boot_info(ha, ddb_index);
  	if (ret != QLA_SUCCESS) {
  		DEBUG2(ql4_printk(KERN_ERR, ha,
  				  "%s: Failed to set boot info.
  ", __func__));
  		return ret;
  	}
13483730a   Mike Christie   [SCSI] qla4xxx: f...
3592
3593
  	if (ql4xdisablesysfsboot)
  		return QLA_SUCCESS;
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3594
3595
3596
3597
  	if (ddb_index[0] == 0xffff)
  		goto sec_target;
  
  	rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3598
  				      ddb_index[0]);
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3599
  	if (rval != QLA_SUCCESS) {
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3600
3601
3602
  		DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
  				  "primary target
  ", __func__));
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3603
3604
  	} else
  		ret = QLA_SUCCESS;
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3605

8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3606
3607
3608
3609
3610
  sec_target:
  	if (ddb_index[1] == 0xffff)
  		goto exit_get_boot_info;
  
  	rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3611
  				      ddb_index[1]);
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3612
  	if (rval != QLA_SUCCESS) {
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3613
3614
3615
  		DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Failed to get "
  				  "secondary target
  ", __func__));
8de5b9582   Lalit Chandivade   [SCSI] qla4xxx: F...
3616
3617
3618
3619
  	} else
  		ret = QLA_SUCCESS;
  
  exit_get_boot_info:
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3620
3621
3622
3623
3624
3625
3626
3627
  	return ret;
  }
  
  static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
  {
  	struct iscsi_boot_kobj *boot_kobj;
  
  	if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
13483730a   Mike Christie   [SCSI] qla4xxx: f...
3628
3629
3630
3631
3632
3633
3634
3635
3636
  		return QLA_ERROR;
  
  	if (ql4xdisablesysfsboot) {
  		ql4_printk(KERN_INFO, ha,
  			   "%s: syfsboot disabled - driver will trigger login"
  			   "and publish session for discovery .
  ", __func__);
  		return QLA_SUCCESS;
  	}
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
  
  	ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
  	if (!ha->boot_kset)
  		goto kset_free;
  
  	if (!scsi_host_get(ha->host))
  		goto kset_free;
  	boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
  					     qla4xxx_show_boot_tgt_pri_info,
  					     qla4xxx_tgt_get_attr_visibility,
  					     qla4xxx_boot_release);
  	if (!boot_kobj)
  		goto put_host;
  
  	if (!scsi_host_get(ha->host))
  		goto kset_free;
  	boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
  					     qla4xxx_show_boot_tgt_sec_info,
  					     qla4xxx_tgt_get_attr_visibility,
  					     qla4xxx_boot_release);
  	if (!boot_kobj)
  		goto put_host;
  
  	if (!scsi_host_get(ha->host))
  		goto kset_free;
  	boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
  					       qla4xxx_show_boot_ini_info,
  					       qla4xxx_ini_get_attr_visibility,
  					       qla4xxx_boot_release);
  	if (!boot_kobj)
  		goto put_host;
  
  	if (!scsi_host_get(ha->host))
  		goto kset_free;
  	boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
  					       qla4xxx_show_boot_eth_info,
  					       qla4xxx_eth_get_attr_visibility,
  					       qla4xxx_boot_release);
  	if (!boot_kobj)
  		goto put_host;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
3677
  	return QLA_SUCCESS;
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
3678
3679
3680
3681
3682
3683
3684
  
  put_host:
  	scsi_host_put(ha->host);
  kset_free:
  	iscsi_boot_destroy_kset(ha->boot_kset);
  	return -ENOMEM;
  }
4549415af   Lalit Chandivade   [SCSI] qla4xxx: D...
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
  
  /**
   * qla4xxx_create chap_list - Create CHAP list from FLASH
   * @ha: pointer to adapter structure
   *
   * Read flash and make a list of CHAP entries, during login when a CHAP entry
   * is received, it will be checked in this list. If entry exist then the CHAP
   * entry index is set in the DDB. If CHAP entry does not exist in this list
   * then a new entry is added in FLASH in CHAP table and the index obtained is
   * used in the DDB.
   **/
  static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
  {
  	int rval = 0;
  	uint8_t *chap_flash_data = NULL;
  	uint32_t offset;
  	dma_addr_t chap_dma;
  	uint32_t chap_size = 0;
  
  	if (is_qla40XX(ha))
  		chap_size = MAX_CHAP_ENTRIES_40XX  *
  					sizeof(struct ql4_chap_table);
  	else	/* Single region contains CHAP info for both
  		 * ports which is divided into half for each port.
  		 */
  		chap_size = ha->hw.flt_chap_size / 2;
  
  	chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
  					  &chap_dma, GFP_KERNEL);
  	if (!chap_flash_data) {
  		ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data
  ");
  		return;
  	}
  	if (is_qla40XX(ha))
  		offset = FLASH_CHAP_OFFSET;
  	else {
  		offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
  		if (ha->port_num == 1)
  			offset += chap_size;
  	}
  
  	rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
  	if (rval != QLA_SUCCESS)
  		goto exit_chap_list;
  
  	if (ha->chap_list == NULL)
  		ha->chap_list = vmalloc(chap_size);
  	if (ha->chap_list == NULL) {
  		ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list
  ");
  		goto exit_chap_list;
  	}
  
  	memcpy(ha->chap_list, chap_flash_data, chap_size);
  
  exit_chap_list:
  	dma_free_coherent(&ha->pdev->dev, chap_size,
  			chap_flash_data, chap_dma);
4549415af   Lalit Chandivade   [SCSI] qla4xxx: D...
3744
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
  static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
  				  struct ql4_tuple_ddb *tddb)
  {
  	struct scsi_qla_host *ha;
  	struct iscsi_cls_session *cls_sess;
  	struct iscsi_cls_conn *cls_conn;
  	struct iscsi_session *sess;
  	struct iscsi_conn *conn;
  
  	DEBUG2(printk(KERN_INFO "Func: %s
  ", __func__));
  	ha = ddb_entry->ha;
  	cls_sess = ddb_entry->sess;
  	sess = cls_sess->dd_data;
  	cls_conn = ddb_entry->conn;
  	conn = cls_conn->dd_data;
  
  	tddb->tpgt = sess->tpgt;
  	tddb->port = conn->persistent_port;
  	strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
  	strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
  }
  
  static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
  				      struct ql4_tuple_ddb *tddb)
  {
  	uint16_t options = 0;
  
  	tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
  	memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
  	       min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
  
  	options = le16_to_cpu(fw_ddb_entry->options);
  	if (options & DDB_OPT_IPV6_DEVICE)
  		sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
  	else
  		sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
  
  	tddb->port = le16_to_cpu(fw_ddb_entry->port);
  }
  
  static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
  				     struct ql4_tuple_ddb *old_tddb,
  				     struct ql4_tuple_ddb *new_tddb)
  {
  	if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
  		return QLA_ERROR;
  
  	if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
  		return QLA_ERROR;
  
  	if (old_tddb->port != new_tddb->port)
  		return QLA_ERROR;
  
  	DEBUG2(ql4_printk(KERN_INFO, ha,
  			  "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
  			  old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
  			  old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
  			  new_tddb->ip_addr, new_tddb->iscsi_name));
  
  	return QLA_SUCCESS;
  }
  
  static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
  				     struct dev_db_entry *fw_ddb_entry)
  {
  	struct ddb_entry *ddb_entry;
  	struct ql4_tuple_ddb *fw_tddb = NULL;
  	struct ql4_tuple_ddb *tmp_tddb = NULL;
  	int idx;
  	int ret = QLA_ERROR;
  
  	fw_tddb = vzalloc(sizeof(*fw_tddb));
  	if (!fw_tddb) {
  		DEBUG2(ql4_printk(KERN_WARNING, ha,
  				  "Memory Allocation failed.
  "));
  		ret = QLA_SUCCESS;
  		goto exit_check;
  	}
  
  	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  	if (!tmp_tddb) {
  		DEBUG2(ql4_printk(KERN_WARNING, ha,
  				  "Memory Allocation failed.
  "));
  		ret = QLA_SUCCESS;
  		goto exit_check;
  	}
  
  	qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  
  	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  		if (ddb_entry == NULL)
  			continue;
  
  		qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
  		if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
  			ret = QLA_SUCCESS; /* found */
  			goto exit_check;
  		}
  	}
  
  exit_check:
  	if (fw_tddb)
  		vfree(fw_tddb);
  	if (tmp_tddb)
  		vfree(tmp_tddb);
  	return ret;
  }
  
  static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
  				       struct list_head *list_nt,
  				       struct dev_db_entry *fw_ddb_entry)
  {
  	struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
  	struct ql4_tuple_ddb *fw_tddb = NULL;
  	struct ql4_tuple_ddb *tmp_tddb = NULL;
  	int ret = QLA_ERROR;
  
  	fw_tddb = vzalloc(sizeof(*fw_tddb));
  	if (!fw_tddb) {
  		DEBUG2(ql4_printk(KERN_WARNING, ha,
  				  "Memory Allocation failed.
  "));
  		ret = QLA_SUCCESS;
  		goto exit_check;
  	}
  
  	tmp_tddb = vzalloc(sizeof(*tmp_tddb));
  	if (!tmp_tddb) {
  		DEBUG2(ql4_printk(KERN_WARNING, ha,
  				  "Memory Allocation failed.
  "));
  		ret = QLA_SUCCESS;
  		goto exit_check;
  	}
  
  	qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb);
  
  	list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  		qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb);
  		if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb)) {
  			ret = QLA_SUCCESS; /* found */
  			goto exit_check;
  		}
  	}
  
  exit_check:
  	if (fw_tddb)
  		vfree(fw_tddb);
  	if (tmp_tddb)
  		vfree(tmp_tddb);
  	return ret;
  }
  
  static void qla4xxx_free_nt_list(struct list_head *list_nt)
  {
  	struct qla_ddb_index  *nt_ddb_idx, *nt_ddb_idx_tmp;
  
  	/* Free up the normaltargets list */
  	list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
  		list_del_init(&nt_ddb_idx->list);
  		vfree(nt_ddb_idx);
  	}
  
  }
  
  static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
  					struct dev_db_entry *fw_ddb_entry)
  {
  	struct iscsi_endpoint *ep;
  	struct sockaddr_in *addr;
  	struct sockaddr_in6 *addr6;
  	struct sockaddr *dst_addr;
  	char *ip;
  
  	/* TODO: need to destroy on unload iscsi_endpoint*/
  	dst_addr = vmalloc(sizeof(*dst_addr));
  	if (!dst_addr)
  		return NULL;
  
  	if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
  		dst_addr->sa_family = AF_INET6;
  		addr6 = (struct sockaddr_in6 *)dst_addr;
  		ip = (char *)&addr6->sin6_addr;
  		memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
  		addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
  
  	} else {
  		dst_addr->sa_family = AF_INET;
  		addr = (struct sockaddr_in *)dst_addr;
  		ip = (char *)&addr->sin_addr;
  		memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
  		addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
  	}
  
  	ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
  	vfree(dst_addr);
  	return ep;
  }
  
  static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
  {
  	if (ql4xdisablesysfsboot)
  		return QLA_SUCCESS;
  	if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
  		return QLA_ERROR;
  	return QLA_SUCCESS;
  }
  
  static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
  					  struct ddb_entry *ddb_entry)
  {
  	ddb_entry->ddb_type = FLASH_DDB;
  	ddb_entry->fw_ddb_index = INVALID_ENTRY;
  	ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
  	ddb_entry->ha = ha;
  	ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
  	ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
  
  	atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
  	atomic_set(&ddb_entry->relogin_timer, 0);
  	atomic_set(&ddb_entry->relogin_retry_count, 0);
  
  	ddb_entry->default_relogin_timeout =
  		le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
  	ddb_entry->default_time2wait =
  		le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
  }
  
  static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
  {
  	uint32_t idx = 0;
  	uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
  	uint32_t sts[MBOX_REG_COUNT];
  	uint32_t ip_state;
  	unsigned long wtime;
  	int ret;
  
  	wtime = jiffies + (HZ * IP_CONFIG_TOV);
  	do {
  		for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
  			if (ip_idx[idx] == -1)
  				continue;
  
  			ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
  
  			if (ret == QLA_ERROR) {
  				ip_idx[idx] = -1;
  				continue;
  			}
  
  			ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
  
  			DEBUG2(ql4_printk(KERN_INFO, ha,
  					  "Waiting for IP state for idx = %d, state = 0x%x
  ",
  					  ip_idx[idx], ip_state));
  			if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
  			    ip_state == IP_ADDRSTATE_INVALID ||
  			    ip_state == IP_ADDRSTATE_PREFERRED ||
  			    ip_state == IP_ADDRSTATE_DEPRICATED ||
  			    ip_state == IP_ADDRSTATE_DISABLING)
  				ip_idx[idx] = -1;
  
  		}
  
  		/* Break if all IP states checked */
  		if ((ip_idx[0] == -1) &&
  		    (ip_idx[1] == -1) &&
  		    (ip_idx[2] == -1) &&
  		    (ip_idx[3] == -1))
  			break;
  		schedule_timeout_uninterruptible(HZ);
  	} while (time_after(wtime, jiffies));
  }
  
  void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
  {
  	int max_ddbs;
  	int ret;
  	uint32_t idx = 0, next_idx = 0;
  	uint32_t state = 0, conn_err = 0;
  	uint16_t conn_id;
  	struct dev_db_entry *fw_ddb_entry;
  	struct ddb_entry *ddb_entry = NULL;
  	dma_addr_t fw_ddb_dma;
  	struct iscsi_cls_session *cls_sess;
  	struct iscsi_session *sess;
  	struct iscsi_cls_conn *cls_conn;
  	struct iscsi_endpoint *ep;
  	uint16_t cmds_max = 32, tmo = 0;
  	uint32_t initial_cmdsn = 0;
  	struct list_head list_st, list_nt; /* List of sendtargets */
  	struct qla_ddb_index  *st_ddb_idx, *st_ddb_idx_tmp;
  	int fw_idx_size;
  	unsigned long wtime;
  	struct qla_ddb_index  *nt_ddb_idx;
  
  	if (!test_bit(AF_LINK_UP, &ha->flags)) {
  		set_bit(AF_BUILD_DDB_LIST, &ha->flags);
  		ha->is_reset = is_reset;
  		return;
  	}
  	max_ddbs =  is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
  				     MAX_DEV_DB_ENTRIES;
  
  	fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
  				      &fw_ddb_dma);
  	if (fw_ddb_entry == NULL) {
  		DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory
  "));
  		goto exit_ddb_list;
  	}
  
  	INIT_LIST_HEAD(&list_st);
  	INIT_LIST_HEAD(&list_nt);
  	fw_idx_size = sizeof(struct qla_ddb_index);
  
  	for (idx = 0; idx < max_ddbs; idx = next_idx) {
  		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
  					      fw_ddb_dma, NULL,
  					      &next_idx, &state, &conn_err,
  					      NULL, &conn_id);
  		if (ret == QLA_ERROR)
  			break;
  
  		if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  			goto continue_next_st;
  
  		/* Check if ST, add to the list_st */
  		if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
  			goto continue_next_st;
  
  		st_ddb_idx = vzalloc(fw_idx_size);
  		if (!st_ddb_idx)
  			break;
  
  		st_ddb_idx->fw_ddb_idx = idx;
  
  		list_add_tail(&st_ddb_idx->list, &list_st);
  continue_next_st:
  		if (next_idx == 0)
  			break;
  	}
  
  	/* Before issuing conn open mbox, ensure all IPs states are configured
  	 * Note, conn open fails if IPs are not configured
  	 */
  	qla4xxx_wait_for_ip_configuration(ha);
  
  	/* Go thru the STs and fire the sendtargets by issuing conn open mbx */
  	list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  		qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
  	}
  
  	/* Wait to ensure all sendtargets are done for min 12 sec wait */
  	tmo = ((ha->def_timeout < LOGIN_TOV) ? LOGIN_TOV : ha->def_timeout);
  	DEBUG2(ql4_printk(KERN_INFO, ha,
  			  "Default time to wait for build ddb %d
  ", tmo));
  
  	wtime = jiffies + (HZ * tmo);
  	do {
  		list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st,
  					 list) {
  			ret = qla4xxx_get_fwddb_entry(ha,
  						      st_ddb_idx->fw_ddb_idx,
  						      NULL, 0, NULL, &next_idx,
  						      &state, &conn_err, NULL,
  						      NULL);
  			if (ret == QLA_ERROR)
  				continue;
  
  			if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  			    state == DDB_DS_SESSION_FAILED) {
  				list_del_init(&st_ddb_idx->list);
  				vfree(st_ddb_idx);
  			}
  		}
  		schedule_timeout_uninterruptible(HZ / 10);
  	} while (time_after(wtime, jiffies));
  
  	/* Free up the sendtargets list */
  	list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
  		list_del_init(&st_ddb_idx->list);
  		vfree(st_ddb_idx);
  	}
  
  	for (idx = 0; idx < max_ddbs; idx = next_idx) {
  		ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry,
  					      fw_ddb_dma, NULL,
  					      &next_idx, &state, &conn_err,
  					      NULL, &conn_id);
  		if (ret == QLA_ERROR)
  			break;
  
  		if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
  			goto continue_next_nt;
  
  		/* Check if NT, then add to list it */
  		if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
  			goto continue_next_nt;
  
  		if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
  		    state == DDB_DS_SESSION_FAILED) {
  			DEBUG2(ql4_printk(KERN_INFO, ha,
  					  "Adding  DDB to session = 0x%x
  ",
  					  idx));
  			if (is_reset == INIT_ADAPTER) {
  				nt_ddb_idx = vmalloc(fw_idx_size);
  				if (!nt_ddb_idx)
  					break;
  
  				nt_ddb_idx->fw_ddb_idx = idx;
  
  				memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
  				       sizeof(struct dev_db_entry));
  
  				if (qla4xxx_is_flash_ddb_exists(ha, &list_nt,
  						fw_ddb_entry) == QLA_SUCCESS) {
  					vfree(nt_ddb_idx);
  					goto continue_next_nt;
  				}
  				list_add_tail(&nt_ddb_idx->list, &list_nt);
  			} else if (is_reset == RESET_ADAPTER) {
  				if (qla4xxx_is_session_exists(ha,
  						   fw_ddb_entry) == QLA_SUCCESS)
  					goto continue_next_nt;
  			}
  
  			/* Create session object, with INVALID_ENTRY,
  			 * the targer_id would get set when we issue the login
  			 */
  			cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport,
  						ha->host, cmds_max,
  						sizeof(struct ddb_entry),
  						sizeof(struct ql4_task_data),
  						initial_cmdsn, INVALID_ENTRY);
  			if (!cls_sess)
  				goto exit_ddb_list;
  
  			/*
  			 * iscsi_session_setup increments the driver reference
  			 * count which wouldn't let the driver to be unloaded.
  			 * so calling module_put function to decrement the
  			 * reference count.
  			 **/
  			module_put(qla4xxx_iscsi_transport.owner);
  			sess = cls_sess->dd_data;
  			ddb_entry = sess->dd_data;
  			ddb_entry->sess = cls_sess;
  
  			cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
  			memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
  			       sizeof(struct dev_db_entry));
  
  			qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
  
  			cls_conn = iscsi_conn_setup(cls_sess,
  						    sizeof(struct qla_conn),
  						    conn_id);
  			if (!cls_conn)
  				goto exit_ddb_list;
  
  			ddb_entry->conn = cls_conn;
  
  			/* Setup ep, for displaying attributes in sysfs */
  			ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
  			if (ep) {
  				ep->conn = cls_conn;
  				cls_conn->ep = ep;
  			} else {
  				DEBUG2(ql4_printk(KERN_ERR, ha,
  						  "Unable to get ep
  "));
  			}
  
  			/* Update sess/conn params */
  			qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess,
  						 cls_conn);
  
  			if (is_reset == RESET_ADAPTER) {
  				iscsi_block_session(cls_sess);
  				/* Use the relogin path to discover new devices
  				 *  by short-circuting the logic of setting
  				 *  timer to relogin - instead set the flags
  				 *  to initiate login right away.
  				 */
  				set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
  				set_bit(DF_RELOGIN, &ddb_entry->flags);
  			}
  		}
  continue_next_nt:
  		if (next_idx == 0)
  			break;
  	}
  exit_ddb_list:
  	qla4xxx_free_nt_list(&list_nt);
  	if (fw_ddb_entry)
  		dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
  
  	qla4xxx_free_ddb_index(ha);
  }
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
  /**
   * qla4xxx_probe_adapter - callback function to probe HBA
   * @pdev: pointer to pci_dev structure
   * @pci_device_id: pointer to pci_device entry
   *
   * This routine will probe for Qlogic 4xxx iSCSI host adapters.
   * It returns zero if successful. It also initializes all data necessary for
   * the driver.
   **/
  static int __devinit qla4xxx_probe_adapter(struct pci_dev *pdev,
  					   const struct pci_device_id *ent)
  {
  	int ret = -ENODEV, status;
  	struct Scsi_Host *host;
  	struct scsi_qla_host *ha;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4267
4268
  	uint8_t init_retry_count = 0;
  	char buf[34];
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4269
  	struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
f9880e76f   Prasanna Mumbai   [SCSI] qla4xxx: D...
4270
  	uint32_t dev_state;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4271
4272
4273
  
  	if (pci_enable_device(pdev))
  		return -1;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4274
  	host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4275
4276
4277
4278
4279
4280
4281
4282
  	if (host == NULL) {
  		printk(KERN_WARNING
  		       "qla4xxx: Couldn't allocate host from scsi layer!
  ");
  		goto probe_disable_device;
  	}
  
  	/* Clear our data area */
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4283
  	ha = to_qla_host(host);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4284
4285
4286
4287
4288
4289
  	memset(ha, 0, sizeof(*ha));
  
  	/* Save the information from PCI BIOS.	*/
  	ha->pdev = pdev;
  	ha->host = host;
  	ha->host_no = host->host_no;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
4290
  	pci_enable_pcie_error_reporting(pdev);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
  	/* Setup Runtime configurable options */
  	if (is_qla8022(ha)) {
  		ha->isp_ops = &qla4_8xxx_isp_ops;
  		rwlock_init(&ha->hw_lock);
  		ha->qdr_sn_window = -1;
  		ha->ddr_mn_window = -1;
  		ha->curr_window = 255;
  		ha->func_num = PCI_FUNC(ha->pdev->devfn);
  		nx_legacy_intr = &legacy_intr[ha->func_num];
  		ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
  		ha->nx_legacy_intr.tgt_status_reg =
  			nx_legacy_intr->tgt_status_reg;
  		ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
  		ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
  	} else {
  		ha->isp_ops = &qla4xxx_isp_ops;
  	}
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
4308
4309
4310
  	/* Set EEH reset type to fundamental if required by hba */
  	if (is_qla8022(ha))
  		pdev->needs_freset = 1;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4311
  	/* Configure PCI I/O space. */
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4312
  	ret = ha->isp_ops->iospace_config(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4313
  	if (ret)
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4314
  		goto probe_failed_ioconfig;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4315

c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4316
4317
  	ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p
  ",
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4318
4319
4320
4321
4322
  		   pdev->device, pdev->irq, ha->reg);
  
  	qla4xxx_config_dma_addressing(ha);
  
  	/* Initialize lists and spinlocks. */
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4323
4324
4325
  	INIT_LIST_HEAD(&ha->free_srb_q);
  
  	mutex_init(&ha->mbox_sem);
4549415af   Lalit Chandivade   [SCSI] qla4xxx: D...
4326
  	mutex_init(&ha->chap_sem);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4327
  	init_completion(&ha->mbx_intr_comp);
95d31262b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4328
  	init_completion(&ha->disable_acb_comp);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4329
4330
  
  	spin_lock_init(&ha->hardware_lock);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4331
4332
4333
  
  	/* Allocate dma buffers */
  	if (qla4xxx_mem_alloc(ha)) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4334
4335
4336
  		ql4_printk(KERN_WARNING, ha,
  		    "[ERROR] Failed to allocate memory for adapter
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4337
4338
4339
4340
  
  		ret = -ENOMEM;
  		goto probe_failed;
  	}
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
  	host->cmd_per_lun = 3;
  	host->max_channel = 0;
  	host->max_lun = MAX_LUNS - 1;
  	host->max_id = MAX_TARGETS;
  	host->max_cmd_len = IOCB_MAX_CDB_LEN;
  	host->can_queue = MAX_SRBS ;
  	host->transportt = qla4xxx_scsi_transport;
  
  	ret = scsi_init_shared_tag_map(host, MAX_SRBS);
  	if (ret) {
  		ql4_printk(KERN_WARNING, ha,
  			   "%s: scsi_init_shared_tag_map failed
  ", __func__);
  		goto probe_failed;
  	}
  
  	pci_set_drvdata(pdev, ha);
  
  	ret = scsi_add_host(host, &pdev->dev);
  	if (ret)
  		goto probe_failed;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4362
4363
  	if (is_qla8022(ha))
  		(void) qla4_8xxx_get_flash_info(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4364
4365
4366
4367
4368
  	/*
  	 * Initialize the Host adapter request/response queues and
  	 * firmware
  	 * NOTE: interrupts enabled upon successful completion
  	 */
13483730a   Mike Christie   [SCSI] qla4xxx: f...
4369
  	status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4370
4371
  	while ((!test_bit(AF_ONLINE, &ha->flags)) &&
  	    init_retry_count++ < MAX_INIT_RETRIES) {
f9880e76f   Prasanna Mumbai   [SCSI] qla4xxx: D...
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
  
  		if (is_qla8022(ha)) {
  			qla4_8xxx_idc_lock(ha);
  			dev_state = qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE);
  			qla4_8xxx_idc_unlock(ha);
  			if (dev_state == QLA82XX_DEV_FAILED) {
  				ql4_printk(KERN_WARNING, ha, "%s: don't retry "
  				    "initialize adapter. H/W is in failed state
  ",
  				    __func__);
  				break;
  			}
  		}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4385
4386
4387
  		DEBUG2(printk("scsi: %s: retrying adapter initialization "
  			      "(%d)
  ", __func__, init_retry_count));
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4388
4389
4390
  
  		if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
  			continue;
13483730a   Mike Christie   [SCSI] qla4xxx: f...
4391
  		status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4392
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4393
4394
  
  	if (!test_bit(AF_ONLINE, &ha->flags)) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4395
4396
  		ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4397

fe998527e   Lalit Chandivade   [SCSI] qla4xxx: p...
4398
4399
4400
4401
4402
4403
4404
4405
4406
  		if (is_qla8022(ha) && ql4xdontresethba) {
  			/* Put the device in failed state. */
  			DEBUG2(printk(KERN_ERR "HW STATE: FAILED
  "));
  			qla4_8xxx_idc_lock(ha);
  			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  			    QLA82XX_DEV_FAILED);
  			qla4_8xxx_idc_unlock(ha);
  		}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4407
  		ret = -ENODEV;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4408
  		goto remove_host;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4409
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4410
4411
4412
4413
4414
4415
4416
  	/* Startup the kernel thread for this host adapter. */
  	DEBUG2(printk("scsi: %s: Starting kernel thread for "
  		      "qla4xxx_dpc
  ", __func__));
  	sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
  	ha->dpc_thread = create_singlethread_workqueue(buf);
  	if (!ha->dpc_thread) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4417
4418
  		ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!
  ");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4419
  		ret = -ENODEV;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4420
  		goto remove_host;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4421
  	}
c4028958b   David Howells   WorkStruct: make ...
4422
  	INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4423

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4424
4425
4426
4427
4428
4429
4430
4431
  	sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
  	ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
  	if (!ha->task_wq) {
  		ql4_printk(KERN_WARNING, ha, "Unable to start task thread!
  ");
  		ret = -ENODEV;
  		goto remove_host;
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
  	/* For ISP-82XX, request_irqs is called in qla4_8xxx_load_risc
  	 * (which is called indirectly by qla4xxx_initialize_adapter),
  	 * so that irqs will be registered after crbinit but before
  	 * mbx_intr_enable.
  	 */
  	if (!is_qla8022(ha)) {
  		ret = qla4xxx_request_irqs(ha);
  		if (ret) {
  			ql4_printk(KERN_WARNING, ha, "Failed to reserve "
  			    "interrupt %d already in use.
  ", pdev->irq);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4443
  			goto remove_host;
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4444
  		}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4445
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4446

2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
4447
  	pci_save_state(ha->pdev);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4448
  	ha->isp_ops->enable_intrs(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4449
4450
4451
4452
4453
  
  	/* Start timer thread. */
  	qla4xxx_start_timer(ha, qla4xxx_timer, 1);
  
  	set_bit(AF_INIT_DONE, &ha->flags);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4454
4455
4456
4457
4458
4459
4460
4461
  	printk(KERN_INFO
  	       " QLogic iSCSI HBA Driver version: %s
  "
  	       "  QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d
  ",
  	       qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
  	       ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
  	       ha->patch_number, ha->build_number);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
4462

2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
4463
4464
4465
4466
  	if (qla4xxx_setup_boot_info(ha))
  		ql4_printk(KERN_ERR, ha, "%s:ISCSI boot info setup failed
  ",
  			   __func__);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
4467
4468
4469
4470
4471
  		/* Perform the build ddb list and login to each */
  	qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
  	iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
  
  	qla4xxx_create_chap_list(ha);
ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
4472
  	qla4xxx_create_ifaces(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4473
  	return 0;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4474
4475
  remove_host:
  	scsi_remove_host(ha->host);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4476
4477
  probe_failed:
  	qla4xxx_free_adapter(ha);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4478
4479
  
  probe_failed_ioconfig:
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
4480
  	pci_disable_pcie_error_reporting(pdev);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4481
4482
4483
4484
4485
4486
4487
4488
4489
  	scsi_host_put(ha->host);
  
  probe_disable_device:
  	pci_disable_device(pdev);
  
  	return ret;
  }
  
  /**
7eece5a08   Karen Higgins   [SCSI] qla4xxx: P...
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
   * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
   * @ha: pointer to adapter structure
   *
   * Mark the other ISP-4xxx port to indicate that the driver is being removed,
   * so that the other port will not re-initialize while in the process of
   * removing the ha due to driver unload or hba hotplug.
   **/
  static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
  {
  	struct scsi_qla_host *other_ha = NULL;
  	struct pci_dev *other_pdev = NULL;
  	int fn = ISP4XXX_PCI_FN_2;
  
  	/*iscsi function numbers for ISP4xxx is 1 and 3*/
  	if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
  		fn = ISP4XXX_PCI_FN_1;
  
  	other_pdev =
  		pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  		ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  		fn));
  
  	/* Get other_ha if other_pdev is valid and state is enable*/
  	if (other_pdev) {
  		if (atomic_read(&other_pdev->enable_cnt)) {
  			other_ha = pci_get_drvdata(other_pdev);
  			if (other_ha) {
  				set_bit(AF_HA_REMOVAL, &other_ha->flags);
  				DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
  				    "Prevent %s reinit
  ", __func__,
  				    dev_name(&other_ha->pdev->dev)));
  			}
  		}
  		pci_dev_put(other_pdev);
  	}
  }
13483730a   Mike Christie   [SCSI] qla4xxx: f...
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
  static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
  {
  	struct ddb_entry *ddb_entry;
  	int options;
  	int idx;
  
  	for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
  
  		ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
  		if ((ddb_entry != NULL) &&
  		    (ddb_entry->ddb_type == FLASH_DDB)) {
  
  			options = LOGOUT_OPTION_CLOSE_SESSION;
  			if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
  			    == QLA_ERROR)
  				ql4_printk(KERN_ERR, ha, "%s: Logout failed
  ",
  					   __func__);
  
  			qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
  			/*
  			 * we have decremented the reference count of the driver
  			 * when we setup the session to have the driver unload
  			 * to be seamless without actually destroying the
  			 * session
  			 **/
  			try_module_get(qla4xxx_iscsi_transport.owner);
  			iscsi_destroy_endpoint(ddb_entry->conn->ep);
  			qla4xxx_free_ddb(ha, ddb_entry);
  			iscsi_session_teardown(ddb_entry->sess);
  		}
  	}
  }
7eece5a08   Karen Higgins   [SCSI] qla4xxx: P...
4560
  /**
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4561
4562
4563
4564
4565
4566
4567
4568
   * qla4xxx_remove_adapter - calback function to remove adapter.
   * @pci_dev: PCI device pointer
   **/
  static void __devexit qla4xxx_remove_adapter(struct pci_dev *pdev)
  {
  	struct scsi_qla_host *ha;
  
  	ha = pci_get_drvdata(pdev);
7eece5a08   Karen Higgins   [SCSI] qla4xxx: P...
4569
4570
  	if (!is_qla8022(ha))
  		qla4xxx_prevent_other_port_reinit(ha);
bee4fe8e6   David C Somayajulu   [SCSI] qla4xxx: q...
4571

ed1086e04   Vikas Chaudhary   [SCSI] qla4xxx: a...
4572
4573
  	/* destroy iface from sysfs */
  	qla4xxx_destroy_ifaces(ha);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
4574
  	if ((!ql4xdisablesysfsboot) && ha->boot_kset)
2a991c215   Manish Rangankar   [SCSI] qla4xxx: B...
4575
  		iscsi_boot_destroy_kset(ha->boot_kset);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
4576
  	qla4xxx_destroy_fw_ddb_session(ha);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4577
4578
4579
4580
4581
  	scsi_remove_host(ha->host);
  
  	qla4xxx_free_adapter(ha);
  
  	scsi_host_put(ha->host);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
4582
  	pci_disable_pcie_error_reporting(pdev);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4583
  	pci_disable_device(pdev);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
  	pci_set_drvdata(pdev, NULL);
  }
  
  /**
   * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
   * @ha: HA context
   *
   * At exit, the @ha's flags.enable_64bit_addressing set to indicated
   * supported addressing method.
   */
479754777   Adrian Bunk   [SCSI] qla4xxx: p...
4594
  static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4595
4596
4597
4598
  {
  	int retval;
  
  	/* Update our PCI device dma_mask for full 64 bit mask */
6a35528a8   Yang Hongyang   dma-mapping: repl...
4599
4600
  	if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
  		if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4601
4602
4603
4604
4605
  			dev_dbg(&ha->pdev->dev,
  				  "Failed to set 64 bit PCI consistent mask; "
  				   "using 32 bit.
  ");
  			retval = pci_set_consistent_dma_mask(ha->pdev,
284901a90   Yang Hongyang   dma-mapping: repl...
4606
  							     DMA_BIT_MASK(32));
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4607
4608
  		}
  	} else
284901a90   Yang Hongyang   dma-mapping: repl...
4609
  		retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4610
4611
4612
4613
  }
  
  static int qla4xxx_slave_alloc(struct scsi_device *sdev)
  {
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4614
4615
4616
  	struct iscsi_cls_session *cls_sess;
  	struct iscsi_session *sess;
  	struct ddb_entry *ddb;
8bb4033d2   Vikas Chaudhary   [SCSI] qla4xxx: A...
4617
  	int queue_depth = QL4_DEF_QDEPTH;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4618

b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4619
4620
4621
  	cls_sess = starget_to_session(sdev->sdev_target);
  	sess = cls_sess->dd_data;
  	ddb = sess->dd_data;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4622
4623
  	sdev->hostdata = ddb;
  	sdev->tagged_supported = 1;
8bb4033d2   Vikas Chaudhary   [SCSI] qla4xxx: A...
4624
4625
4626
4627
4628
  
  	if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
  		queue_depth = ql4xmaxqdepth;
  
  	scsi_activate_tcq(sdev, queue_depth);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
  	return 0;
  }
  
  static int qla4xxx_slave_configure(struct scsi_device *sdev)
  {
  	sdev->tagged_supported = 1;
  	return 0;
  }
  
  static void qla4xxx_slave_destroy(struct scsi_device *sdev)
  {
  	scsi_deactivate_tcq(sdev, 1);
  }
  
  /**
   * qla4xxx_del_from_active_array - returns an active srb
   * @ha: Pointer to host adapter structure.
fd589a8f0   Anand Gadiyar   trivial: fix typo...
4646
   * @index: index into the active_array
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4647
4648
4649
   *
   * This routine removes and returns the srb at the specified index
   **/
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4650
4651
  struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
      uint32_t index)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4652
4653
  {
  	struct srb *srb = NULL;
5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
4654
  	struct scsi_cmnd *cmd = NULL;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4655

5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
4656
4657
  	cmd = scsi_host_find_tag(ha->host, index);
  	if (!cmd)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4658
  		return srb;
5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
4659
4660
  	srb = (struct srb *)CMD_SP(cmd);
  	if (!srb)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4661
4662
4663
4664
4665
4666
4667
  		return srb;
  
  	/* update counters */
  	if (srb->flags & SRB_DMA_VALID) {
  		ha->req_q_count += srb->iocb_cnt;
  		ha->iocb_cnt -= srb->iocb_cnt;
  		if (srb->cmd)
5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
4668
4669
  			srb->cmd->host_scribble =
  				(unsigned char *)(unsigned long) MAX_SRBS;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4670
4671
4672
4673
4674
  	}
  	return srb;
  }
  
  /**
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4675
   * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4676
   * @ha: Pointer to host adapter structure.
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
   * @cmd: Scsi Command to wait on.
   *
   * This routine waits for the command to be returned by the Firmware
   * for some max time.
   **/
  static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
  				      struct scsi_cmnd *cmd)
  {
  	int done = 0;
  	struct srb *rp;
  	uint32_t max_wait_time = EH_WAIT_CMD_TOV;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
  	int ret = SUCCESS;
  
  	/* Dont wait on command if PCI error is being handled
  	 * by PCI AER driver
  	 */
  	if (unlikely(pci_channel_offline(ha->pdev)) ||
  	    (test_bit(AF_EEH_BUSY, &ha->flags))) {
  		ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s
  ",
  		    ha->host_no, __func__);
  		return ret;
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4700
4701
4702
  
  	do {
  		/* Checking to see if its returned to OS */
5369887a9   Vikas Chaudhary   [SCSI] qla4xxx: c...
4703
  		rp = (struct srb *) CMD_SP(cmd);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
  		if (rp == NULL) {
  			done++;
  			break;
  		}
  
  		msleep(2000);
  	} while (max_wait_time--);
  
  	return done;
  }
  
  /**
   * qla4xxx_wait_for_hba_online - waits for HBA to come online
   * @ha: Pointer to host adapter structure
   **/
  static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
  {
  	unsigned long wait_online;
f581a3f79   Vikas Chaudhary   [SCSI] qla4xxx: T...
4722
  	wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4723
4724
4725
4726
  	while (time_before(jiffies, wait_online)) {
  
  		if (adapter_up(ha))
  			return QLA_SUCCESS;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4727
4728
4729
4730
4731
4732
4733
4734
  
  		msleep(2000);
  	}
  
  	return QLA_ERROR;
  }
  
  /**
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4735
   * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
fd589a8f0   Anand Gadiyar   trivial: fix typo...
4736
   * @ha: pointer to HBA
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4737
4738
4739
4740
4741
4742
   * @t: target id
   * @l: lun id
   *
   * This function waits for all outstanding commands to a lun to complete. It
   * returns 0 if all pending commands are returned and 1 otherwise.
   **/
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4743
4744
4745
  static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
  					struct scsi_target *stgt,
  					struct scsi_device *sdev)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4746
4747
4748
4749
4750
4751
  {
  	int cnt;
  	int status = 0;
  	struct scsi_cmnd *cmd;
  
  	/*
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4752
4753
  	 * Waiting for all commands for the designated target or dev
  	 * in the active array
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4754
4755
4756
  	 */
  	for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
  		cmd = scsi_host_find_tag(ha->host, cnt);
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4757
4758
  		if (cmd && stgt == scsi_target(cmd->device) &&
  		    (!sdev || sdev == cmd->device)) {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
  			if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  				status++;
  				break;
  			}
  		}
  	}
  	return status;
  }
  
  /**
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
   * qla4xxx_eh_abort - callback for abort task.
   * @cmd: Pointer to Linux's SCSI command structure
   *
   * This routine is called by the Linux OS to abort the specified
   * command.
   **/
  static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
  {
  	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  	unsigned int id = cmd->device->id;
  	unsigned int lun = cmd->device->lun;
92b3e5bbb   Mike Christie   [SCSI] qla4xxx: g...
4780
  	unsigned long flags;
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4781
4782
4783
  	struct srb *srb = NULL;
  	int ret = SUCCESS;
  	int wait = 0;
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4784
  	ql4_printk(KERN_INFO, ha,
5cd049a59   Christoph Hellwig   [SCSI] remove cmd...
4785
4786
4787
  	    "scsi%ld:%d:%d: Abort command issued cmd=%p
  ",
  	    ha->host_no, id, lun, cmd);
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4788

92b3e5bbb   Mike Christie   [SCSI] qla4xxx: g...
4789
  	spin_lock_irqsave(&ha->hardware_lock, flags);
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4790
  	srb = (struct srb *) CMD_SP(cmd);
92b3e5bbb   Mike Christie   [SCSI] qla4xxx: g...
4791
4792
  	if (!srb) {
  		spin_unlock_irqrestore(&ha->hardware_lock, flags);
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4793
  		return SUCCESS;
92b3e5bbb   Mike Christie   [SCSI] qla4xxx: g...
4794
  	}
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4795
  	kref_get(&srb->srb_ref);
92b3e5bbb   Mike Christie   [SCSI] qla4xxx: g...
4796
  	spin_unlock_irqrestore(&ha->hardware_lock, flags);
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
  
  	if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
  		DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.
  ",
  		    ha->host_no, id, lun));
  		ret = FAILED;
  	} else {
  		DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.
  ",
  		    ha->host_no, id, lun));
  		wait = 1;
  	}
  
  	kref_put(&srb->srb_ref, qla4xxx_srb_compl);
  
  	/* Wait for command to complete */
  	if (wait) {
  		if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
  			DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out
  ",
  			    ha->host_no, id, lun));
  			ret = FAILED;
  		}
  	}
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4821
  	ql4_printk(KERN_INFO, ha,
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4822
4823
  	    "scsi%ld:%d:%d: Abort command - %s
  ",
25985edce   Lucas De Marchi   Fix common misspe...
4824
  	    ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
09a0f7198   Vikas Chaudhary   [SCSI] qla4xxx: a...
4825
4826
4827
4828
4829
  
  	return ret;
  }
  
  /**
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
   * qla4xxx_eh_device_reset - callback for target reset.
   * @cmd: Pointer to Linux's SCSI command structure
   *
   * This routine is called by the Linux OS to reset all luns on the
   * specified target.
   **/
  static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
  {
  	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  	struct ddb_entry *ddb_entry = cmd->device->hostdata;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4840
  	int ret = FAILED, stat;
612f73488   Karen Higgins   [SCSI] qla4xxx: F...
4841
  	if (!ddb_entry)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4842
  		return ret;
c01be6dcb   Mike Christie   [SCSI] iscsi_tran...
4843
4844
4845
4846
  	ret = iscsi_block_scsi_eh(cmd);
  	if (ret)
  		return ret;
  	ret = FAILED;
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4847
  	ql4_printk(KERN_INFO, ha,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4848
4849
4850
4851
4852
4853
4854
4855
  		   "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.
  ", ha->host_no,
  		   cmd->device->channel, cmd->device->id, cmd->device->lun);
  
  	DEBUG2(printk(KERN_INFO
  		      "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
  		      "dpc_flags=%lx, status=%x allowed=%d
  ", ha->host_no,
242f9dcb8   Jens Axboe   block: unify requ...
4856
  		      cmd, jiffies, cmd->request->timeout / HZ,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4857
4858
4859
4860
4861
  		      ha->dpc_flags, cmd->result, cmd->allowed));
  
  	/* FIXME: wait for hba to go online */
  	stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
  	if (stat != QLA_SUCCESS) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4862
4863
  		ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d
  ", stat);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4864
4865
  		goto eh_dev_reset_done;
  	}
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4866
4867
  	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  					 cmd->device)) {
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4868
  		ql4_printk(KERN_INFO, ha,
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4869
4870
4871
4872
  			   "DEVICE RESET FAILED - waiting for "
  			   "commands.
  ");
  		goto eh_dev_reset_done;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4873
  	}
9d5629136   David C Somayajulu   [SCSI] qla4xxx: f...
4874
4875
4876
4877
  	/* Send marker. */
  	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  		MM_LUN_RESET) != QLA_SUCCESS)
  		goto eh_dev_reset_done;
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4878
  	ql4_printk(KERN_INFO, ha,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
  		   "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.
  ",
  		   ha->host_no, cmd->device->channel, cmd->device->id,
  		   cmd->device->lun);
  
  	ret = SUCCESS;
  
  eh_dev_reset_done:
  
  	return ret;
  }
  
  /**
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4892
4893
4894
4895
4896
4897
4898
4899
4900
   * qla4xxx_eh_target_reset - callback for target reset.
   * @cmd: Pointer to Linux's SCSI command structure
   *
   * This routine is called by the Linux OS to reset the target.
   **/
  static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
  {
  	struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
  	struct ddb_entry *ddb_entry = cmd->device->hostdata;
c01be6dcb   Mike Christie   [SCSI] iscsi_tran...
4901
  	int stat, ret;
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4902
4903
4904
  
  	if (!ddb_entry)
  		return FAILED;
c01be6dcb   Mike Christie   [SCSI] iscsi_tran...
4905
4906
4907
  	ret = iscsi_block_scsi_eh(cmd);
  	if (ret)
  		return ret;
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4908
4909
4910
4911
4912
4913
4914
4915
  	starget_printk(KERN_INFO, scsi_target(cmd->device),
  		       "WARM TARGET RESET ISSUED.
  ");
  
  	DEBUG2(printk(KERN_INFO
  		      "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
  		      "to=%x,dpc_flags=%lx, status=%x allowed=%d
  ",
242f9dcb8   Jens Axboe   block: unify requ...
4916
  		      ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4917
4918
4919
4920
4921
4922
4923
4924
4925
  		      ha->dpc_flags, cmd->result, cmd->allowed));
  
  	stat = qla4xxx_reset_target(ha, ddb_entry);
  	if (stat != QLA_SUCCESS) {
  		starget_printk(KERN_INFO, scsi_target(cmd->device),
  			       "WARM TARGET RESET FAILED.
  ");
  		return FAILED;
  	}
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4926
4927
4928
4929
4930
4931
4932
4933
  	if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
  					 NULL)) {
  		starget_printk(KERN_INFO, scsi_target(cmd->device),
  			       "WARM TARGET DEVICE RESET FAILED - "
  			       "waiting for commands.
  ");
  		return FAILED;
  	}
9d5629136   David C Somayajulu   [SCSI] qla4xxx: f...
4934
4935
4936
4937
4938
4939
4940
4941
4942
  	/* Send marker. */
  	if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
  		MM_TGT_WARM_RESET) != QLA_SUCCESS) {
  		starget_printk(KERN_INFO, scsi_target(cmd->device),
  			       "WARM TARGET DEVICE RESET FAILED - "
  			       "marker iocb failed.
  ");
  		return FAILED;
  	}
ce5450392   Mike Christie   [SCSI] qla4xxx: A...
4943
4944
4945
4946
4947
4948
4949
  	starget_printk(KERN_INFO, scsi_target(cmd->device),
  		       "WARM TARGET RESET SUCCEEDED.
  ");
  	return SUCCESS;
  }
  
  /**
8a2889601   Sarang Radke   [SCSI] qla4xxx: f...
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
   * qla4xxx_is_eh_active - check if error handler is running
   * @shost: Pointer to SCSI Host struct
   *
   * This routine finds that if reset host is called in EH
   * scenario or from some application like sg_reset
   **/
  static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
  {
  	if (shost->shost_state == SHOST_RECOVERY)
  		return 1;
  	return 0;
  }
  
  /**
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
   * qla4xxx_eh_host_reset - kernel callback
   * @cmd: Pointer to Linux's SCSI command structure
   *
   * This routine is invoked by the Linux kernel to perform fatal error
   * recovery on the specified adapter.
   **/
  static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
  {
  	int return_status = FAILED;
  	struct scsi_qla_host *ha;
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
4974
  	ha = to_qla_host(cmd->device->host);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4975

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4976
4977
4978
4979
  	if (ql4xdontresethba) {
  		DEBUG2(printk("scsi%ld: %s: Don't Reset HBA
  ",
  		     ha->host_no, __func__));
8a2889601   Sarang Radke   [SCSI] qla4xxx: f...
4980
4981
4982
4983
  
  		/* Clear outstanding srb in queues */
  		if (qla4xxx_is_eh_active(cmd->device->host))
  			qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4984
4985
  		return FAILED;
  	}
c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
4986
  	ql4_printk(KERN_INFO, ha,
dca05c4c0   Karen Higgins   [SCSI] qla4xxx: F...
4987
4988
  		   "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.
  ", ha->host_no,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
  		   cmd->device->channel, cmd->device->id, cmd->device->lun);
  
  	if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
  		DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host.  Adapter "
  			      "DEAD.
  ", ha->host_no, cmd->device->channel,
  			      __func__));
  
  		return FAILED;
  	}
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
4999
5000
5001
5002
5003
5004
  	if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  		if (is_qla8022(ha))
  			set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
  		else
  			set_bit(DPC_RESET_HA, &ha->dpc_flags);
  	}
50a29aec9   Mike Christie   [SCSI] qla4xxx: f...
5005

f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
5006
  	if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5007
  		return_status = SUCCESS;
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5008

c2660df31   Vikas Chaudhary   [SCSI] qla4xxx: r...
5009
5010
  	ql4_printk(KERN_INFO, ha, "HOST RESET %s.
  ",
25985edce   Lucas De Marchi   Fix common misspe...
5011
  		   return_status == FAILED ? "FAILED" : "SUCCEEDED");
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5012
5013
5014
  
  	return return_status;
  }
95d31262b   Vikas Chaudhary   [SCSI] qla4xxx: A...
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
  static int qla4xxx_context_reset(struct scsi_qla_host *ha)
  {
  	uint32_t mbox_cmd[MBOX_REG_COUNT];
  	uint32_t mbox_sts[MBOX_REG_COUNT];
  	struct addr_ctrl_blk_def *acb = NULL;
  	uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
  	int rval = QLA_SUCCESS;
  	dma_addr_t acb_dma;
  
  	acb = dma_alloc_coherent(&ha->pdev->dev,
  				 sizeof(struct addr_ctrl_blk_def),
  				 &acb_dma, GFP_KERNEL);
  	if (!acb) {
  		ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb
  ",
  			   __func__);
  		rval = -ENOMEM;
  		goto exit_port_reset;
  	}
  
  	memset(acb, 0, acb_len);
  
  	rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
  	if (rval != QLA_SUCCESS) {
  		rval = -EIO;
  		goto exit_free_acb;
  	}
  
  	rval = qla4xxx_disable_acb(ha);
  	if (rval != QLA_SUCCESS) {
  		rval = -EIO;
  		goto exit_free_acb;
  	}
  
  	wait_for_completion_timeout(&ha->disable_acb_comp,
  				    DISABLE_ACB_TOV * HZ);
  
  	rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
  	if (rval != QLA_SUCCESS) {
  		rval = -EIO;
  		goto exit_free_acb;
  	}
  
  exit_free_acb:
  	dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
  			  acb, acb_dma);
  exit_port_reset:
  	DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s
  ", __func__,
  			  rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
  	return rval;
  }
  
  static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
  {
  	struct scsi_qla_host *ha = to_qla_host(shost);
  	int rval = QLA_SUCCESS;
  
  	if (ql4xdontresethba) {
  		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA
  ",
  				  __func__));
  		rval = -EPERM;
  		goto exit_host_reset;
  	}
  
  	rval = qla4xxx_wait_for_hba_online(ha);
  	if (rval != QLA_SUCCESS) {
  		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Unable to reset host "
  				  "adapter
  ", __func__));
  		rval = -EIO;
  		goto exit_host_reset;
  	}
  
  	if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
  		goto recover_adapter;
  
  	switch (reset_type) {
  	case SCSI_ADAPTER_RESET:
  		set_bit(DPC_RESET_HA, &ha->dpc_flags);
  		break;
  	case SCSI_FIRMWARE_RESET:
  		if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
  			if (is_qla8022(ha))
  				/* set firmware context reset */
  				set_bit(DPC_RESET_HA_FW_CONTEXT,
  					&ha->dpc_flags);
  			else {
  				rval = qla4xxx_context_reset(ha);
  				goto exit_host_reset;
  			}
  		}
  		break;
  	}
  
  recover_adapter:
  	rval = qla4xxx_recover_adapter(ha);
  	if (rval != QLA_SUCCESS) {
  		DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail
  ",
  				  __func__));
  		rval = -EIO;
  	}
  
  exit_host_reset:
  	return rval;
  }
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
  /* PCI AER driver recovers from all correctable errors w/o
   * driver intervention. For uncorrectable errors PCI AER
   * driver calls the following device driver's callbacks
   *
   * - Fatal Errors - link_reset
   * - Non-Fatal Errors - driver's pci_error_detected() which
   * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
   *
   * PCI AER driver calls
   * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
   *               returns RECOVERED or NEED_RESET if fw_hung
   * NEED_RESET - driver's slot_reset()
   * DISCONNECT - device is dead & cannot recover
   * RECOVERED - driver's pci_resume()
   */
  static pci_ers_result_t
  qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  {
  	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  
  	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x
  ",
  	    ha->host_no, __func__, state);
  
  	if (!is_aer_supported(ha))
  		return PCI_ERS_RESULT_NONE;
  
  	switch (state) {
  	case pci_channel_io_normal:
  		clear_bit(AF_EEH_BUSY, &ha->flags);
  		return PCI_ERS_RESULT_CAN_RECOVER;
  	case pci_channel_io_frozen:
  		set_bit(AF_EEH_BUSY, &ha->flags);
  		qla4xxx_mailbox_premature_completion(ha);
  		qla4xxx_free_irqs(ha);
  		pci_disable_device(pdev);
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5159
5160
  		/* Return back all IOs */
  		qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
  		return PCI_ERS_RESULT_NEED_RESET;
  	case pci_channel_io_perm_failure:
  		set_bit(AF_EEH_BUSY, &ha->flags);
  		set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
  		qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
  		return PCI_ERS_RESULT_DISCONNECT;
  	}
  	return PCI_ERS_RESULT_NEED_RESET;
  }
  
  /**
   * qla4xxx_pci_mmio_enabled() gets called if
   * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
   * and read/write to the device still works.
   **/
  static pci_ers_result_t
  qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
  {
  	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  
  	if (!is_aer_supported(ha))
  		return PCI_ERS_RESULT_NONE;
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5183
  	return PCI_ERS_RESULT_RECOVERED;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5184
  }
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5185
  static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5186
5187
  {
  	uint32_t rval = QLA_ERROR;
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5188
  	uint32_t ret = 0;
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
  	int fn;
  	struct pci_dev *other_pdev = NULL;
  
  	ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s
  ", ha->host_no, __func__);
  
  	set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  
  	if (test_bit(AF_ONLINE, &ha->flags)) {
  		clear_bit(AF_ONLINE, &ha->flags);
b3a271a94   Manish Rangankar   [SCSI] qla4xxx: s...
5199
5200
  		clear_bit(AF_LINK_UP, &ha->flags);
  		iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5201
  		qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
  	}
  
  	fn = PCI_FUNC(ha->pdev->devfn);
  	while (fn > 0) {
  		fn--;
  		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
  		    "func %x
  ", ha->host_no, __func__, fn);
  		/* Get the pci device given the domain, bus,
  		 * slot/function number */
  		other_pdev =
  		    pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
  		    ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
  		    fn));
  
  		if (!other_pdev)
  			continue;
  
  		if (atomic_read(&other_pdev->enable_cnt)) {
  			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
  			    "func in enabled state%x
  ", ha->host_no,
  			    __func__, fn);
  			pci_dev_put(other_pdev);
  			break;
  		}
  		pci_dev_put(other_pdev);
  	}
  
  	/* The first function on the card, the reset owner will
  	 * start & initialize the firmware. The other functions
  	 * on the card will reset the firmware context
  	 */
  	if (!fn) {
  		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
  		    "0x%x is the owner
  ", ha->host_no, __func__,
  		    ha->pdev->devfn);
  
  		qla4_8xxx_idc_lock(ha);
  		qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  		    QLA82XX_DEV_COLD);
  
  		qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_IDC_VERSION,
  		    QLA82XX_IDC_VERSION);
  
  		qla4_8xxx_idc_unlock(ha);
  		clear_bit(AF_FW_RECOVERY, &ha->flags);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
5250
  		rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
  		qla4_8xxx_idc_lock(ha);
  
  		if (rval != QLA_SUCCESS) {
  			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  			    "FAILED
  ", ha->host_no, __func__);
  			qla4_8xxx_clear_drv_active(ha);
  			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  			    QLA82XX_DEV_FAILED);
  		} else {
  			ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
  			    "READY
  ", ha->host_no, __func__);
  			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
  			    QLA82XX_DEV_READY);
  			/* Clear driver state register */
  			qla4_8xxx_wr_32(ha, QLA82XX_CRB_DRV_STATE, 0);
  			qla4_8xxx_set_drv_active(ha);
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
  			ret = qla4xxx_request_irqs(ha);
  			if (ret) {
  				ql4_printk(KERN_WARNING, ha, "Failed to "
  				    "reserve interrupt %d already in use.
  ",
  				    ha->pdev->irq);
  				rval = QLA_ERROR;
  			} else {
  				ha->isp_ops->enable_intrs(ha);
  				rval = QLA_SUCCESS;
  			}
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
  		}
  		qla4_8xxx_idc_unlock(ha);
  	} else {
  		ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
  		    "the reset owner
  ", ha->host_no, __func__,
  		    ha->pdev->devfn);
  		if ((qla4_8xxx_rd_32(ha, QLA82XX_CRB_DEV_STATE) ==
  		    QLA82XX_DEV_READY)) {
  			clear_bit(AF_FW_RECOVERY, &ha->flags);
13483730a   Mike Christie   [SCSI] qla4xxx: f...
5290
  			rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
  			if (rval == QLA_SUCCESS) {
  				ret = qla4xxx_request_irqs(ha);
  				if (ret) {
  					ql4_printk(KERN_WARNING, ha, "Failed to"
  					    " reserve interrupt %d already in"
  					    " use.
  ", ha->pdev->irq);
  					rval = QLA_ERROR;
  				} else {
  					ha->isp_ops->enable_intrs(ha);
  					rval = QLA_SUCCESS;
  				}
  			}
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
  			qla4_8xxx_idc_lock(ha);
  			qla4_8xxx_set_drv_active(ha);
  			qla4_8xxx_idc_unlock(ha);
  		}
  	}
  	clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
  	return rval;
  }
  
  static pci_ers_result_t
  qla4xxx_pci_slot_reset(struct pci_dev *pdev)
  {
  	pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
  	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  	int rc;
  
  	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset
  ",
  	    ha->host_no, __func__);
  
  	if (!is_aer_supported(ha))
  		return PCI_ERS_RESULT_NONE;
  
  	/* Restore the saved state of PCIe device -
  	 * BAR registers, PCI Config space, PCIX, MSI,
  	 * IOV states
  	 */
  	pci_restore_state(pdev);
  
  	/* pci_restore_state() clears the saved_state flag of the device
  	 * save restored state which resets saved_state flag
  	 */
  	pci_save_state(pdev);
  
  	/* Initialize device or resume if in suspended state */
  	rc = pci_enable_device(pdev);
  	if (rc) {
25985edce   Lucas De Marchi   Fix common misspe...
5341
  		ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5342
5343
5344
5345
  		    "device after reset
  ", ha->host_no, __func__);
  		goto exit_slot_reset;
  	}
7b3595df0   Vikas Chaudhary   [SCSI] qla4xxx: u...
5346
  	ha->isp_ops->disable_intrs(ha);
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
  
  	if (is_qla8022(ha)) {
  		if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
  			ret = PCI_ERS_RESULT_RECOVERED;
  			goto exit_slot_reset;
  		} else
  			goto exit_slot_reset;
  	}
  
  exit_slot_reset:
  	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x
  "
  	    "device after reset
  ", ha->host_no, __func__, ret);
  	return ret;
  }
  
  static void
  qla4xxx_pci_resume(struct pci_dev *pdev)
  {
  	struct scsi_qla_host *ha = pci_get_drvdata(pdev);
  	int ret;
  
  	ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume
  ",
  	    ha->host_no, __func__);
  
  	ret = qla4xxx_wait_for_hba_online(ha);
  	if (ret != QLA_SUCCESS) {
  		ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
  		    "resume I/O from slot/link_reset
  ", ha->host_no,
  		     __func__);
  	}
  
  	pci_cleanup_aer_uncorrect_error_status(pdev);
  	clear_bit(AF_EEH_BUSY, &ha->flags);
  }
  
  static struct pci_error_handlers qla4xxx_err_handler = {
  	.error_detected = qla4xxx_pci_error_detected,
  	.mmio_enabled = qla4xxx_pci_mmio_enabled,
  	.slot_reset = qla4xxx_pci_slot_reset,
  	.resume = qla4xxx_pci_resume,
  };
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
  static struct pci_device_id qla4xxx_pci_tbl[] = {
  	{
  		.vendor		= PCI_VENDOR_ID_QLOGIC,
  		.device		= PCI_DEVICE_ID_QLOGIC_ISP4010,
  		.subvendor	= PCI_ANY_ID,
  		.subdevice	= PCI_ANY_ID,
  	},
  	{
  		.vendor		= PCI_VENDOR_ID_QLOGIC,
  		.device		= PCI_DEVICE_ID_QLOGIC_ISP4022,
  		.subvendor	= PCI_ANY_ID,
  		.subdevice	= PCI_ANY_ID,
  	},
d915058f4   David C Somayajulu   [SCSI] qla4xxx: a...
5405
5406
5407
5408
5409
5410
  	{
  		.vendor		= PCI_VENDOR_ID_QLOGIC,
  		.device		= PCI_DEVICE_ID_QLOGIC_ISP4032,
  		.subvendor	= PCI_ANY_ID,
  		.subdevice	= PCI_ANY_ID,
  	},
f4f5df23b   Vikas Chaudhary   [SCSI] qla4xxx: A...
5411
5412
5413
5414
5415
5416
  	{
  		.vendor         = PCI_VENDOR_ID_QLOGIC,
  		.device         = PCI_DEVICE_ID_QLOGIC_ISP8022,
  		.subvendor      = PCI_ANY_ID,
  		.subdevice      = PCI_ANY_ID,
  	},
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5417
5418
5419
  	{0, 0},
  };
  MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
479754777   Adrian Bunk   [SCSI] qla4xxx: p...
5420
  static struct pci_driver qla4xxx_pci_driver = {
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5421
5422
5423
5424
  	.name		= DRIVER_NAME,
  	.id_table	= qla4xxx_pci_tbl,
  	.probe		= qla4xxx_probe_adapter,
  	.remove		= qla4xxx_remove_adapter,
2232be0d5   Lalit Chandivade   [SCSI] qla4xxx: A...
5425
  	.err_handler = &qla4xxx_err_handler,
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5426
5427
5428
5429
5430
5431
5432
5433
  };
  
  static int __init qla4xxx_module_init(void)
  {
  	int ret;
  
  	/* Allocate cache for SRBs. */
  	srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
20c2df83d   Paul Mundt   mm: Remove slab d...
5434
  				       SLAB_HWCACHE_ALIGN, NULL);
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
  	if (srb_cachep == NULL) {
  		printk(KERN_ERR
  		       "%s: Unable to allocate SRB cache..."
  		       "Failing load!
  ", DRIVER_NAME);
  		ret = -ENOMEM;
  		goto no_srp_cache;
  	}
  
  	/* Derive version string. */
  	strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
11010fecd   Andrew Vasquez   [SCSI] Maintain m...
5446
  	if (ql4xextended_error_logging)
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5447
5448
5449
5450
5451
5452
5453
5454
  		strcat(qla4xxx_version_str, "-debug");
  
  	qla4xxx_scsi_transport =
  		iscsi_register_transport(&qla4xxx_iscsi_transport);
  	if (!qla4xxx_scsi_transport){
  		ret = -ENODEV;
  		goto release_srb_cache;
  	}
afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5455
5456
5457
5458
5459
5460
5461
  	ret = pci_register_driver(&qla4xxx_pci_driver);
  	if (ret)
  		goto unregister_transport;
  
  	printk(KERN_INFO "QLogic iSCSI HBA Driver
  ");
  	return 0;
5ae16db36   Doug Maxey   [SCSI] qla4xxx: f...
5462

afaf5a2d3   David Somayajulu   [SCSI] Initial Co...
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
  unregister_transport:
  	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  release_srb_cache:
  	kmem_cache_destroy(srb_cachep);
  no_srp_cache:
  	return ret;
  }
  
  static void __exit qla4xxx_module_exit(void)
  {
  	pci_unregister_driver(&qla4xxx_pci_driver);
  	iscsi_unregister_transport(&qla4xxx_iscsi_transport);
  	kmem_cache_destroy(srb_cachep);
  }
  
  module_init(qla4xxx_module_init);
  module_exit(qla4xxx_module_exit);
  
  MODULE_AUTHOR("QLogic Corporation");
  MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
  MODULE_LICENSE("GPL");
  MODULE_VERSION(QLA4XXX_DRIVER_VERSION);