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