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