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