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