[SCSI] qla4xxx: Allow reset in link down case
[deliverable/linux.git] / drivers / scsi / qla4xxx / ql4_os.c
CommitLineData
afaf5a2d
DS
1/*
2 * QLogic iSCSI HBA Driver
c68cdbf0 3 * Copyright (c) 2003-2012 QLogic Corporation
afaf5a2d
DS
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
5a0e3ad6 8#include <linux/slab.h>
2a991c21
MR
9#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
13483730 11#include <linux/inet.h>
afaf5a2d
DS
12
13#include <scsi/scsi_tcq.h>
14#include <scsi/scsicam.h>
15
16#include "ql4_def.h"
bee4fe8e
DS
17#include "ql4_version.h"
18#include "ql4_glbl.h"
19#include "ql4_dbg.h"
20#include "ql4_inline.h"
6e7b4292 21#include "ql4_83xx.h"
afaf5a2d
DS
22
23/*
24 * Driver version
25 */
47975477 26static char qla4xxx_version_str[40];
afaf5a2d
DS
27
28/*
29 * SRB allocation cache
30 */
e18b890b 31static struct kmem_cache *srb_cachep;
afaf5a2d
DS
32
33/*
34 * Module parameter information and variables
35 */
a7380a65 36static int ql4xdisablesysfsboot = 1;
13483730
MC
37module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(ql4xdisablesysfsboot,
a4e8a715
KH
39 " Set to disable exporting boot targets to sysfs.\n"
40 "\t\t 0 - Export boot targets\n"
41 "\t\t 1 - Do not export boot targets (Default)");
13483730 42
3573bfb2 43int ql4xdontresethba;
f4f5df23 44module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
afaf5a2d 45MODULE_PARM_DESC(ql4xdontresethba,
a4e8a715
KH
46 " Don't reset the HBA for driver recovery.\n"
47 "\t\t 0 - It will reset HBA (Default)\n"
48 "\t\t 1 - It will NOT reset HBA");
afaf5a2d 49
a4e8a715 50int ql4xextended_error_logging;
f4f5df23 51module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
11010fec 52MODULE_PARM_DESC(ql4xextended_error_logging,
a4e8a715
KH
53 " Option to enable extended error logging.\n"
54 "\t\t 0 - no logging (Default)\n"
55 "\t\t 2 - debug logging");
afaf5a2d 56
f4f5df23
VC
57int ql4xenablemsix = 1;
58module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
59MODULE_PARM_DESC(ql4xenablemsix,
a4e8a715
KH
60 " Set to enable MSI or MSI-X interrupt mechanism.\n"
61 "\t\t 0 = enable INTx interrupt mechanism.\n"
62 "\t\t 1 = enable MSI-X interrupt mechanism (Default).\n"
63 "\t\t 2 = enable MSI interrupt mechanism.");
477ffb9d 64
d510d965 65#define QL4_DEF_QDEPTH 32
8bb4033d
VC
66static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
67module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
68MODULE_PARM_DESC(ql4xmaxqdepth,
a4e8a715
KH
69 " Maximum queue depth to report for target devices.\n"
70 "\t\t Default: 32.");
d510d965 71
f7b4aa63
TP
72static int ql4xqfulltracking = 1;
73module_param(ql4xqfulltracking, int, S_IRUGO | S_IWUSR);
74MODULE_PARM_DESC(ql4xqfulltracking,
75 " Enable or disable dynamic tracking and adjustment of\n"
76 "\t\t scsi device queue depth.\n"
77 "\t\t 0 - Disable.\n"
78 "\t\t 1 - Enable. (Default)");
79
3038727c
VC
80static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
81module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
82MODULE_PARM_DESC(ql4xsess_recovery_tmo,
3573bfb2 83 " Target Session Recovery Timeout.\n"
a4e8a715 84 "\t\t Default: 120 sec.");
3038727c 85
068237c8
TP
86int ql4xmdcapmask = 0x1F;
87module_param(ql4xmdcapmask, int, S_IRUGO);
88MODULE_PARM_DESC(ql4xmdcapmask,
89 " Set the Minidump driver capture mask level.\n"
90 "\t\t Default is 0x1F.\n"
91 "\t\t Can be set to 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F");
92
93int ql4xenablemd = 1;
94module_param(ql4xenablemd, int, S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(ql4xenablemd,
96 " Set to enable minidump.\n"
97 "\t\t 0 - disable minidump\n"
98 "\t\t 1 - enable minidump (Default)");
99
b3a271a9 100static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
afaf5a2d
DS
101/*
102 * SCSI host template entry points
103 */
47975477 104static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
afaf5a2d
DS
105
106/*
107 * iSCSI template entry points
108 */
fca9f04d
MC
109static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
110 enum iscsi_param param, char *buf);
afaf5a2d
DS
111static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
112 enum iscsi_param param, char *buf);
aa1e93a2
MC
113static int qla4xxx_host_get_param(struct Scsi_Host *shost,
114 enum iscsi_host_param param, char *buf);
00c31889
MC
115static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
116 uint32_t len);
ed1086e0
VC
117static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
118 enum iscsi_param_type param_type,
119 int param, char *buf);
5c656af7 120static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
b3a271a9
MR
121static struct iscsi_endpoint *qla4xxx_ep_connect(struct Scsi_Host *shost,
122 struct sockaddr *dst_addr,
123 int non_blocking);
124static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms);
125static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep);
126static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
127 enum iscsi_param param, char *buf);
128static int qla4xxx_conn_start(struct iscsi_cls_conn *conn);
129static struct iscsi_cls_conn *
130qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx);
131static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
132 struct iscsi_cls_conn *cls_conn,
133 uint64_t transport_fd, int is_leading);
134static void qla4xxx_conn_destroy(struct iscsi_cls_conn *conn);
135static struct iscsi_cls_session *
136qla4xxx_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
137 uint16_t qdepth, uint32_t initial_cmdsn);
138static void qla4xxx_session_destroy(struct iscsi_cls_session *sess);
139static void qla4xxx_task_work(struct work_struct *wdata);
140static int qla4xxx_alloc_pdu(struct iscsi_task *, uint8_t);
141static int qla4xxx_task_xmit(struct iscsi_task *);
142static void qla4xxx_task_cleanup(struct iscsi_task *);
143static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session);
144static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
145 struct iscsi_stats *stats);
c0b9d3f7
VC
146static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
147 uint32_t iface_type, uint32_t payload_size,
148 uint32_t pid, struct sockaddr *dst_addr);
376738af
NJ
149static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
150 uint32_t *num_entries, char *buf);
151static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx);
c0b9d3f7 152
afaf5a2d
DS
153/*
154 * SCSI host template entry points
155 */
f281233d 156static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
09a0f719 157static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
afaf5a2d 158static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
ce545039 159static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
afaf5a2d
DS
160static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd);
161static int qla4xxx_slave_alloc(struct scsi_device *device);
162static int qla4xxx_slave_configure(struct scsi_device *device);
163static void qla4xxx_slave_destroy(struct scsi_device *sdev);
5e9bcec7 164static umode_t qla4_attr_is_visible(int param_type, int param);
95d31262 165static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
f7b4aa63
TP
166static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
167 int reason);
afaf5a2d 168
f4f5df23
VC
169static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
170 QLA82XX_LEGACY_INTR_CONFIG;
171
afaf5a2d
DS
172static struct scsi_host_template qla4xxx_driver_template = {
173 .module = THIS_MODULE,
174 .name = DRIVER_NAME,
175 .proc_name = DRIVER_NAME,
176 .queuecommand = qla4xxx_queuecommand,
177
09a0f719 178 .eh_abort_handler = qla4xxx_eh_abort,
afaf5a2d 179 .eh_device_reset_handler = qla4xxx_eh_device_reset,
ce545039 180 .eh_target_reset_handler = qla4xxx_eh_target_reset,
afaf5a2d 181 .eh_host_reset_handler = qla4xxx_eh_host_reset,
5c656af7 182 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
afaf5a2d
DS
183
184 .slave_configure = qla4xxx_slave_configure,
185 .slave_alloc = qla4xxx_slave_alloc,
186 .slave_destroy = qla4xxx_slave_destroy,
f7b4aa63 187 .change_queue_depth = qla4xxx_change_queue_depth,
afaf5a2d
DS
188
189 .this_id = -1,
190 .cmd_per_lun = 3,
191 .use_clustering = ENABLE_CLUSTERING,
192 .sg_tablesize = SG_ALL,
193
194 .max_sectors = 0xFFFF,
7ad633c0 195 .shost_attrs = qla4xxx_host_attrs,
95d31262 196 .host_reset = qla4xxx_host_reset,
a355943c 197 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
afaf5a2d
DS
198};
199
200static struct iscsi_transport qla4xxx_iscsi_transport = {
201 .owner = THIS_MODULE,
202 .name = DRIVER_NAME,
b3a271a9
MR
203 .caps = CAP_TEXT_NEGO |
204 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
205 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
206 CAP_MULTI_R2T,
5e9bcec7 207 .attr_is_visible = qla4_attr_is_visible,
b3a271a9
MR
208 .create_session = qla4xxx_session_create,
209 .destroy_session = qla4xxx_session_destroy,
210 .start_conn = qla4xxx_conn_start,
211 .create_conn = qla4xxx_conn_create,
212 .bind_conn = qla4xxx_conn_bind,
213 .stop_conn = iscsi_conn_stop,
214 .destroy_conn = qla4xxx_conn_destroy,
215 .set_param = iscsi_set_param,
afaf5a2d 216 .get_conn_param = qla4xxx_conn_get_param,
fca9f04d 217 .get_session_param = qla4xxx_session_get_param,
b3a271a9
MR
218 .get_ep_param = qla4xxx_get_ep_param,
219 .ep_connect = qla4xxx_ep_connect,
220 .ep_poll = qla4xxx_ep_poll,
221 .ep_disconnect = qla4xxx_ep_disconnect,
222 .get_stats = qla4xxx_conn_get_stats,
223 .send_pdu = iscsi_conn_send_pdu,
224 .xmit_task = qla4xxx_task_xmit,
225 .cleanup_task = qla4xxx_task_cleanup,
226 .alloc_pdu = qla4xxx_alloc_pdu,
227
aa1e93a2 228 .get_host_param = qla4xxx_host_get_param,
d00efe3f 229 .set_iface_param = qla4xxx_iface_set_param,
ed1086e0 230 .get_iface_param = qla4xxx_get_iface_param,
a355943c 231 .bsg_request = qla4xxx_bsg_request,
c0b9d3f7 232 .send_ping = qla4xxx_send_ping,
376738af
NJ
233 .get_chap = qla4xxx_get_chap_list,
234 .delete_chap = qla4xxx_delete_chap,
afaf5a2d
DS
235};
236
237static struct scsi_transport_template *qla4xxx_scsi_transport;
238
c0b9d3f7
VC
239static int qla4xxx_send_ping(struct Scsi_Host *shost, uint32_t iface_num,
240 uint32_t iface_type, uint32_t payload_size,
241 uint32_t pid, struct sockaddr *dst_addr)
242{
243 struct scsi_qla_host *ha = to_qla_host(shost);
244 struct sockaddr_in *addr;
245 struct sockaddr_in6 *addr6;
246 uint32_t options = 0;
247 uint8_t ipaddr[IPv6_ADDR_LEN];
248 int rval;
249
250 memset(ipaddr, 0, IPv6_ADDR_LEN);
251 /* IPv4 to IPv4 */
252 if ((iface_type == ISCSI_IFACE_TYPE_IPV4) &&
253 (dst_addr->sa_family == AF_INET)) {
254 addr = (struct sockaddr_in *)dst_addr;
255 memcpy(ipaddr, &addr->sin_addr.s_addr, IP_ADDR_LEN);
256 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv4 Ping src: %pI4 "
257 "dest: %pI4\n", __func__,
258 &ha->ip_config.ip_address, ipaddr));
259 rval = qla4xxx_ping_iocb(ha, options, payload_size, pid,
260 ipaddr);
261 if (rval)
262 rval = -EINVAL;
263 } else if ((iface_type == ISCSI_IFACE_TYPE_IPV6) &&
264 (dst_addr->sa_family == AF_INET6)) {
265 /* IPv6 to IPv6 */
266 addr6 = (struct sockaddr_in6 *)dst_addr;
267 memcpy(ipaddr, &addr6->sin6_addr.in6_u.u6_addr8, IPv6_ADDR_LEN);
268
269 options |= PING_IPV6_PROTOCOL_ENABLE;
270
271 /* Ping using LinkLocal address */
272 if ((iface_num == 0) || (iface_num == 1)) {
273 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: LinkLocal Ping "
274 "src: %pI6 dest: %pI6\n", __func__,
275 &ha->ip_config.ipv6_link_local_addr,
276 ipaddr));
277 options |= PING_IPV6_LINKLOCAL_ADDR;
278 rval = qla4xxx_ping_iocb(ha, options, payload_size,
279 pid, ipaddr);
280 } else {
281 ql4_printk(KERN_WARNING, ha, "%s: iface num = %d "
282 "not supported\n", __func__, iface_num);
283 rval = -ENOSYS;
284 goto exit_send_ping;
285 }
286
287 /*
288 * If ping using LinkLocal address fails, try ping using
289 * IPv6 address
290 */
291 if (rval != QLA_SUCCESS) {
292 options &= ~PING_IPV6_LINKLOCAL_ADDR;
293 if (iface_num == 0) {
294 options |= PING_IPV6_ADDR0;
295 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
296 "Ping src: %pI6 "
297 "dest: %pI6\n", __func__,
298 &ha->ip_config.ipv6_addr0,
299 ipaddr));
300 } else if (iface_num == 1) {
301 options |= PING_IPV6_ADDR1;
302 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: IPv6 "
303 "Ping src: %pI6 "
304 "dest: %pI6\n", __func__,
305 &ha->ip_config.ipv6_addr1,
306 ipaddr));
307 }
308 rval = qla4xxx_ping_iocb(ha, options, payload_size,
309 pid, ipaddr);
310 if (rval)
311 rval = -EINVAL;
312 }
313 } else
314 rval = -ENOSYS;
315exit_send_ping:
316 return rval;
317}
318
5e9bcec7 319static umode_t qla4_attr_is_visible(int param_type, int param)
3128c6c7
MC
320{
321 switch (param_type) {
f27fb2ef
MC
322 case ISCSI_HOST_PARAM:
323 switch (param) {
324 case ISCSI_HOST_PARAM_HWADDRESS:
325 case ISCSI_HOST_PARAM_IPADDRESS:
326 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3254dbe9
VC
327 case ISCSI_HOST_PARAM_PORT_STATE:
328 case ISCSI_HOST_PARAM_PORT_SPEED:
f27fb2ef
MC
329 return S_IRUGO;
330 default:
331 return 0;
332 }
3128c6c7
MC
333 case ISCSI_PARAM:
334 switch (param) {
590134fa
MC
335 case ISCSI_PARAM_PERSISTENT_ADDRESS:
336 case ISCSI_PARAM_PERSISTENT_PORT:
3128c6c7
MC
337 case ISCSI_PARAM_CONN_ADDRESS:
338 case ISCSI_PARAM_CONN_PORT:
1d063c17
MC
339 case ISCSI_PARAM_TARGET_NAME:
340 case ISCSI_PARAM_TPGT:
341 case ISCSI_PARAM_TARGET_ALIAS:
b3a271a9
MR
342 case ISCSI_PARAM_MAX_BURST:
343 case ISCSI_PARAM_MAX_R2T:
344 case ISCSI_PARAM_FIRST_BURST:
345 case ISCSI_PARAM_MAX_RECV_DLENGTH:
346 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
de37920b 347 case ISCSI_PARAM_IFACE_NAME:
fca9f04d
MC
348 case ISCSI_PARAM_CHAP_OUT_IDX:
349 case ISCSI_PARAM_CHAP_IN_IDX:
350 case ISCSI_PARAM_USERNAME:
351 case ISCSI_PARAM_PASSWORD:
352 case ISCSI_PARAM_USERNAME_IN:
353 case ISCSI_PARAM_PASSWORD_IN:
3128c6c7
MC
354 return S_IRUGO;
355 default:
356 return 0;
357 }
b78dbba0
MC
358 case ISCSI_NET_PARAM:
359 switch (param) {
360 case ISCSI_NET_PARAM_IPV4_ADDR:
361 case ISCSI_NET_PARAM_IPV4_SUBNET:
362 case ISCSI_NET_PARAM_IPV4_GW:
363 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
364 case ISCSI_NET_PARAM_IFACE_ENABLE:
365 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
366 case ISCSI_NET_PARAM_IPV6_ADDR:
367 case ISCSI_NET_PARAM_IPV6_ROUTER:
368 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
369 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
6ac73e8c
VC
370 case ISCSI_NET_PARAM_VLAN_ID:
371 case ISCSI_NET_PARAM_VLAN_PRIORITY:
372 case ISCSI_NET_PARAM_VLAN_ENABLED:
943c157b 373 case ISCSI_NET_PARAM_MTU:
2ada7fc5 374 case ISCSI_NET_PARAM_PORT:
b78dbba0
MC
375 return S_IRUGO;
376 default:
377 return 0;
378 }
3128c6c7
MC
379 }
380
381 return 0;
382}
383
376738af
NJ
384static int qla4xxx_get_chap_list(struct Scsi_Host *shost, uint16_t chap_tbl_idx,
385 uint32_t *num_entries, char *buf)
386{
387 struct scsi_qla_host *ha = to_qla_host(shost);
388 struct ql4_chap_table *chap_table;
389 struct iscsi_chap_rec *chap_rec;
390 int max_chap_entries = 0;
391 int valid_chap_entries = 0;
392 int ret = 0, i;
393
394 if (is_qla8022(ha))
395 max_chap_entries = (ha->hw.flt_chap_size / 2) /
396 sizeof(struct ql4_chap_table);
397 else
398 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
399
400 ql4_printk(KERN_INFO, ha, "%s: num_entries = %d, CHAP idx = %d\n",
401 __func__, *num_entries, chap_tbl_idx);
402
403 if (!buf) {
404 ret = -ENOMEM;
405 goto exit_get_chap_list;
406 }
407
408 chap_rec = (struct iscsi_chap_rec *) buf;
409 mutex_lock(&ha->chap_sem);
410 for (i = chap_tbl_idx; i < max_chap_entries; i++) {
411 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
412 if (chap_table->cookie !=
413 __constant_cpu_to_le16(CHAP_VALID_COOKIE))
414 continue;
415
416 chap_rec->chap_tbl_idx = i;
417 strncpy(chap_rec->username, chap_table->name,
418 ISCSI_CHAP_AUTH_NAME_MAX_LEN);
419 strncpy(chap_rec->password, chap_table->secret,
420 QL4_CHAP_MAX_SECRET_LEN);
421 chap_rec->password_length = chap_table->secret_len;
422
423 if (chap_table->flags & BIT_7) /* local */
424 chap_rec->chap_type = CHAP_TYPE_OUT;
425
426 if (chap_table->flags & BIT_6) /* peer */
427 chap_rec->chap_type = CHAP_TYPE_IN;
428
429 chap_rec++;
430
431 valid_chap_entries++;
432 if (valid_chap_entries == *num_entries)
433 break;
434 else
435 continue;
436 }
437 mutex_unlock(&ha->chap_sem);
438
439exit_get_chap_list:
440 ql4_printk(KERN_INFO, ha, "%s: Valid CHAP Entries = %d\n",
441 __func__, valid_chap_entries);
442 *num_entries = valid_chap_entries;
443 return ret;
444}
445
446static int __qla4xxx_is_chap_active(struct device *dev, void *data)
447{
448 int ret = 0;
449 uint16_t *chap_tbl_idx = (uint16_t *) data;
450 struct iscsi_cls_session *cls_session;
451 struct iscsi_session *sess;
452 struct ddb_entry *ddb_entry;
453
454 if (!iscsi_is_session_dev(dev))
455 goto exit_is_chap_active;
456
457 cls_session = iscsi_dev_to_session(dev);
458 sess = cls_session->dd_data;
459 ddb_entry = sess->dd_data;
460
461 if (iscsi_session_chkready(cls_session))
462 goto exit_is_chap_active;
463
464 if (ddb_entry->chap_tbl_idx == *chap_tbl_idx)
465 ret = 1;
466
467exit_is_chap_active:
468 return ret;
469}
470
471static int qla4xxx_is_chap_active(struct Scsi_Host *shost,
472 uint16_t chap_tbl_idx)
473{
474 int ret = 0;
475
476 ret = device_for_each_child(&shost->shost_gendev, &chap_tbl_idx,
477 __qla4xxx_is_chap_active);
478
479 return ret;
480}
481
482static int qla4xxx_delete_chap(struct Scsi_Host *shost, uint16_t chap_tbl_idx)
483{
484 struct scsi_qla_host *ha = to_qla_host(shost);
485 struct ql4_chap_table *chap_table;
486 dma_addr_t chap_dma;
487 int max_chap_entries = 0;
488 uint32_t offset = 0;
489 uint32_t chap_size;
490 int ret = 0;
491
492 chap_table = dma_pool_alloc(ha->chap_dma_pool, GFP_KERNEL, &chap_dma);
493 if (chap_table == NULL)
494 return -ENOMEM;
495
496 memset(chap_table, 0, sizeof(struct ql4_chap_table));
497
498 if (is_qla8022(ha))
499 max_chap_entries = (ha->hw.flt_chap_size / 2) /
500 sizeof(struct ql4_chap_table);
501 else
502 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
503
504 if (chap_tbl_idx > max_chap_entries) {
505 ret = -EINVAL;
506 goto exit_delete_chap;
507 }
508
509 /* Check if chap index is in use.
510 * If chap is in use don't delet chap entry */
511 ret = qla4xxx_is_chap_active(shost, chap_tbl_idx);
512 if (ret) {
513 ql4_printk(KERN_INFO, ha, "CHAP entry %d is in use, cannot "
514 "delete from flash\n", chap_tbl_idx);
515 ret = -EBUSY;
516 goto exit_delete_chap;
517 }
518
519 chap_size = sizeof(struct ql4_chap_table);
520 if (is_qla40XX(ha))
521 offset = FLASH_CHAP_OFFSET | (chap_tbl_idx * chap_size);
522 else {
523 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
524 /* flt_chap_size is CHAP table size for both ports
525 * so divide it by 2 to calculate the offset for second port
526 */
527 if (ha->port_num == 1)
528 offset += (ha->hw.flt_chap_size / 2);
529 offset += (chap_tbl_idx * chap_size);
530 }
531
532 ret = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
533 if (ret != QLA_SUCCESS) {
534 ret = -EINVAL;
535 goto exit_delete_chap;
536 }
537
538 DEBUG2(ql4_printk(KERN_INFO, ha, "Chap Cookie: x%x\n",
539 __le16_to_cpu(chap_table->cookie)));
540
541 if (__le16_to_cpu(chap_table->cookie) != CHAP_VALID_COOKIE) {
542 ql4_printk(KERN_ERR, ha, "No valid chap entry found\n");
543 goto exit_delete_chap;
544 }
545
546 chap_table->cookie = __constant_cpu_to_le16(0xFFFF);
547
548 offset = FLASH_CHAP_OFFSET |
549 (chap_tbl_idx * sizeof(struct ql4_chap_table));
550 ret = qla4xxx_set_flash(ha, chap_dma, offset, chap_size,
551 FLASH_OPT_RMW_COMMIT);
552 if (ret == QLA_SUCCESS && ha->chap_list) {
553 mutex_lock(&ha->chap_sem);
554 /* Update ha chap_list cache */
555 memcpy((struct ql4_chap_table *)ha->chap_list + chap_tbl_idx,
556 chap_table, sizeof(struct ql4_chap_table));
557 mutex_unlock(&ha->chap_sem);
558 }
559 if (ret != QLA_SUCCESS)
560 ret = -EINVAL;
561
562exit_delete_chap:
563 dma_pool_free(ha->chap_dma_pool, chap_table, chap_dma);
564 return ret;
565}
566
ed1086e0
VC
567static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
568 enum iscsi_param_type param_type,
569 int param, char *buf)
570{
571 struct Scsi_Host *shost = iscsi_iface_to_shost(iface);
572 struct scsi_qla_host *ha = to_qla_host(shost);
573 int len = -ENOSYS;
574
575 if (param_type != ISCSI_NET_PARAM)
576 return -ENOSYS;
577
578 switch (param) {
579 case ISCSI_NET_PARAM_IPV4_ADDR:
580 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
581 break;
582 case ISCSI_NET_PARAM_IPV4_SUBNET:
583 len = sprintf(buf, "%pI4\n", &ha->ip_config.subnet_mask);
584 break;
585 case ISCSI_NET_PARAM_IPV4_GW:
586 len = sprintf(buf, "%pI4\n", &ha->ip_config.gateway);
587 break;
588 case ISCSI_NET_PARAM_IFACE_ENABLE:
589 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
590 len = sprintf(buf, "%s\n",
591 (ha->ip_config.ipv4_options &
592 IPOPT_IPV4_PROTOCOL_ENABLE) ?
593 "enabled" : "disabled");
594 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
595 len = sprintf(buf, "%s\n",
596 (ha->ip_config.ipv6_options &
597 IPV6_OPT_IPV6_PROTOCOL_ENABLE) ?
598 "enabled" : "disabled");
599 break;
600 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
601 len = sprintf(buf, "%s\n",
602 (ha->ip_config.tcp_options & TCPOPT_DHCP_ENABLE) ?
603 "dhcp" : "static");
604 break;
605 case ISCSI_NET_PARAM_IPV6_ADDR:
606 if (iface->iface_num == 0)
607 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr0);
608 if (iface->iface_num == 1)
609 len = sprintf(buf, "%pI6\n", &ha->ip_config.ipv6_addr1);
610 break;
611 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
612 len = sprintf(buf, "%pI6\n",
613 &ha->ip_config.ipv6_link_local_addr);
614 break;
615 case ISCSI_NET_PARAM_IPV6_ROUTER:
616 len = sprintf(buf, "%pI6\n",
617 &ha->ip_config.ipv6_default_router_addr);
618 break;
619 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
620 len = sprintf(buf, "%s\n",
621 (ha->ip_config.ipv6_addl_options &
622 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE) ?
623 "nd" : "static");
624 break;
625 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
626 len = sprintf(buf, "%s\n",
627 (ha->ip_config.ipv6_addl_options &
628 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR) ?
629 "auto" : "static");
630 break;
6ac73e8c
VC
631 case ISCSI_NET_PARAM_VLAN_ID:
632 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
633 len = sprintf(buf, "%d\n",
634 (ha->ip_config.ipv4_vlan_tag &
635 ISCSI_MAX_VLAN_ID));
636 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
637 len = sprintf(buf, "%d\n",
638 (ha->ip_config.ipv6_vlan_tag &
639 ISCSI_MAX_VLAN_ID));
640 break;
641 case ISCSI_NET_PARAM_VLAN_PRIORITY:
642 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
643 len = sprintf(buf, "%d\n",
644 ((ha->ip_config.ipv4_vlan_tag >> 13) &
645 ISCSI_MAX_VLAN_PRIORITY));
646 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
647 len = sprintf(buf, "%d\n",
648 ((ha->ip_config.ipv6_vlan_tag >> 13) &
649 ISCSI_MAX_VLAN_PRIORITY));
650 break;
651 case ISCSI_NET_PARAM_VLAN_ENABLED:
652 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
653 len = sprintf(buf, "%s\n",
654 (ha->ip_config.ipv4_options &
655 IPOPT_VLAN_TAGGING_ENABLE) ?
656 "enabled" : "disabled");
657 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
658 len = sprintf(buf, "%s\n",
659 (ha->ip_config.ipv6_options &
660 IPV6_OPT_VLAN_TAGGING_ENABLE) ?
661 "enabled" : "disabled");
662 break;
943c157b
VC
663 case ISCSI_NET_PARAM_MTU:
664 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
665 break;
2ada7fc5
VC
666 case ISCSI_NET_PARAM_PORT:
667 if (iface->iface_type == ISCSI_IFACE_TYPE_IPV4)
668 len = sprintf(buf, "%d\n", ha->ip_config.ipv4_port);
669 else if (iface->iface_type == ISCSI_IFACE_TYPE_IPV6)
670 len = sprintf(buf, "%d\n", ha->ip_config.ipv6_port);
671 break;
ed1086e0
VC
672 default:
673 len = -ENOSYS;
674 }
675
676 return len;
677}
678
b3a271a9
MR
679static struct iscsi_endpoint *
680qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
681 int non_blocking)
5c656af7 682{
b3a271a9
MR
683 int ret;
684 struct iscsi_endpoint *ep;
685 struct qla_endpoint *qla_ep;
686 struct scsi_qla_host *ha;
687 struct sockaddr_in *addr;
688 struct sockaddr_in6 *addr6;
5c656af7 689
b3a271a9
MR
690 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
691 if (!shost) {
692 ret = -ENXIO;
693 printk(KERN_ERR "%s: shost is NULL\n",
694 __func__);
695 return ERR_PTR(ret);
696 }
5c656af7 697
b3a271a9
MR
698 ha = iscsi_host_priv(shost);
699
700 ep = iscsi_create_endpoint(sizeof(struct qla_endpoint));
701 if (!ep) {
702 ret = -ENOMEM;
703 return ERR_PTR(ret);
704 }
705
706 qla_ep = ep->dd_data;
707 memset(qla_ep, 0, sizeof(struct qla_endpoint));
708 if (dst_addr->sa_family == AF_INET) {
709 memcpy(&qla_ep->dst_addr, dst_addr, sizeof(struct sockaddr_in));
710 addr = (struct sockaddr_in *)&qla_ep->dst_addr;
711 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI4\n", __func__,
712 (char *)&addr->sin_addr));
713 } else if (dst_addr->sa_family == AF_INET6) {
714 memcpy(&qla_ep->dst_addr, dst_addr,
715 sizeof(struct sockaddr_in6));
716 addr6 = (struct sockaddr_in6 *)&qla_ep->dst_addr;
717 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: %pI6\n", __func__,
718 (char *)&addr6->sin6_addr));
719 }
720
721 qla_ep->host = shost;
722
723 return ep;
5c656af7
MC
724}
725
b3a271a9 726static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
afaf5a2d 727{
b3a271a9
MR
728 struct qla_endpoint *qla_ep;
729 struct scsi_qla_host *ha;
730 int ret = 0;
afaf5a2d 731
b3a271a9
MR
732 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
733 qla_ep = ep->dd_data;
734 ha = to_qla_host(qla_ep->host);
735
13483730 736 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
b3a271a9
MR
737 ret = 1;
738
739 return ret;
740}
741
742static void qla4xxx_ep_disconnect(struct iscsi_endpoint *ep)
743{
744 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
745 iscsi_destroy_endpoint(ep);
746}
747
748static int qla4xxx_get_ep_param(struct iscsi_endpoint *ep,
749 enum iscsi_param param,
750 char *buf)
751{
752 struct qla_endpoint *qla_ep = ep->dd_data;
753 struct sockaddr *dst_addr;
754
755 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
756
757 switch (param) {
758 case ISCSI_PARAM_CONN_PORT:
759 case ISCSI_PARAM_CONN_ADDRESS:
760 if (!qla_ep)
761 return -ENOTCONN;
762
763 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
764 if (!dst_addr)
765 return -ENOTCONN;
766
767 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
768 &qla_ep->dst_addr, param, buf);
769 default:
770 return -ENOSYS;
771 }
772}
773
774static void qla4xxx_conn_get_stats(struct iscsi_cls_conn *cls_conn,
775 struct iscsi_stats *stats)
776{
777 struct iscsi_session *sess;
778 struct iscsi_cls_session *cls_sess;
779 struct ddb_entry *ddb_entry;
780 struct scsi_qla_host *ha;
781 struct ql_iscsi_stats *ql_iscsi_stats;
782 int stats_size;
783 int ret;
784 dma_addr_t iscsi_stats_dma;
785
786 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
568d303b 787
b3a271a9
MR
788 cls_sess = iscsi_conn_to_session(cls_conn);
789 sess = cls_sess->dd_data;
790 ddb_entry = sess->dd_data;
791 ha = ddb_entry->ha;
792
793 stats_size = PAGE_ALIGN(sizeof(struct ql_iscsi_stats));
794 /* Allocate memory */
795 ql_iscsi_stats = dma_alloc_coherent(&ha->pdev->dev, stats_size,
796 &iscsi_stats_dma, GFP_KERNEL);
797 if (!ql_iscsi_stats) {
798 ql4_printk(KERN_ERR, ha,
799 "Unable to allocate memory for iscsi stats\n");
800 goto exit_get_stats;
568d303b 801 }
b3a271a9
MR
802
803 ret = qla4xxx_get_mgmt_data(ha, ddb_entry->fw_ddb_index, stats_size,
804 iscsi_stats_dma);
805 if (ret != QLA_SUCCESS) {
806 ql4_printk(KERN_ERR, ha,
59e13d48 807 "Unable to retrieve iscsi stats\n");
b3a271a9
MR
808 goto free_stats;
809 }
810
811 /* octets */
812 stats->txdata_octets = le64_to_cpu(ql_iscsi_stats->tx_data_octets);
813 stats->rxdata_octets = le64_to_cpu(ql_iscsi_stats->rx_data_octets);
814 /* xmit pdus */
815 stats->noptx_pdus = le32_to_cpu(ql_iscsi_stats->tx_nopout_pdus);
816 stats->scsicmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_cmd_pdus);
817 stats->tmfcmd_pdus = le32_to_cpu(ql_iscsi_stats->tx_tmf_cmd_pdus);
818 stats->login_pdus = le32_to_cpu(ql_iscsi_stats->tx_login_cmd_pdus);
819 stats->text_pdus = le32_to_cpu(ql_iscsi_stats->tx_text_cmd_pdus);
820 stats->dataout_pdus = le32_to_cpu(ql_iscsi_stats->tx_scsi_write_pdus);
821 stats->logout_pdus = le32_to_cpu(ql_iscsi_stats->tx_logout_cmd_pdus);
822 stats->snack_pdus = le32_to_cpu(ql_iscsi_stats->tx_snack_req_pdus);
823 /* recv pdus */
824 stats->noprx_pdus = le32_to_cpu(ql_iscsi_stats->rx_nopin_pdus);
825 stats->scsirsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_resp_pdus);
826 stats->tmfrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_tmf_resp_pdus);
827 stats->textrsp_pdus = le32_to_cpu(ql_iscsi_stats->rx_text_resp_pdus);
828 stats->datain_pdus = le32_to_cpu(ql_iscsi_stats->rx_scsi_read_pdus);
829 stats->logoutrsp_pdus =
830 le32_to_cpu(ql_iscsi_stats->rx_logout_resp_pdus);
831 stats->r2t_pdus = le32_to_cpu(ql_iscsi_stats->rx_r2t_pdus);
832 stats->async_pdus = le32_to_cpu(ql_iscsi_stats->rx_async_pdus);
833 stats->rjt_pdus = le32_to_cpu(ql_iscsi_stats->rx_reject_pdus);
834
835free_stats:
836 dma_free_coherent(&ha->pdev->dev, stats_size, ql_iscsi_stats,
837 iscsi_stats_dma);
838exit_get_stats:
839 return;
840}
841
842static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
843{
844 struct iscsi_cls_session *session;
845 struct iscsi_session *sess;
846 unsigned long flags;
847 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
848
849 session = starget_to_session(scsi_target(sc->device));
850 sess = session->dd_data;
851
852 spin_lock_irqsave(&session->lock, flags);
853 if (session->state == ISCSI_SESSION_FAILED)
854 ret = BLK_EH_RESET_TIMER;
855 spin_unlock_irqrestore(&session->lock, flags);
856
857 return ret;
afaf5a2d
DS
858}
859
3254dbe9
VC
860static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
861{
862 struct scsi_qla_host *ha = to_qla_host(shost);
e16d166e 863 struct iscsi_cls_host *ihost = shost->shost_data;
3254dbe9
VC
864 uint32_t speed = ISCSI_PORT_SPEED_UNKNOWN;
865
866 qla4xxx_get_firmware_state(ha);
867
868 switch (ha->addl_fw_state & 0x0F00) {
869 case FW_ADDSTATE_LINK_SPEED_10MBPS:
870 speed = ISCSI_PORT_SPEED_10MBPS;
871 break;
872 case FW_ADDSTATE_LINK_SPEED_100MBPS:
873 speed = ISCSI_PORT_SPEED_100MBPS;
874 break;
875 case FW_ADDSTATE_LINK_SPEED_1GBPS:
876 speed = ISCSI_PORT_SPEED_1GBPS;
877 break;
878 case FW_ADDSTATE_LINK_SPEED_10GBPS:
879 speed = ISCSI_PORT_SPEED_10GBPS;
880 break;
881 }
882 ihost->port_speed = speed;
883}
884
885static void qla4xxx_set_port_state(struct Scsi_Host *shost)
886{
887 struct scsi_qla_host *ha = to_qla_host(shost);
e16d166e 888 struct iscsi_cls_host *ihost = shost->shost_data;
3254dbe9
VC
889 uint32_t state = ISCSI_PORT_STATE_DOWN;
890
891 if (test_bit(AF_LINK_UP, &ha->flags))
892 state = ISCSI_PORT_STATE_UP;
893
894 ihost->port_state = state;
895}
896
aa1e93a2
MC
897static int qla4xxx_host_get_param(struct Scsi_Host *shost,
898 enum iscsi_host_param param, char *buf)
899{
900 struct scsi_qla_host *ha = to_qla_host(shost);
901 int len;
902
903 switch (param) {
904 case ISCSI_HOST_PARAM_HWADDRESS:
7ffc49a6 905 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
8ad5781a 906 break;
22236961 907 case ISCSI_HOST_PARAM_IPADDRESS:
2bab08fc 908 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
22236961 909 break;
8ad5781a 910 case ISCSI_HOST_PARAM_INITIATOR_NAME:
22236961 911 len = sprintf(buf, "%s\n", ha->name_string);
aa1e93a2 912 break;
3254dbe9
VC
913 case ISCSI_HOST_PARAM_PORT_STATE:
914 qla4xxx_set_port_state(shost);
915 len = sprintf(buf, "%s\n", iscsi_get_port_state_name(shost));
916 break;
917 case ISCSI_HOST_PARAM_PORT_SPEED:
918 qla4xxx_set_port_speed(shost);
919 len = sprintf(buf, "%s\n", iscsi_get_port_speed_name(shost));
920 break;
aa1e93a2
MC
921 default:
922 return -ENOSYS;
923 }
924
925 return len;
926}
927
ed1086e0
VC
928static void qla4xxx_create_ipv4_iface(struct scsi_qla_host *ha)
929{
930 if (ha->iface_ipv4)
931 return;
932
933 /* IPv4 */
934 ha->iface_ipv4 = iscsi_create_iface(ha->host,
935 &qla4xxx_iscsi_transport,
936 ISCSI_IFACE_TYPE_IPV4, 0, 0);
937 if (!ha->iface_ipv4)
938 ql4_printk(KERN_ERR, ha, "Could not create IPv4 iSCSI "
939 "iface0.\n");
940}
941
942static void qla4xxx_create_ipv6_iface(struct scsi_qla_host *ha)
943{
944 if (!ha->iface_ipv6_0)
945 /* IPv6 iface-0 */
946 ha->iface_ipv6_0 = iscsi_create_iface(ha->host,
947 &qla4xxx_iscsi_transport,
948 ISCSI_IFACE_TYPE_IPV6, 0,
949 0);
950 if (!ha->iface_ipv6_0)
951 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
952 "iface0.\n");
953
954 if (!ha->iface_ipv6_1)
955 /* IPv6 iface-1 */
956 ha->iface_ipv6_1 = iscsi_create_iface(ha->host,
957 &qla4xxx_iscsi_transport,
958 ISCSI_IFACE_TYPE_IPV6, 1,
959 0);
960 if (!ha->iface_ipv6_1)
961 ql4_printk(KERN_ERR, ha, "Could not create IPv6 iSCSI "
962 "iface1.\n");
963}
964
965static void qla4xxx_create_ifaces(struct scsi_qla_host *ha)
966{
967 if (ha->ip_config.ipv4_options & IPOPT_IPV4_PROTOCOL_ENABLE)
968 qla4xxx_create_ipv4_iface(ha);
969
970 if (ha->ip_config.ipv6_options & IPV6_OPT_IPV6_PROTOCOL_ENABLE)
971 qla4xxx_create_ipv6_iface(ha);
972}
973
974static void qla4xxx_destroy_ipv4_iface(struct scsi_qla_host *ha)
975{
976 if (ha->iface_ipv4) {
977 iscsi_destroy_iface(ha->iface_ipv4);
978 ha->iface_ipv4 = NULL;
979 }
980}
981
982static void qla4xxx_destroy_ipv6_iface(struct scsi_qla_host *ha)
983{
984 if (ha->iface_ipv6_0) {
985 iscsi_destroy_iface(ha->iface_ipv6_0);
986 ha->iface_ipv6_0 = NULL;
987 }
988 if (ha->iface_ipv6_1) {
989 iscsi_destroy_iface(ha->iface_ipv6_1);
990 ha->iface_ipv6_1 = NULL;
991 }
992}
993
994static void qla4xxx_destroy_ifaces(struct scsi_qla_host *ha)
995{
996 qla4xxx_destroy_ipv4_iface(ha);
997 qla4xxx_destroy_ipv6_iface(ha);
998}
999
d00efe3f
MC
1000static void qla4xxx_set_ipv6(struct scsi_qla_host *ha,
1001 struct iscsi_iface_param_info *iface_param,
1002 struct addr_ctrl_blk *init_fw_cb)
1003{
1004 /*
1005 * iface_num 0 is valid for IPv6 Addr, linklocal, router, autocfg.
1006 * iface_num 1 is valid only for IPv6 Addr.
1007 */
1008 switch (iface_param->param) {
1009 case ISCSI_NET_PARAM_IPV6_ADDR:
1010 if (iface_param->iface_num & 0x1)
1011 /* IPv6 Addr 1 */
1012 memcpy(init_fw_cb->ipv6_addr1, iface_param->value,
1013 sizeof(init_fw_cb->ipv6_addr1));
1014 else
1015 /* IPv6 Addr 0 */
1016 memcpy(init_fw_cb->ipv6_addr0, iface_param->value,
1017 sizeof(init_fw_cb->ipv6_addr0));
1018 break;
1019 case ISCSI_NET_PARAM_IPV6_LINKLOCAL:
1020 if (iface_param->iface_num & 0x1)
1021 break;
1022 memcpy(init_fw_cb->ipv6_if_id, &iface_param->value[8],
1023 sizeof(init_fw_cb->ipv6_if_id));
1024 break;
1025 case ISCSI_NET_PARAM_IPV6_ROUTER:
1026 if (iface_param->iface_num & 0x1)
1027 break;
1028 memcpy(init_fw_cb->ipv6_dflt_rtr_addr, iface_param->value,
1029 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1030 break;
1031 case ISCSI_NET_PARAM_IPV6_ADDR_AUTOCFG:
1032 /* Autocfg applies to even interface */
1033 if (iface_param->iface_num & 0x1)
1034 break;
1035
1036 if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_DISABLE)
1037 init_fw_cb->ipv6_addtl_opts &=
1038 cpu_to_le16(
1039 ~IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1040 else if (iface_param->value[0] == ISCSI_IPV6_AUTOCFG_ND_ENABLE)
1041 init_fw_cb->ipv6_addtl_opts |=
1042 cpu_to_le16(
1043 IPV6_ADDOPT_NEIGHBOR_DISCOVERY_ADDR_ENABLE);
1044 else
1045 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1046 "IPv6 addr\n");
1047 break;
1048 case ISCSI_NET_PARAM_IPV6_LINKLOCAL_AUTOCFG:
1049 /* Autocfg applies to even interface */
1050 if (iface_param->iface_num & 0x1)
1051 break;
1052
1053 if (iface_param->value[0] ==
1054 ISCSI_IPV6_LINKLOCAL_AUTOCFG_ENABLE)
1055 init_fw_cb->ipv6_addtl_opts |= cpu_to_le16(
1056 IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1057 else if (iface_param->value[0] ==
1058 ISCSI_IPV6_LINKLOCAL_AUTOCFG_DISABLE)
1059 init_fw_cb->ipv6_addtl_opts &= cpu_to_le16(
1060 ~IPV6_ADDOPT_AUTOCONFIG_LINK_LOCAL_ADDR);
1061 else
1062 ql4_printk(KERN_ERR, ha, "Invalid autocfg setting for "
1063 "IPv6 linklocal addr\n");
1064 break;
1065 case ISCSI_NET_PARAM_IPV6_ROUTER_AUTOCFG:
1066 /* Autocfg applies to even interface */
1067 if (iface_param->iface_num & 0x1)
1068 break;
1069
1070 if (iface_param->value[0] == ISCSI_IPV6_ROUTER_AUTOCFG_ENABLE)
1071 memset(init_fw_cb->ipv6_dflt_rtr_addr, 0,
1072 sizeof(init_fw_cb->ipv6_dflt_rtr_addr));
1073 break;
1074 case ISCSI_NET_PARAM_IFACE_ENABLE:
ed1086e0 1075 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
d00efe3f
MC
1076 init_fw_cb->ipv6_opts |=
1077 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
ed1086e0
VC
1078 qla4xxx_create_ipv6_iface(ha);
1079 } else {
d00efe3f
MC
1080 init_fw_cb->ipv6_opts &=
1081 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
1082 0xFFFF);
ed1086e0
VC
1083 qla4xxx_destroy_ipv6_iface(ha);
1084 }
d00efe3f 1085 break;
2d63673b 1086 case ISCSI_NET_PARAM_VLAN_TAG:
d00efe3f
MC
1087 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
1088 break;
6ac73e8c
VC
1089 init_fw_cb->ipv6_vlan_tag =
1090 cpu_to_be16(*(uint16_t *)iface_param->value);
1091 break;
1092 case ISCSI_NET_PARAM_VLAN_ENABLED:
1093 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1094 init_fw_cb->ipv6_opts |=
1095 cpu_to_le16(IPV6_OPT_VLAN_TAGGING_ENABLE);
1096 else
1097 init_fw_cb->ipv6_opts &=
1098 cpu_to_le16(~IPV6_OPT_VLAN_TAGGING_ENABLE);
d00efe3f 1099 break;
943c157b
VC
1100 case ISCSI_NET_PARAM_MTU:
1101 init_fw_cb->eth_mtu_size =
1102 cpu_to_le16(*(uint16_t *)iface_param->value);
1103 break;
2ada7fc5
VC
1104 case ISCSI_NET_PARAM_PORT:
1105 /* Autocfg applies to even interface */
1106 if (iface_param->iface_num & 0x1)
1107 break;
1108
1109 init_fw_cb->ipv6_port =
1110 cpu_to_le16(*(uint16_t *)iface_param->value);
1111 break;
d00efe3f
MC
1112 default:
1113 ql4_printk(KERN_ERR, ha, "Unknown IPv6 param = %d\n",
1114 iface_param->param);
1115 break;
1116 }
1117}
1118
1119static void qla4xxx_set_ipv4(struct scsi_qla_host *ha,
1120 struct iscsi_iface_param_info *iface_param,
1121 struct addr_ctrl_blk *init_fw_cb)
1122{
1123 switch (iface_param->param) {
1124 case ISCSI_NET_PARAM_IPV4_ADDR:
1125 memcpy(init_fw_cb->ipv4_addr, iface_param->value,
1126 sizeof(init_fw_cb->ipv4_addr));
1127 break;
1128 case ISCSI_NET_PARAM_IPV4_SUBNET:
1129 memcpy(init_fw_cb->ipv4_subnet, iface_param->value,
1130 sizeof(init_fw_cb->ipv4_subnet));
1131 break;
1132 case ISCSI_NET_PARAM_IPV4_GW:
1133 memcpy(init_fw_cb->ipv4_gw_addr, iface_param->value,
1134 sizeof(init_fw_cb->ipv4_gw_addr));
1135 break;
1136 case ISCSI_NET_PARAM_IPV4_BOOTPROTO:
1137 if (iface_param->value[0] == ISCSI_BOOTPROTO_DHCP)
1138 init_fw_cb->ipv4_tcp_opts |=
1139 cpu_to_le16(TCPOPT_DHCP_ENABLE);
1140 else if (iface_param->value[0] == ISCSI_BOOTPROTO_STATIC)
1141 init_fw_cb->ipv4_tcp_opts &=
1142 cpu_to_le16(~TCPOPT_DHCP_ENABLE);
1143 else
1144 ql4_printk(KERN_ERR, ha, "Invalid IPv4 bootproto\n");
1145 break;
1146 case ISCSI_NET_PARAM_IFACE_ENABLE:
ed1086e0 1147 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
d00efe3f 1148 init_fw_cb->ipv4_ip_opts |=
2bab08fc 1149 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
ed1086e0
VC
1150 qla4xxx_create_ipv4_iface(ha);
1151 } else {
d00efe3f 1152 init_fw_cb->ipv4_ip_opts &=
2bab08fc 1153 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
d00efe3f 1154 0xFFFF);
ed1086e0
VC
1155 qla4xxx_destroy_ipv4_iface(ha);
1156 }
d00efe3f 1157 break;
2d63673b 1158 case ISCSI_NET_PARAM_VLAN_TAG:
d00efe3f
MC
1159 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
1160 break;
6ac73e8c
VC
1161 init_fw_cb->ipv4_vlan_tag =
1162 cpu_to_be16(*(uint16_t *)iface_param->value);
1163 break;
1164 case ISCSI_NET_PARAM_VLAN_ENABLED:
1165 if (iface_param->value[0] == ISCSI_VLAN_ENABLE)
1166 init_fw_cb->ipv4_ip_opts |=
1167 cpu_to_le16(IPOPT_VLAN_TAGGING_ENABLE);
1168 else
1169 init_fw_cb->ipv4_ip_opts &=
1170 cpu_to_le16(~IPOPT_VLAN_TAGGING_ENABLE);
d00efe3f 1171 break;
943c157b
VC
1172 case ISCSI_NET_PARAM_MTU:
1173 init_fw_cb->eth_mtu_size =
1174 cpu_to_le16(*(uint16_t *)iface_param->value);
1175 break;
2ada7fc5
VC
1176 case ISCSI_NET_PARAM_PORT:
1177 init_fw_cb->ipv4_port =
1178 cpu_to_le16(*(uint16_t *)iface_param->value);
1179 break;
d00efe3f
MC
1180 default:
1181 ql4_printk(KERN_ERR, ha, "Unknown IPv4 param = %d\n",
1182 iface_param->param);
1183 break;
1184 }
1185}
1186
1187static void
1188qla4xxx_initcb_to_acb(struct addr_ctrl_blk *init_fw_cb)
1189{
1190 struct addr_ctrl_blk_def *acb;
1191 acb = (struct addr_ctrl_blk_def *)init_fw_cb;
1192 memset(acb->reserved1, 0, sizeof(acb->reserved1));
1193 memset(acb->reserved2, 0, sizeof(acb->reserved2));
1194 memset(acb->reserved3, 0, sizeof(acb->reserved3));
1195 memset(acb->reserved4, 0, sizeof(acb->reserved4));
1196 memset(acb->reserved5, 0, sizeof(acb->reserved5));
1197 memset(acb->reserved6, 0, sizeof(acb->reserved6));
1198 memset(acb->reserved7, 0, sizeof(acb->reserved7));
1199 memset(acb->reserved8, 0, sizeof(acb->reserved8));
1200 memset(acb->reserved9, 0, sizeof(acb->reserved9));
1201 memset(acb->reserved10, 0, sizeof(acb->reserved10));
1202 memset(acb->reserved11, 0, sizeof(acb->reserved11));
1203 memset(acb->reserved12, 0, sizeof(acb->reserved12));
1204 memset(acb->reserved13, 0, sizeof(acb->reserved13));
1205 memset(acb->reserved14, 0, sizeof(acb->reserved14));
1206 memset(acb->reserved15, 0, sizeof(acb->reserved15));
1207}
1208
1209static int
00c31889 1210qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
d00efe3f
MC
1211{
1212 struct scsi_qla_host *ha = to_qla_host(shost);
1213 int rval = 0;
1214 struct iscsi_iface_param_info *iface_param = NULL;
1215 struct addr_ctrl_blk *init_fw_cb = NULL;
1216 dma_addr_t init_fw_cb_dma;
1217 uint32_t mbox_cmd[MBOX_REG_COUNT];
1218 uint32_t mbox_sts[MBOX_REG_COUNT];
00c31889
MC
1219 uint32_t rem = len;
1220 struct nlattr *attr;
d00efe3f
MC
1221
1222 init_fw_cb = dma_alloc_coherent(&ha->pdev->dev,
1223 sizeof(struct addr_ctrl_blk),
1224 &init_fw_cb_dma, GFP_KERNEL);
1225 if (!init_fw_cb) {
1226 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc init_cb\n",
1227 __func__);
1228 return -ENOMEM;
1229 }
1230
1231 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1232 memset(&mbox_cmd, 0, sizeof(mbox_cmd));
1233 memset(&mbox_sts, 0, sizeof(mbox_sts));
1234
1235 if (qla4xxx_get_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma)) {
1236 ql4_printk(KERN_ERR, ha, "%s: get ifcb failed\n", __func__);
1237 rval = -EIO;
1238 goto exit_init_fw_cb;
1239 }
1240
00c31889
MC
1241 nla_for_each_attr(attr, data, len, rem) {
1242 iface_param = nla_data(attr);
d00efe3f
MC
1243
1244 if (iface_param->param_type != ISCSI_NET_PARAM)
1245 continue;
1246
1247 switch (iface_param->iface_type) {
1248 case ISCSI_IFACE_TYPE_IPV4:
1249 switch (iface_param->iface_num) {
1250 case 0:
1251 qla4xxx_set_ipv4(ha, iface_param, init_fw_cb);
1252 break;
1253 default:
1254 /* Cannot have more than one IPv4 interface */
1255 ql4_printk(KERN_ERR, ha, "Invalid IPv4 iface "
1256 "number = %d\n",
1257 iface_param->iface_num);
1258 break;
1259 }
1260 break;
1261 case ISCSI_IFACE_TYPE_IPV6:
1262 switch (iface_param->iface_num) {
1263 case 0:
1264 case 1:
1265 qla4xxx_set_ipv6(ha, iface_param, init_fw_cb);
1266 break;
1267 default:
1268 /* Cannot have more than two IPv6 interface */
1269 ql4_printk(KERN_ERR, ha, "Invalid IPv6 iface "
1270 "number = %d\n",
1271 iface_param->iface_num);
1272 break;
1273 }
1274 break;
1275 default:
1276 ql4_printk(KERN_ERR, ha, "Invalid iface type\n");
1277 break;
1278 }
d00efe3f
MC
1279 }
1280
1281 init_fw_cb->cookie = cpu_to_le32(0x11BEAD5A);
1282
1283 rval = qla4xxx_set_flash(ha, init_fw_cb_dma, FLASH_SEGMENT_IFCB,
1284 sizeof(struct addr_ctrl_blk),
1285 FLASH_OPT_RMW_COMMIT);
1286 if (rval != QLA_SUCCESS) {
1287 ql4_printk(KERN_ERR, ha, "%s: set flash mbx failed\n",
1288 __func__);
1289 rval = -EIO;
1290 goto exit_init_fw_cb;
1291 }
1292
ce505f9d
VC
1293 rval = qla4xxx_disable_acb(ha);
1294 if (rval != QLA_SUCCESS) {
1295 ql4_printk(KERN_ERR, ha, "%s: disable acb mbx failed\n",
1296 __func__);
1297 rval = -EIO;
1298 goto exit_init_fw_cb;
1299 }
1300
1301 wait_for_completion_timeout(&ha->disable_acb_comp,
1302 DISABLE_ACB_TOV * HZ);
d00efe3f
MC
1303
1304 qla4xxx_initcb_to_acb(init_fw_cb);
1305
1306 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb_dma);
1307 if (rval != QLA_SUCCESS) {
1308 ql4_printk(KERN_ERR, ha, "%s: set acb mbx failed\n",
1309 __func__);
1310 rval = -EIO;
1311 goto exit_init_fw_cb;
1312 }
1313
1314 memset(init_fw_cb, 0, sizeof(struct addr_ctrl_blk));
1315 qla4xxx_update_local_ifcb(ha, &mbox_cmd[0], &mbox_sts[0], init_fw_cb,
1316 init_fw_cb_dma);
1317
1318exit_init_fw_cb:
1319 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk),
1320 init_fw_cb, init_fw_cb_dma);
1321
1322 return rval;
1323}
1324
fca9f04d
MC
1325static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
1326 enum iscsi_param param, char *buf)
1327{
1328 struct iscsi_session *sess = cls_sess->dd_data;
1329 struct ddb_entry *ddb_entry = sess->dd_data;
1330 struct scsi_qla_host *ha = ddb_entry->ha;
1331 int rval, len;
1332 uint16_t idx;
1333
1334 switch (param) {
1335 case ISCSI_PARAM_CHAP_IN_IDX:
1336 rval = qla4xxx_get_chap_index(ha, sess->username_in,
1337 sess->password_in, BIDI_CHAP,
1338 &idx);
1339 if (rval)
1340 return -EINVAL;
1341
1342 len = sprintf(buf, "%hu\n", idx);
1343 break;
1344 case ISCSI_PARAM_CHAP_OUT_IDX:
1345 rval = qla4xxx_get_chap_index(ha, sess->username,
1346 sess->password, LOCAL_CHAP,
1347 &idx);
1348 if (rval)
1349 return -EINVAL;
1350
1351 len = sprintf(buf, "%hu\n", idx);
1352 break;
1353 default:
1354 return iscsi_session_get_param(cls_sess, param, buf);
1355 }
1356
1357 return len;
1358}
1359
b3a271a9 1360static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
afaf5a2d
DS
1361 enum iscsi_param param, char *buf)
1362{
b3a271a9
MR
1363 struct iscsi_conn *conn;
1364 struct qla_conn *qla_conn;
1365 struct sockaddr *dst_addr;
1366 int len = 0;
afaf5a2d 1367
b3a271a9
MR
1368 conn = cls_conn->dd_data;
1369 qla_conn = conn->dd_data;
d46bdeb1 1370 dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
afaf5a2d
DS
1371
1372 switch (param) {
1373 case ISCSI_PARAM_CONN_PORT:
afaf5a2d 1374 case ISCSI_PARAM_CONN_ADDRESS:
b3a271a9
MR
1375 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1376 dst_addr, param, buf);
afaf5a2d 1377 default:
b3a271a9 1378 return iscsi_conn_get_param(cls_conn, param, buf);
afaf5a2d
DS
1379 }
1380
1381 return len;
b3a271a9 1382
afaf5a2d
DS
1383}
1384
13483730
MC
1385int qla4xxx_get_ddb_index(struct scsi_qla_host *ha, uint16_t *ddb_index)
1386{
1387 uint32_t mbx_sts = 0;
1388 uint16_t tmp_ddb_index;
1389 int ret;
1390
1391get_ddb_index:
1392 tmp_ddb_index = find_first_zero_bit(ha->ddb_idx_map, MAX_DDB_ENTRIES);
1393
1394 if (tmp_ddb_index >= MAX_DDB_ENTRIES) {
1395 DEBUG2(ql4_printk(KERN_INFO, ha,
1396 "Free DDB index not available\n"));
1397 ret = QLA_ERROR;
1398 goto exit_get_ddb_index;
1399 }
1400
1401 if (test_and_set_bit(tmp_ddb_index, ha->ddb_idx_map))
1402 goto get_ddb_index;
1403
1404 DEBUG2(ql4_printk(KERN_INFO, ha,
1405 "Found a free DDB index at %d\n", tmp_ddb_index));
1406 ret = qla4xxx_req_ddb_entry(ha, tmp_ddb_index, &mbx_sts);
1407 if (ret == QLA_ERROR) {
1408 if (mbx_sts == MBOX_STS_COMMAND_ERROR) {
1409 ql4_printk(KERN_INFO, ha,
1410 "DDB index = %d not available trying next\n",
1411 tmp_ddb_index);
1412 goto get_ddb_index;
1413 }
1414 DEBUG2(ql4_printk(KERN_INFO, ha,
1415 "Free FW DDB not available\n"));
1416 }
1417
1418 *ddb_index = tmp_ddb_index;
1419
1420exit_get_ddb_index:
1421 return ret;
1422}
1423
1424static int qla4xxx_match_ipaddress(struct scsi_qla_host *ha,
1425 struct ddb_entry *ddb_entry,
1426 char *existing_ipaddr,
1427 char *user_ipaddr)
1428{
1429 uint8_t dst_ipaddr[IPv6_ADDR_LEN];
1430 char formatted_ipaddr[DDB_IPADDR_LEN];
1431 int status = QLA_SUCCESS, ret = 0;
1432
1433 if (ddb_entry->fw_ddb_entry.options & DDB_OPT_IPV6_DEVICE) {
1434 ret = in6_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1435 '\0', NULL);
1436 if (ret == 0) {
1437 status = QLA_ERROR;
1438 goto out_match;
1439 }
1440 ret = sprintf(formatted_ipaddr, "%pI6", dst_ipaddr);
1441 } else {
1442 ret = in4_pton(user_ipaddr, strlen(user_ipaddr), dst_ipaddr,
1443 '\0', NULL);
1444 if (ret == 0) {
1445 status = QLA_ERROR;
1446 goto out_match;
1447 }
1448 ret = sprintf(formatted_ipaddr, "%pI4", dst_ipaddr);
1449 }
1450
1451 if (strcmp(existing_ipaddr, formatted_ipaddr))
1452 status = QLA_ERROR;
1453
1454out_match:
1455 return status;
1456}
1457
1458static int qla4xxx_match_fwdb_session(struct scsi_qla_host *ha,
1459 struct iscsi_cls_conn *cls_conn)
1460{
1461 int idx = 0, max_ddbs, rval;
1462 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1463 struct iscsi_session *sess, *existing_sess;
1464 struct iscsi_conn *conn, *existing_conn;
1465 struct ddb_entry *ddb_entry;
1466
1467 sess = cls_sess->dd_data;
1468 conn = cls_conn->dd_data;
1469
1470 if (sess->targetname == NULL ||
1471 conn->persistent_address == NULL ||
1472 conn->persistent_port == 0)
1473 return QLA_ERROR;
1474
1475 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
1476 MAX_DEV_DB_ENTRIES;
1477
1478 for (idx = 0; idx < max_ddbs; idx++) {
1479 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
1480 if (ddb_entry == NULL)
1481 continue;
1482
1483 if (ddb_entry->ddb_type != FLASH_DDB)
1484 continue;
1485
1486 existing_sess = ddb_entry->sess->dd_data;
1487 existing_conn = ddb_entry->conn->dd_data;
1488
1489 if (existing_sess->targetname == NULL ||
1490 existing_conn->persistent_address == NULL ||
1491 existing_conn->persistent_port == 0)
1492 continue;
1493
1494 DEBUG2(ql4_printk(KERN_INFO, ha,
1495 "IQN = %s User IQN = %s\n",
1496 existing_sess->targetname,
1497 sess->targetname));
1498
1499 DEBUG2(ql4_printk(KERN_INFO, ha,
1500 "IP = %s User IP = %s\n",
1501 existing_conn->persistent_address,
1502 conn->persistent_address));
1503
1504 DEBUG2(ql4_printk(KERN_INFO, ha,
1505 "Port = %d User Port = %d\n",
1506 existing_conn->persistent_port,
1507 conn->persistent_port));
1508
1509 if (strcmp(existing_sess->targetname, sess->targetname))
1510 continue;
1511 rval = qla4xxx_match_ipaddress(ha, ddb_entry,
1512 existing_conn->persistent_address,
1513 conn->persistent_address);
1514 if (rval == QLA_ERROR)
1515 continue;
1516 if (existing_conn->persistent_port != conn->persistent_port)
1517 continue;
1518 break;
1519 }
1520
1521 if (idx == max_ddbs)
1522 return QLA_ERROR;
1523
1524 DEBUG2(ql4_printk(KERN_INFO, ha,
1525 "Match found in fwdb sessions\n"));
1526 return QLA_SUCCESS;
1527}
1528
b3a271a9
MR
1529static struct iscsi_cls_session *
1530qla4xxx_session_create(struct iscsi_endpoint *ep,
1531 uint16_t cmds_max, uint16_t qdepth,
1532 uint32_t initial_cmdsn)
1533{
1534 struct iscsi_cls_session *cls_sess;
1535 struct scsi_qla_host *ha;
1536 struct qla_endpoint *qla_ep;
1537 struct ddb_entry *ddb_entry;
13483730 1538 uint16_t ddb_index;
b3a271a9
MR
1539 struct iscsi_session *sess;
1540 struct sockaddr *dst_addr;
1541 int ret;
1542
1543 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1544 if (!ep) {
1545 printk(KERN_ERR "qla4xxx: missing ep.\n");
1546 return NULL;
1547 }
1548
1549 qla_ep = ep->dd_data;
1550 dst_addr = (struct sockaddr *)&qla_ep->dst_addr;
1551 ha = to_qla_host(qla_ep->host);
736cf369 1552
13483730
MC
1553 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1554 if (ret == QLA_ERROR)
b3a271a9 1555 return NULL;
b3a271a9
MR
1556
1557 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, qla_ep->host,
1558 cmds_max, sizeof(struct ddb_entry),
1559 sizeof(struct ql4_task_data),
1560 initial_cmdsn, ddb_index);
1561 if (!cls_sess)
1562 return NULL;
1563
1564 sess = cls_sess->dd_data;
1565 ddb_entry = sess->dd_data;
1566 ddb_entry->fw_ddb_index = ddb_index;
1567 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
1568 ddb_entry->ha = ha;
1569 ddb_entry->sess = cls_sess;
13483730
MC
1570 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1571 ddb_entry->ddb_change = qla4xxx_ddb_change;
b3a271a9
MR
1572 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
1573 ha->fw_ddb_index_map[ddb_entry->fw_ddb_index] = ddb_entry;
1574 ha->tot_ddbs++;
1575
1576 return cls_sess;
1577}
1578
1579static void qla4xxx_session_destroy(struct iscsi_cls_session *cls_sess)
1580{
1581 struct iscsi_session *sess;
1582 struct ddb_entry *ddb_entry;
1583 struct scsi_qla_host *ha;
90599b62
MR
1584 unsigned long flags, wtime;
1585 struct dev_db_entry *fw_ddb_entry = NULL;
1586 dma_addr_t fw_ddb_entry_dma;
1587 uint32_t ddb_state;
1588 int ret;
b3a271a9
MR
1589
1590 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1591 sess = cls_sess->dd_data;
1592 ddb_entry = sess->dd_data;
1593 ha = ddb_entry->ha;
1594
90599b62
MR
1595 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1596 &fw_ddb_entry_dma, GFP_KERNEL);
1597 if (!fw_ddb_entry) {
1598 ql4_printk(KERN_ERR, ha,
1599 "%s: Unable to allocate dma buffer\n", __func__);
1600 goto destroy_session;
1601 }
1602
1603 wtime = jiffies + (HZ * LOGOUT_TOV);
1604 do {
1605 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
1606 fw_ddb_entry, fw_ddb_entry_dma,
1607 NULL, NULL, &ddb_state, NULL,
1608 NULL, NULL);
1609 if (ret == QLA_ERROR)
1610 goto destroy_session;
1611
1612 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
1613 (ddb_state == DDB_DS_SESSION_FAILED))
1614 goto destroy_session;
1615
1616 schedule_timeout_uninterruptible(HZ);
1617 } while ((time_after(wtime, jiffies)));
1618
1619destroy_session:
736cf369
MR
1620 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1621
b3a271a9
MR
1622 spin_lock_irqsave(&ha->hardware_lock, flags);
1623 qla4xxx_free_ddb(ha, ddb_entry);
1624 spin_unlock_irqrestore(&ha->hardware_lock, flags);
90599b62 1625
b3a271a9 1626 iscsi_session_teardown(cls_sess);
90599b62
MR
1627
1628 if (fw_ddb_entry)
1629 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1630 fw_ddb_entry, fw_ddb_entry_dma);
b3a271a9
MR
1631}
1632
b3a271a9
MR
1633static struct iscsi_cls_conn *
1634qla4xxx_conn_create(struct iscsi_cls_session *cls_sess, uint32_t conn_idx)
1635{
1636 struct iscsi_cls_conn *cls_conn;
1637 struct iscsi_session *sess;
1638 struct ddb_entry *ddb_entry;
1639
1640 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1641 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn),
1642 conn_idx);
ff1d0319
MC
1643 if (!cls_conn)
1644 return NULL;
1645
b3a271a9
MR
1646 sess = cls_sess->dd_data;
1647 ddb_entry = sess->dd_data;
1648 ddb_entry->conn = cls_conn;
1649
1650 return cls_conn;
1651}
1652
1653static int qla4xxx_conn_bind(struct iscsi_cls_session *cls_session,
1654 struct iscsi_cls_conn *cls_conn,
1655 uint64_t transport_fd, int is_leading)
1656{
1657 struct iscsi_conn *conn;
1658 struct qla_conn *qla_conn;
1659 struct iscsi_endpoint *ep;
1660
1661 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1662
1663 if (iscsi_conn_bind(cls_session, cls_conn, is_leading))
1664 return -EINVAL;
1665 ep = iscsi_lookup_endpoint(transport_fd);
1666 conn = cls_conn->dd_data;
1667 qla_conn = conn->dd_data;
1668 qla_conn->qla_ep = ep->dd_data;
1669 return 0;
1670}
1671
1672static int qla4xxx_conn_start(struct iscsi_cls_conn *cls_conn)
1673{
1674 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1675 struct iscsi_session *sess;
1676 struct ddb_entry *ddb_entry;
1677 struct scsi_qla_host *ha;
13483730 1678 struct dev_db_entry *fw_ddb_entry = NULL;
b3a271a9 1679 dma_addr_t fw_ddb_entry_dma;
b3a271a9
MR
1680 uint32_t mbx_sts = 0;
1681 int ret = 0;
1682 int status = QLA_SUCCESS;
1683
1684 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1685 sess = cls_sess->dd_data;
1686 ddb_entry = sess->dd_data;
1687 ha = ddb_entry->ha;
1688
13483730
MC
1689 /* Check if we have matching FW DDB, if yes then do not
1690 * login to this target. This could cause target to logout previous
1691 * connection
1692 */
1693 ret = qla4xxx_match_fwdb_session(ha, cls_conn);
1694 if (ret == QLA_SUCCESS) {
1695 ql4_printk(KERN_INFO, ha,
1696 "Session already exist in FW.\n");
1697 ret = -EEXIST;
1698 goto exit_conn_start;
1699 }
1700
b3a271a9
MR
1701 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1702 &fw_ddb_entry_dma, GFP_KERNEL);
1703 if (!fw_ddb_entry) {
1704 ql4_printk(KERN_ERR, ha,
1705 "%s: Unable to allocate dma buffer\n", __func__);
13483730
MC
1706 ret = -ENOMEM;
1707 goto exit_conn_start;
b3a271a9
MR
1708 }
1709
1710 ret = qla4xxx_set_param_ddbentry(ha, ddb_entry, cls_conn, &mbx_sts);
1711 if (ret) {
1712 /* If iscsid is stopped and started then no need to do
1713 * set param again since ddb state will be already
1714 * active and FW does not allow set ddb to an
1715 * active session.
1716 */
1717 if (mbx_sts)
1718 if (ddb_entry->fw_ddb_device_state ==
f922da79 1719 DDB_DS_SESSION_ACTIVE) {
13483730 1720 ddb_entry->unblock_sess(ddb_entry->sess);
b3a271a9 1721 goto exit_set_param;
f922da79 1722 }
b3a271a9
MR
1723
1724 ql4_printk(KERN_ERR, ha, "%s: Failed set param for index[%d]\n",
1725 __func__, ddb_entry->fw_ddb_index);
1726 goto exit_conn_start;
1727 }
1728
1729 status = qla4xxx_conn_open(ha, ddb_entry->fw_ddb_index);
1730 if (status == QLA_ERROR) {
0e7e8501
MR
1731 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1732 sess->targetname);
b3a271a9
MR
1733 ret = -EINVAL;
1734 goto exit_conn_start;
1735 }
1736
98270ab4
MR
1737 if (ddb_entry->fw_ddb_device_state == DDB_DS_NO_CONNECTION_ACTIVE)
1738 ddb_entry->fw_ddb_device_state = DDB_DS_LOGIN_IN_PROCESS;
1739
1740 DEBUG2(printk(KERN_INFO "%s: DDB state [%d]\n", __func__,
1741 ddb_entry->fw_ddb_device_state));
b3a271a9
MR
1742
1743exit_set_param:
b3a271a9
MR
1744 ret = 0;
1745
1746exit_conn_start:
13483730
MC
1747 if (fw_ddb_entry)
1748 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1749 fw_ddb_entry, fw_ddb_entry_dma);
b3a271a9
MR
1750 return ret;
1751}
1752
1753static void qla4xxx_conn_destroy(struct iscsi_cls_conn *cls_conn)
1754{
1755 struct iscsi_cls_session *cls_sess = iscsi_conn_to_session(cls_conn);
1756 struct iscsi_session *sess;
1757 struct scsi_qla_host *ha;
1758 struct ddb_entry *ddb_entry;
1759 int options;
1760
1761 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
1762 sess = cls_sess->dd_data;
1763 ddb_entry = sess->dd_data;
1764 ha = ddb_entry->ha;
1765
1766 options = LOGOUT_OPTION_CLOSE_SESSION;
1767 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options) == QLA_ERROR)
1768 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n", __func__);
b3a271a9
MR
1769}
1770
1771static void qla4xxx_task_work(struct work_struct *wdata)
1772{
1773 struct ql4_task_data *task_data;
1774 struct scsi_qla_host *ha;
1775 struct passthru_status *sts;
1776 struct iscsi_task *task;
1777 struct iscsi_hdr *hdr;
1778 uint8_t *data;
1779 uint32_t data_len;
1780 struct iscsi_conn *conn;
1781 int hdr_len;
1782 itt_t itt;
1783
1784 task_data = container_of(wdata, struct ql4_task_data, task_work);
1785 ha = task_data->ha;
1786 task = task_data->task;
1787 sts = &task_data->sts;
1788 hdr_len = sizeof(struct iscsi_hdr);
1789
1790 DEBUG3(printk(KERN_INFO "Status returned\n"));
1791 DEBUG3(qla4xxx_dump_buffer(sts, 64));
1792 DEBUG3(printk(KERN_INFO "Response buffer"));
1793 DEBUG3(qla4xxx_dump_buffer(task_data->resp_buffer, 64));
1794
1795 conn = task->conn;
1796
1797 switch (sts->completionStatus) {
1798 case PASSTHRU_STATUS_COMPLETE:
1799 hdr = (struct iscsi_hdr *)task_data->resp_buffer;
1800 /* Assign back the itt in hdr, until we use the PREASSIGN_TAG */
1801 itt = sts->handle;
1802 hdr->itt = itt;
1803 data = task_data->resp_buffer + hdr_len;
1804 data_len = task_data->resp_len - hdr_len;
1805 iscsi_complete_pdu(conn, hdr, data, data_len);
1806 break;
1807 default:
1808 ql4_printk(KERN_ERR, ha, "Passthru failed status = 0x%x\n",
1809 sts->completionStatus);
1810 break;
1811 }
1812 return;
1813}
1814
1815static int qla4xxx_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
1816{
1817 struct ql4_task_data *task_data;
1818 struct iscsi_session *sess;
1819 struct ddb_entry *ddb_entry;
1820 struct scsi_qla_host *ha;
1821 int hdr_len;
1822
1823 sess = task->conn->session;
1824 ddb_entry = sess->dd_data;
1825 ha = ddb_entry->ha;
1826 task_data = task->dd_data;
1827 memset(task_data, 0, sizeof(struct ql4_task_data));
1828
1829 if (task->sc) {
1830 ql4_printk(KERN_INFO, ha,
1831 "%s: SCSI Commands not implemented\n", __func__);
1832 return -EINVAL;
1833 }
1834
1835 hdr_len = sizeof(struct iscsi_hdr);
1836 task_data->ha = ha;
1837 task_data->task = task;
1838
1839 if (task->data_count) {
1840 task_data->data_dma = dma_map_single(&ha->pdev->dev, task->data,
1841 task->data_count,
1842 PCI_DMA_TODEVICE);
1843 }
1844
1845 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1846 __func__, task->conn->max_recv_dlength, hdr_len));
1847
69ca216e 1848 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
b3a271a9
MR
1849 task_data->resp_buffer = dma_alloc_coherent(&ha->pdev->dev,
1850 task_data->resp_len,
1851 &task_data->resp_dma,
1852 GFP_ATOMIC);
1853 if (!task_data->resp_buffer)
1854 goto exit_alloc_pdu;
1855
69ca216e 1856 task_data->req_len = task->data_count + hdr_len;
b3a271a9 1857 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
69ca216e 1858 task_data->req_len,
b3a271a9
MR
1859 &task_data->req_dma,
1860 GFP_ATOMIC);
1861 if (!task_data->req_buffer)
1862 goto exit_alloc_pdu;
1863
1864 task->hdr = task_data->req_buffer;
1865
1866 INIT_WORK(&task_data->task_work, qla4xxx_task_work);
1867
1868 return 0;
1869
1870exit_alloc_pdu:
1871 if (task_data->resp_buffer)
1872 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1873 task_data->resp_buffer, task_data->resp_dma);
1874
1875 if (task_data->req_buffer)
69ca216e 1876 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
b3a271a9
MR
1877 task_data->req_buffer, task_data->req_dma);
1878 return -ENOMEM;
1879}
1880
1881static void qla4xxx_task_cleanup(struct iscsi_task *task)
1882{
1883 struct ql4_task_data *task_data;
1884 struct iscsi_session *sess;
1885 struct ddb_entry *ddb_entry;
1886 struct scsi_qla_host *ha;
1887 int hdr_len;
1888
1889 hdr_len = sizeof(struct iscsi_hdr);
1890 sess = task->conn->session;
1891 ddb_entry = sess->dd_data;
1892 ha = ddb_entry->ha;
1893 task_data = task->dd_data;
1894
1895 if (task->data_count) {
1896 dma_unmap_single(&ha->pdev->dev, task_data->data_dma,
1897 task->data_count, PCI_DMA_TODEVICE);
1898 }
1899
1900 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: MaxRecvLen %u, iscsi hrd %d\n",
1901 __func__, task->conn->max_recv_dlength, hdr_len));
1902
1903 dma_free_coherent(&ha->pdev->dev, task_data->resp_len,
1904 task_data->resp_buffer, task_data->resp_dma);
69ca216e 1905 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
b3a271a9
MR
1906 task_data->req_buffer, task_data->req_dma);
1907 return;
1908}
1909
1910static int qla4xxx_task_xmit(struct iscsi_task *task)
1911{
1912 struct scsi_cmnd *sc = task->sc;
1913 struct iscsi_session *sess = task->conn->session;
1914 struct ddb_entry *ddb_entry = sess->dd_data;
1915 struct scsi_qla_host *ha = ddb_entry->ha;
1916
1917 if (!sc)
1918 return qla4xxx_send_passthru0(task);
1919
1920 ql4_printk(KERN_INFO, ha, "%s: scsi cmd xmit not implemented\n",
1921 __func__);
1922 return -ENOSYS;
1923}
1924
13483730
MC
1925static void qla4xxx_copy_fwddb_param(struct scsi_qla_host *ha,
1926 struct dev_db_entry *fw_ddb_entry,
1927 struct iscsi_cls_session *cls_sess,
1928 struct iscsi_cls_conn *cls_conn)
1929{
1930 int buflen = 0;
1931 struct iscsi_session *sess;
376738af 1932 struct ddb_entry *ddb_entry;
13483730
MC
1933 struct iscsi_conn *conn;
1934 char ip_addr[DDB_IPADDR_LEN];
1935 uint16_t options = 0;
1936
1937 sess = cls_sess->dd_data;
376738af 1938 ddb_entry = sess->dd_data;
13483730
MC
1939 conn = cls_conn->dd_data;
1940
376738af
NJ
1941 ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
1942
13483730
MC
1943 conn->max_recv_dlength = BYTE_UNITS *
1944 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
1945
1946 conn->max_xmit_dlength = BYTE_UNITS *
1947 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
1948
1949 sess->initial_r2t_en =
1950 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1951
1952 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
1953
1954 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
1955
1956 sess->first_burst = BYTE_UNITS *
1957 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
1958
1959 sess->max_burst = BYTE_UNITS *
1960 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
1961
1962 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
1963
1964 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
1965
1966 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
1967
1968 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
1969
1970 options = le16_to_cpu(fw_ddb_entry->options);
1971 if (options & DDB_OPT_IPV6_DEVICE)
1972 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
1973 else
1974 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
1975
1976 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_NAME,
1977 (char *)fw_ddb_entry->iscsi_name, buflen);
1978 iscsi_set_param(cls_conn, ISCSI_PARAM_INITIATOR_NAME,
1979 (char *)ha->name_string, buflen);
1980 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
1981 (char *)ip_addr, buflen);
6c1b8789
VC
1982 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
1983 (char *)fw_ddb_entry->iscsi_alias, buflen);
13483730
MC
1984}
1985
1986void qla4xxx_update_session_conn_fwddb_param(struct scsi_qla_host *ha,
1987 struct ddb_entry *ddb_entry)
1988{
1989 struct iscsi_cls_session *cls_sess;
1990 struct iscsi_cls_conn *cls_conn;
1991 uint32_t ddb_state;
1992 dma_addr_t fw_ddb_entry_dma;
1993 struct dev_db_entry *fw_ddb_entry;
1994
1995 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1996 &fw_ddb_entry_dma, GFP_KERNEL);
1997 if (!fw_ddb_entry) {
1998 ql4_printk(KERN_ERR, ha,
1999 "%s: Unable to allocate dma buffer\n", __func__);
2000 goto exit_session_conn_fwddb_param;
2001 }
2002
2003 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
2004 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
2005 NULL, NULL, NULL) == QLA_ERROR) {
2006 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
2007 "get_ddb_entry for fw_ddb_index %d\n",
2008 ha->host_no, __func__,
2009 ddb_entry->fw_ddb_index));
2010 goto exit_session_conn_fwddb_param;
2011 }
2012
2013 cls_sess = ddb_entry->sess;
2014
2015 cls_conn = ddb_entry->conn;
2016
2017 /* Update params */
2018 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
2019
2020exit_session_conn_fwddb_param:
2021 if (fw_ddb_entry)
2022 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2023 fw_ddb_entry, fw_ddb_entry_dma);
2024}
2025
b3a271a9
MR
2026void qla4xxx_update_session_conn_param(struct scsi_qla_host *ha,
2027 struct ddb_entry *ddb_entry)
2028{
2029 struct iscsi_cls_session *cls_sess;
2030 struct iscsi_cls_conn *cls_conn;
2031 struct iscsi_session *sess;
2032 struct iscsi_conn *conn;
2033 uint32_t ddb_state;
2034 dma_addr_t fw_ddb_entry_dma;
2035 struct dev_db_entry *fw_ddb_entry;
2036
2037 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2038 &fw_ddb_entry_dma, GFP_KERNEL);
2039 if (!fw_ddb_entry) {
2040 ql4_printk(KERN_ERR, ha,
2041 "%s: Unable to allocate dma buffer\n", __func__);
13483730 2042 goto exit_session_conn_param;
b3a271a9
MR
2043 }
2044
2045 if (qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index, fw_ddb_entry,
2046 fw_ddb_entry_dma, NULL, NULL, &ddb_state,
2047 NULL, NULL, NULL) == QLA_ERROR) {
2048 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: failed "
2049 "get_ddb_entry for fw_ddb_index %d\n",
2050 ha->host_no, __func__,
2051 ddb_entry->fw_ddb_index));
13483730 2052 goto exit_session_conn_param;
b3a271a9
MR
2053 }
2054
2055 cls_sess = ddb_entry->sess;
2056 sess = cls_sess->dd_data;
2057
2058 cls_conn = ddb_entry->conn;
2059 conn = cls_conn->dd_data;
2060
13483730
MC
2061 /* Update timers after login */
2062 ddb_entry->default_relogin_timeout =
c28eaaca
NJ
2063 (le16_to_cpu(fw_ddb_entry->def_timeout) > LOGIN_TOV) &&
2064 (le16_to_cpu(fw_ddb_entry->def_timeout) < LOGIN_TOV * 10) ?
2065 le16_to_cpu(fw_ddb_entry->def_timeout) : LOGIN_TOV;
13483730
MC
2066 ddb_entry->default_time2wait =
2067 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2068
b3a271a9 2069 /* Update params */
376738af 2070 ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
b3a271a9
MR
2071 conn->max_recv_dlength = BYTE_UNITS *
2072 le16_to_cpu(fw_ddb_entry->iscsi_max_rcv_data_seg_len);
2073
2074 conn->max_xmit_dlength = BYTE_UNITS *
2075 le16_to_cpu(fw_ddb_entry->iscsi_max_snd_data_seg_len);
2076
2077 sess->initial_r2t_en =
2078 (BIT_10 & le16_to_cpu(fw_ddb_entry->iscsi_options));
2079
2080 sess->max_r2t = le16_to_cpu(fw_ddb_entry->iscsi_max_outsnd_r2t);
2081
2082 sess->imm_data_en = (BIT_11 & le16_to_cpu(fw_ddb_entry->iscsi_options));
2083
2084 sess->first_burst = BYTE_UNITS *
2085 le16_to_cpu(fw_ddb_entry->iscsi_first_burst_len);
2086
2087 sess->max_burst = BYTE_UNITS *
2088 le16_to_cpu(fw_ddb_entry->iscsi_max_burst_len);
2089
2090 sess->time2wait = le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2091
2092 sess->time2retain = le16_to_cpu(fw_ddb_entry->iscsi_def_time2retain);
2093
2094 sess->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
2095
2096 memcpy(sess->initiatorname, ha->name_string,
2097 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
13483730 2098
6c1b8789
VC
2099 iscsi_set_param(cls_conn, ISCSI_PARAM_TARGET_ALIAS,
2100 (char *)fw_ddb_entry->iscsi_alias, 0);
2101
13483730
MC
2102exit_session_conn_param:
2103 if (fw_ddb_entry)
2104 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
2105 fw_ddb_entry, fw_ddb_entry_dma);
b3a271a9
MR
2106}
2107
afaf5a2d
DS
2108/*
2109 * Timer routines
2110 */
2111
2112static void qla4xxx_start_timer(struct scsi_qla_host *ha, void *func,
2113 unsigned long interval)
2114{
2115 DEBUG(printk("scsi: %s: Starting timer thread for adapter %d\n",
2116 __func__, ha->host->host_no));
2117 init_timer(&ha->timer);
2118 ha->timer.expires = jiffies + interval * HZ;
2119 ha->timer.data = (unsigned long)ha;
2120 ha->timer.function = (void (*)(unsigned long))func;
2121 add_timer(&ha->timer);
2122 ha->timer_active = 1;
2123}
2124
2125static void qla4xxx_stop_timer(struct scsi_qla_host *ha)
2126{
2127 del_timer_sync(&ha->timer);
2128 ha->timer_active = 0;
2129}
2130
2131/***
b3a271a9
MR
2132 * qla4xxx_mark_device_missing - blocks the session
2133 * @cls_session: Pointer to the session to be blocked
afaf5a2d
DS
2134 * @ddb_entry: Pointer to device database entry
2135 *
f4f5df23 2136 * This routine marks a device missing and close connection.
afaf5a2d 2137 **/
b3a271a9 2138void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
afaf5a2d 2139{
b3a271a9 2140 iscsi_block_session(cls_session);
afaf5a2d
DS
2141}
2142
f4f5df23
VC
2143/**
2144 * qla4xxx_mark_all_devices_missing - mark all devices as missing.
2145 * @ha: Pointer to host adapter structure.
2146 *
2147 * This routine marks a device missing and resets the relogin retry count.
2148 **/
2149void qla4xxx_mark_all_devices_missing(struct scsi_qla_host *ha)
2150{
b3a271a9 2151 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
f4f5df23
VC
2152}
2153
afaf5a2d
DS
2154static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
2155 struct ddb_entry *ddb_entry,
8f0722ca 2156 struct scsi_cmnd *cmd)
afaf5a2d
DS
2157{
2158 struct srb *srb;
2159
2160 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2161 if (!srb)
2162 return srb;
2163
09a0f719 2164 kref_init(&srb->srb_ref);
afaf5a2d
DS
2165 srb->ha = ha;
2166 srb->ddb = ddb_entry;
2167 srb->cmd = cmd;
2168 srb->flags = 0;
5369887a 2169 CMD_SP(cmd) = (void *)srb;
afaf5a2d
DS
2170
2171 return srb;
2172}
2173
2174static void qla4xxx_srb_free_dma(struct scsi_qla_host *ha, struct srb *srb)
2175{
2176 struct scsi_cmnd *cmd = srb->cmd;
2177
2178 if (srb->flags & SRB_DMA_VALID) {
5f7186c8 2179 scsi_dma_unmap(cmd);
afaf5a2d
DS
2180 srb->flags &= ~SRB_DMA_VALID;
2181 }
5369887a 2182 CMD_SP(cmd) = NULL;
afaf5a2d
DS
2183}
2184
09a0f719 2185void qla4xxx_srb_compl(struct kref *ref)
afaf5a2d 2186{
09a0f719 2187 struct srb *srb = container_of(ref, struct srb, srb_ref);
afaf5a2d 2188 struct scsi_cmnd *cmd = srb->cmd;
09a0f719 2189 struct scsi_qla_host *ha = srb->ha;
afaf5a2d
DS
2190
2191 qla4xxx_srb_free_dma(ha, srb);
2192
2193 mempool_free(srb, ha->srb_mempool);
2194
2195 cmd->scsi_done(cmd);
2196}
2197
2198/**
2199 * qla4xxx_queuecommand - scsi layer issues scsi command to driver.
8f0722ca 2200 * @host: scsi host
afaf5a2d 2201 * @cmd: Pointer to Linux's SCSI command structure
afaf5a2d
DS
2202 *
2203 * Remarks:
2204 * This routine is invoked by Linux to send a SCSI command to the driver.
2205 * The mid-level driver tries to ensure that queuecommand never gets
2206 * invoked concurrently with itself or the interrupt handler (although
2207 * the interrupt handler may call this routine as part of request-
2208 * completion handling). Unfortunely, it sometimes calls the scheduler
2209 * in interrupt context which is a big NO! NO!.
2210 **/
8f0722ca 2211static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
afaf5a2d 2212{
8f0722ca 2213 struct scsi_qla_host *ha = to_qla_host(host);
afaf5a2d 2214 struct ddb_entry *ddb_entry = cmd->device->hostdata;
7fb1921b 2215 struct iscsi_cls_session *sess = ddb_entry->sess;
afaf5a2d
DS
2216 struct srb *srb;
2217 int rval;
2218
2232be0d
LC
2219 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2220 if (test_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags))
2221 cmd->result = DID_NO_CONNECT << 16;
2222 else
2223 cmd->result = DID_REQUEUE << 16;
2224 goto qc_fail_command;
2225 }
2226
7fb1921b
MC
2227 if (!sess) {
2228 cmd->result = DID_IMM_RETRY << 16;
2229 goto qc_fail_command;
2230 }
2231
2232 rval = iscsi_session_chkready(sess);
2233 if (rval) {
2234 cmd->result = rval;
2235 goto qc_fail_command;
2236 }
2237
f4f5df23
VC
2238 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2239 test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2240 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2241 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2242 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
2243 !test_bit(AF_ONLINE, &ha->flags) ||
b3a271a9 2244 !test_bit(AF_LINK_UP, &ha->flags) ||
f4f5df23 2245 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
477ffb9d
DS
2246 goto qc_host_busy;
2247
8f0722ca 2248 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
afaf5a2d 2249 if (!srb)
8f0722ca 2250 goto qc_host_busy;
afaf5a2d
DS
2251
2252 rval = qla4xxx_send_command_to_isp(ha, srb);
2253 if (rval != QLA_SUCCESS)
2254 goto qc_host_busy_free_sp;
2255
afaf5a2d
DS
2256 return 0;
2257
2258qc_host_busy_free_sp:
2259 qla4xxx_srb_free_dma(ha, srb);
2260 mempool_free(srb, ha->srb_mempool);
2261
afaf5a2d
DS
2262qc_host_busy:
2263 return SCSI_MLQUEUE_HOST_BUSY;
2264
2265qc_fail_command:
8f0722ca 2266 cmd->scsi_done(cmd);
afaf5a2d
DS
2267
2268 return 0;
2269}
2270
2271/**
2272 * qla4xxx_mem_free - frees memory allocated to adapter
2273 * @ha: Pointer to host adapter structure.
2274 *
2275 * Frees memory previously allocated by qla4xxx_mem_alloc
2276 **/
2277static void qla4xxx_mem_free(struct scsi_qla_host *ha)
2278{
2279 if (ha->queues)
2280 dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
2281 ha->queues_dma);
2282
068237c8
TP
2283 if (ha->fw_dump)
2284 vfree(ha->fw_dump);
2285
afaf5a2d
DS
2286 ha->queues_len = 0;
2287 ha->queues = NULL;
2288 ha->queues_dma = 0;
2289 ha->request_ring = NULL;
2290 ha->request_dma = 0;
2291 ha->response_ring = NULL;
2292 ha->response_dma = 0;
2293 ha->shadow_regs = NULL;
2294 ha->shadow_regs_dma = 0;
068237c8
TP
2295 ha->fw_dump = NULL;
2296 ha->fw_dump_size = 0;
afaf5a2d
DS
2297
2298 /* Free srb pool. */
2299 if (ha->srb_mempool)
2300 mempool_destroy(ha->srb_mempool);
2301
2302 ha->srb_mempool = NULL;
2303
b3a271a9
MR
2304 if (ha->chap_dma_pool)
2305 dma_pool_destroy(ha->chap_dma_pool);
2306
4549415a
LC
2307 if (ha->chap_list)
2308 vfree(ha->chap_list);
2309 ha->chap_list = NULL;
2310
13483730
MC
2311 if (ha->fw_ddb_dma_pool)
2312 dma_pool_destroy(ha->fw_ddb_dma_pool);
2313
afaf5a2d 2314 /* release io space registers */
f4f5df23
VC
2315 if (is_qla8022(ha)) {
2316 if (ha->nx_pcibase)
2317 iounmap(
2318 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
6e7b4292
VC
2319 } else if (is_qla8032(ha)) {
2320 if (ha->nx_pcibase)
2321 iounmap(
2322 (struct device_reg_83xx __iomem *)ha->nx_pcibase);
2323 } else if (ha->reg) {
afaf5a2d 2324 iounmap(ha->reg);
6e7b4292
VC
2325 }
2326
2327 if (ha->reset_tmplt.buff)
2328 vfree(ha->reset_tmplt.buff);
2329
afaf5a2d
DS
2330 pci_release_regions(ha->pdev);
2331}
2332
2333/**
2334 * qla4xxx_mem_alloc - allocates memory for use by adapter.
2335 * @ha: Pointer to host adapter structure
2336 *
2337 * Allocates DMA memory for request and response queues. Also allocates memory
2338 * for srbs.
2339 **/
2340static int qla4xxx_mem_alloc(struct scsi_qla_host *ha)
2341{
2342 unsigned long align;
2343
2344 /* Allocate contiguous block of DMA memory for queues. */
2345 ha->queues_len = ((REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
2346 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE) +
2347 sizeof(struct shadow_regs) +
2348 MEM_ALIGN_VALUE +
2349 (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);
2350 ha->queues = dma_alloc_coherent(&ha->pdev->dev, ha->queues_len,
2351 &ha->queues_dma, GFP_KERNEL);
2352 if (ha->queues == NULL) {
c2660df3
VC
2353 ql4_printk(KERN_WARNING, ha,
2354 "Memory Allocation failed - queues.\n");
afaf5a2d
DS
2355
2356 goto mem_alloc_error_exit;
2357 }
2358 memset(ha->queues, 0, ha->queues_len);
2359
2360 /*
2361 * As per RISC alignment requirements -- the bus-address must be a
2362 * multiple of the request-ring size (in bytes).
2363 */
2364 align = 0;
2365 if ((unsigned long)ha->queues_dma & (MEM_ALIGN_VALUE - 1))
2366 align = MEM_ALIGN_VALUE - ((unsigned long)ha->queues_dma &
2367 (MEM_ALIGN_VALUE - 1));
2368
2369 /* Update request and response queue pointers. */
2370 ha->request_dma = ha->queues_dma + align;
2371 ha->request_ring = (struct queue_entry *) (ha->queues + align);
2372 ha->response_dma = ha->queues_dma + align +
2373 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE);
2374 ha->response_ring = (struct queue_entry *) (ha->queues + align +
2375 (REQUEST_QUEUE_DEPTH *
2376 QUEUE_SIZE));
2377 ha->shadow_regs_dma = ha->queues_dma + align +
2378 (REQUEST_QUEUE_DEPTH * QUEUE_SIZE) +
2379 (RESPONSE_QUEUE_DEPTH * QUEUE_SIZE);
2380 ha->shadow_regs = (struct shadow_regs *) (ha->queues + align +
2381 (REQUEST_QUEUE_DEPTH *
2382 QUEUE_SIZE) +
2383 (RESPONSE_QUEUE_DEPTH *
2384 QUEUE_SIZE));
2385
2386 /* Allocate memory for srb pool. */
2387 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2388 mempool_free_slab, srb_cachep);
2389 if (ha->srb_mempool == NULL) {
c2660df3
VC
2390 ql4_printk(KERN_WARNING, ha,
2391 "Memory Allocation failed - SRB Pool.\n");
afaf5a2d
DS
2392
2393 goto mem_alloc_error_exit;
2394 }
2395
b3a271a9
MR
2396 ha->chap_dma_pool = dma_pool_create("ql4_chap", &ha->pdev->dev,
2397 CHAP_DMA_BLOCK_SIZE, 8, 0);
2398
2399 if (ha->chap_dma_pool == NULL) {
2400 ql4_printk(KERN_WARNING, ha,
2401 "%s: chap_dma_pool allocation failed..\n", __func__);
2402 goto mem_alloc_error_exit;
2403 }
2404
13483730
MC
2405 ha->fw_ddb_dma_pool = dma_pool_create("ql4_fw_ddb", &ha->pdev->dev,
2406 DDB_DMA_BLOCK_SIZE, 8, 0);
2407
2408 if (ha->fw_ddb_dma_pool == NULL) {
2409 ql4_printk(KERN_WARNING, ha,
2410 "%s: fw_ddb_dma_pool allocation failed..\n",
2411 __func__);
2412 goto mem_alloc_error_exit;
2413 }
2414
afaf5a2d
DS
2415 return QLA_SUCCESS;
2416
2417mem_alloc_error_exit:
2418 qla4xxx_mem_free(ha);
2419 return QLA_ERROR;
2420}
2421
4f77083e
MH
2422/**
2423 * qla4_8xxx_check_temp - Check the ISP82XX temperature.
2424 * @ha: adapter block pointer.
2425 *
2426 * Note: The caller should not hold the idc lock.
2427 **/
2428static int qla4_8xxx_check_temp(struct scsi_qla_host *ha)
2429{
2430 uint32_t temp, temp_state, temp_val;
2431 int status = QLA_SUCCESS;
2432
33693c7a 2433 temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
4f77083e
MH
2434
2435 temp_state = qla82xx_get_temp_state(temp);
2436 temp_val = qla82xx_get_temp_val(temp);
2437
2438 if (temp_state == QLA82XX_TEMP_PANIC) {
2439 ql4_printk(KERN_WARNING, ha, "Device temperature %d degrees C"
2440 " exceeds maximum allowed. Hardware has been shut"
2441 " down.\n", temp_val);
2442 status = QLA_ERROR;
2443 } else if (temp_state == QLA82XX_TEMP_WARN) {
2444 if (ha->temperature == QLA82XX_TEMP_NORMAL)
2445 ql4_printk(KERN_WARNING, ha, "Device temperature %d"
2446 " degrees C exceeds operating range."
2447 " Immediate action needed.\n", temp_val);
2448 } else {
2449 if (ha->temperature == QLA82XX_TEMP_WARN)
2450 ql4_printk(KERN_INFO, ha, "Device temperature is"
2451 " now %d degrees C in normal range.\n",
2452 temp_val);
2453 }
2454 ha->temperature = temp_state;
2455 return status;
2456}
2457
f4f5df23
VC
2458/**
2459 * qla4_8xxx_check_fw_alive - Check firmware health
2460 * @ha: Pointer to host adapter structure.
2461 *
2462 * Context: Interrupt
2463 **/
9ee91a38 2464static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
f4f5df23 2465{
9ee91a38
SS
2466 uint32_t fw_heartbeat_counter;
2467 int status = QLA_SUCCESS;
f4f5df23 2468
33693c7a
VC
2469 fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
2470 QLA8XXX_PEG_ALIVE_COUNTER);
2232be0d
LC
2471 /* If PEG_ALIVE_COUNTER is 0xffffffff, AER/EEH is in progress, ignore */
2472 if (fw_heartbeat_counter == 0xffffffff) {
2473 DEBUG2(printk(KERN_WARNING "scsi%ld: %s: Device in frozen "
2474 "state, QLA82XX_PEG_ALIVE_COUNTER is 0xffffffff\n",
2475 ha->host_no, __func__));
9ee91a38 2476 return status;
2232be0d 2477 }
f4f5df23
VC
2478
2479 if (ha->fw_heartbeat_counter == fw_heartbeat_counter) {
2480 ha->seconds_since_last_heartbeat++;
2481 /* FW not alive after 2 seconds */
2482 if (ha->seconds_since_last_heartbeat == 2) {
2483 ha->seconds_since_last_heartbeat = 0;
6e7b4292 2484 qla4_8xxx_dump_peg_reg(ha);
9ee91a38 2485 status = QLA_ERROR;
f4f5df23 2486 }
99457d75
LC
2487 } else
2488 ha->seconds_since_last_heartbeat = 0;
2489
f4f5df23 2490 ha->fw_heartbeat_counter = fw_heartbeat_counter;
9ee91a38 2491 return status;
f4f5df23
VC
2492}
2493
6e7b4292
VC
2494static void qla4_8xxx_process_fw_error(struct scsi_qla_host *ha)
2495{
2496 uint32_t halt_status;
2497 int halt_status_unrecoverable = 0;
2498
2499 halt_status = qla4_8xxx_rd_direct(ha, QLA8XXX_PEG_HALT_STATUS1);
2500
2501 if (is_qla8022(ha)) {
2502 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
2503 __func__);
2504 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2505 CRB_NIU_XG_PAUSE_CTL_P0 |
2506 CRB_NIU_XG_PAUSE_CTL_P1);
2507
2508 if (QLA82XX_FWERROR_CODE(halt_status) == 0x67)
2509 ql4_printk(KERN_ERR, ha, "%s: Firmware aborted with error code 0x00006700. Device is being reset\n",
2510 __func__);
2511 if (halt_status & HALT_STATUS_UNRECOVERABLE)
2512 halt_status_unrecoverable = 1;
2513 } else if (is_qla8032(ha)) {
2514 if (halt_status & QLA83XX_HALT_STATUS_FW_RESET)
2515 ql4_printk(KERN_ERR, ha, "%s: Firmware error detected device is being reset\n",
2516 __func__);
2517 else if (halt_status & QLA83XX_HALT_STATUS_UNRECOVERABLE)
2518 halt_status_unrecoverable = 1;
2519 }
2520
2521 /*
2522 * Since we cannot change dev_state in interrupt context,
2523 * set appropriate DPC flag then wakeup DPC
2524 */
2525 if (halt_status_unrecoverable) {
2526 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2527 } else {
2528 ql4_printk(KERN_INFO, ha, "%s: detect abort needed!\n",
2529 __func__);
2530 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2531 }
2532 qla4xxx_mailbox_premature_completion(ha);
2533 qla4xxx_wake_dpc(ha);
2534}
2535
f4f5df23
VC
2536/**
2537 * qla4_8xxx_watchdog - Poll dev state
2538 * @ha: Pointer to host adapter structure.
2539 *
2540 * Context: Interrupt
2541 **/
2542void qla4_8xxx_watchdog(struct scsi_qla_host *ha)
2543{
6e7b4292 2544 uint32_t dev_state;
f4f5df23
VC
2545
2546 /* don't poll if reset is going on */
d56a1f7b
LC
2547 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2548 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
977f46a4 2549 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
33693c7a 2550 dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
4f77083e
MH
2551
2552 if (qla4_8xxx_check_temp(ha)) {
6e7b4292
VC
2553 if (is_qla8022(ha)) {
2554 ql4_printk(KERN_INFO, ha, "disabling pause transmit on port 0 & 1.\n");
2555 qla4_82xx_wr_32(ha, QLA82XX_CRB_NIU + 0x98,
2556 CRB_NIU_XG_PAUSE_CTL_P0 |
2557 CRB_NIU_XG_PAUSE_CTL_P1);
2558 }
4f77083e
MH
2559 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2560 qla4xxx_wake_dpc(ha);
de8c72da 2561 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
6e7b4292
VC
2562 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
2563 if (is_qla8032(ha) ||
2564 (is_qla8022(ha) && !ql4xdontresethba)) {
3930b8c1
VC
2565 ql4_printk(KERN_INFO, ha, "%s: HW State: "
2566 "NEED RESET!\n", __func__);
2567 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2568 qla4xxx_wake_dpc(ha);
3930b8c1 2569 }
de8c72da 2570 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
f4f5df23 2571 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
3930b8c1
VC
2572 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
2573 __func__);
f4f5df23
VC
2574 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
2575 qla4xxx_wake_dpc(ha);
2576 } else {
2577 /* Check firmware health */
6e7b4292
VC
2578 if (qla4_8xxx_check_fw_alive(ha))
2579 qla4_8xxx_process_fw_error(ha);
f4f5df23
VC
2580 }
2581 }
2582}
2583
4a4bc2e9 2584static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
13483730
MC
2585{
2586 struct iscsi_session *sess;
2587 struct ddb_entry *ddb_entry;
2588 struct scsi_qla_host *ha;
2589
2590 sess = cls_sess->dd_data;
2591 ddb_entry = sess->dd_data;
2592 ha = ddb_entry->ha;
2593
2594 if (!(ddb_entry->ddb_type == FLASH_DDB))
2595 return;
2596
2597 if (adapter_up(ha) && !test_bit(DF_RELOGIN, &ddb_entry->flags) &&
2598 !iscsi_is_session_online(cls_sess)) {
2599 if (atomic_read(&ddb_entry->retry_relogin_timer) !=
2600 INVALID_ENTRY) {
2601 if (atomic_read(&ddb_entry->retry_relogin_timer) ==
2602 0) {
2603 atomic_set(&ddb_entry->retry_relogin_timer,
2604 INVALID_ENTRY);
2605 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2606 set_bit(DF_RELOGIN, &ddb_entry->flags);
2607 DEBUG2(ql4_printk(KERN_INFO, ha,
2608 "%s: index [%d] login device\n",
2609 __func__, ddb_entry->fw_ddb_index));
2610 } else
2611 atomic_dec(&ddb_entry->retry_relogin_timer);
2612 }
2613 }
2614
2615 /* Wait for relogin to timeout */
2616 if (atomic_read(&ddb_entry->relogin_timer) &&
2617 (atomic_dec_and_test(&ddb_entry->relogin_timer) != 0)) {
2618 /*
2619 * If the relogin times out and the device is
2620 * still NOT ONLINE then try and relogin again.
2621 */
2622 if (!iscsi_is_session_online(cls_sess)) {
2623 /* Reset retry relogin timer */
2624 atomic_inc(&ddb_entry->relogin_retry_count);
2625 DEBUG2(ql4_printk(KERN_INFO, ha,
2626 "%s: index[%d] relogin timed out-retrying"
2627 " relogin (%d), retry (%d)\n", __func__,
2628 ddb_entry->fw_ddb_index,
2629 atomic_read(&ddb_entry->relogin_retry_count),
2630 ddb_entry->default_time2wait + 4));
2631 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
2632 atomic_set(&ddb_entry->retry_relogin_timer,
2633 ddb_entry->default_time2wait + 4);
2634 }
2635 }
2636}
2637
afaf5a2d
DS
2638/**
2639 * qla4xxx_timer - checks every second for work to do.
2640 * @ha: Pointer to host adapter structure.
2641 **/
2642static void qla4xxx_timer(struct scsi_qla_host *ha)
2643{
afaf5a2d 2644 int start_dpc = 0;
2232be0d
LC
2645 uint16_t w;
2646
13483730
MC
2647 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
2648
2232be0d
LC
2649 /* If we are in the middle of AER/EEH processing
2650 * skip any processing and reschedule the timer
2651 */
2652 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
2653 mod_timer(&ha->timer, jiffies + HZ);
2654 return;
2655 }
2656
2657 /* Hardware read to trigger an EEH error during mailbox waits. */
2658 if (!pci_channel_offline(ha->pdev))
2659 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
afaf5a2d 2660
6e7b4292 2661 if (is_qla80XX(ha))
f4f5df23 2662 qla4_8xxx_watchdog(ha);
f4f5df23 2663
ee996a69 2664 if (is_qla40XX(ha)) {
f4f5df23
VC
2665 /* Check for heartbeat interval. */
2666 if (ha->firmware_options & FWOPT_HEARTBEAT_ENABLE &&
2667 ha->heartbeat_interval != 0) {
2668 ha->seconds_since_last_heartbeat++;
2669 if (ha->seconds_since_last_heartbeat >
2670 ha->heartbeat_interval + 2)
2671 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2672 }
afaf5a2d
DS
2673 }
2674
ff884430
VC
2675 /* Process any deferred work. */
2676 if (!list_empty(&ha->work_list))
2677 start_dpc++;
2678
afaf5a2d 2679 /* Wakeup the dpc routine for this adapter, if needed. */
1b46807e 2680 if (start_dpc ||
afaf5a2d
DS
2681 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
2682 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags) ||
2683 test_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags) ||
f4f5df23 2684 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
afaf5a2d
DS
2685 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
2686 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
065aa1b4 2687 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
f4f5df23
VC
2688 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
2689 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
1b46807e 2690 test_bit(DPC_AEN, &ha->dpc_flags)) {
afaf5a2d
DS
2691 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
2692 " - dpc flags = 0x%lx\n",
2693 ha->host_no, __func__, ha->dpc_flags));
f4f5df23 2694 qla4xxx_wake_dpc(ha);
afaf5a2d
DS
2695 }
2696
2697 /* Reschedule timer thread to call us back in one second */
2698 mod_timer(&ha->timer, jiffies + HZ);
2699
2700 DEBUG2(ha->seconds_since_last_intr++);
2701}
2702
2703/**
2704 * qla4xxx_cmd_wait - waits for all outstanding commands to complete
2705 * @ha: Pointer to host adapter structure.
2706 *
2707 * This routine stalls the driver until all outstanding commands are returned.
2708 * Caller must release the Hardware Lock prior to calling this routine.
2709 **/
2710static int qla4xxx_cmd_wait(struct scsi_qla_host *ha)
2711{
2712 uint32_t index = 0;
afaf5a2d
DS
2713 unsigned long flags;
2714 struct scsi_cmnd *cmd;
afaf5a2d 2715
f4f5df23
VC
2716 unsigned long wtime = jiffies + (WAIT_CMD_TOV * HZ);
2717
2718 DEBUG2(ql4_printk(KERN_INFO, ha, "Wait up to %d seconds for cmds to "
2719 "complete\n", WAIT_CMD_TOV));
2720
2721 while (!time_after_eq(jiffies, wtime)) {
afaf5a2d
DS
2722 spin_lock_irqsave(&ha->hardware_lock, flags);
2723 /* Find a command that hasn't completed. */
2724 for (index = 0; index < ha->host->can_queue; index++) {
2725 cmd = scsi_host_find_tag(ha->host, index);
a1e0063d
MC
2726 /*
2727 * We cannot just check if the index is valid,
2728 * becase if we are run from the scsi eh, then
2729 * the scsi/block layer is going to prevent
2730 * the tag from being released.
2731 */
2732 if (cmd != NULL && CMD_SP(cmd))
afaf5a2d
DS
2733 break;
2734 }
2735 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2736
2737 /* If No Commands are pending, wait is complete */
f4f5df23
VC
2738 if (index == ha->host->can_queue)
2739 return QLA_SUCCESS;
afaf5a2d 2740
f4f5df23
VC
2741 msleep(1000);
2742 }
2743 /* If we timed out on waiting for commands to come back
2744 * return ERROR. */
2745 return QLA_ERROR;
afaf5a2d
DS
2746}
2747
f4f5df23 2748int qla4xxx_hw_reset(struct scsi_qla_host *ha)
afaf5a2d 2749{
afaf5a2d 2750 uint32_t ctrl_status;
477ffb9d
DS
2751 unsigned long flags = 0;
2752
2753 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
afaf5a2d 2754
f4f5df23
VC
2755 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
2756 return QLA_ERROR;
2757
afaf5a2d
DS
2758 spin_lock_irqsave(&ha->hardware_lock, flags);
2759
2760 /*
2761 * If the SCSI Reset Interrupt bit is set, clear it.
2762 * Otherwise, the Soft Reset won't work.
2763 */
2764 ctrl_status = readw(&ha->reg->ctrl_status);
2765 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0)
2766 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2767
2768 /* Issue Soft Reset */
2769 writel(set_rmask(CSR_SOFT_RESET), &ha->reg->ctrl_status);
2770 readl(&ha->reg->ctrl_status);
2771
2772 spin_unlock_irqrestore(&ha->hardware_lock, flags);
f4f5df23 2773 return QLA_SUCCESS;
477ffb9d
DS
2774}
2775
2776/**
2777 * qla4xxx_soft_reset - performs soft reset.
2778 * @ha: Pointer to host adapter structure.
2779 **/
2780int qla4xxx_soft_reset(struct scsi_qla_host *ha)
2781{
2782 uint32_t max_wait_time;
2783 unsigned long flags = 0;
f931c534 2784 int status;
477ffb9d
DS
2785 uint32_t ctrl_status;
2786
f931c534
VC
2787 status = qla4xxx_hw_reset(ha);
2788 if (status != QLA_SUCCESS)
2789 return status;
afaf5a2d 2790
f931c534 2791 status = QLA_ERROR;
afaf5a2d
DS
2792 /* Wait until the Network Reset Intr bit is cleared */
2793 max_wait_time = RESET_INTR_TOV;
2794 do {
2795 spin_lock_irqsave(&ha->hardware_lock, flags);
2796 ctrl_status = readw(&ha->reg->ctrl_status);
2797 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2798
2799 if ((ctrl_status & CSR_NET_RESET_INTR) == 0)
2800 break;
2801
2802 msleep(1000);
2803 } while ((--max_wait_time));
2804
2805 if ((ctrl_status & CSR_NET_RESET_INTR) != 0) {
2806 DEBUG2(printk(KERN_WARNING
2807 "scsi%ld: Network Reset Intr not cleared by "
2808 "Network function, clearing it now!\n",
2809 ha->host_no));
2810 spin_lock_irqsave(&ha->hardware_lock, flags);
2811 writel(set_rmask(CSR_NET_RESET_INTR), &ha->reg->ctrl_status);
2812 readl(&ha->reg->ctrl_status);
2813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2814 }
2815
2816 /* Wait until the firmware tells us the Soft Reset is done */
2817 max_wait_time = SOFT_RESET_TOV;
2818 do {
2819 spin_lock_irqsave(&ha->hardware_lock, flags);
2820 ctrl_status = readw(&ha->reg->ctrl_status);
2821 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2822
2823 if ((ctrl_status & CSR_SOFT_RESET) == 0) {
2824 status = QLA_SUCCESS;
2825 break;
2826 }
2827
2828 msleep(1000);
2829 } while ((--max_wait_time));
2830
2831 /*
2832 * Also, make sure that the SCSI Reset Interrupt bit has been cleared
2833 * after the soft reset has taken place.
2834 */
2835 spin_lock_irqsave(&ha->hardware_lock, flags);
2836 ctrl_status = readw(&ha->reg->ctrl_status);
2837 if ((ctrl_status & CSR_SCSI_RESET_INTR) != 0) {
2838 writel(set_rmask(CSR_SCSI_RESET_INTR), &ha->reg->ctrl_status);
2839 readl(&ha->reg->ctrl_status);
2840 }
2841 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2842
2843 /* If soft reset fails then most probably the bios on other
2844 * function is also enabled.
2845 * Since the initialization is sequential the other fn
2846 * wont be able to acknowledge the soft reset.
2847 * Issue a force soft reset to workaround this scenario.
2848 */
2849 if (max_wait_time == 0) {
2850 /* Issue Force Soft Reset */
2851 spin_lock_irqsave(&ha->hardware_lock, flags);
2852 writel(set_rmask(CSR_FORCE_SOFT_RESET), &ha->reg->ctrl_status);
2853 readl(&ha->reg->ctrl_status);
2854 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2855 /* Wait until the firmware tells us the Soft Reset is done */
2856 max_wait_time = SOFT_RESET_TOV;
2857 do {
2858 spin_lock_irqsave(&ha->hardware_lock, flags);
2859 ctrl_status = readw(&ha->reg->ctrl_status);
2860 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2861
2862 if ((ctrl_status & CSR_FORCE_SOFT_RESET) == 0) {
2863 status = QLA_SUCCESS;
2864 break;
2865 }
2866
2867 msleep(1000);
2868 } while ((--max_wait_time));
2869 }
2870
2871 return status;
2872}
2873
afaf5a2d 2874/**
f4f5df23 2875 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
afaf5a2d 2876 * @ha: Pointer to host adapter structure.
f4f5df23 2877 * @res: returned scsi status
afaf5a2d
DS
2878 *
2879 * This routine is called just prior to a HARD RESET to return all
2880 * outstanding commands back to the Operating System.
2881 * Caller should make sure that the following locks are released
2882 * before this calling routine: Hardware lock, and io_request_lock.
2883 **/
f4f5df23 2884static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
afaf5a2d
DS
2885{
2886 struct srb *srb;
2887 int i;
2888 unsigned long flags;
2889
2890 spin_lock_irqsave(&ha->hardware_lock, flags);
2891 for (i = 0; i < ha->host->can_queue; i++) {
2892 srb = qla4xxx_del_from_active_array(ha, i);
2893 if (srb != NULL) {
f4f5df23 2894 srb->cmd->result = res;
09a0f719 2895 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
afaf5a2d
DS
2896 }
2897 }
2898 spin_unlock_irqrestore(&ha->hardware_lock, flags);
afaf5a2d
DS
2899}
2900
f4f5df23
VC
2901void qla4xxx_dead_adapter_cleanup(struct scsi_qla_host *ha)
2902{
2903 clear_bit(AF_ONLINE, &ha->flags);
2904
2905 /* Disable the board */
2906 ql4_printk(KERN_INFO, ha, "Disabling the board\n");
f4f5df23
VC
2907
2908 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
2909 qla4xxx_mark_all_devices_missing(ha);
2910 clear_bit(AF_INIT_DONE, &ha->flags);
2911}
2912
b3a271a9
MR
2913static void qla4xxx_fail_session(struct iscsi_cls_session *cls_session)
2914{
2915 struct iscsi_session *sess;
2916 struct ddb_entry *ddb_entry;
2917
2918 sess = cls_session->dd_data;
2919 ddb_entry = sess->dd_data;
2920 ddb_entry->fw_ddb_device_state = DDB_DS_SESSION_FAILED;
13483730
MC
2921
2922 if (ddb_entry->ddb_type == FLASH_DDB)
2923 iscsi_block_session(ddb_entry->sess);
2924 else
2925 iscsi_session_failure(cls_session->dd_data,
2926 ISCSI_ERR_CONN_FAILED);
b3a271a9
MR
2927}
2928
afaf5a2d
DS
2929/**
2930 * qla4xxx_recover_adapter - recovers adapter after a fatal error
2931 * @ha: Pointer to host adapter structure.
afaf5a2d 2932 **/
f4f5df23 2933static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
afaf5a2d 2934{
f4f5df23
VC
2935 int status = QLA_ERROR;
2936 uint8_t reset_chip = 0;
8e0f3a66 2937 uint32_t dev_state;
9ee91a38 2938 unsigned long wait;
afaf5a2d
DS
2939
2940 /* Stall incoming I/O until we are done */
f4f5df23 2941 scsi_block_requests(ha->host);
afaf5a2d 2942 clear_bit(AF_ONLINE, &ha->flags);
b3a271a9 2943 clear_bit(AF_LINK_UP, &ha->flags);
50a29aec 2944
f4f5df23 2945 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
afaf5a2d 2946
f4f5df23 2947 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
afaf5a2d 2948
546fef27
TP
2949 if (is_qla8032(ha) &&
2950 !test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2951 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
2952 __func__);
2953 /* disable pause frame for ISP83xx */
2954 qla4_83xx_disable_pause(ha);
2955 }
2956
b3a271a9
MR
2957 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2958
f4f5df23
VC
2959 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
2960 reset_chip = 1;
afaf5a2d 2961
f4f5df23
VC
2962 /* For the DPC_RESET_HA_INTR case (ISP-4xxx specific)
2963 * do not reset adapter, jump to initialize_adapter */
2964 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
2965 status = QLA_SUCCESS;
2966 goto recover_ha_init_adapter;
2967 }
afaf5a2d 2968
6e7b4292 2969 /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
f4f5df23 2970 * from eh_host_reset or ioctl module */
6e7b4292 2971 if (is_qla80XX(ha) && !reset_chip &&
f4f5df23
VC
2972 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags)) {
2973
2974 DEBUG2(ql4_printk(KERN_INFO, ha,
2975 "scsi%ld: %s - Performing stop_firmware...\n",
2976 ha->host_no, __func__));
2977 status = ha->isp_ops->reset_firmware(ha);
2978 if (status == QLA_SUCCESS) {
2bd1e2be
NJ
2979 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
2980 qla4xxx_cmd_wait(ha);
5c19b92a 2981
f4f5df23
VC
2982 ha->isp_ops->disable_intrs(ha);
2983 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2984 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2985 } else {
2986 /* If the stop_firmware fails then
2987 * reset the entire chip */
2988 reset_chip = 1;
2989 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
2990 set_bit(DPC_RESET_HA, &ha->dpc_flags);
2991 }
2992 }
dca05c4c 2993
f4f5df23 2994 /* Issue full chip reset if recovering from a catastrophic error,
6e7b4292 2995 * or if stop_firmware fails for ISP-8xxx.
f4f5df23 2996 * This is the default case for ISP-4xxx */
ee996a69
VC
2997 if (is_qla40XX(ha) || reset_chip) {
2998 if (is_qla40XX(ha))
9ee91a38
SS
2999 goto chip_reset;
3000
6e7b4292 3001 /* Check if 8XXX firmware is alive or not
9ee91a38
SS
3002 * We may have arrived here from NEED_RESET
3003 * detection only */
3004 if (test_bit(AF_FW_RECOVERY, &ha->flags))
3005 goto chip_reset;
3006
3007 wait = jiffies + (FW_ALIVE_WAIT_TOV * HZ);
3008 while (time_before(jiffies, wait)) {
3009 if (qla4_8xxx_check_fw_alive(ha)) {
3010 qla4xxx_mailbox_premature_completion(ha);
3011 break;
3012 }
3013
3014 set_current_state(TASK_UNINTERRUPTIBLE);
3015 schedule_timeout(HZ);
3016 }
da106212 3017chip_reset:
2bd1e2be
NJ
3018 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
3019 qla4xxx_cmd_wait(ha);
da106212 3020
f4f5df23
VC
3021 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3022 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
3023 DEBUG2(ql4_printk(KERN_INFO, ha,
3024 "scsi%ld: %s - Performing chip reset..\n",
3025 ha->host_no, __func__));
3026 status = ha->isp_ops->reset_chip(ha);
3027 }
afaf5a2d
DS
3028
3029 /* Flush any pending ddb changed AENs */
3030 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3031
f4f5df23
VC
3032recover_ha_init_adapter:
3033 /* Upon successful firmware/chip reset, re-initialize the adapter */
afaf5a2d 3034 if (status == QLA_SUCCESS) {
f4f5df23
VC
3035 /* For ISP-4xxx, force function 1 to always initialize
3036 * before function 3 to prevent both funcions from
3037 * stepping on top of the other */
ee996a69 3038 if (is_qla40XX(ha) && (ha->mac_index == 3))
f4f5df23
VC
3039 ssleep(6);
3040
3041 /* NOTE: AF_ONLINE flag set upon successful completion of
3042 * qla4xxx_initialize_adapter */
13483730 3043 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
afaf5a2d
DS
3044 }
3045
f4f5df23
VC
3046 /* Retry failed adapter initialization, if necessary
3047 * Do not retry initialize_adapter for RESET_HA_INTR (ISP-4xxx specific)
3048 * case to prevent ping-pong resets between functions */
3049 if (!test_bit(AF_ONLINE, &ha->flags) &&
3050 !test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d 3051 /* Adapter initialization failed, see if we can retry
f4f5df23
VC
3052 * resetting the ha.
3053 * Since we don't want to block the DPC for too long
3054 * with multiple resets in the same thread,
3055 * utilize DPC to retry */
6e7b4292 3056 if (is_qla80XX(ha)) {
33693c7a
VC
3057 ha->isp_ops->idc_lock(ha);
3058 dev_state = qla4_8xxx_rd_direct(ha,
3059 QLA8XXX_CRB_DEV_STATE);
3060 ha->isp_ops->idc_unlock(ha);
de8c72da 3061 if (dev_state == QLA8XXX_DEV_FAILED) {
8e0f3a66
SR
3062 ql4_printk(KERN_INFO, ha, "%s: don't retry "
3063 "recover adapter. H/W is in Failed "
3064 "state\n", __func__);
3065 qla4xxx_dead_adapter_cleanup(ha);
3066 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3067 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3068 clear_bit(DPC_RESET_HA_FW_CONTEXT,
3069 &ha->dpc_flags);
3070 status = QLA_ERROR;
3071
3072 goto exit_recover;
3073 }
3074 }
3075
afaf5a2d
DS
3076 if (!test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags)) {
3077 ha->retry_reset_ha_cnt = MAX_RESET_HA_RETRIES;
3078 DEBUG2(printk("scsi%ld: recover adapter - retrying "
3079 "(%d) more times\n", ha->host_no,
3080 ha->retry_reset_ha_cnt));
3081 set_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3082 status = QLA_ERROR;
3083 } else {
3084 if (ha->retry_reset_ha_cnt > 0) {
3085 /* Schedule another Reset HA--DPC will retry */
3086 ha->retry_reset_ha_cnt--;
3087 DEBUG2(printk("scsi%ld: recover adapter - "
3088 "retry remaining %d\n",
3089 ha->host_no,
3090 ha->retry_reset_ha_cnt));
3091 status = QLA_ERROR;
3092 }
3093
3094 if (ha->retry_reset_ha_cnt == 0) {
3095 /* Recover adapter retries have been exhausted.
3096 * Adapter DEAD */
3097 DEBUG2(printk("scsi%ld: recover adapter "
3098 "failed - board disabled\n",
3099 ha->host_no));
f4f5df23 3100 qla4xxx_dead_adapter_cleanup(ha);
afaf5a2d
DS
3101 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3102 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23 3103 clear_bit(DPC_RESET_HA_FW_CONTEXT,
afaf5a2d
DS
3104 &ha->dpc_flags);
3105 status = QLA_ERROR;
3106 }
3107 }
3108 } else {
3109 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
f4f5df23 3110 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
afaf5a2d
DS
3111 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3112 }
3113
8e0f3a66 3114exit_recover:
afaf5a2d
DS
3115 ha->adapter_error_count++;
3116
f4f5df23
VC
3117 if (test_bit(AF_ONLINE, &ha->flags))
3118 ha->isp_ops->enable_intrs(ha);
3119
3120 scsi_unblock_requests(ha->host);
3121
3122 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
3123 DEBUG2(printk("scsi%ld: recover adapter: %s\n", ha->host_no,
25985edc 3124 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
afaf5a2d 3125
afaf5a2d
DS
3126 return status;
3127}
3128
b3a271a9 3129static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
2d7924e6 3130{
b3a271a9
MR
3131 struct iscsi_session *sess;
3132 struct ddb_entry *ddb_entry;
3133 struct scsi_qla_host *ha;
2d7924e6 3134
b3a271a9
MR
3135 sess = cls_session->dd_data;
3136 ddb_entry = sess->dd_data;
3137 ha = ddb_entry->ha;
3138 if (!iscsi_is_session_online(cls_session)) {
3139 if (ddb_entry->fw_ddb_device_state == DDB_DS_SESSION_ACTIVE) {
3140 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3141 " unblock session\n", ha->host_no, __func__,
3142 ddb_entry->fw_ddb_index);
3143 iscsi_unblock_session(ddb_entry->sess);
3144 } else {
3145 /* Trigger relogin */
13483730
MC
3146 if (ddb_entry->ddb_type == FLASH_DDB) {
3147 if (!test_bit(DF_RELOGIN, &ddb_entry->flags))
3148 qla4xxx_arm_relogin_timer(ddb_entry);
3149 } else
3150 iscsi_session_failure(cls_session->dd_data,
3151 ISCSI_ERR_CONN_FAILED);
2d7924e6
VC
3152 }
3153 }
3154}
3155
13483730
MC
3156int qla4xxx_unblock_flash_ddb(struct iscsi_cls_session *cls_session)
3157{
3158 struct iscsi_session *sess;
3159 struct ddb_entry *ddb_entry;
3160 struct scsi_qla_host *ha;
3161
3162 sess = cls_session->dd_data;
3163 ddb_entry = sess->dd_data;
3164 ha = ddb_entry->ha;
3165 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3166 " unblock session\n", ha->host_no, __func__,
3167 ddb_entry->fw_ddb_index);
3168
3169 iscsi_unblock_session(ddb_entry->sess);
3170
3171 /* Start scan target */
3172 if (test_bit(AF_ONLINE, &ha->flags)) {
3173 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3174 " start scan\n", ha->host_no, __func__,
3175 ddb_entry->fw_ddb_index);
3176 scsi_queue_work(ha->host, &ddb_entry->sess->scan_work);
3177 }
3178 return QLA_SUCCESS;
3179}
3180
3181int qla4xxx_unblock_ddb(struct iscsi_cls_session *cls_session)
3182{
3183 struct iscsi_session *sess;
3184 struct ddb_entry *ddb_entry;
3185 struct scsi_qla_host *ha;
80c53e64 3186 int status = QLA_SUCCESS;
13483730
MC
3187
3188 sess = cls_session->dd_data;
3189 ddb_entry = sess->dd_data;
3190 ha = ddb_entry->ha;
3191 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: ddb[%d]"
3192 " unblock user space session\n", ha->host_no, __func__,
3193 ddb_entry->fw_ddb_index);
13483730 3194
80c53e64
MR
3195 if (!iscsi_is_session_online(cls_session)) {
3196 iscsi_conn_start(ddb_entry->conn);
3197 iscsi_conn_login_event(ddb_entry->conn,
3198 ISCSI_CONN_STATE_LOGGED_IN);
3199 } else {
3200 ql4_printk(KERN_INFO, ha,
3201 "scsi%ld: %s: ddb[%d] session [%d] already logged in\n",
3202 ha->host_no, __func__, ddb_entry->fw_ddb_index,
3203 cls_session->sid);
3204 status = QLA_ERROR;
3205 }
3206
3207 return status;
13483730
MC
3208}
3209
b3a271a9
MR
3210static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
3211{
3212 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
3213}
3214
13483730
MC
3215static void qla4xxx_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
3216{
3217 uint16_t relogin_timer;
3218 struct iscsi_session *sess;
3219 struct ddb_entry *ddb_entry;
3220 struct scsi_qla_host *ha;
3221
3222 sess = cls_sess->dd_data;
3223 ddb_entry = sess->dd_data;
3224 ha = ddb_entry->ha;
3225
3226 relogin_timer = max(ddb_entry->default_relogin_timeout,
3227 (uint16_t)RELOGIN_TOV);
3228 atomic_set(&ddb_entry->relogin_timer, relogin_timer);
3229
3230 DEBUG2(ql4_printk(KERN_INFO, ha,
3231 "scsi%ld: Relogin index [%d]. TOV=%d\n", ha->host_no,
3232 ddb_entry->fw_ddb_index, relogin_timer));
3233
3234 qla4xxx_login_flash_ddb(cls_sess);
3235}
3236
3237static void qla4xxx_dpc_relogin(struct iscsi_cls_session *cls_sess)
3238{
3239 struct iscsi_session *sess;
3240 struct ddb_entry *ddb_entry;
3241 struct scsi_qla_host *ha;
3242
3243 sess = cls_sess->dd_data;
3244 ddb_entry = sess->dd_data;
3245 ha = ddb_entry->ha;
3246
3247 if (!(ddb_entry->ddb_type == FLASH_DDB))
3248 return;
3249
3250 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags) &&
3251 !iscsi_is_session_online(cls_sess)) {
3252 DEBUG2(ql4_printk(KERN_INFO, ha,
3253 "relogin issued\n"));
3254 qla4xxx_relogin_flash_ddb(cls_sess);
3255 }
3256}
3257
f4f5df23
VC
3258void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
3259{
1b46807e 3260 if (ha->dpc_thread)
f4f5df23 3261 queue_work(ha->dpc_thread, &ha->dpc_work);
f4f5df23
VC
3262}
3263
ff884430
VC
3264static struct qla4_work_evt *
3265qla4xxx_alloc_work(struct scsi_qla_host *ha, uint32_t data_size,
3266 enum qla4_work_type type)
3267{
3268 struct qla4_work_evt *e;
3269 uint32_t size = sizeof(struct qla4_work_evt) + data_size;
3270
3271 e = kzalloc(size, GFP_ATOMIC);
3272 if (!e)
3273 return NULL;
3274
3275 INIT_LIST_HEAD(&e->list);
3276 e->type = type;
3277 return e;
3278}
3279
3280static void qla4xxx_post_work(struct scsi_qla_host *ha,
3281 struct qla4_work_evt *e)
3282{
3283 unsigned long flags;
3284
3285 spin_lock_irqsave(&ha->work_lock, flags);
3286 list_add_tail(&e->list, &ha->work_list);
3287 spin_unlock_irqrestore(&ha->work_lock, flags);
3288 qla4xxx_wake_dpc(ha);
3289}
3290
3291int qla4xxx_post_aen_work(struct scsi_qla_host *ha,
3292 enum iscsi_host_event_code aen_code,
3293 uint32_t data_size, uint8_t *data)
3294{
3295 struct qla4_work_evt *e;
3296
3297 e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_AEN);
3298 if (!e)
3299 return QLA_ERROR;
3300
3301 e->u.aen.code = aen_code;
3302 e->u.aen.data_size = data_size;
3303 memcpy(e->u.aen.data, data, data_size);
3304
3305 qla4xxx_post_work(ha, e);
3306
3307 return QLA_SUCCESS;
3308}
3309
c0b9d3f7
VC
3310int qla4xxx_post_ping_evt_work(struct scsi_qla_host *ha,
3311 uint32_t status, uint32_t pid,
3312 uint32_t data_size, uint8_t *data)
3313{
3314 struct qla4_work_evt *e;
3315
3316 e = qla4xxx_alloc_work(ha, data_size, QLA4_EVENT_PING_STATUS);
3317 if (!e)
3318 return QLA_ERROR;
3319
3320 e->u.ping.status = status;
3321 e->u.ping.pid = pid;
3322 e->u.ping.data_size = data_size;
3323 memcpy(e->u.ping.data, data, data_size);
3324
3325 qla4xxx_post_work(ha, e);
3326
3327 return QLA_SUCCESS;
3328}
3329
a7380a65 3330static void qla4xxx_do_work(struct scsi_qla_host *ha)
ff884430
VC
3331{
3332 struct qla4_work_evt *e, *tmp;
3333 unsigned long flags;
3334 LIST_HEAD(work);
3335
3336 spin_lock_irqsave(&ha->work_lock, flags);
3337 list_splice_init(&ha->work_list, &work);
3338 spin_unlock_irqrestore(&ha->work_lock, flags);
3339
3340 list_for_each_entry_safe(e, tmp, &work, list) {
3341 list_del_init(&e->list);
3342
3343 switch (e->type) {
3344 case QLA4_EVENT_AEN:
3345 iscsi_post_host_event(ha->host_no,
3346 &qla4xxx_iscsi_transport,
3347 e->u.aen.code,
3348 e->u.aen.data_size,
3349 e->u.aen.data);
3350 break;
c0b9d3f7
VC
3351 case QLA4_EVENT_PING_STATUS:
3352 iscsi_ping_comp_event(ha->host_no,
3353 &qla4xxx_iscsi_transport,
3354 e->u.ping.status,
3355 e->u.ping.pid,
3356 e->u.ping.data_size,
3357 e->u.ping.data);
3358 break;
ff884430
VC
3359 default:
3360 ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
3361 "supported", e->type);
3362 }
3363 kfree(e);
3364 }
3365}
3366
afaf5a2d
DS
3367/**
3368 * qla4xxx_do_dpc - dpc routine
3369 * @data: in our case pointer to adapter structure
3370 *
3371 * This routine is a task that is schedule by the interrupt handler
3372 * to perform the background processing for interrupts. We put it
3373 * on a task queue that is consumed whenever the scheduler runs; that's
3374 * so you can do anything (i.e. put the process to sleep etc). In fact,
3375 * the mid-level tries to sleep when it reaches the driver threshold
3376 * "host->can_queue". This can cause a panic if we were in our interrupt code.
3377 **/
c4028958 3378static void qla4xxx_do_dpc(struct work_struct *work)
afaf5a2d 3379{
c4028958
DH
3380 struct scsi_qla_host *ha =
3381 container_of(work, struct scsi_qla_host, dpc_work);
477ffb9d 3382 int status = QLA_ERROR;
afaf5a2d 3383
f26b9044 3384 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
f4f5df23
VC
3385 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
3386 ha->host_no, __func__, ha->flags, ha->dpc_flags))
afaf5a2d
DS
3387
3388 /* Initialization not yet finished. Don't do anything yet. */
3389 if (!test_bit(AF_INIT_DONE, &ha->flags))
1b46807e 3390 return;
afaf5a2d 3391
2232be0d
LC
3392 if (test_bit(AF_EEH_BUSY, &ha->flags)) {
3393 DEBUG2(printk(KERN_INFO "scsi%ld: %s: flags = %lx\n",
3394 ha->host_no, __func__, ha->flags));
1b46807e 3395 return;
2232be0d
LC
3396 }
3397
ff884430
VC
3398 /* post events to application */
3399 qla4xxx_do_work(ha);
3400
6e7b4292 3401 if (is_qla80XX(ha)) {
f4f5df23 3402 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
546fef27
TP
3403 if (is_qla8032(ha)) {
3404 ql4_printk(KERN_INFO, ha, "%s: disabling pause transmit on port 0 & 1.\n",
3405 __func__);
3406 /* disable pause frame for ISP83xx */
3407 qla4_83xx_disable_pause(ha);
3408 }
3409
33693c7a
VC
3410 ha->isp_ops->idc_lock(ha);
3411 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
3412 QLA8XXX_DEV_FAILED);
3413 ha->isp_ops->idc_unlock(ha);
f4f5df23
VC
3414 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
3415 qla4_8xxx_device_state_handler(ha);
3416 }
320a61de
NJ
3417
3418 if (test_and_clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags))
3419 qla4_83xx_post_idc_ack(ha);
3420
f4f5df23
VC
3421 if (test_and_clear_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
3422 qla4_8xxx_need_qsnt_handler(ha);
3423 }
3424 }
3425
3426 if (!test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) &&
3427 (test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
afaf5a2d 3428 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
f4f5df23 3429 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
6e7b4292
VC
3430 if ((is_qla8022(ha) && ql4xdontresethba) ||
3431 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
f4f5df23
VC
3432 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
3433 ha->host_no, __func__));
3434 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
3435 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3436 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
3437 goto dpc_post_reset_ha;
3438 }
3439 if (test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
3440 test_bit(DPC_RESET_HA, &ha->dpc_flags))
3441 qla4xxx_recover_adapter(ha);
afaf5a2d 3442
477ffb9d 3443 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
afaf5a2d 3444 uint8_t wait_time = RESET_INTR_TOV;
afaf5a2d 3445
afaf5a2d
DS
3446 while ((readw(&ha->reg->ctrl_status) &
3447 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
3448 if (--wait_time == 0)
3449 break;
afaf5a2d 3450 msleep(1000);
afaf5a2d 3451 }
afaf5a2d
DS
3452 if (wait_time == 0)
3453 DEBUG2(printk("scsi%ld: %s: SR|FSR "
3454 "bit not cleared-- resetting\n",
3455 ha->host_no, __func__));
f4f5df23 3456 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
477ffb9d
DS
3457 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
3458 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
f4f5df23 3459 status = qla4xxx_recover_adapter(ha);
477ffb9d
DS
3460 }
3461 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3462 if (status == QLA_SUCCESS)
f4f5df23 3463 ha->isp_ops->enable_intrs(ha);
afaf5a2d
DS
3464 }
3465 }
3466
f4f5df23 3467dpc_post_reset_ha:
afaf5a2d
DS
3468 /* ---- process AEN? --- */
3469 if (test_and_clear_bit(DPC_AEN, &ha->dpc_flags))
3470 qla4xxx_process_aen(ha, PROCESS_ALL_AENS);
3471
3472 /* ---- Get DHCP IP Address? --- */
3473 if (test_and_clear_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags))
3474 qla4xxx_get_dhcp_ip_address(ha);
3475
13483730
MC
3476 /* ---- relogin device? --- */
3477 if (adapter_up(ha) &&
3478 test_and_clear_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags)) {
3479 iscsi_host_for_each_session(ha->host, qla4xxx_dpc_relogin);
3480 }
3481
065aa1b4
VC
3482 /* ---- link change? --- */
3483 if (test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
3484 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3485 /* ---- link down? --- */
2d7924e6 3486 qla4xxx_mark_all_devices_missing(ha);
065aa1b4
VC
3487 } else {
3488 /* ---- link up? --- *
3489 * F/W will auto login to all devices ONLY ONCE after
3490 * link up during driver initialization and runtime
3491 * fatal error recovery. Therefore, the driver must
3492 * manually relogin to devices when recovering from
3493 * connection failures, logouts, expired KATO, etc. */
13483730
MC
3494 if (test_and_clear_bit(AF_BUILD_DDB_LIST, &ha->flags)) {
3495 qla4xxx_build_ddb_list(ha, ha->is_reset);
3496 iscsi_host_for_each_session(ha->host,
3497 qla4xxx_login_flash_ddb);
3498 } else
3499 qla4xxx_relogin_all_devices(ha);
065aa1b4
VC
3500 }
3501 }
afaf5a2d
DS
3502}
3503
3504/**
3505 * qla4xxx_free_adapter - release the adapter
3506 * @ha: pointer to adapter structure
3507 **/
3508static void qla4xxx_free_adapter(struct scsi_qla_host *ha)
3509{
8a288960 3510 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
afaf5a2d 3511
5c19b92a
VC
3512 /* Turn-off interrupts on the card. */
3513 ha->isp_ops->disable_intrs(ha);
afaf5a2d 3514
d9e62e51
VC
3515 if (is_qla40XX(ha)) {
3516 writel(set_rmask(CSR_SCSI_PROCESSOR_INTR),
3517 &ha->reg->ctrl_status);
3518 readl(&ha->reg->ctrl_status);
3519 } else if (is_qla8022(ha)) {
7664a1fd
VC
3520 writel(0, &ha->qla4_82xx_reg->host_int);
3521 readl(&ha->qla4_82xx_reg->host_int);
fbd8107c
VC
3522 } else if (is_qla8032(ha)) {
3523 writel(0, &ha->qla4_83xx_reg->risc_intr);
3524 readl(&ha->qla4_83xx_reg->risc_intr);
d9e62e51
VC
3525 }
3526
f4f5df23
VC
3527 /* Remove timer thread, if present */
3528 if (ha->timer_active)
3529 qla4xxx_stop_timer(ha);
3530
afaf5a2d
DS
3531 /* Kill the kernel thread for this host */
3532 if (ha->dpc_thread)
3533 destroy_workqueue(ha->dpc_thread);
3534
b3a271a9
MR
3535 /* Kill the kernel thread for this host */
3536 if (ha->task_wq)
3537 destroy_workqueue(ha->task_wq);
3538
f4f5df23
VC
3539 /* Put firmware in known state */
3540 ha->isp_ops->reset_firmware(ha);
afaf5a2d 3541
6e7b4292 3542 if (is_qla80XX(ha)) {
33693c7a 3543 ha->isp_ops->idc_lock(ha);
f4f5df23 3544 qla4_8xxx_clear_drv_active(ha);
33693c7a 3545 ha->isp_ops->idc_unlock(ha);
f4f5df23 3546 }
afaf5a2d 3547
afaf5a2d 3548 /* Detach interrupts */
5c19b92a 3549 qla4xxx_free_irqs(ha);
afaf5a2d 3550
bee4fe8e
DS
3551 /* free extra memory */
3552 qla4xxx_mem_free(ha);
f4f5df23
VC
3553}
3554
3555int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
3556{
3557 int status = 0;
f4f5df23
VC
3558 unsigned long mem_base, mem_len, db_base, db_len;
3559 struct pci_dev *pdev = ha->pdev;
3560
3561 status = pci_request_regions(pdev, DRIVER_NAME);
3562 if (status) {
3563 printk(KERN_WARNING
3564 "scsi(%ld) Failed to reserve PIO regions (%s) "
3565 "status=%d\n", ha->host_no, pci_name(pdev), status);
3566 goto iospace_error_exit;
3567 }
3568
f4f5df23 3569 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
7d7311c4
SS
3570 __func__, pdev->revision));
3571 ha->revision_id = pdev->revision;
bee4fe8e 3572
f4f5df23
VC
3573 /* remap phys address */
3574 mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
3575 mem_len = pci_resource_len(pdev, 0);
3576 DEBUG2(printk(KERN_INFO "%s: ioremap from %lx a size of %lx\n",
3577 __func__, mem_base, mem_len));
afaf5a2d 3578
f4f5df23
VC
3579 /* mapping of pcibase pointer */
3580 ha->nx_pcibase = (unsigned long)ioremap(mem_base, mem_len);
3581 if (!ha->nx_pcibase) {
3582 printk(KERN_ERR
3583 "cannot remap MMIO (%s), aborting\n", pci_name(pdev));
3584 pci_release_regions(ha->pdev);
3585 goto iospace_error_exit;
3586 }
3587
3588 /* Mapping of IO base pointer, door bell read and write pointer */
3589
3590 /* mapping of IO base pointer */
6e7b4292
VC
3591 if (is_qla8022(ha)) {
3592 ha->qla4_82xx_reg = (struct device_reg_82xx __iomem *)
3593 ((uint8_t *)ha->nx_pcibase + 0xbc000 +
3594 (ha->pdev->devfn << 11));
3595 ha->nx_db_wr_ptr = (ha->pdev->devfn == 4 ? QLA82XX_CAM_RAM_DB1 :
3596 QLA82XX_CAM_RAM_DB2);
3597 } else if (is_qla8032(ha)) {
3598 ha->qla4_83xx_reg = (struct device_reg_83xx __iomem *)
3599 ((uint8_t *)ha->nx_pcibase);
3600 }
f4f5df23
VC
3601
3602 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
3603 db_len = pci_resource_len(pdev, 4);
3604
2657c800 3605 return 0;
f4f5df23
VC
3606iospace_error_exit:
3607 return -ENOMEM;
afaf5a2d
DS
3608}
3609
3610/***
3611 * qla4xxx_iospace_config - maps registers
3612 * @ha: pointer to adapter structure
3613 *
3614 * This routines maps HBA's registers from the pci address space
3615 * into the kernel virtual address space for memory mapped i/o.
3616 **/
f4f5df23 3617int qla4xxx_iospace_config(struct scsi_qla_host *ha)
afaf5a2d
DS
3618{
3619 unsigned long pio, pio_len, pio_flags;
3620 unsigned long mmio, mmio_len, mmio_flags;
3621
3622 pio = pci_resource_start(ha->pdev, 0);
3623 pio_len = pci_resource_len(ha->pdev, 0);
3624 pio_flags = pci_resource_flags(ha->pdev, 0);
3625 if (pio_flags & IORESOURCE_IO) {
3626 if (pio_len < MIN_IOBASE_LEN) {
c2660df3 3627 ql4_printk(KERN_WARNING, ha,
afaf5a2d
DS
3628 "Invalid PCI I/O region size\n");
3629 pio = 0;
3630 }
3631 } else {
c2660df3 3632 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
afaf5a2d
DS
3633 pio = 0;
3634 }
3635
3636 /* Use MMIO operations for all accesses. */
3637 mmio = pci_resource_start(ha->pdev, 1);
3638 mmio_len = pci_resource_len(ha->pdev, 1);
3639 mmio_flags = pci_resource_flags(ha->pdev, 1);
3640
3641 if (!(mmio_flags & IORESOURCE_MEM)) {
c2660df3
VC
3642 ql4_printk(KERN_ERR, ha,
3643 "region #0 not an MMIO resource, aborting\n");
afaf5a2d
DS
3644
3645 goto iospace_error_exit;
3646 }
c2660df3 3647
afaf5a2d 3648 if (mmio_len < MIN_IOBASE_LEN) {
c2660df3
VC
3649 ql4_printk(KERN_ERR, ha,
3650 "Invalid PCI mem region size, aborting\n");
afaf5a2d
DS
3651 goto iospace_error_exit;
3652 }
3653
3654 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
c2660df3
VC
3655 ql4_printk(KERN_WARNING, ha,
3656 "Failed to reserve PIO/MMIO regions\n");
afaf5a2d
DS
3657
3658 goto iospace_error_exit;
3659 }
3660
3661 ha->pio_address = pio;
3662 ha->pio_length = pio_len;
3663 ha->reg = ioremap(mmio, MIN_IOBASE_LEN);
3664 if (!ha->reg) {
c2660df3
VC
3665 ql4_printk(KERN_ERR, ha,
3666 "cannot remap MMIO, aborting\n");
afaf5a2d
DS
3667
3668 goto iospace_error_exit;
3669 }
3670
3671 return 0;
3672
3673iospace_error_exit:
3674 return -ENOMEM;
3675}
3676
f4f5df23
VC
3677static struct isp_operations qla4xxx_isp_ops = {
3678 .iospace_config = qla4xxx_iospace_config,
3679 .pci_config = qla4xxx_pci_config,
3680 .disable_intrs = qla4xxx_disable_intrs,
3681 .enable_intrs = qla4xxx_enable_intrs,
3682 .start_firmware = qla4xxx_start_firmware,
3683 .intr_handler = qla4xxx_intr_handler,
3684 .interrupt_service_routine = qla4xxx_interrupt_service_routine,
3685 .reset_chip = qla4xxx_soft_reset,
3686 .reset_firmware = qla4xxx_hw_reset,
3687 .queue_iocb = qla4xxx_queue_iocb,
3688 .complete_iocb = qla4xxx_complete_iocb,
3689 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
3690 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
3691 .get_sys_info = qla4xxx_get_sys_info,
33693c7a
VC
3692 .queue_mailbox_command = qla4xxx_queue_mbox_cmd,
3693 .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
f4f5df23
VC
3694};
3695
7664a1fd 3696static struct isp_operations qla4_82xx_isp_ops = {
f4f5df23
VC
3697 .iospace_config = qla4_8xxx_iospace_config,
3698 .pci_config = qla4_8xxx_pci_config,
f8086f4f
VC
3699 .disable_intrs = qla4_82xx_disable_intrs,
3700 .enable_intrs = qla4_82xx_enable_intrs,
f4f5df23 3701 .start_firmware = qla4_8xxx_load_risc,
33693c7a 3702 .restart_firmware = qla4_82xx_try_start_fw,
f8086f4f
VC
3703 .intr_handler = qla4_82xx_intr_handler,
3704 .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
33693c7a 3705 .need_reset = qla4_8xxx_need_reset,
f8086f4f 3706 .reset_chip = qla4_82xx_isp_reset,
f4f5df23 3707 .reset_firmware = qla4_8xxx_stop_firmware,
f8086f4f
VC
3708 .queue_iocb = qla4_82xx_queue_iocb,
3709 .complete_iocb = qla4_82xx_complete_iocb,
3710 .rd_shdw_req_q_out = qla4_82xx_rd_shdw_req_q_out,
3711 .rd_shdw_rsp_q_in = qla4_82xx_rd_shdw_rsp_q_in,
f4f5df23 3712 .get_sys_info = qla4_8xxx_get_sys_info,
33693c7a
VC
3713 .rd_reg_direct = qla4_82xx_rd_32,
3714 .wr_reg_direct = qla4_82xx_wr_32,
3715 .rd_reg_indirect = qla4_82xx_md_rd_32,
3716 .wr_reg_indirect = qla4_82xx_md_wr_32,
3717 .idc_lock = qla4_82xx_idc_lock,
3718 .idc_unlock = qla4_82xx_idc_unlock,
3719 .rom_lock_recovery = qla4_82xx_rom_lock_recovery,
3720 .queue_mailbox_command = qla4_82xx_queue_mbox_cmd,
3721 .process_mailbox_interrupt = qla4_82xx_process_mbox_intr,
f4f5df23
VC
3722};
3723
6e7b4292
VC
3724static struct isp_operations qla4_83xx_isp_ops = {
3725 .iospace_config = qla4_8xxx_iospace_config,
3726 .pci_config = qla4_8xxx_pci_config,
3727 .disable_intrs = qla4_83xx_disable_intrs,
3728 .enable_intrs = qla4_83xx_enable_intrs,
3729 .start_firmware = qla4_8xxx_load_risc,
3730 .restart_firmware = qla4_83xx_start_firmware,
3731 .intr_handler = qla4_83xx_intr_handler,
3732 .interrupt_service_routine = qla4_83xx_interrupt_service_routine,
3733 .need_reset = qla4_8xxx_need_reset,
3734 .reset_chip = qla4_83xx_isp_reset,
3735 .reset_firmware = qla4_8xxx_stop_firmware,
3736 .queue_iocb = qla4_83xx_queue_iocb,
3737 .complete_iocb = qla4_83xx_complete_iocb,
3738 .rd_shdw_req_q_out = qla4_83xx_rd_shdw_req_q_out,
3739 .rd_shdw_rsp_q_in = qla4_83xx_rd_shdw_rsp_q_in,
3740 .get_sys_info = qla4_8xxx_get_sys_info,
3741 .rd_reg_direct = qla4_83xx_rd_reg,
3742 .wr_reg_direct = qla4_83xx_wr_reg,
3743 .rd_reg_indirect = qla4_83xx_rd_reg_indirect,
3744 .wr_reg_indirect = qla4_83xx_wr_reg_indirect,
3745 .idc_lock = qla4_83xx_drv_lock,
3746 .idc_unlock = qla4_83xx_drv_unlock,
3747 .rom_lock_recovery = qla4_83xx_rom_lock_recovery,
3748 .queue_mailbox_command = qla4_83xx_queue_mbox_cmd,
3749 .process_mailbox_interrupt = qla4_83xx_process_mbox_intr,
f4f5df23
VC
3750};
3751
3752uint16_t qla4xxx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3753{
3754 return (uint16_t)le32_to_cpu(ha->shadow_regs->req_q_out);
3755}
3756
f8086f4f 3757uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
f4f5df23 3758{
7664a1fd 3759 return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
f4f5df23
VC
3760}
3761
6e7b4292
VC
3762uint16_t qla4_83xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
3763{
3764 return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->req_q_out));
f4f5df23
VC
3765}
3766
3767uint16_t qla4xxx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3768{
3769 return (uint16_t)le32_to_cpu(ha->shadow_regs->rsp_q_in);
3770}
3771
f8086f4f 3772uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
f4f5df23 3773{
7664a1fd 3774 return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
f4f5df23
VC
3775}
3776
6e7b4292
VC
3777uint16_t qla4_83xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
3778{
3779 return (uint16_t)le32_to_cpu(readl(&ha->qla4_83xx_reg->rsp_q_in));
f4f5df23
VC
3780}
3781
2a991c21
MR
3782static ssize_t qla4xxx_show_boot_eth_info(void *data, int type, char *buf)
3783{
3784 struct scsi_qla_host *ha = data;
3785 char *str = buf;
3786 int rc;
3787
3788 switch (type) {
3789 case ISCSI_BOOT_ETH_FLAGS:
3790 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3791 break;
3792 case ISCSI_BOOT_ETH_INDEX:
3793 rc = sprintf(str, "0\n");
3794 break;
3795 case ISCSI_BOOT_ETH_MAC:
3796 rc = sysfs_format_mac(str, ha->my_mac,
3797 MAC_ADDR_LEN);
3798 break;
3799 default:
3800 rc = -ENOSYS;
3801 break;
3802 }
3803 return rc;
3804}
3805
587a1f16 3806static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
2a991c21
MR
3807{
3808 int rc;
3809
3810 switch (type) {
3811 case ISCSI_BOOT_ETH_FLAGS:
3812 case ISCSI_BOOT_ETH_MAC:
3813 case ISCSI_BOOT_ETH_INDEX:
3814 rc = S_IRUGO;
3815 break;
3816 default:
3817 rc = 0;
3818 break;
3819 }
3820 return rc;
3821}
3822
3823static ssize_t qla4xxx_show_boot_ini_info(void *data, int type, char *buf)
3824{
3825 struct scsi_qla_host *ha = data;
3826 char *str = buf;
3827 int rc;
3828
3829 switch (type) {
3830 case ISCSI_BOOT_INI_INITIATOR_NAME:
3831 rc = sprintf(str, "%s\n", ha->name_string);
3832 break;
3833 default:
3834 rc = -ENOSYS;
3835 break;
3836 }
3837 return rc;
3838}
3839
587a1f16 3840static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
2a991c21
MR
3841{
3842 int rc;
3843
3844 switch (type) {
3845 case ISCSI_BOOT_INI_INITIATOR_NAME:
3846 rc = S_IRUGO;
3847 break;
3848 default:
3849 rc = 0;
3850 break;
3851 }
3852 return rc;
3853}
3854
3855static ssize_t
3856qla4xxx_show_boot_tgt_info(struct ql4_boot_session_info *boot_sess, int type,
3857 char *buf)
3858{
3859 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
3860 char *str = buf;
3861 int rc;
3862
3863 switch (type) {
3864 case ISCSI_BOOT_TGT_NAME:
3865 rc = sprintf(buf, "%s\n", (char *)&boot_sess->target_name);
3866 break;
3867 case ISCSI_BOOT_TGT_IP_ADDR:
3868 if (boot_sess->conn_list[0].dest_ipaddr.ip_type == 0x1)
3869 rc = sprintf(buf, "%pI4\n",
3870 &boot_conn->dest_ipaddr.ip_address);
3871 else
3872 rc = sprintf(str, "%pI6\n",
3873 &boot_conn->dest_ipaddr.ip_address);
3874 break;
3875 case ISCSI_BOOT_TGT_PORT:
3876 rc = sprintf(str, "%d\n", boot_conn->dest_port);
3877 break;
3878 case ISCSI_BOOT_TGT_CHAP_NAME:
3879 rc = sprintf(str, "%.*s\n",
3880 boot_conn->chap.target_chap_name_length,
3881 (char *)&boot_conn->chap.target_chap_name);
3882 break;
3883 case ISCSI_BOOT_TGT_CHAP_SECRET:
3884 rc = sprintf(str, "%.*s\n",
3885 boot_conn->chap.target_secret_length,
3886 (char *)&boot_conn->chap.target_secret);
3887 break;
3888 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3889 rc = sprintf(str, "%.*s\n",
3890 boot_conn->chap.intr_chap_name_length,
3891 (char *)&boot_conn->chap.intr_chap_name);
3892 break;
3893 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3894 rc = sprintf(str, "%.*s\n",
3895 boot_conn->chap.intr_secret_length,
3896 (char *)&boot_conn->chap.intr_secret);
3897 break;
3898 case ISCSI_BOOT_TGT_FLAGS:
3899 rc = sprintf(str, "%d\n", SYSFS_FLAG_FW_SEL_BOOT);
3900 break;
3901 case ISCSI_BOOT_TGT_NIC_ASSOC:
3902 rc = sprintf(str, "0\n");
3903 break;
3904 default:
3905 rc = -ENOSYS;
3906 break;
3907 }
3908 return rc;
3909}
3910
3911static ssize_t qla4xxx_show_boot_tgt_pri_info(void *data, int type, char *buf)
3912{
3913 struct scsi_qla_host *ha = data;
3914 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_pri_sess);
3915
3916 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3917}
3918
3919static ssize_t qla4xxx_show_boot_tgt_sec_info(void *data, int type, char *buf)
3920{
3921 struct scsi_qla_host *ha = data;
3922 struct ql4_boot_session_info *boot_sess = &(ha->boot_tgt.boot_sec_sess);
3923
3924 return qla4xxx_show_boot_tgt_info(boot_sess, type, buf);
3925}
3926
587a1f16 3927static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
2a991c21
MR
3928{
3929 int rc;
3930
3931 switch (type) {
3932 case ISCSI_BOOT_TGT_NAME:
3933 case ISCSI_BOOT_TGT_IP_ADDR:
3934 case ISCSI_BOOT_TGT_PORT:
3935 case ISCSI_BOOT_TGT_CHAP_NAME:
3936 case ISCSI_BOOT_TGT_CHAP_SECRET:
3937 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
3938 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
3939 case ISCSI_BOOT_TGT_NIC_ASSOC:
3940 case ISCSI_BOOT_TGT_FLAGS:
3941 rc = S_IRUGO;
3942 break;
3943 default:
3944 rc = 0;
3945 break;
3946 }
3947 return rc;
3948}
3949
3950static void qla4xxx_boot_release(void *data)
3951{
3952 struct scsi_qla_host *ha = data;
3953
3954 scsi_host_put(ha->host);
3955}
3956
3957static int get_fw_boot_info(struct scsi_qla_host *ha, uint16_t ddb_index[])
3958{
3959 dma_addr_t buf_dma;
3960 uint32_t addr, pri_addr, sec_addr;
3961 uint32_t offset;
3962 uint16_t func_num;
3963 uint8_t val;
3964 uint8_t *buf = NULL;
3965 size_t size = 13 * sizeof(uint8_t);
3966 int ret = QLA_SUCCESS;
3967
3968 func_num = PCI_FUNC(ha->pdev->devfn);
3969
0d5b36b8
MR
3970 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
3971 __func__, ha->pdev->device, func_num);
2a991c21 3972
0d5b36b8 3973 if (is_qla40XX(ha)) {
2a991c21
MR
3974 if (func_num == 1) {
3975 addr = NVRAM_PORT0_BOOT_MODE;
3976 pri_addr = NVRAM_PORT0_BOOT_PRI_TGT;
3977 sec_addr = NVRAM_PORT0_BOOT_SEC_TGT;
3978 } else if (func_num == 3) {
3979 addr = NVRAM_PORT1_BOOT_MODE;
3980 pri_addr = NVRAM_PORT1_BOOT_PRI_TGT;
3981 sec_addr = NVRAM_PORT1_BOOT_SEC_TGT;
3982 } else {
3983 ret = QLA_ERROR;
3984 goto exit_boot_info;
3985 }
3986
3987 /* Check Boot Mode */
3988 val = rd_nvram_byte(ha, addr);
3989 if (!(val & 0x07)) {
e8fb00e0
MR
3990 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
3991 "options : 0x%x\n", __func__, val));
2a991c21
MR
3992 ret = QLA_ERROR;
3993 goto exit_boot_info;
3994 }
3995
3996 /* get primary valid target index */
3997 val = rd_nvram_byte(ha, pri_addr);
3998 if (val & BIT_7)
3999 ddb_index[0] = (val & 0x7f);
2a991c21
MR
4000
4001 /* get secondary valid target index */
4002 val = rd_nvram_byte(ha, sec_addr);
4003 if (val & BIT_7)
4004 ddb_index[1] = (val & 0x7f);
2a991c21
MR
4005
4006 } else if (is_qla8022(ha)) {
4007 buf = dma_alloc_coherent(&ha->pdev->dev, size,
4008 &buf_dma, GFP_KERNEL);
4009 if (!buf) {
4010 DEBUG2(ql4_printk(KERN_ERR, ha,
4011 "%s: Unable to allocate dma buffer\n",
4012 __func__));
4013 ret = QLA_ERROR;
4014 goto exit_boot_info;
4015 }
4016
4017 if (ha->port_num == 0)
4018 offset = BOOT_PARAM_OFFSET_PORT0;
4019 else if (ha->port_num == 1)
4020 offset = BOOT_PARAM_OFFSET_PORT1;
4021 else {
4022 ret = QLA_ERROR;
4023 goto exit_boot_info_free;
4024 }
4025 addr = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_iscsi_param * 4) +
4026 offset;
4027 if (qla4xxx_get_flash(ha, buf_dma, addr,
4028 13 * sizeof(uint8_t)) != QLA_SUCCESS) {
4029 DEBUG2(ql4_printk(KERN_ERR, ha, "scsi%ld: %s: Get Flash"
0bd7f842 4030 " failed\n", ha->host_no, __func__));
2a991c21
MR
4031 ret = QLA_ERROR;
4032 goto exit_boot_info_free;
4033 }
4034 /* Check Boot Mode */
4035 if (!(buf[1] & 0x07)) {
e8fb00e0
MR
4036 DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
4037 " : 0x%x\n", buf[1]));
2a991c21
MR
4038 ret = QLA_ERROR;
4039 goto exit_boot_info_free;
4040 }
4041
4042 /* get primary valid target index */
4043 if (buf[2] & BIT_7)
4044 ddb_index[0] = buf[2] & 0x7f;
2a991c21
MR
4045
4046 /* get secondary valid target index */
4047 if (buf[11] & BIT_7)
4048 ddb_index[1] = buf[11] & 0x7f;
2a991c21
MR
4049 } else {
4050 ret = QLA_ERROR;
4051 goto exit_boot_info;
4052 }
4053
4054 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary target ID %d, Secondary"
4055 " target ID %d\n", __func__, ddb_index[0],
4056 ddb_index[1]));
4057
4058exit_boot_info_free:
4059 dma_free_coherent(&ha->pdev->dev, size, buf, buf_dma);
4060exit_boot_info:
20e835b4
LC
4061 ha->pri_ddb_idx = ddb_index[0];
4062 ha->sec_ddb_idx = ddb_index[1];
2a991c21
MR
4063 return ret;
4064}
4065
28deb45c
LC
4066/**
4067 * qla4xxx_get_bidi_chap - Get a BIDI CHAP user and password
4068 * @ha: pointer to adapter structure
4069 * @username: CHAP username to be returned
4070 * @password: CHAP password to be returned
4071 *
4072 * If a boot entry has BIDI CHAP enabled then we need to set the BIDI CHAP
4073 * user and password in the sysfs entry in /sys/firmware/iscsi_boot#/.
4074 * So from the CHAP cache find the first BIDI CHAP entry and set it
4075 * to the boot record in sysfs.
4076 **/
4077static int qla4xxx_get_bidi_chap(struct scsi_qla_host *ha, char *username,
4078 char *password)
4079{
4080 int i, ret = -EINVAL;
4081 int max_chap_entries = 0;
4082 struct ql4_chap_table *chap_table;
4083
4084 if (is_qla8022(ha))
4085 max_chap_entries = (ha->hw.flt_chap_size / 2) /
4086 sizeof(struct ql4_chap_table);
4087 else
4088 max_chap_entries = MAX_CHAP_ENTRIES_40XX;
4089
4090 if (!ha->chap_list) {
4091 ql4_printk(KERN_ERR, ha, "Do not have CHAP table cache\n");
4092 return ret;
4093 }
4094
4095 mutex_lock(&ha->chap_sem);
4096 for (i = 0; i < max_chap_entries; i++) {
4097 chap_table = (struct ql4_chap_table *)ha->chap_list + i;
4098 if (chap_table->cookie !=
4099 __constant_cpu_to_le16(CHAP_VALID_COOKIE)) {
4100 continue;
4101 }
4102
4103 if (chap_table->flags & BIT_7) /* local */
4104 continue;
4105
4106 if (!(chap_table->flags & BIT_6)) /* Not BIDI */
4107 continue;
4108
4109 strncpy(password, chap_table->secret, QL4_CHAP_MAX_SECRET_LEN);
4110 strncpy(username, chap_table->name, QL4_CHAP_MAX_NAME_LEN);
4111 ret = 0;
4112 break;
4113 }
4114 mutex_unlock(&ha->chap_sem);
4115
4116 return ret;
4117}
4118
4119
2a991c21
MR
4120static int qla4xxx_get_boot_target(struct scsi_qla_host *ha,
4121 struct ql4_boot_session_info *boot_sess,
4122 uint16_t ddb_index)
4123{
4124 struct ql4_conn_info *boot_conn = &boot_sess->conn_list[0];
4125 struct dev_db_entry *fw_ddb_entry;
4126 dma_addr_t fw_ddb_entry_dma;
4127 uint16_t idx;
4128 uint16_t options;
4129 int ret = QLA_SUCCESS;
4130
4131 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
4132 &fw_ddb_entry_dma, GFP_KERNEL);
4133 if (!fw_ddb_entry) {
4134 DEBUG2(ql4_printk(KERN_ERR, ha,
4135 "%s: Unable to allocate dma buffer.\n",
4136 __func__));
4137 ret = QLA_ERROR;
4138 return ret;
4139 }
4140
4141 if (qla4xxx_bootdb_by_index(ha, fw_ddb_entry,
4142 fw_ddb_entry_dma, ddb_index)) {
e8fb00e0
MR
4143 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
4144 "index [%d]\n", __func__, ddb_index));
2a991c21
MR
4145 ret = QLA_ERROR;
4146 goto exit_boot_target;
4147 }
4148
4149 /* Update target name and IP from DDB */
4150 memcpy(boot_sess->target_name, fw_ddb_entry->iscsi_name,
4151 min(sizeof(boot_sess->target_name),
4152 sizeof(fw_ddb_entry->iscsi_name)));
4153
4154 options = le16_to_cpu(fw_ddb_entry->options);
4155 if (options & DDB_OPT_IPV6_DEVICE) {
4156 memcpy(&boot_conn->dest_ipaddr.ip_address,
4157 &fw_ddb_entry->ip_addr[0], IPv6_ADDR_LEN);
4158 } else {
4159 boot_conn->dest_ipaddr.ip_type = 0x1;
4160 memcpy(&boot_conn->dest_ipaddr.ip_address,
4161 &fw_ddb_entry->ip_addr[0], IP_ADDR_LEN);
4162 }
4163
4164 boot_conn->dest_port = le16_to_cpu(fw_ddb_entry->port);
4165
4166 /* update chap information */
4167 idx = __le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
4168
4169 if (BIT_7 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
4170
4171 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting chap\n"));
4172
4173 ret = qla4xxx_get_chap(ha, (char *)&boot_conn->chap.
4174 target_chap_name,
4175 (char *)&boot_conn->chap.target_secret,
4176 idx);
4177 if (ret) {
4178 ql4_printk(KERN_ERR, ha, "Failed to set chap\n");
4179 ret = QLA_ERROR;
4180 goto exit_boot_target;
4181 }
4182
4183 boot_conn->chap.target_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
4184 boot_conn->chap.target_secret_length = QL4_CHAP_MAX_SECRET_LEN;
4185 }
4186
4187 if (BIT_4 & le16_to_cpu(fw_ddb_entry->iscsi_options)) {
4188
4189 DEBUG2(ql4_printk(KERN_INFO, ha, "Setting BIDI chap\n"));
4190
28deb45c
LC
4191 ret = qla4xxx_get_bidi_chap(ha,
4192 (char *)&boot_conn->chap.intr_chap_name,
4193 (char *)&boot_conn->chap.intr_secret);
4194
2a991c21
MR
4195 if (ret) {
4196 ql4_printk(KERN_ERR, ha, "Failed to set BIDI chap\n");
4197 ret = QLA_ERROR;
4198 goto exit_boot_target;
4199 }
4200
4201 boot_conn->chap.intr_chap_name_length = QL4_CHAP_MAX_NAME_LEN;
4202 boot_conn->chap.intr_secret_length = QL4_CHAP_MAX_SECRET_LEN;
4203 }
4204
4205exit_boot_target:
4206 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
4207 fw_ddb_entry, fw_ddb_entry_dma);
4208 return ret;
4209}
4210
4211static int qla4xxx_get_boot_info(struct scsi_qla_host *ha)
4212{
4213 uint16_t ddb_index[2];
8de5b958
LC
4214 int ret = QLA_ERROR;
4215 int rval;
2a991c21
MR
4216
4217 memset(ddb_index, 0, sizeof(ddb_index));
8de5b958
LC
4218 ddb_index[0] = 0xffff;
4219 ddb_index[1] = 0xffff;
2a991c21
MR
4220 ret = get_fw_boot_info(ha, ddb_index);
4221 if (ret != QLA_SUCCESS) {
e8fb00e0
MR
4222 DEBUG2(ql4_printk(KERN_INFO, ha,
4223 "%s: No boot target configured.\n", __func__));
2a991c21
MR
4224 return ret;
4225 }
4226
13483730
MC
4227 if (ql4xdisablesysfsboot)
4228 return QLA_SUCCESS;
4229
8de5b958
LC
4230 if (ddb_index[0] == 0xffff)
4231 goto sec_target;
4232
4233 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
2a991c21 4234 ddb_index[0]);
8de5b958 4235 if (rval != QLA_SUCCESS) {
e8fb00e0
MR
4236 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
4237 "configured\n", __func__));
8de5b958
LC
4238 } else
4239 ret = QLA_SUCCESS;
2a991c21 4240
8de5b958
LC
4241sec_target:
4242 if (ddb_index[1] == 0xffff)
4243 goto exit_get_boot_info;
4244
4245 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_sec_sess),
2a991c21 4246 ddb_index[1]);
8de5b958 4247 if (rval != QLA_SUCCESS) {
e8fb00e0
MR
4248 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
4249 " configured\n", __func__));
8de5b958
LC
4250 } else
4251 ret = QLA_SUCCESS;
4252
4253exit_get_boot_info:
2a991c21
MR
4254 return ret;
4255}
4256
4257static int qla4xxx_setup_boot_info(struct scsi_qla_host *ha)
4258{
4259 struct iscsi_boot_kobj *boot_kobj;
4260
4261 if (qla4xxx_get_boot_info(ha) != QLA_SUCCESS)
13483730
MC
4262 return QLA_ERROR;
4263
4264 if (ql4xdisablesysfsboot) {
4265 ql4_printk(KERN_INFO, ha,
0bd7f842 4266 "%s: syfsboot disabled - driver will trigger login "
13483730
MC
4267 "and publish session for discovery .\n", __func__);
4268 return QLA_SUCCESS;
4269 }
4270
2a991c21
MR
4271
4272 ha->boot_kset = iscsi_boot_create_host_kset(ha->host->host_no);
4273 if (!ha->boot_kset)
4274 goto kset_free;
4275
4276 if (!scsi_host_get(ha->host))
4277 goto kset_free;
4278 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 0, ha,
4279 qla4xxx_show_boot_tgt_pri_info,
4280 qla4xxx_tgt_get_attr_visibility,
4281 qla4xxx_boot_release);
4282 if (!boot_kobj)
4283 goto put_host;
4284
4285 if (!scsi_host_get(ha->host))
4286 goto kset_free;
4287 boot_kobj = iscsi_boot_create_target(ha->boot_kset, 1, ha,
4288 qla4xxx_show_boot_tgt_sec_info,
4289 qla4xxx_tgt_get_attr_visibility,
4290 qla4xxx_boot_release);
4291 if (!boot_kobj)
4292 goto put_host;
4293
4294 if (!scsi_host_get(ha->host))
4295 goto kset_free;
4296 boot_kobj = iscsi_boot_create_initiator(ha->boot_kset, 0, ha,
4297 qla4xxx_show_boot_ini_info,
4298 qla4xxx_ini_get_attr_visibility,
4299 qla4xxx_boot_release);
4300 if (!boot_kobj)
4301 goto put_host;
4302
4303 if (!scsi_host_get(ha->host))
4304 goto kset_free;
4305 boot_kobj = iscsi_boot_create_ethernet(ha->boot_kset, 0, ha,
4306 qla4xxx_show_boot_eth_info,
4307 qla4xxx_eth_get_attr_visibility,
4308 qla4xxx_boot_release);
4309 if (!boot_kobj)
4310 goto put_host;
4311
13483730 4312 return QLA_SUCCESS;
2a991c21
MR
4313
4314put_host:
4315 scsi_host_put(ha->host);
4316kset_free:
4317 iscsi_boot_destroy_kset(ha->boot_kset);
4318 return -ENOMEM;
4319}
4320
4549415a
LC
4321
4322/**
4323 * qla4xxx_create chap_list - Create CHAP list from FLASH
4324 * @ha: pointer to adapter structure
4325 *
4326 * Read flash and make a list of CHAP entries, during login when a CHAP entry
4327 * is received, it will be checked in this list. If entry exist then the CHAP
4328 * entry index is set in the DDB. If CHAP entry does not exist in this list
4329 * then a new entry is added in FLASH in CHAP table and the index obtained is
4330 * used in the DDB.
4331 **/
4332static void qla4xxx_create_chap_list(struct scsi_qla_host *ha)
4333{
4334 int rval = 0;
4335 uint8_t *chap_flash_data = NULL;
4336 uint32_t offset;
4337 dma_addr_t chap_dma;
4338 uint32_t chap_size = 0;
4339
4340 if (is_qla40XX(ha))
4341 chap_size = MAX_CHAP_ENTRIES_40XX *
4342 sizeof(struct ql4_chap_table);
4343 else /* Single region contains CHAP info for both
4344 * ports which is divided into half for each port.
4345 */
4346 chap_size = ha->hw.flt_chap_size / 2;
4347
4348 chap_flash_data = dma_alloc_coherent(&ha->pdev->dev, chap_size,
4349 &chap_dma, GFP_KERNEL);
4350 if (!chap_flash_data) {
4351 ql4_printk(KERN_ERR, ha, "No memory for chap_flash_data\n");
4352 return;
4353 }
4354 if (is_qla40XX(ha))
4355 offset = FLASH_CHAP_OFFSET;
4356 else {
4357 offset = FLASH_RAW_ACCESS_ADDR + (ha->hw.flt_region_chap << 2);
4358 if (ha->port_num == 1)
4359 offset += chap_size;
4360 }
4361
4362 rval = qla4xxx_get_flash(ha, chap_dma, offset, chap_size);
4363 if (rval != QLA_SUCCESS)
4364 goto exit_chap_list;
4365
4366 if (ha->chap_list == NULL)
4367 ha->chap_list = vmalloc(chap_size);
4368 if (ha->chap_list == NULL) {
4369 ql4_printk(KERN_ERR, ha, "No memory for ha->chap_list\n");
4370 goto exit_chap_list;
4371 }
4372
4373 memcpy(ha->chap_list, chap_flash_data, chap_size);
4374
4375exit_chap_list:
4376 dma_free_coherent(&ha->pdev->dev, chap_size,
4377 chap_flash_data, chap_dma);
4549415a
LC
4378}
4379
13483730
MC
4380static void qla4xxx_get_param_ddb(struct ddb_entry *ddb_entry,
4381 struct ql4_tuple_ddb *tddb)
4382{
4383 struct scsi_qla_host *ha;
4384 struct iscsi_cls_session *cls_sess;
4385 struct iscsi_cls_conn *cls_conn;
4386 struct iscsi_session *sess;
4387 struct iscsi_conn *conn;
4388
4389 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
4390 ha = ddb_entry->ha;
4391 cls_sess = ddb_entry->sess;
4392 sess = cls_sess->dd_data;
4393 cls_conn = ddb_entry->conn;
4394 conn = cls_conn->dd_data;
4395
4396 tddb->tpgt = sess->tpgt;
4397 tddb->port = conn->persistent_port;
4398 strncpy(tddb->iscsi_name, sess->targetname, ISCSI_NAME_SIZE);
4399 strncpy(tddb->ip_addr, conn->persistent_address, DDB_IPADDR_LEN);
4400}
4401
4402static void qla4xxx_convert_param_ddb(struct dev_db_entry *fw_ddb_entry,
1cb78d73
VC
4403 struct ql4_tuple_ddb *tddb,
4404 uint8_t *flash_isid)
13483730
MC
4405{
4406 uint16_t options = 0;
4407
4408 tddb->tpgt = le32_to_cpu(fw_ddb_entry->tgt_portal_grp);
4409 memcpy(&tddb->iscsi_name[0], &fw_ddb_entry->iscsi_name[0],
4410 min(sizeof(tddb->iscsi_name), sizeof(fw_ddb_entry->iscsi_name)));
4411
4412 options = le16_to_cpu(fw_ddb_entry->options);
4413 if (options & DDB_OPT_IPV6_DEVICE)
4414 sprintf(tddb->ip_addr, "%pI6", fw_ddb_entry->ip_addr);
4415 else
4416 sprintf(tddb->ip_addr, "%pI4", fw_ddb_entry->ip_addr);
4417
4418 tddb->port = le16_to_cpu(fw_ddb_entry->port);
1cb78d73
VC
4419
4420 if (flash_isid == NULL)
4421 memcpy(&tddb->isid[0], &fw_ddb_entry->isid[0],
4422 sizeof(tddb->isid));
4423 else
4424 memcpy(&tddb->isid[0], &flash_isid[0], sizeof(tddb->isid));
13483730
MC
4425}
4426
4427static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
4428 struct ql4_tuple_ddb *old_tddb,
173269ef
MR
4429 struct ql4_tuple_ddb *new_tddb,
4430 uint8_t is_isid_compare)
13483730
MC
4431{
4432 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
4433 return QLA_ERROR;
4434
4435 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr))
4436 return QLA_ERROR;
4437
4438 if (old_tddb->port != new_tddb->port)
4439 return QLA_ERROR;
4440
173269ef 4441 /* For multi sessions, driver generates the ISID, so do not compare
59e13d48 4442 * ISID in reset path since it would be a comparison between the
173269ef
MR
4443 * driver generated ISID and firmware generated ISID. This could
4444 * lead to adding duplicated DDBs in the list as driver generated
4445 * ISID would not match firmware generated ISID.
4446 */
4447 if (is_isid_compare) {
4448 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: old ISID [%02x%02x%02x"
4449 "%02x%02x%02x] New ISID [%02x%02x%02x%02x%02x%02x]\n",
4450 __func__, old_tddb->isid[5], old_tddb->isid[4],
4451 old_tddb->isid[3], old_tddb->isid[2], old_tddb->isid[1],
4452 old_tddb->isid[0], new_tddb->isid[5], new_tddb->isid[4],
4453 new_tddb->isid[3], new_tddb->isid[2], new_tddb->isid[1],
4454 new_tddb->isid[0]));
4455
4456 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
4457 sizeof(old_tddb->isid)))
4458 return QLA_ERROR;
4459 }
4460
13483730
MC
4461 DEBUG2(ql4_printk(KERN_INFO, ha,
4462 "Match Found, fw[%d,%d,%s,%s], [%d,%d,%s,%s]",
4463 old_tddb->port, old_tddb->tpgt, old_tddb->ip_addr,
4464 old_tddb->iscsi_name, new_tddb->port, new_tddb->tpgt,
4465 new_tddb->ip_addr, new_tddb->iscsi_name));
4466
4467 return QLA_SUCCESS;
4468}
4469
4470static int qla4xxx_is_session_exists(struct scsi_qla_host *ha,
4471 struct dev_db_entry *fw_ddb_entry)
4472{
4473 struct ddb_entry *ddb_entry;
4474 struct ql4_tuple_ddb *fw_tddb = NULL;
4475 struct ql4_tuple_ddb *tmp_tddb = NULL;
4476 int idx;
4477 int ret = QLA_ERROR;
4478
4479 fw_tddb = vzalloc(sizeof(*fw_tddb));
4480 if (!fw_tddb) {
4481 DEBUG2(ql4_printk(KERN_WARNING, ha,
4482 "Memory Allocation failed.\n"));
4483 ret = QLA_SUCCESS;
4484 goto exit_check;
4485 }
4486
4487 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
4488 if (!tmp_tddb) {
4489 DEBUG2(ql4_printk(KERN_WARNING, ha,
4490 "Memory Allocation failed.\n"));
4491 ret = QLA_SUCCESS;
4492 goto exit_check;
4493 }
4494
1cb78d73 4495 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
13483730
MC
4496
4497 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
4498 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
4499 if (ddb_entry == NULL)
4500 continue;
4501
4502 qla4xxx_get_param_ddb(ddb_entry, tmp_tddb);
173269ef 4503 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
13483730
MC
4504 ret = QLA_SUCCESS; /* found */
4505 goto exit_check;
4506 }
4507 }
4508
4509exit_check:
4510 if (fw_tddb)
4511 vfree(fw_tddb);
4512 if (tmp_tddb)
4513 vfree(tmp_tddb);
4514 return ret;
4515}
4516
1cb78d73
VC
4517/**
4518 * qla4xxx_check_existing_isid - check if target with same isid exist
4519 * in target list
4520 * @list_nt: list of target
4521 * @isid: isid to check
4522 *
4523 * This routine return QLA_SUCCESS if target with same isid exist
4524 **/
4525static int qla4xxx_check_existing_isid(struct list_head *list_nt, uint8_t *isid)
4526{
4527 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
4528 struct dev_db_entry *fw_ddb_entry;
4529
4530 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4531 fw_ddb_entry = &nt_ddb_idx->fw_ddb;
4532
4533 if (memcmp(&fw_ddb_entry->isid[0], &isid[0],
4534 sizeof(nt_ddb_idx->fw_ddb.isid)) == 0) {
4535 return QLA_SUCCESS;
4536 }
4537 }
4538 return QLA_ERROR;
4539}
4540
4541/**
4542 * qla4xxx_update_isid - compare ddbs and updated isid
4543 * @ha: Pointer to host adapter structure.
4544 * @list_nt: list of nt target
4545 * @fw_ddb_entry: firmware ddb entry
4546 *
4547 * This routine update isid if ddbs have same iqn, same isid and
4548 * different IP addr.
4549 * Return QLA_SUCCESS if isid is updated.
4550 **/
4551static int qla4xxx_update_isid(struct scsi_qla_host *ha,
4552 struct list_head *list_nt,
4553 struct dev_db_entry *fw_ddb_entry)
4554{
4555 uint8_t base_value, i;
4556
4557 base_value = fw_ddb_entry->isid[1] & 0x1f;
4558 for (i = 0; i < 8; i++) {
4559 fw_ddb_entry->isid[1] = (base_value | (i << 5));
4560 if (qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
4561 break;
4562 }
4563
4564 if (!qla4xxx_check_existing_isid(list_nt, fw_ddb_entry->isid))
4565 return QLA_ERROR;
4566
4567 return QLA_SUCCESS;
4568}
4569
4570/**
4571 * qla4xxx_should_update_isid - check if isid need to update
4572 * @ha: Pointer to host adapter structure.
4573 * @old_tddb: ddb tuple
4574 * @new_tddb: ddb tuple
4575 *
4576 * Return QLA_SUCCESS if different IP, different PORT, same iqn,
4577 * same isid
4578 **/
4579static int qla4xxx_should_update_isid(struct scsi_qla_host *ha,
4580 struct ql4_tuple_ddb *old_tddb,
4581 struct ql4_tuple_ddb *new_tddb)
4582{
4583 if (strcmp(old_tddb->ip_addr, new_tddb->ip_addr) == 0) {
4584 /* Same ip */
4585 if (old_tddb->port == new_tddb->port)
4586 return QLA_ERROR;
4587 }
4588
4589 if (strcmp(old_tddb->iscsi_name, new_tddb->iscsi_name))
4590 /* different iqn */
4591 return QLA_ERROR;
4592
4593 if (memcmp(&old_tddb->isid[0], &new_tddb->isid[0],
4594 sizeof(old_tddb->isid)))
4595 /* different isid */
4596 return QLA_ERROR;
4597
4598 return QLA_SUCCESS;
4599}
4600
4601/**
4602 * qla4xxx_is_flash_ddb_exists - check if fw_ddb_entry already exists in list_nt
4603 * @ha: Pointer to host adapter structure.
4604 * @list_nt: list of nt target.
4605 * @fw_ddb_entry: firmware ddb entry.
4606 *
4607 * This routine check if fw_ddb_entry already exists in list_nt to avoid
4608 * duplicate ddb in list_nt.
4609 * Return QLA_SUCCESS if duplicate ddb exit in list_nl.
4610 * Note: This function also update isid of DDB if required.
4611 **/
4612
13483730
MC
4613static int qla4xxx_is_flash_ddb_exists(struct scsi_qla_host *ha,
4614 struct list_head *list_nt,
4615 struct dev_db_entry *fw_ddb_entry)
4616{
4617 struct qla_ddb_index *nt_ddb_idx, *nt_ddb_idx_tmp;
4618 struct ql4_tuple_ddb *fw_tddb = NULL;
4619 struct ql4_tuple_ddb *tmp_tddb = NULL;
1cb78d73 4620 int rval, ret = QLA_ERROR;
13483730
MC
4621
4622 fw_tddb = vzalloc(sizeof(*fw_tddb));
4623 if (!fw_tddb) {
4624 DEBUG2(ql4_printk(KERN_WARNING, ha,
4625 "Memory Allocation failed.\n"));
4626 ret = QLA_SUCCESS;
4627 goto exit_check;
4628 }
4629
4630 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
4631 if (!tmp_tddb) {
4632 DEBUG2(ql4_printk(KERN_WARNING, ha,
4633 "Memory Allocation failed.\n"));
4634 ret = QLA_SUCCESS;
4635 goto exit_check;
4636 }
4637
1cb78d73 4638 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
13483730
MC
4639
4640 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
1cb78d73
VC
4641 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb,
4642 nt_ddb_idx->flash_isid);
4643 ret = qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, true);
4644 /* found duplicate ddb */
4645 if (ret == QLA_SUCCESS)
4646 goto exit_check;
4647 }
4648
4649 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
4650 qla4xxx_convert_param_ddb(&nt_ddb_idx->fw_ddb, tmp_tddb, NULL);
4651
4652 ret = qla4xxx_should_update_isid(ha, tmp_tddb, fw_tddb);
4653 if (ret == QLA_SUCCESS) {
4654 rval = qla4xxx_update_isid(ha, list_nt, fw_ddb_entry);
4655 if (rval == QLA_SUCCESS)
4656 ret = QLA_ERROR;
4657 else
4658 ret = QLA_SUCCESS;
4659
13483730
MC
4660 goto exit_check;
4661 }
4662 }
4663
4664exit_check:
4665 if (fw_tddb)
4666 vfree(fw_tddb);
4667 if (tmp_tddb)
4668 vfree(tmp_tddb);
4669 return ret;
4670}
4671
4a4bc2e9 4672static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
13483730 4673{
4a4bc2e9 4674 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
13483730 4675
4a4bc2e9
LC
4676 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4677 list_del_init(&ddb_idx->list);
4678 vfree(ddb_idx);
13483730 4679 }
13483730
MC
4680}
4681
4682static struct iscsi_endpoint *qla4xxx_get_ep_fwdb(struct scsi_qla_host *ha,
4683 struct dev_db_entry *fw_ddb_entry)
4684{
4685 struct iscsi_endpoint *ep;
4686 struct sockaddr_in *addr;
4687 struct sockaddr_in6 *addr6;
4688 struct sockaddr *dst_addr;
4689 char *ip;
4690
4691 /* TODO: need to destroy on unload iscsi_endpoint*/
4692 dst_addr = vmalloc(sizeof(*dst_addr));
4693 if (!dst_addr)
4694 return NULL;
4695
4696 if (fw_ddb_entry->options & DDB_OPT_IPV6_DEVICE) {
4697 dst_addr->sa_family = AF_INET6;
4698 addr6 = (struct sockaddr_in6 *)dst_addr;
4699 ip = (char *)&addr6->sin6_addr;
4700 memcpy(ip, fw_ddb_entry->ip_addr, IPv6_ADDR_LEN);
4701 addr6->sin6_port = htons(le16_to_cpu(fw_ddb_entry->port));
4702
4703 } else {
4704 dst_addr->sa_family = AF_INET;
4705 addr = (struct sockaddr_in *)dst_addr;
4706 ip = (char *)&addr->sin_addr;
4707 memcpy(ip, fw_ddb_entry->ip_addr, IP_ADDR_LEN);
4708 addr->sin_port = htons(le16_to_cpu(fw_ddb_entry->port));
4709 }
4710
4711 ep = qla4xxx_ep_connect(ha->host, dst_addr, 0);
4712 vfree(dst_addr);
4713 return ep;
4714}
4715
4716static int qla4xxx_verify_boot_idx(struct scsi_qla_host *ha, uint16_t idx)
4717{
4718 if (ql4xdisablesysfsboot)
4719 return QLA_SUCCESS;
4720 if (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx)
4721 return QLA_ERROR;
4722 return QLA_SUCCESS;
4723}
4724
4725static void qla4xxx_setup_flash_ddb_entry(struct scsi_qla_host *ha,
4726 struct ddb_entry *ddb_entry)
4727{
c28eaaca
NJ
4728 uint16_t def_timeout;
4729
13483730
MC
4730 ddb_entry->ddb_type = FLASH_DDB;
4731 ddb_entry->fw_ddb_index = INVALID_ENTRY;
4732 ddb_entry->fw_ddb_device_state = DDB_DS_NO_CONNECTION_ACTIVE;
4733 ddb_entry->ha = ha;
4734 ddb_entry->unblock_sess = qla4xxx_unblock_flash_ddb;
4735 ddb_entry->ddb_change = qla4xxx_flash_ddb_change;
4736
4737 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
4738 atomic_set(&ddb_entry->relogin_timer, 0);
4739 atomic_set(&ddb_entry->relogin_retry_count, 0);
c28eaaca 4740 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
13483730 4741 ddb_entry->default_relogin_timeout =
c28eaaca
NJ
4742 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
4743 def_timeout : LOGIN_TOV;
13483730
MC
4744 ddb_entry->default_time2wait =
4745 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
4746}
4747
4748static void qla4xxx_wait_for_ip_configuration(struct scsi_qla_host *ha)
4749{
4750 uint32_t idx = 0;
4751 uint32_t ip_idx[IP_ADDR_COUNT] = {0, 1, 2, 3}; /* 4 IP interfaces */
4752 uint32_t sts[MBOX_REG_COUNT];
4753 uint32_t ip_state;
4754 unsigned long wtime;
4755 int ret;
4756
4757 wtime = jiffies + (HZ * IP_CONFIG_TOV);
4758 do {
4759 for (idx = 0; idx < IP_ADDR_COUNT; idx++) {
4760 if (ip_idx[idx] == -1)
4761 continue;
4762
4763 ret = qla4xxx_get_ip_state(ha, 0, ip_idx[idx], sts);
4764
4765 if (ret == QLA_ERROR) {
4766 ip_idx[idx] = -1;
4767 continue;
4768 }
4769
4770 ip_state = (sts[1] & IP_STATE_MASK) >> IP_STATE_SHIFT;
4771
4772 DEBUG2(ql4_printk(KERN_INFO, ha,
4773 "Waiting for IP state for idx = %d, state = 0x%x\n",
4774 ip_idx[idx], ip_state));
4775 if (ip_state == IP_ADDRSTATE_UNCONFIGURED ||
4776 ip_state == IP_ADDRSTATE_INVALID ||
4777 ip_state == IP_ADDRSTATE_PREFERRED ||
4778 ip_state == IP_ADDRSTATE_DEPRICATED ||
4779 ip_state == IP_ADDRSTATE_DISABLING)
4780 ip_idx[idx] = -1;
13483730
MC
4781 }
4782
4783 /* Break if all IP states checked */
4784 if ((ip_idx[0] == -1) &&
4785 (ip_idx[1] == -1) &&
4786 (ip_idx[2] == -1) &&
4787 (ip_idx[3] == -1))
4788 break;
4789 schedule_timeout_uninterruptible(HZ);
4790 } while (time_after(wtime, jiffies));
4791}
4792
4a4bc2e9
LC
4793static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
4794 struct list_head *list_st)
13483730 4795{
4a4bc2e9 4796 struct qla_ddb_index *st_ddb_idx;
13483730 4797 int max_ddbs;
4a4bc2e9
LC
4798 int fw_idx_size;
4799 struct dev_db_entry *fw_ddb_entry;
4800 dma_addr_t fw_ddb_dma;
13483730
MC
4801 int ret;
4802 uint32_t idx = 0, next_idx = 0;
4803 uint32_t state = 0, conn_err = 0;
4a4bc2e9 4804 uint16_t conn_id = 0;
13483730
MC
4805
4806 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4807 &fw_ddb_dma);
4808 if (fw_ddb_entry == NULL) {
4809 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4a4bc2e9 4810 goto exit_st_list;
13483730
MC
4811 }
4812
4a4bc2e9
LC
4813 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4814 MAX_DEV_DB_ENTRIES;
13483730
MC
4815 fw_idx_size = sizeof(struct qla_ddb_index);
4816
4817 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4a4bc2e9
LC
4818 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4819 NULL, &next_idx, &state,
4820 &conn_err, NULL, &conn_id);
13483730
MC
4821 if (ret == QLA_ERROR)
4822 break;
4823
981c982c
LC
4824 /* Ignore DDB if invalid state (unassigned) */
4825 if (state == DDB_DS_UNASSIGNED)
4826 goto continue_next_st;
4827
13483730
MC
4828 /* Check if ST, add to the list_st */
4829 if (strlen((char *) fw_ddb_entry->iscsi_name) != 0)
4830 goto continue_next_st;
4831
4832 st_ddb_idx = vzalloc(fw_idx_size);
4833 if (!st_ddb_idx)
4834 break;
4835
4836 st_ddb_idx->fw_ddb_idx = idx;
4837
4a4bc2e9 4838 list_add_tail(&st_ddb_idx->list, list_st);
13483730
MC
4839continue_next_st:
4840 if (next_idx == 0)
4841 break;
4842 }
4843
4a4bc2e9
LC
4844exit_st_list:
4845 if (fw_ddb_entry)
4846 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4847}
4848
4849/**
4850 * qla4xxx_remove_failed_ddb - Remove inactive or failed ddb from list
4851 * @ha: pointer to adapter structure
4852 * @list_ddb: List from which failed ddb to be removed
4853 *
4854 * Iterate over the list of DDBs and find and remove DDBs that are either in
4855 * no connection active state or failed state
4856 **/
4857static void qla4xxx_remove_failed_ddb(struct scsi_qla_host *ha,
4858 struct list_head *list_ddb)
4859{
4860 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
4861 uint32_t next_idx = 0;
4862 uint32_t state = 0, conn_err = 0;
4863 int ret;
4864
4865 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
4866 ret = qla4xxx_get_fwddb_entry(ha, ddb_idx->fw_ddb_idx,
4867 NULL, 0, NULL, &next_idx, &state,
4868 &conn_err, NULL, NULL);
4869 if (ret == QLA_ERROR)
4870 continue;
4871
4872 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
4873 state == DDB_DS_SESSION_FAILED) {
4874 list_del_init(&ddb_idx->list);
4875 vfree(ddb_idx);
4876 }
4877 }
4878}
4879
4880static int qla4xxx_sess_conn_setup(struct scsi_qla_host *ha,
4881 struct dev_db_entry *fw_ddb_entry,
4882 int is_reset)
4883{
4884 struct iscsi_cls_session *cls_sess;
4885 struct iscsi_session *sess;
4886 struct iscsi_cls_conn *cls_conn;
4887 struct iscsi_endpoint *ep;
4888 uint16_t cmds_max = 32;
4889 uint16_t conn_id = 0;
4890 uint32_t initial_cmdsn = 0;
4891 int ret = QLA_SUCCESS;
4892
4893 struct ddb_entry *ddb_entry = NULL;
4894
4895 /* Create session object, with INVALID_ENTRY,
4896 * the targer_id would get set when we issue the login
13483730 4897 */
4a4bc2e9
LC
4898 cls_sess = iscsi_session_setup(&qla4xxx_iscsi_transport, ha->host,
4899 cmds_max, sizeof(struct ddb_entry),
4900 sizeof(struct ql4_task_data),
4901 initial_cmdsn, INVALID_ENTRY);
4902 if (!cls_sess) {
4903 ret = QLA_ERROR;
4904 goto exit_setup;
4905 }
13483730 4906
4a4bc2e9
LC
4907 /*
4908 * so calling module_put function to decrement the
4909 * reference count.
4910 **/
4911 module_put(qla4xxx_iscsi_transport.owner);
4912 sess = cls_sess->dd_data;
4913 ddb_entry = sess->dd_data;
4914 ddb_entry->sess = cls_sess;
4915
4916 cls_sess->recovery_tmo = ql4xsess_recovery_tmo;
4917 memcpy(&ddb_entry->fw_ddb_entry, fw_ddb_entry,
4918 sizeof(struct dev_db_entry));
4919
4920 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry);
4921
4922 cls_conn = iscsi_conn_setup(cls_sess, sizeof(struct qla_conn), conn_id);
4923
4924 if (!cls_conn) {
4925 ret = QLA_ERROR;
4926 goto exit_setup;
13483730
MC
4927 }
4928
4a4bc2e9 4929 ddb_entry->conn = cls_conn;
13483730 4930
4a4bc2e9
LC
4931 /* Setup ep, for displaying attributes in sysfs */
4932 ep = qla4xxx_get_ep_fwdb(ha, fw_ddb_entry);
4933 if (ep) {
4934 ep->conn = cls_conn;
4935 cls_conn->ep = ep;
4936 } else {
4937 DEBUG2(ql4_printk(KERN_ERR, ha, "Unable to get ep\n"));
4938 ret = QLA_ERROR;
4939 goto exit_setup;
4940 }
13483730 4941
4a4bc2e9
LC
4942 /* Update sess/conn params */
4943 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
13483730 4944
4a4bc2e9
LC
4945 if (is_reset == RESET_ADAPTER) {
4946 iscsi_block_session(cls_sess);
4947 /* Use the relogin path to discover new devices
4948 * by short-circuting the logic of setting
4949 * timer to relogin - instead set the flags
4950 * to initiate login right away.
4951 */
4952 set_bit(DPC_RELOGIN_DEVICE, &ha->dpc_flags);
4953 set_bit(DF_RELOGIN, &ddb_entry->flags);
4954 }
4955
4956exit_setup:
4957 return ret;
4958}
4959
4960static void qla4xxx_build_nt_list(struct scsi_qla_host *ha,
4961 struct list_head *list_nt, int is_reset)
4962{
4963 struct dev_db_entry *fw_ddb_entry;
4964 dma_addr_t fw_ddb_dma;
4965 int max_ddbs;
4966 int fw_idx_size;
4967 int ret;
4968 uint32_t idx = 0, next_idx = 0;
4969 uint32_t state = 0, conn_err = 0;
4970 uint16_t conn_id = 0;
4971 struct qla_ddb_index *nt_ddb_idx;
4972
4973 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
4974 &fw_ddb_dma);
4975 if (fw_ddb_entry == NULL) {
4976 DEBUG2(ql4_printk(KERN_ERR, ha, "Out of memory\n"));
4977 goto exit_nt_list;
13483730 4978 }
4a4bc2e9
LC
4979 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
4980 MAX_DEV_DB_ENTRIES;
4981 fw_idx_size = sizeof(struct qla_ddb_index);
13483730
MC
4982
4983 for (idx = 0; idx < max_ddbs; idx = next_idx) {
4a4bc2e9
LC
4984 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
4985 NULL, &next_idx, &state,
4986 &conn_err, NULL, &conn_id);
13483730
MC
4987 if (ret == QLA_ERROR)
4988 break;
4989
4990 if (qla4xxx_verify_boot_idx(ha, idx) != QLA_SUCCESS)
4991 goto continue_next_nt;
4992
4993 /* Check if NT, then add to list it */
4994 if (strlen((char *) fw_ddb_entry->iscsi_name) == 0)
4995 goto continue_next_nt;
4996
4a4bc2e9
LC
4997 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
4998 state == DDB_DS_SESSION_FAILED))
4999 goto continue_next_nt;
13483730 5000
4a4bc2e9
LC
5001 DEBUG2(ql4_printk(KERN_INFO, ha,
5002 "Adding DDB to session = 0x%x\n", idx));
5003 if (is_reset == INIT_ADAPTER) {
5004 nt_ddb_idx = vmalloc(fw_idx_size);
5005 if (!nt_ddb_idx)
5006 break;
13483730 5007
4a4bc2e9 5008 nt_ddb_idx->fw_ddb_idx = idx;
13483730 5009
1cb78d73
VC
5010 /* Copy original isid as it may get updated in function
5011 * qla4xxx_update_isid(). We need original isid in
5012 * function qla4xxx_compare_tuple_ddb to find duplicate
5013 * target */
5014 memcpy(&nt_ddb_idx->flash_isid[0],
5015 &fw_ddb_entry->isid[0],
5016 sizeof(nt_ddb_idx->flash_isid));
5017
5018 ret = qla4xxx_is_flash_ddb_exists(ha, list_nt,
5019 fw_ddb_entry);
5020 if (ret == QLA_SUCCESS) {
5021 /* free nt_ddb_idx and do not add to list_nt */
4a4bc2e9
LC
5022 vfree(nt_ddb_idx);
5023 goto continue_next_nt;
13483730 5024 }
1cb78d73
VC
5025
5026 /* Copy updated isid */
5027 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
5028 sizeof(struct dev_db_entry));
5029
4a4bc2e9
LC
5030 list_add_tail(&nt_ddb_idx->list, list_nt);
5031 } else if (is_reset == RESET_ADAPTER) {
5032 if (qla4xxx_is_session_exists(ha, fw_ddb_entry) ==
5033 QLA_SUCCESS)
5034 goto continue_next_nt;
13483730 5035 }
4a4bc2e9
LC
5036
5037 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset);
5038 if (ret == QLA_ERROR)
5039 goto exit_nt_list;
5040
13483730
MC
5041continue_next_nt:
5042 if (next_idx == 0)
5043 break;
5044 }
4a4bc2e9
LC
5045
5046exit_nt_list:
13483730
MC
5047 if (fw_ddb_entry)
5048 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
4a4bc2e9
LC
5049}
5050
5051/**
5052 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
5053 * @ha: pointer to adapter structure
5054 * @is_reset: Is this init path or reset path
5055 *
5056 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
5057 * using connection open, then create the list of normal targets (nt)
5058 * from firmware DDBs. Based on the list of nt setup session and connection
5059 * objects.
5060 **/
5061void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
5062{
5063 uint16_t tmo = 0;
5064 struct list_head list_st, list_nt;
5065 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
5066 unsigned long wtime;
5067
5068 if (!test_bit(AF_LINK_UP, &ha->flags)) {
5069 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
5070 ha->is_reset = is_reset;
5071 return;
5072 }
5073
5074 INIT_LIST_HEAD(&list_st);
5075 INIT_LIST_HEAD(&list_nt);
5076
5077 qla4xxx_build_st_list(ha, &list_st);
5078
5079 /* Before issuing conn open mbox, ensure all IPs states are configured
5080 * Note, conn open fails if IPs are not configured
5081 */
5082 qla4xxx_wait_for_ip_configuration(ha);
5083
5084 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
5085 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
5086 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
5087 }
5088
5089 /* Wait to ensure all sendtargets are done for min 12 sec wait */
c28eaaca
NJ
5090 tmo = ((ha->def_timeout > LOGIN_TOV) &&
5091 (ha->def_timeout < LOGIN_TOV * 10) ?
5092 ha->def_timeout : LOGIN_TOV);
5093
4a4bc2e9
LC
5094 DEBUG2(ql4_printk(KERN_INFO, ha,
5095 "Default time to wait for build ddb %d\n", tmo));
5096
5097 wtime = jiffies + (HZ * tmo);
5098 do {
f1f2e60e
NJ
5099 if (list_empty(&list_st))
5100 break;
5101
4a4bc2e9
LC
5102 qla4xxx_remove_failed_ddb(ha, &list_st);
5103 schedule_timeout_uninterruptible(HZ / 10);
5104 } while (time_after(wtime, jiffies));
5105
5106 /* Free up the sendtargets list */
5107 qla4xxx_free_ddb_list(&list_st);
5108
5109 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
5110
5111 qla4xxx_free_ddb_list(&list_nt);
13483730
MC
5112
5113 qla4xxx_free_ddb_index(ha);
5114}
5115
afaf5a2d
DS
5116/**
5117 * qla4xxx_probe_adapter - callback function to probe HBA
5118 * @pdev: pointer to pci_dev structure
5119 * @pci_device_id: pointer to pci_device entry
5120 *
5121 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
5122 * It returns zero if successful. It also initializes all data necessary for
5123 * the driver.
5124 **/
6f039790
GKH
5125static int qla4xxx_probe_adapter(struct pci_dev *pdev,
5126 const struct pci_device_id *ent)
afaf5a2d
DS
5127{
5128 int ret = -ENODEV, status;
5129 struct Scsi_Host *host;
5130 struct scsi_qla_host *ha;
afaf5a2d
DS
5131 uint8_t init_retry_count = 0;
5132 char buf[34];
f4f5df23 5133 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
f9880e76 5134 uint32_t dev_state;
afaf5a2d
DS
5135
5136 if (pci_enable_device(pdev))
5137 return -1;
5138
b3a271a9 5139 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
afaf5a2d
DS
5140 if (host == NULL) {
5141 printk(KERN_WARNING
5142 "qla4xxx: Couldn't allocate host from scsi layer!\n");
5143 goto probe_disable_device;
5144 }
5145
5146 /* Clear our data area */
b3a271a9 5147 ha = to_qla_host(host);
afaf5a2d
DS
5148 memset(ha, 0, sizeof(*ha));
5149
5150 /* Save the information from PCI BIOS. */
5151 ha->pdev = pdev;
5152 ha->host = host;
5153 ha->host_no = host->host_no;
6e7b4292 5154 ha->func_num = PCI_FUNC(ha->pdev->devfn);
afaf5a2d 5155
2232be0d
LC
5156 pci_enable_pcie_error_reporting(pdev);
5157
f4f5df23
VC
5158 /* Setup Runtime configurable options */
5159 if (is_qla8022(ha)) {
7664a1fd 5160 ha->isp_ops = &qla4_82xx_isp_ops;
33693c7a 5161 ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
f4f5df23
VC
5162 ha->qdr_sn_window = -1;
5163 ha->ddr_mn_window = -1;
5164 ha->curr_window = 255;
f4f5df23
VC
5165 nx_legacy_intr = &legacy_intr[ha->func_num];
5166 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
5167 ha->nx_legacy_intr.tgt_status_reg =
5168 nx_legacy_intr->tgt_status_reg;
5169 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
5170 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
6e7b4292
VC
5171 } else if (is_qla8032(ha)) {
5172 ha->isp_ops = &qla4_83xx_isp_ops;
5173 ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
f4f5df23
VC
5174 } else {
5175 ha->isp_ops = &qla4xxx_isp_ops;
5176 }
5177
6e7b4292
VC
5178 if (is_qla80XX(ha)) {
5179 rwlock_init(&ha->hw_lock);
5180 ha->pf_bit = ha->func_num << 16;
5181 /* Set EEH reset type to fundamental if required by hba */
2232be0d 5182 pdev->needs_freset = 1;
6e7b4292 5183 }
2232be0d 5184
afaf5a2d 5185 /* Configure PCI I/O space. */
f4f5df23 5186 ret = ha->isp_ops->iospace_config(ha);
afaf5a2d 5187 if (ret)
f4f5df23 5188 goto probe_failed_ioconfig;
afaf5a2d 5189
c2660df3 5190 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
afaf5a2d
DS
5191 pdev->device, pdev->irq, ha->reg);
5192
5193 qla4xxx_config_dma_addressing(ha);
5194
5195 /* Initialize lists and spinlocks. */
afaf5a2d
DS
5196 INIT_LIST_HEAD(&ha->free_srb_q);
5197
5198 mutex_init(&ha->mbox_sem);
4549415a 5199 mutex_init(&ha->chap_sem);
f4f5df23 5200 init_completion(&ha->mbx_intr_comp);
95d31262 5201 init_completion(&ha->disable_acb_comp);
afaf5a2d
DS
5202
5203 spin_lock_init(&ha->hardware_lock);
8e9157c8 5204 spin_lock_init(&ha->work_lock);
afaf5a2d 5205
ff884430
VC
5206 /* Initialize work list */
5207 INIT_LIST_HEAD(&ha->work_list);
5208
afaf5a2d
DS
5209 /* Allocate dma buffers */
5210 if (qla4xxx_mem_alloc(ha)) {
c2660df3
VC
5211 ql4_printk(KERN_WARNING, ha,
5212 "[ERROR] Failed to allocate memory for adapter\n");
afaf5a2d
DS
5213
5214 ret = -ENOMEM;
5215 goto probe_failed;
5216 }
5217
b3a271a9
MR
5218 host->cmd_per_lun = 3;
5219 host->max_channel = 0;
5220 host->max_lun = MAX_LUNS - 1;
5221 host->max_id = MAX_TARGETS;
5222 host->max_cmd_len = IOCB_MAX_CDB_LEN;
5223 host->can_queue = MAX_SRBS ;
5224 host->transportt = qla4xxx_scsi_transport;
5225
5226 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
5227 if (ret) {
5228 ql4_printk(KERN_WARNING, ha,
5229 "%s: scsi_init_shared_tag_map failed\n", __func__);
5230 goto probe_failed;
5231 }
5232
5233 pci_set_drvdata(pdev, ha);
5234
5235 ret = scsi_add_host(host, &pdev->dev);
5236 if (ret)
5237 goto probe_failed;
5238
6e7b4292
VC
5239 if (is_qla80XX(ha))
5240 qla4_8xxx_get_flash_info(ha);
5241
5242 if (is_qla8032(ha)) {
5243 qla4_83xx_read_reset_template(ha);
5244 /*
5245 * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
5246 * If DONRESET_BIT0 is set, drivers should not set dev_state
5247 * to NEED_RESET. But if NEED_RESET is set, drivers should
5248 * should honor the reset.
5249 */
5250 if (ql4xdontresethba == 1)
5251 qla4_83xx_set_idc_dontreset(ha);
5252 }
f4f5df23 5253
afaf5a2d
DS
5254 /*
5255 * Initialize the Host adapter request/response queues and
5256 * firmware
5257 * NOTE: interrupts enabled upon successful completion
5258 */
13483730 5259 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
7977f825
PV
5260
5261 /* Dont retry adapter initialization if IRQ allocation failed */
5262 if (!test_bit(AF_IRQ_ATTACHED, &ha->flags))
5263 goto skip_retry_init;
5264
f4f5df23
VC
5265 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
5266 init_retry_count++ < MAX_INIT_RETRIES) {
f9880e76 5267
6e7b4292 5268 if (is_qla80XX(ha)) {
33693c7a
VC
5269 ha->isp_ops->idc_lock(ha);
5270 dev_state = qla4_8xxx_rd_direct(ha,
5271 QLA82XX_CRB_DEV_STATE);
5272 ha->isp_ops->idc_unlock(ha);
de8c72da 5273 if (dev_state == QLA8XXX_DEV_FAILED) {
f9880e76
PM
5274 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
5275 "initialize adapter. H/W is in failed state\n",
5276 __func__);
5277 break;
5278 }
5279 }
afaf5a2d
DS
5280 DEBUG2(printk("scsi: %s: retrying adapter initialization "
5281 "(%d)\n", __func__, init_retry_count));
f4f5df23
VC
5282
5283 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
5284 continue;
5285
13483730 5286 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
afaf5a2d 5287 }
f4f5df23 5288
7977f825 5289skip_retry_init:
f4f5df23 5290 if (!test_bit(AF_ONLINE, &ha->flags)) {
c2660df3 5291 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
afaf5a2d 5292
6e7b4292
VC
5293 if ((is_qla8022(ha) && ql4xdontresethba) ||
5294 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
fe998527
LC
5295 /* Put the device in failed state. */
5296 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
33693c7a
VC
5297 ha->isp_ops->idc_lock(ha);
5298 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
5299 QLA8XXX_DEV_FAILED);
5300 ha->isp_ops->idc_unlock(ha);
fe998527 5301 }
afaf5a2d 5302 ret = -ENODEV;
b3a271a9 5303 goto remove_host;
afaf5a2d
DS
5304 }
5305
afaf5a2d
DS
5306 /* Startup the kernel thread for this host adapter. */
5307 DEBUG2(printk("scsi: %s: Starting kernel thread for "
5308 "qla4xxx_dpc\n", __func__));
5309 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
5310 ha->dpc_thread = create_singlethread_workqueue(buf);
5311 if (!ha->dpc_thread) {
c2660df3 5312 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
afaf5a2d 5313 ret = -ENODEV;
b3a271a9 5314 goto remove_host;
afaf5a2d 5315 }
c4028958 5316 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
afaf5a2d 5317
b3a271a9
MR
5318 sprintf(buf, "qla4xxx_%lu_task", ha->host_no);
5319 ha->task_wq = alloc_workqueue(buf, WQ_MEM_RECLAIM, 1);
5320 if (!ha->task_wq) {
5321 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
5322 ret = -ENODEV;
5323 goto remove_host;
5324 }
5325
6e7b4292
VC
5326 /*
5327 * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
f4f5df23
VC
5328 * (which is called indirectly by qla4xxx_initialize_adapter),
5329 * so that irqs will be registered after crbinit but before
5330 * mbx_intr_enable.
5331 */
ee996a69 5332 if (is_qla40XX(ha)) {
f4f5df23
VC
5333 ret = qla4xxx_request_irqs(ha);
5334 if (ret) {
5335 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
5336 "interrupt %d already in use.\n", pdev->irq);
b3a271a9 5337 goto remove_host;
f4f5df23 5338 }
afaf5a2d 5339 }
afaf5a2d 5340
2232be0d 5341 pci_save_state(ha->pdev);
f4f5df23 5342 ha->isp_ops->enable_intrs(ha);
afaf5a2d
DS
5343
5344 /* Start timer thread. */
5345 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
5346
5347 set_bit(AF_INIT_DONE, &ha->flags);
5348
068237c8
TP
5349 qla4_8xxx_alloc_sysfs_attr(ha);
5350
afaf5a2d
DS
5351 printk(KERN_INFO
5352 " QLogic iSCSI HBA Driver version: %s\n"
5353 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
5354 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
5355 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
5356 ha->patch_number, ha->build_number);
ed1086e0 5357
cfb27874
MD
5358 /* Set the driver version */
5359 if (is_qla80XX(ha))
5360 qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
5361
2a991c21 5362 if (qla4xxx_setup_boot_info(ha))
3573bfb2
VC
5363 ql4_printk(KERN_ERR, ha,
5364 "%s: No iSCSI boot target configured\n", __func__);
2a991c21 5365
13483730
MC
5366 /* Perform the build ddb list and login to each */
5367 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
5368 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
5369
5370 qla4xxx_create_chap_list(ha);
5371
ed1086e0 5372 qla4xxx_create_ifaces(ha);
afaf5a2d
DS
5373 return 0;
5374
b3a271a9
MR
5375remove_host:
5376 scsi_remove_host(ha->host);
5377
afaf5a2d
DS
5378probe_failed:
5379 qla4xxx_free_adapter(ha);
f4f5df23
VC
5380
5381probe_failed_ioconfig:
2232be0d 5382 pci_disable_pcie_error_reporting(pdev);
afaf5a2d
DS
5383 scsi_host_put(ha->host);
5384
5385probe_disable_device:
5386 pci_disable_device(pdev);
5387
5388 return ret;
5389}
5390
7eece5a0
KH
5391/**
5392 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
5393 * @ha: pointer to adapter structure
5394 *
5395 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
5396 * so that the other port will not re-initialize while in the process of
5397 * removing the ha due to driver unload or hba hotplug.
5398 **/
5399static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
5400{
5401 struct scsi_qla_host *other_ha = NULL;
5402 struct pci_dev *other_pdev = NULL;
5403 int fn = ISP4XXX_PCI_FN_2;
5404
5405 /*iscsi function numbers for ISP4xxx is 1 and 3*/
5406 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
5407 fn = ISP4XXX_PCI_FN_1;
5408
5409 other_pdev =
5410 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
5411 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
5412 fn));
5413
5414 /* Get other_ha if other_pdev is valid and state is enable*/
5415 if (other_pdev) {
5416 if (atomic_read(&other_pdev->enable_cnt)) {
5417 other_ha = pci_get_drvdata(other_pdev);
5418 if (other_ha) {
5419 set_bit(AF_HA_REMOVAL, &other_ha->flags);
5420 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
5421 "Prevent %s reinit\n", __func__,
5422 dev_name(&other_ha->pdev->dev)));
5423 }
5424 }
5425 pci_dev_put(other_pdev);
5426 }
5427}
5428
13483730
MC
5429static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
5430{
5431 struct ddb_entry *ddb_entry;
5432 int options;
5433 int idx;
5434
5435 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
5436
5437 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
5438 if ((ddb_entry != NULL) &&
5439 (ddb_entry->ddb_type == FLASH_DDB)) {
5440
5441 options = LOGOUT_OPTION_CLOSE_SESSION;
5442 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
5443 == QLA_ERROR)
5444 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
5445 __func__);
5446
5447 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
5448 /*
5449 * we have decremented the reference count of the driver
5450 * when we setup the session to have the driver unload
5451 * to be seamless without actually destroying the
5452 * session
5453 **/
5454 try_module_get(qla4xxx_iscsi_transport.owner);
5455 iscsi_destroy_endpoint(ddb_entry->conn->ep);
5456 qla4xxx_free_ddb(ha, ddb_entry);
5457 iscsi_session_teardown(ddb_entry->sess);
5458 }
5459 }
5460}
afaf5a2d 5461/**
59e13d48 5462 * qla4xxx_remove_adapter - callback function to remove adapter.
afaf5a2d
DS
5463 * @pci_dev: PCI device pointer
5464 **/
6f039790 5465static void qla4xxx_remove_adapter(struct pci_dev *pdev)
afaf5a2d
DS
5466{
5467 struct scsi_qla_host *ha;
5468
f8b0751d
VC
5469 /*
5470 * If the PCI device is disabled then it means probe_adapter had
5471 * failed and resources already cleaned up on probe_adapter exit.
5472 */
5473 if (!pci_is_enabled(pdev))
5474 return;
5475
afaf5a2d
DS
5476 ha = pci_get_drvdata(pdev);
5477
ee996a69 5478 if (is_qla40XX(ha))
7eece5a0 5479 qla4xxx_prevent_other_port_reinit(ha);
bee4fe8e 5480
ed1086e0
VC
5481 /* destroy iface from sysfs */
5482 qla4xxx_destroy_ifaces(ha);
5483
13483730 5484 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
2a991c21
MR
5485 iscsi_boot_destroy_kset(ha->boot_kset);
5486
13483730 5487 qla4xxx_destroy_fw_ddb_session(ha);
068237c8 5488 qla4_8xxx_free_sysfs_attr(ha);
13483730 5489
afaf5a2d
DS
5490 scsi_remove_host(ha->host);
5491
5492 qla4xxx_free_adapter(ha);
5493
5494 scsi_host_put(ha->host);
5495
2232be0d 5496 pci_disable_pcie_error_reporting(pdev);
f4f5df23 5497 pci_disable_device(pdev);
afaf5a2d
DS
5498 pci_set_drvdata(pdev, NULL);
5499}
5500
5501/**
5502 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
5503 * @ha: HA context
5504 *
5505 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
5506 * supported addressing method.
5507 */
47975477 5508static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
afaf5a2d
DS
5509{
5510 int retval;
5511
5512 /* Update our PCI device dma_mask for full 64 bit mask */
6a35528a
YH
5513 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
5514 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
afaf5a2d
DS
5515 dev_dbg(&ha->pdev->dev,
5516 "Failed to set 64 bit PCI consistent mask; "
5517 "using 32 bit.\n");
5518 retval = pci_set_consistent_dma_mask(ha->pdev,
284901a9 5519 DMA_BIT_MASK(32));
afaf5a2d
DS
5520 }
5521 } else
284901a9 5522 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
afaf5a2d
DS
5523}
5524
5525static int qla4xxx_slave_alloc(struct scsi_device *sdev)
5526{
b3a271a9
MR
5527 struct iscsi_cls_session *cls_sess;
5528 struct iscsi_session *sess;
5529 struct ddb_entry *ddb;
8bb4033d 5530 int queue_depth = QL4_DEF_QDEPTH;
afaf5a2d 5531
b3a271a9
MR
5532 cls_sess = starget_to_session(sdev->sdev_target);
5533 sess = cls_sess->dd_data;
5534 ddb = sess->dd_data;
5535
afaf5a2d
DS
5536 sdev->hostdata = ddb;
5537 sdev->tagged_supported = 1;
8bb4033d
VC
5538
5539 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
5540 queue_depth = ql4xmaxqdepth;
5541
5542 scsi_activate_tcq(sdev, queue_depth);
afaf5a2d
DS
5543 return 0;
5544}
5545
5546static int qla4xxx_slave_configure(struct scsi_device *sdev)
5547{
5548 sdev->tagged_supported = 1;
5549 return 0;
5550}
5551
5552static void qla4xxx_slave_destroy(struct scsi_device *sdev)
5553{
5554 scsi_deactivate_tcq(sdev, 1);
5555}
5556
f7b4aa63
TP
5557static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
5558 int reason)
5559{
5560 if (!ql4xqfulltracking)
5561 return -EOPNOTSUPP;
5562
5563 return iscsi_change_queue_depth(sdev, qdepth, reason);
5564}
5565
afaf5a2d
DS
5566/**
5567 * qla4xxx_del_from_active_array - returns an active srb
5568 * @ha: Pointer to host adapter structure.
fd589a8f 5569 * @index: index into the active_array
afaf5a2d
DS
5570 *
5571 * This routine removes and returns the srb at the specified index
5572 **/
f4f5df23
VC
5573struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
5574 uint32_t index)
afaf5a2d
DS
5575{
5576 struct srb *srb = NULL;
5369887a 5577 struct scsi_cmnd *cmd = NULL;
afaf5a2d 5578
5369887a
VC
5579 cmd = scsi_host_find_tag(ha->host, index);
5580 if (!cmd)
afaf5a2d
DS
5581 return srb;
5582
5369887a
VC
5583 srb = (struct srb *)CMD_SP(cmd);
5584 if (!srb)
afaf5a2d
DS
5585 return srb;
5586
5587 /* update counters */
5588 if (srb->flags & SRB_DMA_VALID) {
5589 ha->req_q_count += srb->iocb_cnt;
5590 ha->iocb_cnt -= srb->iocb_cnt;
5591 if (srb->cmd)
5369887a
VC
5592 srb->cmd->host_scribble =
5593 (unsigned char *)(unsigned long) MAX_SRBS;
afaf5a2d
DS
5594 }
5595 return srb;
5596}
5597
afaf5a2d
DS
5598/**
5599 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
09a0f719 5600 * @ha: Pointer to host adapter structure.
afaf5a2d
DS
5601 * @cmd: Scsi Command to wait on.
5602 *
5603 * This routine waits for the command to be returned by the Firmware
5604 * for some max time.
5605 **/
5606static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
5607 struct scsi_cmnd *cmd)
5608{
5609 int done = 0;
5610 struct srb *rp;
5611 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
2232be0d
LC
5612 int ret = SUCCESS;
5613
5614 /* Dont wait on command if PCI error is being handled
5615 * by PCI AER driver
5616 */
5617 if (unlikely(pci_channel_offline(ha->pdev)) ||
5618 (test_bit(AF_EEH_BUSY, &ha->flags))) {
5619 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
5620 ha->host_no, __func__);
5621 return ret;
5622 }
afaf5a2d
DS
5623
5624 do {
5625 /* Checking to see if its returned to OS */
5369887a 5626 rp = (struct srb *) CMD_SP(cmd);
afaf5a2d
DS
5627 if (rp == NULL) {
5628 done++;
5629 break;
5630 }
5631
5632 msleep(2000);
5633 } while (max_wait_time--);
5634
5635 return done;
5636}
5637
5638/**
5639 * qla4xxx_wait_for_hba_online - waits for HBA to come online
5640 * @ha: Pointer to host adapter structure
5641 **/
5642static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
5643{
5644 unsigned long wait_online;
5645
f581a3f7 5646 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
afaf5a2d
DS
5647 while (time_before(jiffies, wait_online)) {
5648
5649 if (adapter_up(ha))
5650 return QLA_SUCCESS;
afaf5a2d
DS
5651
5652 msleep(2000);
5653 }
5654
5655 return QLA_ERROR;
5656}
5657
5658/**
ce545039 5659 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
fd589a8f 5660 * @ha: pointer to HBA
afaf5a2d
DS
5661 * @t: target id
5662 * @l: lun id
5663 *
5664 * This function waits for all outstanding commands to a lun to complete. It
5665 * returns 0 if all pending commands are returned and 1 otherwise.
5666 **/
ce545039
MC
5667static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
5668 struct scsi_target *stgt,
5669 struct scsi_device *sdev)
afaf5a2d
DS
5670{
5671 int cnt;
5672 int status = 0;
5673 struct scsi_cmnd *cmd;
5674
5675 /*
ce545039
MC
5676 * Waiting for all commands for the designated target or dev
5677 * in the active array
afaf5a2d
DS
5678 */
5679 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
5680 cmd = scsi_host_find_tag(ha->host, cnt);
ce545039
MC
5681 if (cmd && stgt == scsi_target(cmd->device) &&
5682 (!sdev || sdev == cmd->device)) {
afaf5a2d
DS
5683 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5684 status++;
5685 break;
5686 }
5687 }
5688 }
5689 return status;
5690}
5691
09a0f719
VC
5692/**
5693 * qla4xxx_eh_abort - callback for abort task.
5694 * @cmd: Pointer to Linux's SCSI command structure
5695 *
5696 * This routine is called by the Linux OS to abort the specified
5697 * command.
5698 **/
5699static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
5700{
5701 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5702 unsigned int id = cmd->device->id;
5703 unsigned int lun = cmd->device->lun;
92b3e5bb 5704 unsigned long flags;
09a0f719
VC
5705 struct srb *srb = NULL;
5706 int ret = SUCCESS;
5707 int wait = 0;
5708
c2660df3 5709 ql4_printk(KERN_INFO, ha,
5cd049a5
CH
5710 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
5711 ha->host_no, id, lun, cmd);
09a0f719 5712
92b3e5bb 5713 spin_lock_irqsave(&ha->hardware_lock, flags);
09a0f719 5714 srb = (struct srb *) CMD_SP(cmd);
92b3e5bb
MC
5715 if (!srb) {
5716 spin_unlock_irqrestore(&ha->hardware_lock, flags);
09a0f719 5717 return SUCCESS;
92b3e5bb 5718 }
09a0f719 5719 kref_get(&srb->srb_ref);
92b3e5bb 5720 spin_unlock_irqrestore(&ha->hardware_lock, flags);
09a0f719
VC
5721
5722 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
5723 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
5724 ha->host_no, id, lun));
5725 ret = FAILED;
5726 } else {
5727 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
5728 ha->host_no, id, lun));
5729 wait = 1;
5730 }
5731
5732 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
5733
5734 /* Wait for command to complete */
5735 if (wait) {
5736 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
5737 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
5738 ha->host_no, id, lun));
5739 ret = FAILED;
5740 }
5741 }
5742
c2660df3 5743 ql4_printk(KERN_INFO, ha,
09a0f719 5744 "scsi%ld:%d:%d: Abort command - %s\n",
25985edc 5745 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
09a0f719
VC
5746
5747 return ret;
5748}
5749
afaf5a2d
DS
5750/**
5751 * qla4xxx_eh_device_reset - callback for target reset.
5752 * @cmd: Pointer to Linux's SCSI command structure
5753 *
5754 * This routine is called by the Linux OS to reset all luns on the
5755 * specified target.
5756 **/
5757static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
5758{
5759 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5760 struct ddb_entry *ddb_entry = cmd->device->hostdata;
afaf5a2d
DS
5761 int ret = FAILED, stat;
5762
612f7348 5763 if (!ddb_entry)
afaf5a2d
DS
5764 return ret;
5765
c01be6dc
MC
5766 ret = iscsi_block_scsi_eh(cmd);
5767 if (ret)
5768 return ret;
5769 ret = FAILED;
5770
c2660df3 5771 ql4_printk(KERN_INFO, ha,
afaf5a2d
DS
5772 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
5773 cmd->device->channel, cmd->device->id, cmd->device->lun);
5774
5775 DEBUG2(printk(KERN_INFO
5776 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
5777 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
242f9dcb 5778 cmd, jiffies, cmd->request->timeout / HZ,
afaf5a2d
DS
5779 ha->dpc_flags, cmd->result, cmd->allowed));
5780
5781 /* FIXME: wait for hba to go online */
5782 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
5783 if (stat != QLA_SUCCESS) {
c2660df3 5784 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
afaf5a2d
DS
5785 goto eh_dev_reset_done;
5786 }
5787
ce545039
MC
5788 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5789 cmd->device)) {
c2660df3 5790 ql4_printk(KERN_INFO, ha,
ce545039
MC
5791 "DEVICE RESET FAILED - waiting for "
5792 "commands.\n");
5793 goto eh_dev_reset_done;
afaf5a2d
DS
5794 }
5795
9d562913
DS
5796 /* Send marker. */
5797 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5798 MM_LUN_RESET) != QLA_SUCCESS)
5799 goto eh_dev_reset_done;
5800
c2660df3 5801 ql4_printk(KERN_INFO, ha,
afaf5a2d
DS
5802 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
5803 ha->host_no, cmd->device->channel, cmd->device->id,
5804 cmd->device->lun);
5805
5806 ret = SUCCESS;
5807
5808eh_dev_reset_done:
5809
5810 return ret;
5811}
5812
ce545039
MC
5813/**
5814 * qla4xxx_eh_target_reset - callback for target reset.
5815 * @cmd: Pointer to Linux's SCSI command structure
5816 *
5817 * This routine is called by the Linux OS to reset the target.
5818 **/
5819static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
5820{
5821 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
5822 struct ddb_entry *ddb_entry = cmd->device->hostdata;
c01be6dc 5823 int stat, ret;
ce545039
MC
5824
5825 if (!ddb_entry)
5826 return FAILED;
5827
c01be6dc
MC
5828 ret = iscsi_block_scsi_eh(cmd);
5829 if (ret)
5830 return ret;
5831
ce545039
MC
5832 starget_printk(KERN_INFO, scsi_target(cmd->device),
5833 "WARM TARGET RESET ISSUED.\n");
5834
5835 DEBUG2(printk(KERN_INFO
5836 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
5837 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
242f9dcb 5838 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
ce545039
MC
5839 ha->dpc_flags, cmd->result, cmd->allowed));
5840
5841 stat = qla4xxx_reset_target(ha, ddb_entry);
5842 if (stat != QLA_SUCCESS) {
5843 starget_printk(KERN_INFO, scsi_target(cmd->device),
5844 "WARM TARGET RESET FAILED.\n");
5845 return FAILED;
5846 }
5847
ce545039
MC
5848 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
5849 NULL)) {
5850 starget_printk(KERN_INFO, scsi_target(cmd->device),
5851 "WARM TARGET DEVICE RESET FAILED - "
5852 "waiting for commands.\n");
5853 return FAILED;
5854 }
5855
9d562913
DS
5856 /* Send marker. */
5857 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
5858 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
5859 starget_printk(KERN_INFO, scsi_target(cmd->device),
5860 "WARM TARGET DEVICE RESET FAILED - "
5861 "marker iocb failed.\n");
5862 return FAILED;
5863 }
5864
ce545039
MC
5865 starget_printk(KERN_INFO, scsi_target(cmd->device),
5866 "WARM TARGET RESET SUCCEEDED.\n");
5867 return SUCCESS;
5868}
5869
8a288960
SR
5870/**
5871 * qla4xxx_is_eh_active - check if error handler is running
5872 * @shost: Pointer to SCSI Host struct
5873 *
5874 * This routine finds that if reset host is called in EH
5875 * scenario or from some application like sg_reset
5876 **/
5877static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
5878{
5879 if (shost->shost_state == SHOST_RECOVERY)
5880 return 1;
5881 return 0;
5882}
5883
afaf5a2d
DS
5884/**
5885 * qla4xxx_eh_host_reset - kernel callback
5886 * @cmd: Pointer to Linux's SCSI command structure
5887 *
5888 * This routine is invoked by the Linux kernel to perform fatal error
5889 * recovery on the specified adapter.
5890 **/
5891static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
5892{
5893 int return_status = FAILED;
5894 struct scsi_qla_host *ha;
5895
b3a271a9 5896 ha = to_qla_host(cmd->device->host);
afaf5a2d 5897
6e7b4292
VC
5898 if (is_qla8032(ha) && ql4xdontresethba)
5899 qla4_83xx_set_idc_dontreset(ha);
5900
5901 /*
5902 * For ISP8324, if IDC_CTRL DONTRESET_BIT0 is set by other
5903 * protocol drivers, we should not set device_state to
5904 * NEED_RESET
5905 */
5906 if (ql4xdontresethba ||
5907 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
f4f5df23
VC
5908 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
5909 ha->host_no, __func__));
8a288960
SR
5910
5911 /* Clear outstanding srb in queues */
5912 if (qla4xxx_is_eh_active(cmd->device->host))
5913 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
5914
f4f5df23
VC
5915 return FAILED;
5916 }
5917
c2660df3 5918 ql4_printk(KERN_INFO, ha,
dca05c4c 5919 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
afaf5a2d
DS
5920 cmd->device->channel, cmd->device->id, cmd->device->lun);
5921
5922 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
5923 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
5924 "DEAD.\n", ha->host_no, cmd->device->channel,
5925 __func__));
5926
5927 return FAILED;
5928 }
5929
f4f5df23 5930 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
6e7b4292 5931 if (is_qla80XX(ha))
f4f5df23
VC
5932 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
5933 else
5934 set_bit(DPC_RESET_HA, &ha->dpc_flags);
5935 }
50a29aec 5936
f4f5df23 5937 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
afaf5a2d 5938 return_status = SUCCESS;
afaf5a2d 5939
c2660df3 5940 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
25985edc 5941 return_status == FAILED ? "FAILED" : "SUCCEEDED");
afaf5a2d
DS
5942
5943 return return_status;
5944}
5945
95d31262
VC
5946static int qla4xxx_context_reset(struct scsi_qla_host *ha)
5947{
5948 uint32_t mbox_cmd[MBOX_REG_COUNT];
5949 uint32_t mbox_sts[MBOX_REG_COUNT];
5950 struct addr_ctrl_blk_def *acb = NULL;
5951 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
5952 int rval = QLA_SUCCESS;
5953 dma_addr_t acb_dma;
5954
5955 acb = dma_alloc_coherent(&ha->pdev->dev,
5956 sizeof(struct addr_ctrl_blk_def),
5957 &acb_dma, GFP_KERNEL);
5958 if (!acb) {
5959 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
5960 __func__);
5961 rval = -ENOMEM;
5962 goto exit_port_reset;
5963 }
5964
5965 memset(acb, 0, acb_len);
5966
5967 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
5968 if (rval != QLA_SUCCESS) {
5969 rval = -EIO;
5970 goto exit_free_acb;
5971 }
5972
5973 rval = qla4xxx_disable_acb(ha);
5974 if (rval != QLA_SUCCESS) {
5975 rval = -EIO;
5976 goto exit_free_acb;
5977 }
5978
5979 wait_for_completion_timeout(&ha->disable_acb_comp,
5980 DISABLE_ACB_TOV * HZ);
5981
5982 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
5983 if (rval != QLA_SUCCESS) {
5984 rval = -EIO;
5985 goto exit_free_acb;
5986 }
5987
5988exit_free_acb:
5989 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
5990 acb, acb_dma);
5991exit_port_reset:
5992 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
5993 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
5994 return rval;
5995}
5996
5997static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
5998{
5999 struct scsi_qla_host *ha = to_qla_host(shost);
6000 int rval = QLA_SUCCESS;
6001
6002 if (ql4xdontresethba) {
6003 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
6004 __func__));
6005 rval = -EPERM;
6006 goto exit_host_reset;
6007 }
6008
95d31262
VC
6009 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
6010 goto recover_adapter;
6011
6012 switch (reset_type) {
6013 case SCSI_ADAPTER_RESET:
6014 set_bit(DPC_RESET_HA, &ha->dpc_flags);
6015 break;
6016 case SCSI_FIRMWARE_RESET:
6017 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
6e7b4292 6018 if (is_qla80XX(ha))
95d31262
VC
6019 /* set firmware context reset */
6020 set_bit(DPC_RESET_HA_FW_CONTEXT,
6021 &ha->dpc_flags);
6022 else {
6023 rval = qla4xxx_context_reset(ha);
6024 goto exit_host_reset;
6025 }
6026 }
6027 break;
6028 }
6029
6030recover_adapter:
6031 rval = qla4xxx_recover_adapter(ha);
6032 if (rval != QLA_SUCCESS) {
6033 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
6034 __func__));
6035 rval = -EIO;
6036 }
6037
6038exit_host_reset:
6039 return rval;
6040}
6041
2232be0d
LC
6042/* PCI AER driver recovers from all correctable errors w/o
6043 * driver intervention. For uncorrectable errors PCI AER
6044 * driver calls the following device driver's callbacks
6045 *
6046 * - Fatal Errors - link_reset
6047 * - Non-Fatal Errors - driver's pci_error_detected() which
6048 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
6049 *
6050 * PCI AER driver calls
6051 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
6052 * returns RECOVERED or NEED_RESET if fw_hung
6053 * NEED_RESET - driver's slot_reset()
6054 * DISCONNECT - device is dead & cannot recover
6055 * RECOVERED - driver's pci_resume()
6056 */
6057static pci_ers_result_t
6058qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6059{
6060 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6061
6062 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
6063 ha->host_no, __func__, state);
6064
6065 if (!is_aer_supported(ha))
6066 return PCI_ERS_RESULT_NONE;
6067
6068 switch (state) {
6069 case pci_channel_io_normal:
6070 clear_bit(AF_EEH_BUSY, &ha->flags);
6071 return PCI_ERS_RESULT_CAN_RECOVER;
6072 case pci_channel_io_frozen:
6073 set_bit(AF_EEH_BUSY, &ha->flags);
6074 qla4xxx_mailbox_premature_completion(ha);
6075 qla4xxx_free_irqs(ha);
6076 pci_disable_device(pdev);
7b3595df
VC
6077 /* Return back all IOs */
6078 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
2232be0d
LC
6079 return PCI_ERS_RESULT_NEED_RESET;
6080 case pci_channel_io_perm_failure:
6081 set_bit(AF_EEH_BUSY, &ha->flags);
6082 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
6083 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
6084 return PCI_ERS_RESULT_DISCONNECT;
6085 }
6086 return PCI_ERS_RESULT_NEED_RESET;
6087}
6088
6089/**
6090 * qla4xxx_pci_mmio_enabled() gets called if
6091 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
6092 * and read/write to the device still works.
6093 **/
6094static pci_ers_result_t
6095qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
6096{
6097 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6098
6099 if (!is_aer_supported(ha))
6100 return PCI_ERS_RESULT_NONE;
6101
7b3595df 6102 return PCI_ERS_RESULT_RECOVERED;
2232be0d
LC
6103}
6104
7b3595df 6105static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
2232be0d
LC
6106{
6107 uint32_t rval = QLA_ERROR;
7b3595df 6108 uint32_t ret = 0;
2232be0d
LC
6109 int fn;
6110 struct pci_dev *other_pdev = NULL;
6111
6112 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
6113
6114 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
6115
6116 if (test_bit(AF_ONLINE, &ha->flags)) {
6117 clear_bit(AF_ONLINE, &ha->flags);
b3a271a9
MR
6118 clear_bit(AF_LINK_UP, &ha->flags);
6119 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
2232be0d 6120 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
2232be0d
LC
6121 }
6122
6123 fn = PCI_FUNC(ha->pdev->devfn);
6124 while (fn > 0) {
6125 fn--;
6126 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
6127 "func %x\n", ha->host_no, __func__, fn);
6128 /* Get the pci device given the domain, bus,
6129 * slot/function number */
6130 other_pdev =
6131 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
6132 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
6133 fn));
6134
6135 if (!other_pdev)
6136 continue;
6137
6138 if (atomic_read(&other_pdev->enable_cnt)) {
6139 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
6140 "func in enabled state%x\n", ha->host_no,
6141 __func__, fn);
6142 pci_dev_put(other_pdev);
6143 break;
6144 }
6145 pci_dev_put(other_pdev);
6146 }
6147
6148 /* The first function on the card, the reset owner will
6149 * start & initialize the firmware. The other functions
6150 * on the card will reset the firmware context
6151 */
6152 if (!fn) {
6153 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
6154 "0x%x is the owner\n", ha->host_no, __func__,
6155 ha->pdev->devfn);
6156
33693c7a
VC
6157 ha->isp_ops->idc_lock(ha);
6158 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6159 QLA8XXX_DEV_COLD);
33693c7a 6160 ha->isp_ops->idc_unlock(ha);
39c95826
VC
6161
6162 rval = qla4_8xxx_update_idc_reg(ha);
6163 if (rval == QLA_ERROR) {
6164 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
6165 ha->host_no, __func__);
6166 ha->isp_ops->idc_lock(ha);
6167 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6168 QLA8XXX_DEV_FAILED);
6169 ha->isp_ops->idc_unlock(ha);
6170 goto exit_error_recovery;
6171 }
2232be0d 6172
2232be0d 6173 clear_bit(AF_FW_RECOVERY, &ha->flags);
13483730 6174 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
2232be0d
LC
6175
6176 if (rval != QLA_SUCCESS) {
6177 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
6178 "FAILED\n", ha->host_no, __func__);
82761907 6179 ha->isp_ops->idc_lock(ha);
2232be0d 6180 qla4_8xxx_clear_drv_active(ha);
33693c7a
VC
6181 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6182 QLA8XXX_DEV_FAILED);
82761907 6183 ha->isp_ops->idc_unlock(ha);
2232be0d
LC
6184 } else {
6185 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
6186 "READY\n", ha->host_no, __func__);
82761907 6187 ha->isp_ops->idc_lock(ha);
33693c7a
VC
6188 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
6189 QLA8XXX_DEV_READY);
2232be0d 6190 /* Clear driver state register */
33693c7a 6191 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
2232be0d 6192 qla4_8xxx_set_drv_active(ha);
82761907 6193 ha->isp_ops->idc_unlock(ha);
7b3595df
VC
6194 ret = qla4xxx_request_irqs(ha);
6195 if (ret) {
6196 ql4_printk(KERN_WARNING, ha, "Failed to "
6197 "reserve interrupt %d already in use.\n",
6198 ha->pdev->irq);
6199 rval = QLA_ERROR;
6200 } else {
6201 ha->isp_ops->enable_intrs(ha);
6202 rval = QLA_SUCCESS;
6203 }
2232be0d 6204 }
2232be0d
LC
6205 } else {
6206 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
6207 "the reset owner\n", ha->host_no, __func__,
6208 ha->pdev->devfn);
33693c7a
VC
6209 if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
6210 QLA8XXX_DEV_READY)) {
2232be0d 6211 clear_bit(AF_FW_RECOVERY, &ha->flags);
13483730 6212 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
7b3595df
VC
6213 if (rval == QLA_SUCCESS) {
6214 ret = qla4xxx_request_irqs(ha);
6215 if (ret) {
6216 ql4_printk(KERN_WARNING, ha, "Failed to"
6217 " reserve interrupt %d already in"
6218 " use.\n", ha->pdev->irq);
6219 rval = QLA_ERROR;
6220 } else {
6221 ha->isp_ops->enable_intrs(ha);
6222 rval = QLA_SUCCESS;
6223 }
6224 }
33693c7a 6225 ha->isp_ops->idc_lock(ha);
2232be0d 6226 qla4_8xxx_set_drv_active(ha);
33693c7a 6227 ha->isp_ops->idc_unlock(ha);
2232be0d
LC
6228 }
6229 }
39c95826 6230exit_error_recovery:
2232be0d
LC
6231 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
6232 return rval;
6233}
6234
6235static pci_ers_result_t
6236qla4xxx_pci_slot_reset(struct pci_dev *pdev)
6237{
6238 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
6239 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6240 int rc;
6241
6242 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
6243 ha->host_no, __func__);
6244
6245 if (!is_aer_supported(ha))
6246 return PCI_ERS_RESULT_NONE;
6247
6248 /* Restore the saved state of PCIe device -
6249 * BAR registers, PCI Config space, PCIX, MSI,
6250 * IOV states
6251 */
6252 pci_restore_state(pdev);
6253
6254 /* pci_restore_state() clears the saved_state flag of the device
6255 * save restored state which resets saved_state flag
6256 */
6257 pci_save_state(pdev);
6258
6259 /* Initialize device or resume if in suspended state */
6260 rc = pci_enable_device(pdev);
6261 if (rc) {
25985edc 6262 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
2232be0d
LC
6263 "device after reset\n", ha->host_no, __func__);
6264 goto exit_slot_reset;
6265 }
6266
7b3595df 6267 ha->isp_ops->disable_intrs(ha);
2232be0d 6268
6e7b4292 6269 if (is_qla80XX(ha)) {
2232be0d
LC
6270 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
6271 ret = PCI_ERS_RESULT_RECOVERED;
6272 goto exit_slot_reset;
6273 } else
6274 goto exit_slot_reset;
6275 }
6276
6277exit_slot_reset:
6278 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
6279 "device after reset\n", ha->host_no, __func__, ret);
6280 return ret;
6281}
6282
6283static void
6284qla4xxx_pci_resume(struct pci_dev *pdev)
6285{
6286 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
6287 int ret;
6288
6289 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
6290 ha->host_no, __func__);
6291
6292 ret = qla4xxx_wait_for_hba_online(ha);
6293 if (ret != QLA_SUCCESS) {
6294 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
6295 "resume I/O from slot/link_reset\n", ha->host_no,
6296 __func__);
6297 }
6298
6299 pci_cleanup_aer_uncorrect_error_status(pdev);
6300 clear_bit(AF_EEH_BUSY, &ha->flags);
6301}
6302
a55b2d21 6303static const struct pci_error_handlers qla4xxx_err_handler = {
2232be0d
LC
6304 .error_detected = qla4xxx_pci_error_detected,
6305 .mmio_enabled = qla4xxx_pci_mmio_enabled,
6306 .slot_reset = qla4xxx_pci_slot_reset,
6307 .resume = qla4xxx_pci_resume,
6308};
6309
afaf5a2d
DS
6310static struct pci_device_id qla4xxx_pci_tbl[] = {
6311 {
6312 .vendor = PCI_VENDOR_ID_QLOGIC,
6313 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
6314 .subvendor = PCI_ANY_ID,
6315 .subdevice = PCI_ANY_ID,
6316 },
6317 {
6318 .vendor = PCI_VENDOR_ID_QLOGIC,
6319 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
6320 .subvendor = PCI_ANY_ID,
6321 .subdevice = PCI_ANY_ID,
6322 },
d915058f
DS
6323 {
6324 .vendor = PCI_VENDOR_ID_QLOGIC,
6325 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
6326 .subvendor = PCI_ANY_ID,
6327 .subdevice = PCI_ANY_ID,
6328 },
f4f5df23
VC
6329 {
6330 .vendor = PCI_VENDOR_ID_QLOGIC,
6331 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
6332 .subvendor = PCI_ANY_ID,
6333 .subdevice = PCI_ANY_ID,
6334 },
6e7b4292
VC
6335 {
6336 .vendor = PCI_VENDOR_ID_QLOGIC,
6337 .device = PCI_DEVICE_ID_QLOGIC_ISP8324,
6338 .subvendor = PCI_ANY_ID,
6339 .subdevice = PCI_ANY_ID,
6340 },
afaf5a2d
DS
6341 {0, 0},
6342};
6343MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
6344
47975477 6345static struct pci_driver qla4xxx_pci_driver = {
afaf5a2d
DS
6346 .name = DRIVER_NAME,
6347 .id_table = qla4xxx_pci_tbl,
6348 .probe = qla4xxx_probe_adapter,
6349 .remove = qla4xxx_remove_adapter,
2232be0d 6350 .err_handler = &qla4xxx_err_handler,
afaf5a2d
DS
6351};
6352
6353static int __init qla4xxx_module_init(void)
6354{
6355 int ret;
6356
6357 /* Allocate cache for SRBs. */
6358 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
20c2df83 6359 SLAB_HWCACHE_ALIGN, NULL);
afaf5a2d
DS
6360 if (srb_cachep == NULL) {
6361 printk(KERN_ERR
6362 "%s: Unable to allocate SRB cache..."
6363 "Failing load!\n", DRIVER_NAME);
6364 ret = -ENOMEM;
6365 goto no_srp_cache;
6366 }
6367
6368 /* Derive version string. */
6369 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
11010fec 6370 if (ql4xextended_error_logging)
afaf5a2d
DS
6371 strcat(qla4xxx_version_str, "-debug");
6372
6373 qla4xxx_scsi_transport =
6374 iscsi_register_transport(&qla4xxx_iscsi_transport);
6375 if (!qla4xxx_scsi_transport){
6376 ret = -ENODEV;
6377 goto release_srb_cache;
6378 }
6379
afaf5a2d
DS
6380 ret = pci_register_driver(&qla4xxx_pci_driver);
6381 if (ret)
6382 goto unregister_transport;
6383
6384 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
6385 return 0;
5ae16db3 6386
afaf5a2d
DS
6387unregister_transport:
6388 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
6389release_srb_cache:
6390 kmem_cache_destroy(srb_cachep);
6391no_srp_cache:
6392 return ret;
6393}
6394
6395static void __exit qla4xxx_module_exit(void)
6396{
6397 pci_unregister_driver(&qla4xxx_pci_driver);
6398 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
6399 kmem_cache_destroy(srb_cachep);
6400}
6401
6402module_init(qla4xxx_module_init);
6403module_exit(qla4xxx_module_exit);
6404
6405MODULE_AUTHOR("QLogic Corporation");
6406MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
6407MODULE_LICENSE("GPL");
6408MODULE_VERSION(QLA4XXX_DRIVER_VERSION);
This page took 1.126555 seconds and 5 git commands to generate.