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