blob: bfee27e5786b30469119dd3b48d603ed575962bd [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
Vikas Chaudharyc68cdbf2012-08-22 07:55:09 -04003 * Copyright (c) 2003-2012 QLogic Corporation
David Somayajuluafaf5a22006-09-19 10:28:00 -07004 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Manish Rangankar2a991c22011-07-25 13:48:55 -05009#include <linux/blkdev.h>
10#include <linux/iscsi_boot_sysfs.h>
Mike Christie13483732011-12-01 21:38:41 -060011#include <linux/inet.h>
David Somayajuluafaf5a22006-09-19 10:28:00 -070012
13#include <scsi/scsi_tcq.h>
14#include <scsi/scsicam.h>
15
16#include "ql4_def.h"
David C Somayajulubee4fe82007-05-23 18:03:32 -070017#include "ql4_version.h"
18#include "ql4_glbl.h"
19#include "ql4_dbg.h"
20#include "ql4_inline.h"
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -040021#include "ql4_83xx.h"
David Somayajuluafaf5a22006-09-19 10:28:00 -070022
23/*
24 * Driver version
25 */
Adrian Bunk47975472007-04-26 00:35:16 -070026static char qla4xxx_version_str[40];
David Somayajuluafaf5a22006-09-19 10:28:00 -070027
28/*
29 * SRB allocation cache
30 */
Christoph Lametere18b8902006-12-06 20:33:20 -080031static struct kmem_cache *srb_cachep;
David Somayajuluafaf5a22006-09-19 10:28:00 -070032
33/*
34 * Module parameter information and variables
35 */
Vikas Chaudharya7380a62012-02-27 03:08:56 -080036static int ql4xdisablesysfsboot = 1;
Mike Christie13483732011-12-01 21:38:41 -060037module_param(ql4xdisablesysfsboot, int, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(ql4xdisablesysfsboot,
Karen Higginsa4e8a712012-01-11 02:44:20 -080039 " 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)");
Mike Christie13483732011-12-01 21:38:41 -060042
Vikas Chaudhary3573bfb2012-02-27 03:08:57 -080043int ql4xdontresethba;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053044module_param(ql4xdontresethba, int, S_IRUGO | S_IWUSR);
David Somayajuluafaf5a22006-09-19 10:28:00 -070045MODULE_PARM_DESC(ql4xdontresethba,
Karen Higginsa4e8a712012-01-11 02:44:20 -080046 " 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");
David Somayajuluafaf5a22006-09-19 10:28:00 -070049
Karen Higginsa4e8a712012-01-11 02:44:20 -080050int ql4xextended_error_logging;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053051module_param(ql4xextended_error_logging, int, S_IRUGO | S_IWUSR);
Andrew Vasquez11010fe2006-10-06 09:54:59 -070052MODULE_PARM_DESC(ql4xextended_error_logging,
Karen Higginsa4e8a712012-01-11 02:44:20 -080053 " Option to enable extended error logging.\n"
54 "\t\t 0 - no logging (Default)\n"
55 "\t\t 2 - debug logging");
David Somayajuluafaf5a22006-09-19 10:28:00 -070056
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +053057int ql4xenablemsix = 1;
58module_param(ql4xenablemsix, int, S_IRUGO|S_IWUSR);
59MODULE_PARM_DESC(ql4xenablemsix,
Karen Higginsa4e8a712012-01-11 02:44:20 -080060 " 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.");
David C Somayajulu477ffb92007-01-22 12:26:11 -080064
Mike Christied510d962008-07-11 19:50:33 -050065#define QL4_DEF_QDEPTH 32
Vikas Chaudhary8bb40332011-03-21 03:34:31 -070066static int ql4xmaxqdepth = QL4_DEF_QDEPTH;
67module_param(ql4xmaxqdepth, int, S_IRUGO | S_IWUSR);
68MODULE_PARM_DESC(ql4xmaxqdepth,
Karen Higginsa4e8a712012-01-11 02:44:20 -080069 " Maximum queue depth to report for target devices.\n"
70 "\t\t Default: 32.");
Mike Christied510d962008-07-11 19:50:33 -050071
Tej Parkashf7b4aa62012-04-30 04:12:19 -070072static 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
Vikas Chaudhary30387272011-03-21 03:34:32 -070080static int ql4xsess_recovery_tmo = QL4_SESS_RECOVERY_TMO;
81module_param(ql4xsess_recovery_tmo, int, S_IRUGO);
82MODULE_PARM_DESC(ql4xsess_recovery_tmo,
Vikas Chaudhary3573bfb2012-02-27 03:08:57 -080083 " Target Session Recovery Timeout.\n"
Karen Higginsa4e8a712012-01-11 02:44:20 -080084 "\t\t Default: 120 sec.");
Vikas Chaudhary30387272011-03-21 03:34:32 -070085
Tej Parkash068237c82012-05-18 04:41:44 -040086int 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
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500100static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700101/*
102 * SCSI host template entry points
103 */
Adrian Bunk47975472007-04-26 00:35:16 -0700104static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700105
106/*
107 * iSCSI template entry points
108 */
Mike Christiefca9f042012-02-27 03:08:54 -0800109static int qla4xxx_session_get_param(struct iscsi_cls_session *cls_sess,
110 enum iscsi_param param, char *buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700111static int qla4xxx_conn_get_param(struct iscsi_cls_conn *conn,
112 enum iscsi_param param, char *buf);
Mike Christieaa1e93a2007-05-30 12:57:09 -0500113static int qla4xxx_host_get_param(struct Scsi_Host *shost,
114 enum iscsi_host_param param, char *buf);
Mike Christie00c31882011-10-06 03:56:59 -0500115static int qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data,
116 uint32_t len);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500117static int qla4xxx_get_iface_param(struct iscsi_iface *iface,
118 enum iscsi_param_type param_type,
119 int param, char *buf);
Mike Christie5c656af2009-07-15 15:02:59 -0500120static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc);
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500121static 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);
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530146static 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);
Nilesh Javali376738a2012-02-27 03:08:52 -0800149static 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);
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530152
David Somayajuluafaf5a22006-09-19 10:28:00 -0700153/*
154 * SCSI host template entry points
155 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500156static int qla4xxx_queuecommand(struct Scsi_Host *h, struct scsi_cmnd *cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530157static int qla4xxx_eh_abort(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700158static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd);
Mike Christiece545032008-02-29 18:25:20 -0600159static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700160static 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);
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -0400164static umode_t qla4_attr_is_visible(int param_type, int param);
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700165static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type);
Tej Parkashf7b4aa62012-04-30 04:12:19 -0700166static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
167 int reason);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700168
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -0400169/*
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
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530189static struct qla4_8xxx_legacy_intr_set legacy_intr[] =
190 QLA82XX_LEGACY_INTR_CONFIG;
191
David Somayajuluafaf5a22006-09-19 10:28:00 -0700192static 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
Vikas Chaudhary09a0f712010-04-28 11:42:24 +0530198 .eh_abort_handler = qla4xxx_eh_abort,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700199 .eh_device_reset_handler = qla4xxx_eh_device_reset,
Mike Christiece545032008-02-29 18:25:20 -0600200 .eh_target_reset_handler = qla4xxx_eh_target_reset,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700201 .eh_host_reset_handler = qla4xxx_eh_host_reset,
Mike Christie5c656af2009-07-15 15:02:59 -0500202 .eh_timed_out = qla4xxx_eh_cmd_timed_out,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700203
204 .slave_configure = qla4xxx_slave_configure,
205 .slave_alloc = qla4xxx_slave_alloc,
206 .slave_destroy = qla4xxx_slave_destroy,
Tej Parkashf7b4aa62012-04-30 04:12:19 -0700207 .change_queue_depth = qla4xxx_change_queue_depth,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700208
209 .this_id = -1,
210 .cmd_per_lun = 3,
211 .use_clustering = ENABLE_CLUSTERING,
212 .sg_tablesize = SG_ALL,
213
214 .max_sectors = 0xFFFF,
Harish Zunjarrao7ad633c2011-05-17 23:17:11 -0700215 .shost_attrs = qla4xxx_host_attrs,
Vikas Chaudhary95d31262011-08-12 02:51:29 -0700216 .host_reset = qla4xxx_host_reset,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500217 .vendor_id = SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700218};
219
220static struct iscsi_transport qla4xxx_iscsi_transport = {
221 .owner = THIS_MODULE,
222 .name = DRIVER_NAME,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500223 .caps = CAP_TEXT_NEGO |
224 CAP_DATA_PATH_OFFLOAD | CAP_HDRDGST |
225 CAP_DATADGST | CAP_LOGIN_OFFLOAD |
226 CAP_MULTI_R2T,
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -0400227 .attr_is_visible = qla4_attr_is_visible,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500228 .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,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700236 .get_conn_param = qla4xxx_conn_get_param,
Mike Christiefca9f042012-02-27 03:08:54 -0800237 .get_session_param = qla4xxx_session_get_param,
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500238 .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
Mike Christieaa1e93a2007-05-30 12:57:09 -0500248 .get_host_param = qla4xxx_host_get_param,
Mike Christied00efe32011-07-25 13:48:38 -0500249 .set_iface_param = qla4xxx_iface_set_param,
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500250 .get_iface_param = qla4xxx_get_iface_param,
Vikas Chaudharya3559432011-07-25 13:48:51 -0500251 .bsg_request = qla4xxx_bsg_request,
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530252 .send_ping = qla4xxx_send_ping,
Nilesh Javali376738a2012-02-27 03:08:52 -0800253 .get_chap = qla4xxx_get_chap_list,
254 .delete_chap = qla4xxx_delete_chap,
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -0400255 .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,
David Somayajuluafaf5a22006-09-19 10:28:00 -0700262};
263
264static struct scsi_transport_template *qla4xxx_scsi_transport;
265
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +0530266static 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
Vikas Chaudhary5e9bcec2012-08-22 07:55:01 -0400346static umode_t qla4_attr_is_visible(int param_type, int param)
Mike Christie3128c6c2011-07-25 13:48:42 -0500347{
348 switch (param_type) {
Mike Christief27fb2e2011-07-25 13:48:45 -0500349 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:
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800354 case ISCSI_HOST_PARAM_PORT_STATE:
355 case ISCSI_HOST_PARAM_PORT_SPEED:
Mike Christief27fb2e2011-07-25 13:48:45 -0500356 return S_IRUGO;
357 default:
358 return 0;
359 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500360 case ISCSI_PARAM:
361 switch (param) {
Mike Christie590134f2011-10-17 22:42:13 -0500362 case ISCSI_PARAM_PERSISTENT_ADDRESS:
363 case ISCSI_PARAM_PERSISTENT_PORT:
Mike Christie3128c6c2011-07-25 13:48:42 -0500364 case ISCSI_PARAM_CONN_ADDRESS:
365 case ISCSI_PARAM_CONN_PORT:
Mike Christie1d063c12011-07-25 13:48:43 -0500366 case ISCSI_PARAM_TARGET_NAME:
367 case ISCSI_PARAM_TPGT:
368 case ISCSI_PARAM_TARGET_ALIAS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500369 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:
Mike Christiede379202011-08-14 20:42:56 -0500374 case ISCSI_PARAM_IFACE_NAME:
Mike Christiefca9f042012-02-27 03:08:54 -0800375 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:
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -0400381 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:
Mike Christie3128c6c2011-07-25 13:48:42 -0500419 return S_IRUGO;
420 default:
421 return 0;
422 }
Mike Christieb78dbba2011-07-25 13:48:44 -0500423 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:
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500435 case ISCSI_NET_PARAM_VLAN_ID:
436 case ISCSI_NET_PARAM_VLAN_PRIORITY:
437 case ISCSI_NET_PARAM_VLAN_ENABLED:
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700438 case ISCSI_NET_PARAM_MTU:
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700439 case ISCSI_NET_PARAM_PORT:
Mike Christieb78dbba2011-07-25 13:48:44 -0500440 return S_IRUGO;
441 default:
442 return 0;
443 }
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -0400444 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 }
Mike Christie3128c6c2011-07-25 13:48:42 -0500506 }
507
508 return 0;
509}
510
Nilesh Javali376738a2012-02-27 03:08:52 -0800511static 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
Vikas Chaudharyd11b0ca2013-03-22 07:08:31 -0400521 if (is_qla80XX(ha))
Nilesh Javali376738a2012-02-27 03:08:52 -0800522 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
Vikas Chaudharyd11b0ca2013-03-22 07:08:31 -0400625 if (is_qla80XX(ha))
Nilesh Javali376738a2012-02-27 03:08:52 -0800626 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
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500694static 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;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -0500758 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;
Vikas Chaudhary943c1572011-08-01 03:26:13 -0700790 case ISCSI_NET_PARAM_MTU:
791 len = sprintf(buf, "%d\n", ha->ip_config.eth_mtu_size);
792 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -0700793 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;
Vikas Chaudharyed1086e2011-07-25 13:48:41 -0500799 default:
800 len = -ENOSYS;
801 }
802
803 return len;
804}
805
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500806static struct iscsi_endpoint *
807qla4xxx_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
808 int non_blocking)
809{
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;
816
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 }
824
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;
851}
852
853static int qla4xxx_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
854{
855 struct qla_endpoint *qla_ep;
856 struct scsi_qla_host *ha;
857 int ret = 0;
858
859 DEBUG2(printk(KERN_INFO "Func: %s\n", __func__));
860 qla_ep = ep->dd_data;
861 ha = to_qla_host(qla_ep->host);
862
Mike Christie13483732011-12-01 21:38:41 -0600863 if (adapter_up(ha) && !test_bit(AF_BUILD_DDB_LIST, &ha->flags))
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500864 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__));
914
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;
928 }
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,
Masanari Iida59e13d42012-04-25 00:24:16 +0900934 "Unable to retrieve iscsi stats\n");
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500935 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
Mike Christie5c656af2009-07-15 15:02:59 -0500969static enum blk_eh_timer_return qla4xxx_eh_cmd_timed_out(struct scsi_cmnd *sc)
970{
971 struct iscsi_cls_session *session;
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500972 struct iscsi_session *sess;
973 unsigned long flags;
974 enum blk_eh_timer_return ret = BLK_EH_NOT_HANDLED;
Mike Christie5c656af2009-07-15 15:02:59 -0500975
976 session = starget_to_session(scsi_target(sc->device));
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500977 sess = session->dd_data;
Mike Christie5c656af2009-07-15 15:02:59 -0500978
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500979 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);
Mike Christie5c656af2009-07-15 15:02:59 -0500983
Manish Rangankarb3a271a2011-07-25 13:48:53 -0500984 return ret;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700985}
986
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800987static void qla4xxx_set_port_speed(struct Scsi_Host *shost)
988{
989 struct scsi_qla_host *ha = to_qla_host(shost);
Vikas Chaudharye16d1662012-03-02 01:55:31 -0800990 struct iscsi_cls_host *ihost = shost->shost_data;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -0800991 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);
Vikas Chaudharye16d1662012-03-02 01:55:31 -08001015 struct iscsi_cls_host *ihost = shost->shost_data;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -08001016 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
Mike Christieaa1e93a2007-05-30 12:57:09 -05001024static 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:
Michael Chan7ffc49a2007-12-24 21:28:09 -08001032 len = sysfs_format_mac(buf, ha->my_mac, MAC_ADDR_LEN);
Mike Christieaa1e93a2007-05-30 12:57:09 -05001033 break;
Mike Christie22236962007-05-30 12:57:24 -05001034 case ISCSI_HOST_PARAM_IPADDRESS:
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001035 len = sprintf(buf, "%pI4\n", &ha->ip_config.ip_address);
Mike Christie22236962007-05-30 12:57:24 -05001036 break;
Mike Christie8ad57812007-05-30 12:57:13 -05001037 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie22236962007-05-30 12:57:24 -05001038 len = sprintf(buf, "%s\n", ha->name_string);
Mike Christie8ad57812007-05-30 12:57:13 -05001039 break;
Vikas Chaudhary3254dbe2012-01-19 03:06:56 -08001040 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;
Mike Christieaa1e93a2007-05-30 12:57:09 -05001048 default:
1049 return -ENOSYS;
1050 }
1051
1052 return len;
1053}
1054
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001055static 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
Mike Christied00efe32011-07-25 13:48:38 -05001127static 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:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001202 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -05001203 init_fw_cb->ipv6_opts |=
1204 cpu_to_le16(IPV6_OPT_IPV6_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001205 qla4xxx_create_ipv6_iface(ha);
1206 } else {
Mike Christied00efe32011-07-25 13:48:38 -05001207 init_fw_cb->ipv6_opts &=
1208 cpu_to_le16(~IPV6_OPT_IPV6_PROTOCOL_ENABLE &
1209 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001210 qla4xxx_destroy_ipv6_iface(ha);
1211 }
Mike Christied00efe32011-07-25 13:48:38 -05001212 break;
Mike Christie2d636732011-10-11 17:55:11 -05001213 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -05001214 if (iface_param->len != sizeof(init_fw_cb->ipv6_vlan_tag))
1215 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -05001216 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);
Mike Christied00efe32011-07-25 13:48:38 -05001226 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -07001227 case ISCSI_NET_PARAM_MTU:
1228 init_fw_cb->eth_mtu_size =
1229 cpu_to_le16(*(uint16_t *)iface_param->value);
1230 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -07001231 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;
Mike Christied00efe32011-07-25 13:48:38 -05001239 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:
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001274 if (iface_param->value[0] == ISCSI_IFACE_ENABLE) {
Mike Christied00efe32011-07-25 13:48:38 -05001275 init_fw_cb->ipv4_ip_opts |=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001276 cpu_to_le16(IPOPT_IPV4_PROTOCOL_ENABLE);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001277 qla4xxx_create_ipv4_iface(ha);
1278 } else {
Mike Christied00efe32011-07-25 13:48:38 -05001279 init_fw_cb->ipv4_ip_opts &=
Vikas Chaudhary2bab08f2011-07-25 13:48:39 -05001280 cpu_to_le16(~IPOPT_IPV4_PROTOCOL_ENABLE &
Mike Christied00efe32011-07-25 13:48:38 -05001281 0xFFFF);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05001282 qla4xxx_destroy_ipv4_iface(ha);
1283 }
Mike Christied00efe32011-07-25 13:48:38 -05001284 break;
Mike Christie2d636732011-10-11 17:55:11 -05001285 case ISCSI_NET_PARAM_VLAN_TAG:
Mike Christied00efe32011-07-25 13:48:38 -05001286 if (iface_param->len != sizeof(init_fw_cb->ipv4_vlan_tag))
1287 break;
Vikas Chaudhary6ac73e82011-07-25 13:48:49 -05001288 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);
Mike Christied00efe32011-07-25 13:48:38 -05001298 break;
Vikas Chaudhary943c1572011-08-01 03:26:13 -07001299 case ISCSI_NET_PARAM_MTU:
1300 init_fw_cb->eth_mtu_size =
1301 cpu_to_le16(*(uint16_t *)iface_param->value);
1302 break;
Vikas Chaudhary2ada7fc2011-08-01 03:26:19 -07001303 case ISCSI_NET_PARAM_PORT:
1304 init_fw_cb->ipv4_port =
1305 cpu_to_le16(*(uint16_t *)iface_param->value);
1306 break;
Mike Christied00efe32011-07-25 13:48:38 -05001307 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
Mike Christie00c31882011-10-06 03:56:59 -05001337qla4xxx_iface_set_param(struct Scsi_Host *shost, void *data, uint32_t len)
Mike Christied00efe32011-07-25 13:48:38 -05001338{
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];
Mike Christie00c31882011-10-06 03:56:59 -05001346 uint32_t rem = len;
1347 struct nlattr *attr;
Mike Christied00efe32011-07-25 13:48:38 -05001348
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
Mike Christie00c31882011-10-06 03:56:59 -05001368 nla_for_each_attr(attr, data, len, rem) {
1369 iface_param = nla_data(attr);
Mike Christied00efe32011-07-25 13:48:38 -05001370
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 }
Mike Christied00efe32011-07-25 13:48:38 -05001406 }
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
Vikas Chaudharyce505f92011-12-01 22:42:10 -08001420 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);
Mike Christied00efe32011-07-25 13:48:38 -05001430
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
Mike Christiefca9f042012-02-27 03:08:54 -08001452static 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)
Manish Rangankarc7a5e0d2013-01-20 23:51:04 -05001467 len = sprintf(buf, "\n");
1468 else
1469 len = sprintf(buf, "%hu\n", idx);
Mike Christiefca9f042012-02-27 03:08:54 -08001470 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)
Manish Rangankarc7a5e0d2013-01-20 23:51:04 -05001476 len = sprintf(buf, "\n");
1477 else
1478 len = sprintf(buf, "%hu\n", idx);
Mike Christiefca9f042012-02-27 03:08:54 -08001479 break;
1480 default:
1481 return iscsi_session_get_param(cls_sess, param, buf);
1482 }
1483
1484 return len;
1485}
1486
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001487static int qla4xxx_conn_get_param(struct iscsi_cls_conn *cls_conn,
David Somayajuluafaf5a22006-09-19 10:28:00 -07001488 enum iscsi_param param, char *buf)
1489{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001490 struct iscsi_conn *conn;
1491 struct qla_conn *qla_conn;
1492 struct sockaddr *dst_addr;
1493 int len = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001494
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001495 conn = cls_conn->dd_data;
1496 qla_conn = conn->dd_data;
Manish Rangankard46bdeb12012-08-07 07:57:13 -04001497 dst_addr = (struct sockaddr *)&qla_conn->qla_ep->dst_addr;
David Somayajuluafaf5a22006-09-19 10:28:00 -07001498
1499 switch (param) {
1500 case ISCSI_PARAM_CONN_PORT:
David Somayajuluafaf5a22006-09-19 10:28:00 -07001501 case ISCSI_PARAM_CONN_ADDRESS:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001502 return iscsi_conn_get_addr_param((struct sockaddr_storage *)
1503 dst_addr, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001504 default:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001505 return iscsi_conn_get_param(cls_conn, param, buf);
David Somayajuluafaf5a22006-09-19 10:28:00 -07001506 }
1507
1508 return len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001509
David Somayajuluafaf5a22006-09-19 10:28:00 -07001510}
1511
Mike Christie13483732011-12-01 21:38:41 -06001512int 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
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001656static 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;
Mike Christie13483732011-12-01 21:38:41 -06001665 uint16_t ddb_index;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001666 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);
Manish Rangankar736cf362011-10-07 16:55:46 -07001679
Mike Christie13483732011-12-01 21:38:41 -06001680 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
1681 if (ret == QLA_ERROR)
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001682 return NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001683
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;
Mike Christie13483732011-12-01 21:38:41 -06001697 ddb_entry->unblock_sess = qla4xxx_unblock_ddb;
1698 ddb_entry->ddb_change = qla4xxx_ddb_change;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001699 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;
Manish Rangankar90599b62012-04-23 22:32:34 -07001711 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;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001716
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
Manish Rangankar90599b62012-04-23 22:32:34 -07001722 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:
Manish Rangankar736cf362011-10-07 16:55:46 -07001747 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
1748
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001749 spin_lock_irqsave(&ha->hardware_lock, flags);
1750 qla4xxx_free_ddb(ha, ddb_entry);
1751 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Manish Rangankar90599b62012-04-23 22:32:34 -07001752
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001753 iscsi_session_teardown(cls_sess);
Manish Rangankar90599b62012-04-23 22:32:34 -07001754
1755 if (fw_ddb_entry)
1756 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1757 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001758}
1759
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001760static 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);
Mike Christieff1d0312011-12-01 21:38:43 -06001770 if (!cls_conn)
1771 return NULL;
1772
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001773 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;
Mike Christie13483732011-12-01 21:38:41 -06001805 struct dev_db_entry *fw_ddb_entry = NULL;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001806 dma_addr_t fw_ddb_entry_dma;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001807 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
Mike Christie13483732011-12-01 21:38:41 -06001816 /* 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
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001828 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__);
Mike Christie13483732011-12-01 21:38:41 -06001833 ret = -ENOMEM;
1834 goto exit_conn_start;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001835 }
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 ==
Manish Rangankarf922da72011-10-07 16:55:49 -07001846 DDB_DS_SESSION_ACTIVE) {
Mike Christie13483732011-12-01 21:38:41 -06001847 ddb_entry->unblock_sess(ddb_entry->sess);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001848 goto exit_set_param;
Manish Rangankarf922da72011-10-07 16:55:49 -07001849 }
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001850
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) {
Manish Rangankar0e7e8502011-07-25 13:48:54 -05001858 ql4_printk(KERN_ERR, ha, "%s: Login failed: %s\n", __func__,
1859 sess->targetname);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001860 ret = -EINVAL;
1861 goto exit_conn_start;
1862 }
1863
Manish Rangankar98270ab2011-10-07 16:55:47 -07001864 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));
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001869
1870exit_set_param:
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001871 ret = 0;
1872
1873exit_conn_start:
Mike Christie13483732011-12-01 21:38:41 -06001874 if (fw_ddb_entry)
1875 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
1876 fw_ddb_entry, fw_ddb_entry_dma);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001877 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__);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001896}
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
Manish Rangankar69ca2162011-10-07 16:55:50 -07001975 task_data->resp_len = task->conn->max_recv_dlength + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001976 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
Manish Rangankar69ca2162011-10-07 16:55:50 -07001983 task_data->req_len = task->data_count + hdr_len;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001984 task_data->req_buffer = dma_alloc_coherent(&ha->pdev->dev,
Manish Rangankar69ca2162011-10-07 16:55:50 -07001985 task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05001986 &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)
Manish Rangankar69ca2162011-10-07 16:55:50 -07002003 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002004 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);
Manish Rangankar69ca2162011-10-07 16:55:50 -07002032 dma_free_coherent(&ha->pdev->dev, task_data->req_len,
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002033 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
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002052static 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)) {
Adheer Chandravanshic962c182013-03-25 08:08:32 -04002062 rc = iscsi_switch_str_param(&sess->portal_type,
2063 PORTAL_TYPE_IPV6);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002064 if (rc)
2065 goto exit_copy;
2066 } else {
Adheer Chandravanshic962c182013-03-25 08:08:32 -04002067 rc = iscsi_switch_str_param(&sess->portal_type,
2068 PORTAL_TYPE_IPV4);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002069 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);
Adheer Chandravanshic962c182013-03-25 08:08:32 -04002204 if (!strncmp(sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002205 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);
Adheer Chandravanshifbcd4832013-04-17 05:15:29 -04002257 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);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002259 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);
Adheer Chandravanshifbcd4832013-04-17 05:15:29 -04002262 fw_ddb_entry->stat_sn = cpu_to_le32(conn->statsn);
2263 fw_ddb_entry->exp_stat_sn = cpu_to_le32(conn->exp_statsn);
Adheer Chandravanshi65560162013-07-08 08:33:06 -04002264 fw_ddb_entry->ddb_link = cpu_to_le16(sess->discovery_parent_idx);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002265 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
Adheer Chandravanshi84595802013-07-08 08:33:07 -04002271 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
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04002276 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
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002302static 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
Mike Christie13483732011-12-01 21:38:41 -06002397static 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;
Nilesh Javali376738a2012-02-27 03:08:52 -08002404 struct ddb_entry *ddb_entry;
Mike Christie13483732011-12-01 21:38:41 -06002405 struct iscsi_conn *conn;
2406 char ip_addr[DDB_IPADDR_LEN];
2407 uint16_t options = 0;
2408
2409 sess = cls_sess->dd_data;
Nilesh Javali376738a2012-02-27 03:08:52 -08002410 ddb_entry = sess->dd_data;
Mike Christie13483732011-12-01 21:38:41 -06002411 conn = cls_conn->dd_data;
2412
Nilesh Javali376738a2012-02-27 03:08:52 -08002413 ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
2414
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002415 qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
Mike Christie13483732011-12-01 21:38:41 -06002416
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002417 sess->def_taskmgmt_tmo = le16_to_cpu(fw_ddb_entry->def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06002418 conn->persistent_port = le16_to_cpu(fw_ddb_entry->port);
2419
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002420 memset(ip_addr, 0, sizeof(ip_addr));
Mike Christie13483732011-12-01 21:38:41 -06002421 options = le16_to_cpu(fw_ddb_entry->options);
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002422 if (options & DDB_OPT_IPV6_DEVICE) {
2423 iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv6", 4);
Mike Christie13483732011-12-01 21:38:41 -06002424
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002425 memset(ip_addr, 0, sizeof(ip_addr));
2426 sprintf(ip_addr, "%pI6", fw_ddb_entry->ip_addr);
2427 } else {
2428 iscsi_set_param(cls_conn, ISCSI_PARAM_PORTAL_TYPE, "ipv4", 4);
2429 sprintf(ip_addr, "%pI4", fw_ddb_entry->ip_addr);
2430 }
2431
2432 iscsi_set_param(cls_conn, ISCSI_PARAM_PERSISTENT_ADDRESS,
2433 (char *)ip_addr, buflen);
Mike Christie13483732011-12-01 21:38:41 -06002434 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);
Mike Christie13483732011-12-01 21:38:41 -06002438}
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
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002480void 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__);
Mike Christie13483732011-12-01 21:38:41 -06002496 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002497 }
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));
Mike Christie13483732011-12-01 21:38:41 -06002506 goto exit_session_conn_param;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002507 }
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
Mike Christie13483732011-12-01 21:38:41 -06002515 /* Update timers after login */
2516 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08002517 (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;
Mike Christie13483732011-12-01 21:38:41 -06002520 ddb_entry->default_time2wait =
2521 le16_to_cpu(fw_ddb_entry->iscsi_def_time2wait);
2522
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002523 /* Update params */
Nilesh Javali376738a2012-02-27 03:08:52 -08002524 ddb_entry->chap_tbl_idx = le16_to_cpu(fw_ddb_entry->chap_tbl_idx);
Adheer Chandravanshi8cc91d42013-07-01 05:54:13 -04002525 qla4xxx_copy_to_sess_conn_params(conn, sess, fw_ddb_entry);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002526
2527 memcpy(sess->initiatorname, ha->name_string,
2528 min(sizeof(ha->name_string), sizeof(sess->initiatorname)));
Mike Christie13483732011-12-01 21:38:41 -06002529
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);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002534}
2535
David Somayajuluafaf5a22006-09-19 10:28:00 -07002536/*
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/***
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002560 * qla4xxx_mark_device_missing - blocks the session
2561 * @cls_session: Pointer to the session to be blocked
David Somayajuluafaf5a22006-09-19 10:28:00 -07002562 * @ddb_entry: Pointer to device database entry
2563 *
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302564 * This routine marks a device missing and close connection.
David Somayajuluafaf5a22006-09-19 10:28:00 -07002565 **/
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002566void qla4xxx_mark_device_missing(struct iscsi_cls_session *cls_session)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002567{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002568 iscsi_block_session(cls_session);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002569}
2570
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302571/**
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{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002579 iscsi_host_for_each_session(ha->host, qla4xxx_mark_device_missing);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302580}
2581
David Somayajuluafaf5a22006-09-19 10:28:00 -07002582static struct srb* qla4xxx_get_new_srb(struct scsi_qla_host *ha,
2583 struct ddb_entry *ddb_entry,
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002584 struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002585{
2586 struct srb *srb;
2587
2588 srb = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2589 if (!srb)
2590 return srb;
2591
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302592 kref_init(&srb->srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002593 srb->ha = ha;
2594 srb->ddb = ddb_entry;
2595 srb->cmd = cmd;
2596 srb->flags = 0;
Vikas Chaudhary53698872010-04-28 11:41:59 +05302597 CMD_SP(cmd) = (void *)srb;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002598
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) {
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +09002607 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002608 srb->flags &= ~SRB_DMA_VALID;
2609 }
Vikas Chaudhary53698872010-04-28 11:41:59 +05302610 CMD_SP(cmd) = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002611}
2612
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302613void qla4xxx_srb_compl(struct kref *ref)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002614{
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302615 struct srb *srb = container_of(ref, struct srb, srb_ref);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002616 struct scsi_cmnd *cmd = srb->cmd;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05302617 struct scsi_qla_host *ha = srb->ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002618
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.
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002628 * @host: scsi host
David Somayajuluafaf5a22006-09-19 10:28:00 -07002629 * @cmd: Pointer to Linux's SCSI command structure
David Somayajuluafaf5a22006-09-19 10:28:00 -07002630 *
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 **/
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002639static int qla4xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07002640{
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002641 struct scsi_qla_host *ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002642 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christie7fb19212008-01-31 13:36:45 -06002643 struct iscsi_cls_session *sess = ddb_entry->sess;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002644 struct srb *srb;
2645 int rval;
2646
Lalit Chandivade2232be02010-07-30 14:38:47 +05302647 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
Mike Christie7fb19212008-01-31 13:36:45 -06002655 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
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302666 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) ||
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002672 !test_bit(AF_LINK_UP, &ha->flags) ||
Nilesh Javali026fbd32013-01-20 23:50:58 -05002673 test_bit(AF_LOOPBACK, &ha->flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302674 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))
David C Somayajulu477ffb92007-01-22 12:26:11 -08002675 goto qc_host_busy;
2676
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002677 srb = qla4xxx_get_new_srb(ha, ddb_entry, cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002678 if (!srb)
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002679 goto qc_host_busy;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002680
2681 rval = qla4xxx_send_command_to_isp(ha, srb);
2682 if (rval != QLA_SUCCESS)
2683 goto qc_host_busy_free_sp;
2684
David Somayajuluafaf5a22006-09-19 10:28:00 -07002685 return 0;
2686
2687qc_host_busy_free_sp:
2688 qla4xxx_srb_free_dma(ha, srb);
2689 mempool_free(srb, ha->srb_mempool);
2690
David Somayajuluafaf5a22006-09-19 10:28:00 -07002691qc_host_busy:
2692 return SCSI_MLQUEUE_HOST_BUSY;
2693
2694qc_fail_command:
Vikas Chaudhary8f0722c2011-05-17 23:17:10 -07002695 cmd->scsi_done(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07002696
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
Tej Parkash068237c82012-05-18 04:41:44 -04002712 if (ha->fw_dump)
2713 vfree(ha->fw_dump);
2714
David Somayajuluafaf5a22006-09-19 10:28:00 -07002715 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;
Tej Parkash068237c82012-05-18 04:41:44 -04002724 ha->fw_dump = NULL;
2725 ha->fw_dump_size = 0;
David Somayajuluafaf5a22006-09-19 10:28:00 -07002726
2727 /* Free srb pool. */
2728 if (ha->srb_mempool)
2729 mempool_destroy(ha->srb_mempool);
2730
2731 ha->srb_mempool = NULL;
2732
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002733 if (ha->chap_dma_pool)
2734 dma_pool_destroy(ha->chap_dma_pool);
2735
Lalit Chandivade45494152011-10-07 16:55:42 -07002736 if (ha->chap_list)
2737 vfree(ha->chap_list);
2738 ha->chap_list = NULL;
2739
Mike Christie13483732011-12-01 21:38:41 -06002740 if (ha->fw_ddb_dma_pool)
2741 dma_pool_destroy(ha->fw_ddb_dma_pool);
2742
David Somayajuluafaf5a22006-09-19 10:28:00 -07002743 /* release io space registers */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302744 if (is_qla8022(ha)) {
2745 if (ha->nx_pcibase)
2746 iounmap(
2747 (struct device_reg_82xx __iomem *)ha->nx_pcibase);
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002748 } 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) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07002753 iounmap(ha->reg);
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002754 }
2755
2756 if (ha->reset_tmplt.buff)
2757 vfree(ha->reset_tmplt.buff);
2758
David Somayajuluafaf5a22006-09-19 10:28:00 -07002759 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) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302782 ql4_printk(KERN_WARNING, ha,
2783 "Memory Allocation failed - queues.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002784
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) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05302819 ql4_printk(KERN_WARNING, ha,
2820 "Memory Allocation failed - SRB Pool.\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07002821
2822 goto mem_alloc_error_exit;
2823 }
2824
Manish Rangankarb3a271a2011-07-25 13:48:53 -05002825 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
Mike Christie13483732011-12-01 21:38:41 -06002834 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
David Somayajuluafaf5a22006-09-19 10:28:00 -07002844 return QLA_SUCCESS;
2845
2846mem_alloc_error_exit:
2847 qla4xxx_mem_free(ha);
2848 return QLA_ERROR;
2849}
2850
2851/**
Mike Hernandez4f770832012-01-11 02:44:15 -08002852 * 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
Vikas Chaudhary33693c72012-08-22 07:55:04 -04002862 temp = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_TEMP_STATE);
Mike Hernandez4f770832012-01-11 02:44:15 -08002863
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
2887/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302888 * qla4_8xxx_check_fw_alive - Check firmware health
2889 * @ha: Pointer to host adapter structure.
2890 *
2891 * Context: Interrupt
2892 **/
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002893static int qla4_8xxx_check_fw_alive(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302894{
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002895 uint32_t fw_heartbeat_counter;
2896 int status = QLA_SUCCESS;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302897
Vikas Chaudhary33693c72012-08-22 07:55:04 -04002898 fw_heartbeat_counter = qla4_8xxx_rd_direct(ha,
2899 QLA8XXX_PEG_ALIVE_COUNTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05302900 /* 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__));
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002905 return status;
Lalit Chandivade2232be02010-07-30 14:38:47 +05302906 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302907
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;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002913 qla4_8xxx_dump_peg_reg(ha);
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002914 status = QLA_ERROR;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302915 }
Lalit Chandivade99457d72010-10-06 22:49:32 -07002916 } else
2917 ha->seconds_since_last_heartbeat = 0;
2918
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302919 ha->fw_heartbeat_counter = fw_heartbeat_counter;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08002920 return status;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302921}
2922
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002923static 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
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302965/**
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{
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002973 uint32_t dev_state;
Vikas Chaudhary6cf94122013-03-07 05:43:10 -05002974 uint32_t idc_ctrl;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05302975
2976 /* don't poll if reset is going on */
Lalit Chandivaded56a1f72010-12-02 22:12:45 -08002977 if (!(test_bit(DPC_RESET_ACTIVE, &ha->dpc_flags) ||
2978 test_bit(DPC_RESET_HA, &ha->dpc_flags) ||
Vikas Chaudhary977f46a2011-05-17 23:17:08 -07002979 test_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags))) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04002980 dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
Mike Hernandez4f770832012-01-11 02:44:15 -08002981
2982 if (qla4_8xxx_check_temp(ha)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002983 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 }
Mike Hernandez4f770832012-01-11 02:44:15 -08002989 set_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags);
2990 qla4xxx_wake_dpc(ha);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04002991 } else if (dev_state == QLA8XXX_DEV_NEED_RESET &&
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04002992 !test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary6cf94122013-03-07 05:43:10 -05002993
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
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003008 if (is_qla8032(ha) ||
3009 (is_qla8022(ha) && !ql4xdontresethba)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08003010 set_bit(DPC_RESET_HA, &ha->dpc_flags);
3011 qla4xxx_wake_dpc(ha);
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08003012 }
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04003013 } else if (dev_state == QLA8XXX_DEV_NEED_QUIESCENT &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303014 !test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags)) {
Vikas Chaudhary3930b8c2010-12-02 22:12:47 -08003015 ql4_printk(KERN_INFO, ha, "%s: HW State: NEED QUIES!\n",
3016 __func__);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303017 set_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags);
3018 qla4xxx_wake_dpc(ha);
3019 } else {
3020 /* Check firmware health */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003021 if (qla4_8xxx_check_fw_alive(ha))
3022 qla4_8xxx_process_fw_error(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303023 }
3024 }
3025}
3026
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08003027static void qla4xxx_check_relogin_flash_ddb(struct iscsi_cls_session *cls_sess)
Mike Christie13483732011-12-01 21:38:41 -06003028{
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
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303081/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003082 * 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{
David Somayajuluafaf5a22006-09-19 10:28:00 -07003087 int start_dpc = 0;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303088 uint16_t w;
3089
Mike Christie13483732011-12-01 21:38:41 -06003090 iscsi_host_for_each_session(ha->host, qla4xxx_check_relogin_flash_ddb);
3091
Lalit Chandivade2232be02010-07-30 14:38:47 +05303092 /* 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);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003103
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003104 if (is_qla80XX(ha))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303105 qla4_8xxx_watchdog(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303106
Vikas Chaudharyee996a62012-08-22 07:55:05 -04003107 if (is_qla40XX(ha)) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303108 /* 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 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003116 }
3117
Vikas Chaudharyff884432011-08-29 23:43:02 +05303118 /* Process any deferred work. */
3119 if (!list_empty(&ha->work_list))
3120 start_dpc++;
3121
David Somayajuluafaf5a22006-09-19 10:28:00 -07003122 /* Wakeup the dpc routine for this adapter, if needed. */
Lalit Chandivade1b468072011-05-17 23:17:09 -07003123 if (start_dpc ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07003124 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) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303127 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07003128 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
3129 test_bit(DPC_GET_DHCP_IP_ADDR, &ha->dpc_flags) ||
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303130 test_bit(DPC_LINK_CHANGED, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303131 test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags) ||
3132 test_bit(DPC_HA_NEED_QUIESCENT, &ha->dpc_flags) ||
Lalit Chandivade1b468072011-05-17 23:17:09 -07003133 test_bit(DPC_AEN, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003134 DEBUG2(printk("scsi%ld: %s: scheduling dpc routine"
3135 " - dpc flags = 0x%lx\n",
3136 ha->host_no, __func__, ha->dpc_flags));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303137 qla4xxx_wake_dpc(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003138 }
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;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003156 unsigned long flags;
3157 struct scsi_cmnd *cmd;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003158
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303159 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)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003165 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);
Mike Christiea1e00632010-10-26 05:45:30 -07003169 /*
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))
David Somayajuluafaf5a22006-09-19 10:28:00 -07003176 break;
3177 }
3178 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3179
3180 /* If No Commands are pending, wait is complete */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303181 if (index == ha->host->can_queue)
3182 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003183
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303184 msleep(1000);
3185 }
3186 /* If we timed out on waiting for commands to come back
3187 * return ERROR. */
3188 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003189}
3190
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303191int qla4xxx_hw_reset(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003192{
David Somayajuluafaf5a22006-09-19 10:28:00 -07003193 uint32_t ctrl_status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08003194 unsigned long flags = 0;
3195
3196 DEBUG2(printk(KERN_ERR "scsi%ld: %s\n", ha->host_no, __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003197
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303198 if (ql4xxx_lock_drvr_wait(ha) != QLA_SUCCESS)
3199 return QLA_ERROR;
3200
David Somayajuluafaf5a22006-09-19 10:28:00 -07003201 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);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303216 return QLA_SUCCESS;
David C Somayajulu477ffb92007-01-22 12:26:11 -08003217}
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;
Vikas Chaudharyf931c532010-10-06 22:48:07 -07003227 int status;
David C Somayajulu477ffb92007-01-22 12:26:11 -08003228 uint32_t ctrl_status;
3229
Vikas Chaudharyf931c532010-10-06 22:48:07 -07003230 status = qla4xxx_hw_reset(ha);
3231 if (status != QLA_SUCCESS)
3232 return status;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003233
Vikas Chaudharyf931c532010-10-06 22:48:07 -07003234 status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003235 /* 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
3317/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303318 * qla4xxx_abort_active_cmds - returns all outstanding i/o requests to O.S.
David Somayajuluafaf5a22006-09-19 10:28:00 -07003319 * @ha: Pointer to host adapter structure.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303320 * @res: returned scsi status
David Somayajuluafaf5a22006-09-19 10:28:00 -07003321 *
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 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303327static void qla4xxx_abort_active_cmds(struct scsi_qla_host *ha, int res)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003328{
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) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303337 srb->cmd->result = res;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05303338 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003339 }
3340 }
3341 spin_unlock_irqrestore(&ha->hardware_lock, flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003342}
3343
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303344void 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");
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303350
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
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003356static 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;
Mike Christie13483732011-12-01 21:38:41 -06003364
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);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003370}
3371
David Somayajuluafaf5a22006-09-19 10:28:00 -07003372/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07003373 * qla4xxx_recover_adapter - recovers adapter after a fatal error
3374 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07003375 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303376static int qla4xxx_recover_adapter(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003377{
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303378 int status = QLA_ERROR;
3379 uint8_t reset_chip = 0;
Sarang Radke8e0f3a62011-12-01 22:42:09 -08003380 uint32_t dev_state;
Shyam Sunder9ee91a32011-12-01 22:42:13 -08003381 unsigned long wait;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003382
3383 /* Stall incoming I/O until we are done */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303384 scsi_block_requests(ha->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003385 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003386 clear_bit(AF_LINK_UP, &ha->flags);
Mike Christie50a29ae2008-03-04 13:26:53 -06003387
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303388 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: adapter OFFLINE\n", __func__));
David Somayajuluafaf5a22006-09-19 10:28:00 -07003389
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303390 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003391
Tej Parkash546fef22012-09-20 07:35:12 -04003392 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
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003400 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
3401
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303402 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
3403 reset_chip = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003404
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303405 /* 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;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003410 }
3411
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003412 /* For the ISP-8xxx adapter, issue a stop_firmware if invoked
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303413 * from eh_host_reset or ioctl module */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003414 if (is_qla80XX(ha) && !reset_chip &&
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303415 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) {
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07003422 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
3423 qla4xxx_cmd_wait(ha);
Vikas Chaudhary5c19b922012-11-23 06:58:38 -05003424
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303425 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 }
3436
3437 /* Issue full chip reset if recovering from a catastrophic error,
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003438 * or if stop_firmware fails for ISP-8xxx.
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303439 * This is the default case for ISP-4xxx */
Vikas Chaudharyee996a62012-08-22 07:55:05 -04003440 if (is_qla40XX(ha) || reset_chip) {
3441 if (is_qla40XX(ha))
Shyam Sunder9ee91a32011-12-01 22:42:13 -08003442 goto chip_reset;
3443
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003444 /* Check if 8XXX firmware is alive or not
Shyam Sunder9ee91a32011-12-01 22:42:13 -08003445 * 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 }
Vikas Chaudharyda106212012-08-22 07:45:26 -04003460chip_reset:
Nilesh Javali2bd1e2b2010-10-06 22:49:20 -07003461 if (!test_bit(AF_FW_RECOVERY, &ha->flags))
3462 qla4xxx_cmd_wait(ha);
Vikas Chaudharyda106212012-08-22 07:45:26 -04003463
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303464 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 }
3471
3472 /* Flush any pending ddb changed AENs */
3473 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
3474
3475recover_ha_init_adapter:
3476 /* Upon successful firmware/chip reset, re-initialize the adapter */
3477 if (status == QLA_SUCCESS) {
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 */
Vikas Chaudharyee996a62012-08-22 07:55:05 -04003481 if (is_qla40XX(ha) && (ha->mac_index == 3))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303482 ssleep(6);
3483
3484 /* NOTE: AF_ONLINE flag set upon successful completion of
3485 * qla4xxx_initialize_adapter */
Mike Christie13483732011-12-01 21:38:41 -06003486 status = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303487 }
3488
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)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003494 /* Adapter initialization failed, see if we can retry
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303495 * 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 */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003499 if (is_qla80XX(ha)) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003500 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);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04003504 if (dev_state == QLA8XXX_DEV_FAILED) {
Sarang Radke8e0f3a62011-12-01 22:42:09 -08003505 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
David Somayajuluafaf5a22006-09-19 10:28:00 -07003519 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));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303543 qla4xxx_dead_adapter_cleanup(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003544 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3545 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303546 clear_bit(DPC_RESET_HA_FW_CONTEXT,
David Somayajuluafaf5a22006-09-19 10:28:00 -07003547 &ha->dpc_flags);
3548 status = QLA_ERROR;
3549 }
3550 }
3551 } else {
3552 clear_bit(DPC_RESET_HA, &ha->dpc_flags);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303553 clear_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003554 clear_bit(DPC_RETRY_RESET_HA, &ha->dpc_flags);
3555 }
3556
Sarang Radke8e0f3a62011-12-01 22:42:09 -08003557exit_recover:
David Somayajuluafaf5a22006-09-19 10:28:00 -07003558 ha->adapter_error_count++;
3559
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303560 if (test_bit(AF_ONLINE, &ha->flags))
3561 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003562
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303563 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,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003567 status == QLA_ERROR ? "FAILED" : "SUCCEEDED"));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303568
David Somayajuluafaf5a22006-09-19 10:28:00 -07003569 return status;
3570}
3571
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003572static void qla4xxx_relogin_devices(struct iscsi_cls_session *cls_session)
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003573{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003574 struct iscsi_session *sess;
3575 struct ddb_entry *ddb_entry;
3576 struct scsi_qla_host *ha;
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003577
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003578 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 */
Mike Christie13483732011-12-01 21:38:41 -06003589 if (ddb_entry->ddb_type == FLASH_DDB) {
Adheer Chandravanshi99c6a332013-07-08 08:33:05 -04003590 if (!(test_bit(DF_RELOGIN, &ddb_entry->flags) ||
3591 test_bit(DF_DISABLE_RELOGIN,
3592 &ddb_entry->flags)))
Mike Christie13483732011-12-01 21:38:41 -06003593 qla4xxx_arm_relogin_timer(ddb_entry);
3594 } else
3595 iscsi_session_failure(cls_session->dd_data,
3596 ISCSI_ERR_CONN_FAILED);
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003597 }
3598 }
3599}
3600
Mike Christie13483732011-12-01 21:38:41 -06003601int 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;
Manish Rangankar80c53e62012-08-07 07:57:15 -04003631 int status = QLA_SUCCESS;
Mike Christie13483732011-12-01 21:38:41 -06003632
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);
Mike Christie13483732011-12-01 21:38:41 -06003639
Manish Rangankar80c53e62012-08-07 07:57:15 -04003640 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;
Mike Christie13483732011-12-01 21:38:41 -06003653}
3654
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003655static void qla4xxx_relogin_all_devices(struct scsi_qla_host *ha)
3656{
3657 iscsi_host_for_each_session(ha->host, qla4xxx_relogin_devices);
3658}
3659
Mike Christie13483732011-12-01 21:38:41 -06003660static 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
Adheer Chandravanshi99c6a332013-07-08 08:33:05 -04003695 if (test_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
3696 return;
3697
Mike Christie13483732011-12-01 21:38:41 -06003698 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
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303706void qla4xxx_wake_dpc(struct scsi_qla_host *ha)
3707{
Lalit Chandivade1b468072011-05-17 23:17:09 -07003708 if (ha->dpc_thread)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303709 queue_work(ha->dpc_thread, &ha->dpc_work);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303710}
3711
Vikas Chaudharyff884432011-08-29 23:43:02 +05303712static 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
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +05303758int 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
Vikas Chaudharya7380a62012-02-27 03:08:56 -08003778static void qla4xxx_do_work(struct scsi_qla_host *ha)
Vikas Chaudharyff884432011-08-29 23:43:02 +05303779{
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;
Vikas Chaudharyc0b9d3f2012-02-13 18:30:49 +05303799 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;
Vikas Chaudharyff884432011-08-29 23:43:02 +05303807 default:
3808 ql4_printk(KERN_WARNING, ha, "event type: 0x%x not "
3809 "supported", e->type);
3810 }
3811 kfree(e);
3812 }
3813}
3814
David Somayajuluafaf5a22006-09-19 10:28:00 -07003815/**
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 **/
David Howellsc4028952006-11-22 14:57:56 +00003826static void qla4xxx_do_dpc(struct work_struct *work)
David Somayajuluafaf5a22006-09-19 10:28:00 -07003827{
David Howellsc4028952006-11-22 14:57:56 +00003828 struct scsi_qla_host *ha =
3829 container_of(work, struct scsi_qla_host, dpc_work);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003830 int status = QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003831
David C Somayajuluf26b9042006-11-15 16:41:09 -08003832 DEBUG2(printk("scsi%ld: %s: DPC handler waking up."
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303833 "flags = 0x%08lx, dpc_flags = 0x%08lx\n",
3834 ha->host_no, __func__, ha->flags, ha->dpc_flags))
David Somayajuluafaf5a22006-09-19 10:28:00 -07003835
3836 /* Initialization not yet finished. Don't do anything yet. */
3837 if (!test_bit(AF_INIT_DONE, &ha->flags))
Lalit Chandivade1b468072011-05-17 23:17:09 -07003838 return;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003839
Lalit Chandivade2232be02010-07-30 14:38:47 +05303840 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));
Lalit Chandivade1b468072011-05-17 23:17:09 -07003843 return;
Lalit Chandivade2232be02010-07-30 14:38:47 +05303844 }
3845
Vikas Chaudharyff884432011-08-29 23:43:02 +05303846 /* post events to application */
3847 qla4xxx_do_work(ha);
3848
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003849 if (is_qla80XX(ha)) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303850 if (test_bit(DPC_HA_UNRECOVERABLE, &ha->dpc_flags)) {
Tej Parkash546fef22012-09-20 07:35:12 -04003851 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
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003858 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);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303862 ql4_printk(KERN_INFO, ha, "HW State: FAILED\n");
3863 qla4_8xxx_device_state_handler(ha);
3864 }
Nilesh Javali320a61d2012-09-20 07:35:10 -04003865
3866 if (test_and_clear_bit(DPC_POST_IDC_ACK, &ha->dpc_flags))
3867 qla4_83xx_post_idc_ack(ha);
3868
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303869 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) ||
David Somayajuluafaf5a22006-09-19 10:28:00 -07003876 test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags) ||
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303877 test_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags))) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003878 if ((is_qla8022(ha) && ql4xdontresethba) ||
3879 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303880 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);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003890
David C Somayajulu477ffb92007-01-22 12:26:11 -08003891 if (test_bit(DPC_RESET_HA_INTR, &ha->dpc_flags)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07003892 uint8_t wait_time = RESET_INTR_TOV;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003893
David Somayajuluafaf5a22006-09-19 10:28:00 -07003894 while ((readw(&ha->reg->ctrl_status) &
3895 (CSR_SOFT_RESET | CSR_FORCE_SOFT_RESET)) != 0) {
3896 if (--wait_time == 0)
3897 break;
David Somayajuluafaf5a22006-09-19 10:28:00 -07003898 msleep(1000);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003899 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003900 if (wait_time == 0)
3901 DEBUG2(printk("scsi%ld: %s: SR|FSR "
3902 "bit not cleared-- resetting\n",
3903 ha->host_no, __func__));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303904 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003905 if (ql4xxx_lock_drvr_wait(ha) == QLA_SUCCESS) {
3906 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303907 status = qla4xxx_recover_adapter(ha);
David C Somayajulu477ffb92007-01-22 12:26:11 -08003908 }
3909 clear_bit(DPC_RESET_HA_INTR, &ha->dpc_flags);
3910 if (status == QLA_SUCCESS)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303911 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003912 }
3913 }
3914
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303915dpc_post_reset_ha:
David Somayajuluafaf5a22006-09-19 10:28:00 -07003916 /* ---- 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
Mike Christie13483732011-12-01 21:38:41 -06003924 /* ---- 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
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303930 /* ---- link change? --- */
Nilesh Javali026fbd32013-01-20 23:50:58 -05003931 if (!test_bit(AF_LOOPBACK, &ha->flags) &&
3932 test_and_clear_bit(DPC_LINK_CHANGED, &ha->dpc_flags)) {
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303933 if (!test_bit(AF_LINK_UP, &ha->flags)) {
3934 /* ---- link down? --- */
Vikas Chaudhary2d7924e2011-03-21 03:34:33 -07003935 qla4xxx_mark_all_devices_missing(ha);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303936 } 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. */
Mike Christie13483732011-12-01 21:38:41 -06003943 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);
Vikas Chaudhary065aa1b2010-04-28 11:38:11 +05303949 }
3950 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07003951}
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{
Sarang Radke8a28896012011-12-06 02:34:10 -08003959 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003960
Vikas Chaudhary5c19b922012-11-23 06:58:38 -05003961 /* Turn-off interrupts on the card. */
3962 ha->isp_ops->disable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003963
Vikas Chaudharyd9e62e52012-08-22 07:45:25 -04003964 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)) {
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04003969 writel(0, &ha->qla4_82xx_reg->host_int);
3970 readl(&ha->qla4_82xx_reg->host_int);
Vikas Chaudharyfbd81072012-09-20 07:35:03 -04003971 } else if (is_qla8032(ha)) {
3972 writel(0, &ha->qla4_83xx_reg->risc_intr);
3973 readl(&ha->qla4_83xx_reg->risc_intr);
Vikas Chaudharyd9e62e52012-08-22 07:45:25 -04003974 }
3975
David Somayajuluafaf5a22006-09-19 10:28:00 -07003976 /* Remove timer thread, if present */
3977 if (ha->timer_active)
3978 qla4xxx_stop_timer(ha);
3979
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303980 /* Kill the kernel thread for this host */
3981 if (ha->dpc_thread)
3982 destroy_workqueue(ha->dpc_thread);
3983
Manish Rangankarb3a271a2011-07-25 13:48:53 -05003984 /* Kill the kernel thread for this host */
3985 if (ha->task_wq)
3986 destroy_workqueue(ha->task_wq);
3987
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303988 /* Put firmware in known state */
3989 ha->isp_ops->reset_firmware(ha);
3990
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04003991 if (is_qla80XX(ha)) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003992 ha->isp_ops->idc_lock(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303993 qla4_8xxx_clear_drv_active(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04003994 ha->isp_ops->idc_unlock(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05303995 }
3996
David Somayajuluafaf5a22006-09-19 10:28:00 -07003997 /* Detach interrupts */
Vikas Chaudhary5c19b922012-11-23 06:58:38 -05003998 qla4xxx_free_irqs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07003999
David C Somayajulubee4fe82007-05-23 18:03:32 -07004000 /* free extra memory */
4001 qla4xxx_mem_free(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304002}
David C Somayajulubee4fe82007-05-23 18:03:32 -07004003
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304004int qla4_8xxx_iospace_config(struct scsi_qla_host *ha)
4005{
4006 int status = 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304007 unsigned long mem_base, mem_len, db_base, db_len;
4008 struct pci_dev *pdev = ha->pdev;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004009
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304010 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
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304018 DEBUG2(printk(KERN_INFO "%s: revision-id=%d\n",
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03004019 __func__, pdev->revision));
4020 ha->revision_id = pdev->revision;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304021
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));
4027
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 */
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04004040 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 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304050
4051 db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
4052 db_len = pci_resource_len(pdev, 4);
4053
Shyam Sundar2657c802010-10-06 22:50:29 -07004054 return 0;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304055iospace_error_exit:
4056 return -ENOMEM;
David Somayajuluafaf5a22006-09-19 10:28:00 -07004057}
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 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304066int qla4xxx_iospace_config(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07004067{
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) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304076 ql4_printk(KERN_WARNING, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07004077 "Invalid PCI I/O region size\n");
4078 pio = 0;
4079 }
4080 } else {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304081 ql4_printk(KERN_WARNING, ha, "region #0 not a PIO resource\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004082 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)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304091 ql4_printk(KERN_ERR, ha,
4092 "region #0 not an MMIO resource, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004093
4094 goto iospace_error_exit;
4095 }
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304096
David Somayajuluafaf5a22006-09-19 10:28:00 -07004097 if (mmio_len < MIN_IOBASE_LEN) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304098 ql4_printk(KERN_ERR, ha,
4099 "Invalid PCI mem region size, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004100 goto iospace_error_exit;
4101 }
4102
4103 if (pci_request_regions(ha->pdev, DRIVER_NAME)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304104 ql4_printk(KERN_WARNING, ha,
4105 "Failed to reserve PIO/MMIO regions\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004106
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) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05304114 ql4_printk(KERN_ERR, ha,
4115 "cannot remap MMIO, aborting\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07004116
4117 goto iospace_error_exit;
4118 }
4119
4120 return 0;
4121
4122iospace_error_exit:
4123 return -ENOMEM;
4124}
4125
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304126static 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,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04004141 .queue_mailbox_command = qla4xxx_queue_mbox_cmd,
4142 .process_mailbox_interrupt = qla4xxx_process_mbox_intr,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304143};
4144
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04004145static struct isp_operations qla4_82xx_isp_ops = {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304146 .iospace_config = qla4_8xxx_iospace_config,
4147 .pci_config = qla4_8xxx_pci_config,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04004148 .disable_intrs = qla4_82xx_disable_intrs,
4149 .enable_intrs = qla4_82xx_enable_intrs,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304150 .start_firmware = qla4_8xxx_load_risc,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04004151 .restart_firmware = qla4_82xx_try_start_fw,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04004152 .intr_handler = qla4_82xx_intr_handler,
4153 .interrupt_service_routine = qla4_82xx_interrupt_service_routine,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04004154 .need_reset = qla4_8xxx_need_reset,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04004155 .reset_chip = qla4_82xx_isp_reset,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304156 .reset_firmware = qla4_8xxx_stop_firmware,
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04004157 .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,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304161 .get_sys_info = qla4_8xxx_get_sys_info,
Vikas Chaudhary33693c72012-08-22 07:55:04 -04004162 .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,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304171};
4172
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04004173static 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,
Tej Parkasha24058f2013-03-07 05:43:13 -05004187 .rd_shdw_req_q_out = qla4xxx_rd_shdw_req_q_out,
4188 .rd_shdw_rsp_q_in = qla4xxx_rd_shdw_rsp_q_in,
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04004189 .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,
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304199};
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
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04004206uint16_t qla4_82xx_rd_shdw_req_q_out(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304207{
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04004208 return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->req_q_out));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304209}
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
Vikas Chaudharyf8086f42012-08-22 07:54:59 -04004216uint16_t qla4_82xx_rd_shdw_rsp_q_in(struct scsi_qla_host *ha)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304217{
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04004218 return (uint16_t)le32_to_cpu(readl(&ha->qla4_82xx_reg->rsp_q_in));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05304219}
4220
Manish Rangankar2a991c22011-07-25 13:48:55 -05004221static 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
Al Viro587a1f12011-07-23 23:11:19 -04004245static umode_t qla4xxx_eth_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004246{
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
Al Viro587a1f12011-07-23 23:11:19 -04004279static umode_t qla4xxx_ini_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004280{
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
Al Viro587a1f12011-07-23 23:11:19 -04004366static umode_t qla4xxx_tgt_get_attr_visibility(void *data, int type)
Manish Rangankar2a991c22011-07-25 13:48:55 -05004367{
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
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07004409 ql4_printk(KERN_INFO, ha, "%s: Get FW boot info for 0x%x func %d\n",
4410 __func__, ha->pdev->device, func_num);
Manish Rangankar2a991c22011-07-25 13:48:55 -05004411
Manish Rangankar0d5b36b2011-10-07 16:55:51 -07004412 if (is_qla40XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05004413 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)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304429 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Adapter boot "
4430 "options : 0x%x\n", __func__, val));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004431 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);
Manish Rangankar2a991c22011-07-25 13:48:55 -05004439
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);
Manish Rangankar2a991c22011-07-25 13:48:55 -05004444
Vikas Chaudhary3e788fb2013-03-07 05:43:08 -05004445 } else if (is_qla80XX(ha)) {
Manish Rangankar2a991c22011-07-25 13:48:55 -05004446 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"
Petr Uzel0bd7f842012-02-24 16:32:59 +01004469 " failed\n", ha->host_no, __func__));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004470 ret = QLA_ERROR;
4471 goto exit_boot_info_free;
4472 }
4473 /* Check Boot Mode */
4474 if (!(buf[1] & 0x07)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304475 DEBUG2(ql4_printk(KERN_INFO, ha, "Firmware boot options"
4476 " : 0x%x\n", buf[1]));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004477 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;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004484
4485 /* get secondary valid target index */
4486 if (buf[11] & BIT_7)
4487 ddb_index[1] = buf[11] & 0x7f;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004488 } 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:
Lalit Chandivade20e835b2012-02-13 18:30:42 +05304500 ha->pri_ddb_idx = ddb_index[0];
4501 ha->sec_ddb_idx = ddb_index[1];
Manish Rangankar2a991c22011-07-25 13:48:55 -05004502 return ret;
4503}
4504
Lalit Chandivade28deb452011-10-07 16:55:44 -07004505/**
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
Vikas Chaudharyd11b0ca2013-03-22 07:08:31 -04004523 if (is_qla80XX(ha))
Lalit Chandivade28deb452011-10-07 16:55:44 -07004524 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
Manish Rangankar2a991c22011-07-25 13:48:55 -05004559static 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)) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304582 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: No Flash DDB found at "
4583 "index [%d]\n", __func__, ddb_index));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004584 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
Lalit Chandivade28deb452011-10-07 16:55:44 -07004630 ret = qla4xxx_get_bidi_chap(ha,
4631 (char *)&boot_conn->chap.intr_chap_name,
4632 (char *)&boot_conn->chap.intr_secret);
4633
Manish Rangankar2a991c22011-07-25 13:48:55 -05004634 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];
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004653 int ret = QLA_ERROR;
4654 int rval;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004655
4656 memset(ddb_index, 0, sizeof(ddb_index));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004657 ddb_index[0] = 0xffff;
4658 ddb_index[1] = 0xffff;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004659 ret = get_fw_boot_info(ha, ddb_index);
4660 if (ret != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304661 DEBUG2(ql4_printk(KERN_INFO, ha,
4662 "%s: No boot target configured.\n", __func__));
Manish Rangankar2a991c22011-07-25 13:48:55 -05004663 return ret;
4664 }
4665
Mike Christie13483732011-12-01 21:38:41 -06004666 if (ql4xdisablesysfsboot)
4667 return QLA_SUCCESS;
4668
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004669 if (ddb_index[0] == 0xffff)
4670 goto sec_target;
4671
4672 rval = qla4xxx_get_boot_target(ha, &(ha->boot_tgt.boot_pri_sess),
Manish Rangankar2a991c22011-07-25 13:48:55 -05004673 ddb_index[0]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004674 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304675 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Primary boot target not "
4676 "configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004677 } else
4678 ret = QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004679
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004680sec_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),
Manish Rangankar2a991c22011-07-25 13:48:55 -05004685 ddb_index[1]);
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004686 if (rval != QLA_SUCCESS) {
Manish Rangankare8fb00e2012-02-13 18:30:43 +05304687 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Secondary boot target not"
4688 " configured\n", __func__));
Lalit Chandivade8de5b952011-10-07 16:55:43 -07004689 } else
4690 ret = QLA_SUCCESS;
4691
4692exit_get_boot_info:
Manish Rangankar2a991c22011-07-25 13:48:55 -05004693 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)
Mike Christie13483732011-12-01 21:38:41 -06004701 return QLA_ERROR;
4702
4703 if (ql4xdisablesysfsboot) {
4704 ql4_printk(KERN_INFO, ha,
Petr Uzel0bd7f842012-02-24 16:32:59 +01004705 "%s: syfsboot disabled - driver will trigger login "
Mike Christie13483732011-12-01 21:38:41 -06004706 "and publish session for discovery .\n", __func__);
4707 return QLA_SUCCESS;
4708 }
4709
Manish Rangankar2a991c22011-07-25 13:48:55 -05004710
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
Mike Christie13483732011-12-01 21:38:41 -06004751 return QLA_SUCCESS;
Manish Rangankar2a991c22011-07-25 13:48:55 -05004752
4753put_host:
4754 scsi_host_put(ha->host);
4755kset_free:
4756 iscsi_boot_destroy_kset(ha->boot_kset);
4757 return -ENOMEM;
4758}
4759
Lalit Chandivade45494152011-10-07 16:55:42 -07004760
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);
Lalit Chandivade45494152011-10-07 16:55:42 -07004817}
4818
Mike Christie13483732011-12-01 21:38:41 -06004819static 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,
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004842 struct ql4_tuple_ddb *tddb,
4843 uint8_t *flash_isid)
Mike Christie13483732011-12-01 21:38:41 -06004844{
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);
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004858
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));
Mike Christie13483732011-12-01 21:38:41 -06004864}
4865
4866static int qla4xxx_compare_tuple_ddb(struct scsi_qla_host *ha,
4867 struct ql4_tuple_ddb *old_tddb,
Manish Rangankar173269e2012-02-27 03:08:55 -08004868 struct ql4_tuple_ddb *new_tddb,
4869 uint8_t is_isid_compare)
Mike Christie13483732011-12-01 21:38:41 -06004870{
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
Manish Rangankar173269e2012-02-27 03:08:55 -08004880 /* For multi sessions, driver generates the ISID, so do not compare
Masanari Iida59e13d42012-04-25 00:24:16 +09004881 * ISID in reset path since it would be a comparison between the
Manish Rangankar173269e2012-02-27 03:08:55 -08004882 * 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
Mike Christie13483732011-12-01 21:38:41 -06004900 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
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004934 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
Mike Christie13483732011-12-01 21:38:41 -06004935
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);
Manish Rangankar173269e2012-02-27 03:08:55 -08004942 if (!qla4xxx_compare_tuple_ddb(ha, fw_tddb, tmp_tddb, false)) {
Mike Christie13483732011-12-01 21:38:41 -06004943 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
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04004956/**
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
Mike Christie13483732011-12-01 21:38:41 -06005052static 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;
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005059 int rval, ret = QLA_ERROR;
Mike Christie13483732011-12-01 21:38:41 -06005060
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
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005077 qla4xxx_convert_param_ddb(fw_ddb_entry, fw_tddb, NULL);
Mike Christie13483732011-12-01 21:38:41 -06005078
5079 list_for_each_entry_safe(nt_ddb_idx, nt_ddb_idx_tmp, list_nt, list) {
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005080 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
Mike Christie13483732011-12-01 21:38:41 -06005099 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
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005111static void qla4xxx_free_ddb_list(struct list_head *list_ddb)
Mike Christie13483732011-12-01 21:38:41 -06005112{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005113 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
Mike Christie13483732011-12-01 21:38:41 -06005114
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005115 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, list_ddb, list) {
5116 list_del_init(&ddb_idx->list);
5117 vfree(ddb_idx);
Mike Christie13483732011-12-01 21:38:41 -06005118 }
Mike Christie13483732011-12-01 21:38:41 -06005119}
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;
Manish Rangankar3dd48492012-11-23 06:58:40 -05005127 struct sockaddr *t_addr;
5128 struct sockaddr_storage *dst_addr;
Mike Christie13483732011-12-01 21:38:41 -06005129 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) {
Manish Rangankar3dd48492012-11-23 06:58:40 -05005137 t_addr = (struct sockaddr *)dst_addr;
5138 t_addr->sa_family = AF_INET6;
Mike Christie13483732011-12-01 21:38:41 -06005139 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 {
Manish Rangankar3dd48492012-11-23 06:58:40 -05005145 t_addr = (struct sockaddr *)dst_addr;
5146 t_addr->sa_family = AF_INET;
Mike Christie13483732011-12-01 21:38:41 -06005147 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
Manish Rangankar3dd48492012-11-23 06:58:40 -05005153 ep = qla4xxx_ep_connect(ha->host, (struct sockaddr *)dst_addr, 0);
Mike Christie13483732011-12-01 21:38:41 -06005154 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,
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005168 struct ddb_entry *ddb_entry,
5169 uint16_t idx)
Mike Christie13483732011-12-01 21:38:41 -06005170{
Nilesh Javalic28eaac2011-12-18 21:40:44 -08005171 uint16_t def_timeout;
5172
Mike Christie13483732011-12-01 21:38:41 -06005173 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);
Nilesh Javalic28eaac2011-12-18 21:40:44 -08005183 def_timeout = le16_to_cpu(ddb_entry->fw_ddb_entry.def_timeout);
Mike Christie13483732011-12-01 21:38:41 -06005184 ddb_entry->default_relogin_timeout =
Nilesh Javalic28eaac2011-12-18 21:40:44 -08005185 (def_timeout > LOGIN_TOV) && (def_timeout < LOGIN_TOV * 10) ?
5186 def_timeout : LOGIN_TOV;
Mike Christie13483732011-12-01 21:38:41 -06005187 ddb_entry->default_time2wait =
5188 le16_to_cpu(ddb_entry->fw_ddb_entry.iscsi_def_time2wait);
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005189
5190 if (ql4xdisablesysfsboot &&
5191 (idx == ha->pri_ddb_idx || idx == ha->sec_ddb_idx))
5192 set_bit(DF_BOOT_TGT, &ddb_entry->flags);
Mike Christie13483732011-12-01 21:38:41 -06005193}
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;
Mike Christie13483732011-12-01 21:38:41 -06005228 }
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
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005240static void qla4xxx_build_st_list(struct scsi_qla_host *ha,
5241 struct list_head *list_st)
Mike Christie13483732011-12-01 21:38:41 -06005242{
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005243 struct qla_ddb_index *st_ddb_idx;
Mike Christie13483732011-12-01 21:38:41 -06005244 int max_ddbs;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005245 int fw_idx_size;
5246 struct dev_db_entry *fw_ddb_entry;
5247 dma_addr_t fw_ddb_dma;
Mike Christie13483732011-12-01 21:38:41 -06005248 int ret;
5249 uint32_t idx = 0, next_idx = 0;
5250 uint32_t state = 0, conn_err = 0;
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005251 uint16_t conn_id = 0;
Mike Christie13483732011-12-01 21:38:41 -06005252
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"));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005257 goto exit_st_list;
Mike Christie13483732011-12-01 21:38:41 -06005258 }
5259
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005260 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
5261 MAX_DEV_DB_ENTRIES;
Mike Christie13483732011-12-01 21:38:41 -06005262 fw_idx_size = sizeof(struct qla_ddb_index);
5263
5264 for (idx = 0; idx < max_ddbs; idx = next_idx) {
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005265 ret = qla4xxx_get_fwddb_entry(ha, idx, fw_ddb_entry, fw_ddb_dma,
5266 NULL, &next_idx, &state,
5267 &conn_err, NULL, &conn_id);
Mike Christie13483732011-12-01 21:38:41 -06005268 if (ret == QLA_ERROR)
5269 break;
5270
Lalit Chandivade981c9822012-02-13 18:30:41 +05305271 /* Ignore DDB if invalid state (unassigned) */
5272 if (state == DDB_DS_UNASSIGNED)
5273 goto continue_next_st;
5274
Mike Christie13483732011-12-01 21:38:41 -06005275 /* 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
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005285 list_add_tail(&st_ddb_idx->list, list_st);
Mike Christie13483732011-12-01 21:38:41 -06005286continue_next_st:
5287 if (next_idx == 0)
5288 break;
5289 }
5290
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005291exit_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,
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005329 int is_reset, uint16_t idx)
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005330{
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
5344 */
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 }
5353
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
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005367 qla4xxx_setup_flash_ddb_entry(ha, ddb_entry, idx);
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005368
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;
5374 }
5375
5376 ddb_entry->conn = cls_conn;
5377
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 }
5388
5389 /* Update sess/conn params */
5390 qla4xxx_copy_fwddb_param(ha, fw_ddb_entry, cls_sess, cls_conn);
5391
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;
5425 }
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);
5429
5430 for (idx = 0; idx < max_ddbs; idx = next_idx) {
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);
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
5444 if (!(state == DDB_DS_NO_CONNECTION_ACTIVE ||
5445 state == DDB_DS_SESSION_FAILED))
5446 goto continue_next_nt;
5447
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;
5454
5455 nt_ddb_idx->fw_ddb_idx = idx;
5456
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005457 /* 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));
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005464
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005465 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 */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005469 vfree(nt_ddb_idx);
5470 goto continue_next_nt;
5471 }
Vikas Chaudhary1cb78d72012-06-14 06:35:48 -04005472
5473 /* Copy updated isid */
5474 memcpy(&nt_ddb_idx->fw_ddb, fw_ddb_entry,
5475 sizeof(struct dev_db_entry));
5476
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005477 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;
5482 }
5483
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05005484 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, is_reset, idx);
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08005485 if (ret == QLA_ERROR)
5486 goto exit_nt_list;
5487
5488continue_next_nt:
5489 if (next_idx == 0)
5490 break;
5491 }
5492
5493exit_nt_list:
5494 if (fw_ddb_entry)
5495 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
5496}
5497
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005498static 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
5568/**
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
5572 *
5573 * Returns:
5574 * 1: if flashnode entry is non-persistent
5575 * 0: if flashnode entry is persistent
5576 **/
5577static int qla4xxx_sysfs_ddb_is_non_persistent(struct device *dev, void *data)
5578{
5579 struct iscsi_bus_flash_session *fnode_sess;
5580
5581 if (!iscsi_flashnode_bus_match(dev, NULL))
5582 return 0;
5583
5584 fnode_sess = iscsi_dev_to_flash_session(dev);
5585
5586 return (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT);
5587}
5588
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 **/
Vikas Chaudhary28e02f12013-04-17 05:15:27 -04005605static 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)
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005608{
5609 struct iscsi_bus_flash_session *fnode_sess = NULL;
5610 struct iscsi_bus_flash_conn *fnode_conn = NULL;
5611 int rc = QLA_ERROR;
5612
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;
5620 }
5621
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 }
5630
5631 if (user) {
5632 fnode_sess->flash_state = DEV_DB_NON_PERSISTENT;
5633 } else {
5634 fnode_sess->flash_state = DEV_DB_PERSISTENT;
5635
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 }
5641
5642 rc = qla4xxx_copy_from_fwddb_param(fnode_sess, fnode_conn,
5643 fw_ddb_entry);
5644
5645 ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
5646 __func__, fnode_sess->dev.kobj.name);
5647
5648 ql4_printk(KERN_INFO, ha, "%s: sysfs entry %s created\n",
5649 __func__, fnode_conn->dev.kobj.name);
5650
5651 return QLA_SUCCESS;
5652
5653free_sess:
5654 iscsi_destroy_flashnode_sess(fnode_sess);
5655
5656exit_tgt_create:
5657 return QLA_ERROR;
5658}
5659
5660/**
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
5665 *
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.
5668 **/
5669static int qla4xxx_sysfs_ddb_add(struct Scsi_Host *shost, const char *buf,
5670 int len)
5671{
5672 struct scsi_qla_host *ha = to_qla_host(shost);
5673 struct dev_db_entry *fw_ddb_entry = NULL;
5674 dma_addr_t fw_ddb_entry_dma;
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
Adheer Chandravanshic962c182013-03-25 08:08:32 -04005681 if (strncasecmp(PORTAL_TYPE_IPV4, buf, 4) &&
5682 strncasecmp(PORTAL_TYPE_IPV6, buf, 4)) {
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005683 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: Invalid portal type\n",
5684 __func__));
5685 goto exit_ddb_add;
5686 }
5687
Adheer Chandravanshia957a7d2013-04-05 07:06:08 -04005688 max_ddbs = is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005689 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) {
5694 DEBUG2(ql4_printk(KERN_ERR, ha,
5695 "%s: Unable to allocate dma buffer\n",
5696 __func__));
5697 goto exit_ddb_add;
5698 }
5699
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);
Mike Christie8526cb12013-05-06 12:06:56 -05005706 put_device(dev);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005707 goto exit_ddb_add;
5708 }
5709
5710 for (idx = 0; idx < max_ddbs; idx++) {
5711 if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry,
5712 fw_ddb_entry_dma, idx))
5713 break;
5714 }
5715
5716 if (idx == max_ddbs)
5717 goto exit_ddb_add;
5718
5719 if (!strncasecmp("ipv6", buf, 4))
5720 options |= IPV6_DEFAULT_DDB_ENTRY;
5721
5722 rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
5723 if (rval == QLA_ERROR)
5724 goto exit_ddb_add;
5725
5726 rval = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 1);
5727
5728exit_ddb_add:
5729 if (fw_ddb_entry)
5730 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5731 fw_ddb_entry, fw_ddb_entry_dma);
5732 if (rval == QLA_SUCCESS)
5733 return idx;
5734 else
5735 return -EIO;
5736}
5737
5738/**
5739 * qla4xxx_sysfs_ddb_apply - write the target ddb contents to Flash
5740 * @fnode_sess: pointer to session attrs of flash ddb entry
5741 * @fnode_conn: pointer to connection attrs of flash ddb entry
5742 *
5743 * This writes the contents of target ddb buffer to Flash with a valid cookie
5744 * value in order to make the ddb entry persistent.
5745 **/
5746static int qla4xxx_sysfs_ddb_apply(struct iscsi_bus_flash_session *fnode_sess,
5747 struct iscsi_bus_flash_conn *fnode_conn)
5748{
5749 struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
5750 struct scsi_qla_host *ha = to_qla_host(shost);
5751 uint32_t dev_db_start_offset = FLASH_OFFSET_DB_INFO;
5752 struct dev_db_entry *fw_ddb_entry = NULL;
5753 dma_addr_t fw_ddb_entry_dma;
5754 uint32_t options = 0;
5755 int rval = 0;
5756
5757 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5758 &fw_ddb_entry_dma, GFP_KERNEL);
5759 if (!fw_ddb_entry) {
5760 DEBUG2(ql4_printk(KERN_ERR, ha,
5761 "%s: Unable to allocate dma buffer\n",
5762 __func__));
5763 rval = -ENOMEM;
5764 goto exit_ddb_apply;
5765 }
5766
Adheer Chandravanshic962c182013-03-25 08:08:32 -04005767 if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005768 options |= IPV6_DEFAULT_DDB_ENTRY;
5769
5770 rval = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
5771 if (rval == QLA_ERROR)
5772 goto exit_ddb_apply;
5773
5774 dev_db_start_offset += (fnode_sess->target_id *
5775 sizeof(*fw_ddb_entry));
5776
5777 qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
5778 fw_ddb_entry->cookie = DDB_VALID_COOKIE;
5779
5780 rval = qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
5781 sizeof(*fw_ddb_entry), FLASH_OPT_RMW_COMMIT);
5782
5783 if (rval == QLA_SUCCESS) {
5784 fnode_sess->flash_state = DEV_DB_PERSISTENT;
5785 ql4_printk(KERN_INFO, ha,
5786 "%s: flash node %u of host %lu written to flash\n",
5787 __func__, fnode_sess->target_id, ha->host_no);
5788 } else {
5789 rval = -EIO;
5790 ql4_printk(KERN_ERR, ha,
5791 "%s: Error while writing flash node %u of host %lu to flash\n",
5792 __func__, fnode_sess->target_id, ha->host_no);
5793 }
5794
5795exit_ddb_apply:
5796 if (fw_ddb_entry)
5797 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5798 fw_ddb_entry, fw_ddb_entry_dma);
5799 return rval;
5800}
5801
5802static ssize_t qla4xxx_sysfs_ddb_conn_open(struct scsi_qla_host *ha,
5803 struct dev_db_entry *fw_ddb_entry,
5804 uint16_t idx)
5805{
5806 struct dev_db_entry *ddb_entry = NULL;
5807 dma_addr_t ddb_entry_dma;
5808 unsigned long wtime;
5809 uint32_t mbx_sts = 0;
5810 uint32_t state = 0, conn_err = 0;
5811 uint16_t tmo = 0;
5812 int ret = 0;
5813
5814 ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
5815 &ddb_entry_dma, GFP_KERNEL);
5816 if (!ddb_entry) {
5817 DEBUG2(ql4_printk(KERN_ERR, ha,
5818 "%s: Unable to allocate dma buffer\n",
5819 __func__));
5820 return QLA_ERROR;
5821 }
5822
5823 memcpy(ddb_entry, fw_ddb_entry, sizeof(*ddb_entry));
5824
5825 ret = qla4xxx_set_ddb_entry(ha, idx, ddb_entry_dma, &mbx_sts);
5826 if (ret != QLA_SUCCESS) {
5827 DEBUG2(ql4_printk(KERN_ERR, ha,
5828 "%s: Unable to set ddb entry for index %d\n",
5829 __func__, idx));
5830 goto exit_ddb_conn_open;
5831 }
5832
5833 qla4xxx_conn_open(ha, idx);
5834
5835 /* To ensure that sendtargets is done, wait for at least 12 secs */
5836 tmo = ((ha->def_timeout > LOGIN_TOV) &&
5837 (ha->def_timeout < LOGIN_TOV * 10) ?
5838 ha->def_timeout : LOGIN_TOV);
5839
5840 DEBUG2(ql4_printk(KERN_INFO, ha,
5841 "Default time to wait for login to ddb %d\n", tmo));
5842
5843 wtime = jiffies + (HZ * tmo);
5844 do {
5845 ret = qla4xxx_get_fwddb_entry(ha, idx, NULL, 0, NULL,
5846 NULL, &state, &conn_err, NULL,
5847 NULL);
5848 if (ret == QLA_ERROR)
5849 continue;
5850
5851 if (state == DDB_DS_NO_CONNECTION_ACTIVE ||
5852 state == DDB_DS_SESSION_FAILED)
5853 break;
5854
5855 schedule_timeout_uninterruptible(HZ / 10);
5856 } while (time_after(wtime, jiffies));
5857
5858exit_ddb_conn_open:
5859 if (ddb_entry)
5860 dma_free_coherent(&ha->pdev->dev, sizeof(*ddb_entry),
5861 ddb_entry, ddb_entry_dma);
5862 return ret;
5863}
5864
5865static int qla4xxx_ddb_login_st(struct scsi_qla_host *ha,
5866 struct dev_db_entry *fw_ddb_entry)
5867{
5868 struct qla_ddb_index *ddb_idx, *ddb_idx_tmp;
5869 struct list_head list_nt;
5870 uint16_t ddb_index;
5871 int ret = 0;
5872
5873 if (test_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags)) {
5874 ql4_printk(KERN_WARNING, ha,
5875 "%s: A discovery already in progress!\n", __func__);
5876 return QLA_ERROR;
5877 }
5878
5879 INIT_LIST_HEAD(&list_nt);
5880
5881 set_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
5882
5883 ret = qla4xxx_get_ddb_index(ha, &ddb_index);
5884 if (ret == QLA_ERROR)
5885 goto exit_login_st_clr_bit;
5886
5887 ret = qla4xxx_sysfs_ddb_conn_open(ha, fw_ddb_entry, ddb_index);
5888 if (ret == QLA_ERROR)
5889 goto exit_login_st;
5890
5891 qla4xxx_build_new_nt_list(ha, &list_nt);
5892
5893 list_for_each_entry_safe(ddb_idx, ddb_idx_tmp, &list_nt, list) {
5894 list_del_init(&ddb_idx->list);
5895 qla4xxx_clear_ddb_entry(ha, ddb_idx->fw_ddb_idx);
5896 vfree(ddb_idx);
5897 }
5898
5899exit_login_st:
5900 if (qla4xxx_clear_ddb_entry(ha, ddb_index) == QLA_ERROR) {
5901 ql4_printk(KERN_ERR, ha,
5902 "Unable to clear DDB index = 0x%x\n", ddb_index);
5903 }
5904
5905 clear_bit(ddb_index, ha->ddb_idx_map);
5906
5907exit_login_st_clr_bit:
5908 clear_bit(AF_ST_DISCOVERY_IN_PROGRESS, &ha->flags);
5909 return ret;
5910}
5911
5912static int qla4xxx_ddb_login_nt(struct scsi_qla_host *ha,
5913 struct dev_db_entry *fw_ddb_entry,
5914 uint16_t idx)
5915{
5916 int ret = QLA_ERROR;
5917
5918 ret = qla4xxx_is_session_exists(ha, fw_ddb_entry);
5919 if (ret != QLA_SUCCESS)
5920 ret = qla4xxx_sess_conn_setup(ha, fw_ddb_entry, RESET_ADAPTER,
5921 idx);
5922 else
5923 ret = -EPERM;
5924
5925 return ret;
5926}
5927
5928/**
5929 * qla4xxx_sysfs_ddb_login - Login to the specified target
5930 * @fnode_sess: pointer to session attrs of flash ddb entry
5931 * @fnode_conn: pointer to connection attrs of flash ddb entry
5932 *
5933 * This logs in to the specified target
5934 **/
5935static int qla4xxx_sysfs_ddb_login(struct iscsi_bus_flash_session *fnode_sess,
5936 struct iscsi_bus_flash_conn *fnode_conn)
5937{
5938 struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
5939 struct scsi_qla_host *ha = to_qla_host(shost);
5940 struct dev_db_entry *fw_ddb_entry = NULL;
5941 dma_addr_t fw_ddb_entry_dma;
5942 uint32_t options = 0;
5943 int ret = 0;
5944
5945 if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT) {
5946 ql4_printk(KERN_ERR, ha,
5947 "%s: Target info is not persistent\n", __func__);
5948 ret = -EIO;
5949 goto exit_ddb_login;
5950 }
5951
5952 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5953 &fw_ddb_entry_dma, GFP_KERNEL);
5954 if (!fw_ddb_entry) {
5955 DEBUG2(ql4_printk(KERN_ERR, ha,
5956 "%s: Unable to allocate dma buffer\n",
5957 __func__));
5958 ret = -ENOMEM;
5959 goto exit_ddb_login;
5960 }
5961
Adheer Chandravanshic962c182013-03-25 08:08:32 -04005962 if (!strncasecmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04005963 options |= IPV6_DEFAULT_DDB_ENTRY;
5964
5965 ret = qla4xxx_get_default_ddb(ha, options, fw_ddb_entry_dma);
5966 if (ret == QLA_ERROR)
5967 goto exit_ddb_login;
5968
5969 qla4xxx_copy_to_fwddb_param(fnode_sess, fnode_conn, fw_ddb_entry);
5970 fw_ddb_entry->cookie = DDB_VALID_COOKIE;
5971
5972 if (strlen((char *)fw_ddb_entry->iscsi_name) == 0)
5973 ret = qla4xxx_ddb_login_st(ha, fw_ddb_entry);
5974 else
5975 ret = qla4xxx_ddb_login_nt(ha, fw_ddb_entry,
5976 fnode_sess->target_id);
5977
5978 if (ret > 0)
5979 ret = -EIO;
5980
5981exit_ddb_login:
5982 if (fw_ddb_entry)
5983 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
5984 fw_ddb_entry, fw_ddb_entry_dma);
5985 return ret;
5986}
5987
5988/**
5989 * qla4xxx_sysfs_ddb_logout_sid - Logout session for the specified target
5990 * @cls_sess: pointer to session to be logged out
5991 *
5992 * This performs session log out from the specified target
5993 **/
5994static int qla4xxx_sysfs_ddb_logout_sid(struct iscsi_cls_session *cls_sess)
5995{
5996 struct iscsi_session *sess;
5997 struct ddb_entry *ddb_entry = NULL;
5998 struct scsi_qla_host *ha;
5999 struct dev_db_entry *fw_ddb_entry = NULL;
6000 dma_addr_t fw_ddb_entry_dma;
6001 unsigned long flags;
6002 unsigned long wtime;
6003 uint32_t ddb_state;
6004 int options;
6005 int ret = 0;
6006
6007 sess = cls_sess->dd_data;
6008 ddb_entry = sess->dd_data;
6009 ha = ddb_entry->ha;
6010
6011 if (ddb_entry->ddb_type != FLASH_DDB) {
6012 ql4_printk(KERN_ERR, ha, "%s: Not a flash node session\n",
6013 __func__);
6014 ret = -ENXIO;
6015 goto exit_ddb_logout;
6016 }
6017
Adheer Chandravanshi37719c22013-04-05 07:06:07 -04006018 if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
6019 ql4_printk(KERN_ERR, ha,
6020 "%s: Logout from boot target entry is not permitted.\n",
6021 __func__);
6022 ret = -EPERM;
6023 goto exit_ddb_logout;
6024 }
6025
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006026 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6027 &fw_ddb_entry_dma, GFP_KERNEL);
6028 if (!fw_ddb_entry) {
6029 ql4_printk(KERN_ERR, ha,
6030 "%s: Unable to allocate dma buffer\n", __func__);
6031 ret = -ENOMEM;
6032 goto exit_ddb_logout;
6033 }
6034
Adheer Chandravanshi99c6a332013-07-08 08:33:05 -04006035 if (test_and_set_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags))
6036 goto ddb_logout_init;
6037
6038 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
6039 fw_ddb_entry, fw_ddb_entry_dma,
6040 NULL, NULL, &ddb_state, NULL,
6041 NULL, NULL);
6042 if (ret == QLA_ERROR)
6043 goto ddb_logout_init;
6044
6045 if (ddb_state == DDB_DS_SESSION_ACTIVE)
6046 goto ddb_logout_init;
6047
6048 /* wait until next relogin is triggered using DF_RELOGIN and
6049 * clear DF_RELOGIN to avoid invocation of further relogin
6050 */
6051 wtime = jiffies + (HZ * RELOGIN_TOV);
6052 do {
6053 if (test_and_clear_bit(DF_RELOGIN, &ddb_entry->flags))
6054 goto ddb_logout_init;
6055
6056 schedule_timeout_uninterruptible(HZ);
6057 } while ((time_after(wtime, jiffies)));
6058
6059ddb_logout_init:
6060 atomic_set(&ddb_entry->retry_relogin_timer, INVALID_ENTRY);
6061 atomic_set(&ddb_entry->relogin_timer, 0);
6062
6063 options = LOGOUT_OPTION_CLOSE_SESSION;
6064 qla4xxx_session_logout_ddb(ha, ddb_entry, options);
6065
6066 memset(fw_ddb_entry, 0, sizeof(*fw_ddb_entry));
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006067 wtime = jiffies + (HZ * LOGOUT_TOV);
6068 do {
6069 ret = qla4xxx_get_fwddb_entry(ha, ddb_entry->fw_ddb_index,
6070 fw_ddb_entry, fw_ddb_entry_dma,
6071 NULL, NULL, &ddb_state, NULL,
6072 NULL, NULL);
6073 if (ret == QLA_ERROR)
6074 goto ddb_logout_clr_sess;
6075
6076 if ((ddb_state == DDB_DS_NO_CONNECTION_ACTIVE) ||
6077 (ddb_state == DDB_DS_SESSION_FAILED))
6078 goto ddb_logout_clr_sess;
6079
6080 schedule_timeout_uninterruptible(HZ);
6081 } while ((time_after(wtime, jiffies)));
6082
6083ddb_logout_clr_sess:
6084 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
6085 /*
6086 * we have decremented the reference count of the driver
6087 * when we setup the session to have the driver unload
6088 * to be seamless without actually destroying the
6089 * session
6090 **/
6091 try_module_get(qla4xxx_iscsi_transport.owner);
6092 iscsi_destroy_endpoint(ddb_entry->conn->ep);
6093
6094 spin_lock_irqsave(&ha->hardware_lock, flags);
6095 qla4xxx_free_ddb(ha, ddb_entry);
Adheer Chandravanshi99c6a332013-07-08 08:33:05 -04006096 clear_bit(ddb_entry->fw_ddb_index, ha->ddb_idx_map);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006097 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6098
6099 iscsi_session_teardown(ddb_entry->sess);
6100
Adheer Chandravanshi99c6a332013-07-08 08:33:05 -04006101 clear_bit(DF_DISABLE_RELOGIN, &ddb_entry->flags);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006102 ret = QLA_SUCCESS;
6103
6104exit_ddb_logout:
6105 if (fw_ddb_entry)
6106 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6107 fw_ddb_entry, fw_ddb_entry_dma);
6108 return ret;
6109}
6110
6111/**
6112 * qla4xxx_sysfs_ddb_logout - Logout from the specified target
6113 * @fnode_sess: pointer to session attrs of flash ddb entry
6114 * @fnode_conn: pointer to connection attrs of flash ddb entry
6115 *
6116 * This performs log out from the specified target
6117 **/
6118static int qla4xxx_sysfs_ddb_logout(struct iscsi_bus_flash_session *fnode_sess,
6119 struct iscsi_bus_flash_conn *fnode_conn)
6120{
6121 struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
6122 struct scsi_qla_host *ha = to_qla_host(shost);
6123 struct ql4_tuple_ddb *flash_tddb = NULL;
6124 struct ql4_tuple_ddb *tmp_tddb = NULL;
6125 struct dev_db_entry *fw_ddb_entry = NULL;
6126 struct ddb_entry *ddb_entry = NULL;
6127 dma_addr_t fw_ddb_dma;
6128 uint32_t next_idx = 0;
6129 uint32_t state = 0, conn_err = 0;
6130 uint16_t conn_id = 0;
6131 int idx, index;
6132 int status, ret = 0;
6133
6134 fw_ddb_entry = dma_pool_alloc(ha->fw_ddb_dma_pool, GFP_KERNEL,
6135 &fw_ddb_dma);
6136 if (fw_ddb_entry == NULL) {
6137 ql4_printk(KERN_ERR, ha, "%s:Out of memory\n", __func__);
6138 ret = -ENOMEM;
6139 goto exit_ddb_logout;
6140 }
6141
6142 flash_tddb = vzalloc(sizeof(*flash_tddb));
6143 if (!flash_tddb) {
6144 ql4_printk(KERN_WARNING, ha,
6145 "%s:Memory Allocation failed.\n", __func__);
6146 ret = -ENOMEM;
6147 goto exit_ddb_logout;
6148 }
6149
6150 tmp_tddb = vzalloc(sizeof(*tmp_tddb));
6151 if (!tmp_tddb) {
6152 ql4_printk(KERN_WARNING, ha,
6153 "%s:Memory Allocation failed.\n", __func__);
6154 ret = -ENOMEM;
6155 goto exit_ddb_logout;
6156 }
6157
6158 if (!fnode_sess->targetname) {
6159 ql4_printk(KERN_ERR, ha,
6160 "%s:Cannot logout from SendTarget entry\n",
6161 __func__);
6162 ret = -EPERM;
6163 goto exit_ddb_logout;
6164 }
6165
6166 if (fnode_sess->is_boot_target) {
6167 ql4_printk(KERN_ERR, ha,
6168 "%s: Logout from boot target entry is not permitted.\n",
6169 __func__);
6170 ret = -EPERM;
6171 goto exit_ddb_logout;
6172 }
6173
6174 strncpy(flash_tddb->iscsi_name, fnode_sess->targetname,
6175 ISCSI_NAME_SIZE);
6176
Adheer Chandravanshic962c182013-03-25 08:08:32 -04006177 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006178 sprintf(flash_tddb->ip_addr, "%pI6", fnode_conn->ipaddress);
6179 else
6180 sprintf(flash_tddb->ip_addr, "%pI4", fnode_conn->ipaddress);
6181
6182 flash_tddb->tpgt = fnode_sess->tpgt;
6183 flash_tddb->port = fnode_conn->port;
6184
6185 COPY_ISID(flash_tddb->isid, fnode_sess->isid);
6186
6187 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
6188 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
6189 if (ddb_entry == NULL)
6190 continue;
6191
6192 if (ddb_entry->ddb_type != FLASH_DDB)
6193 continue;
6194
6195 index = ddb_entry->sess->target_id;
6196 status = qla4xxx_get_fwddb_entry(ha, index, fw_ddb_entry,
6197 fw_ddb_dma, NULL, &next_idx,
6198 &state, &conn_err, NULL,
6199 &conn_id);
6200 if (status == QLA_ERROR) {
6201 ret = -ENOMEM;
6202 break;
6203 }
6204
6205 qla4xxx_convert_param_ddb(fw_ddb_entry, tmp_tddb, NULL);
6206
6207 status = qla4xxx_compare_tuple_ddb(ha, flash_tddb, tmp_tddb,
6208 true);
6209 if (status == QLA_SUCCESS) {
6210 ret = qla4xxx_sysfs_ddb_logout_sid(ddb_entry->sess);
6211 break;
6212 }
6213 }
6214
6215 if (idx == MAX_DDB_ENTRIES)
6216 ret = -ESRCH;
6217
6218exit_ddb_logout:
6219 if (flash_tddb)
6220 vfree(flash_tddb);
6221 if (tmp_tddb)
6222 vfree(tmp_tddb);
6223 if (fw_ddb_entry)
6224 dma_pool_free(ha->fw_ddb_dma_pool, fw_ddb_entry, fw_ddb_dma);
6225
6226 return ret;
6227}
6228
6229static int
6230qla4xxx_sysfs_ddb_get_param(struct iscsi_bus_flash_session *fnode_sess,
6231 int param, char *buf)
6232{
6233 struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
6234 struct scsi_qla_host *ha = to_qla_host(shost);
6235 struct iscsi_bus_flash_conn *fnode_conn;
6236 struct ql4_chap_table chap_tbl;
6237 struct device *dev;
6238 int parent_type, parent_index = 0xffff;
6239 int rc = 0;
6240
Mike Christie8526cb12013-05-06 12:06:56 -05006241 dev = iscsi_find_flashnode_conn(fnode_sess);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006242 if (!dev)
6243 return -EIO;
6244
6245 fnode_conn = iscsi_dev_to_flash_conn(dev);
6246
6247 switch (param) {
6248 case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
6249 rc = sprintf(buf, "%u\n", fnode_conn->is_fw_assigned_ipv6);
6250 break;
6251 case ISCSI_FLASHNODE_PORTAL_TYPE:
6252 rc = sprintf(buf, "%s\n", fnode_sess->portal_type);
6253 break;
6254 case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
6255 rc = sprintf(buf, "%u\n", fnode_sess->auto_snd_tgt_disable);
6256 break;
6257 case ISCSI_FLASHNODE_DISCOVERY_SESS:
6258 rc = sprintf(buf, "%u\n", fnode_sess->discovery_sess);
6259 break;
6260 case ISCSI_FLASHNODE_ENTRY_EN:
6261 rc = sprintf(buf, "%u\n", fnode_sess->entry_state);
6262 break;
6263 case ISCSI_FLASHNODE_HDR_DGST_EN:
6264 rc = sprintf(buf, "%u\n", fnode_conn->hdrdgst_en);
6265 break;
6266 case ISCSI_FLASHNODE_DATA_DGST_EN:
6267 rc = sprintf(buf, "%u\n", fnode_conn->datadgst_en);
6268 break;
6269 case ISCSI_FLASHNODE_IMM_DATA_EN:
6270 rc = sprintf(buf, "%u\n", fnode_sess->imm_data_en);
6271 break;
6272 case ISCSI_FLASHNODE_INITIAL_R2T_EN:
6273 rc = sprintf(buf, "%u\n", fnode_sess->initial_r2t_en);
6274 break;
6275 case ISCSI_FLASHNODE_DATASEQ_INORDER:
6276 rc = sprintf(buf, "%u\n", fnode_sess->dataseq_inorder_en);
6277 break;
6278 case ISCSI_FLASHNODE_PDU_INORDER:
6279 rc = sprintf(buf, "%u\n", fnode_sess->pdu_inorder_en);
6280 break;
6281 case ISCSI_FLASHNODE_CHAP_AUTH_EN:
6282 rc = sprintf(buf, "%u\n", fnode_sess->chap_auth_en);
6283 break;
6284 case ISCSI_FLASHNODE_SNACK_REQ_EN:
6285 rc = sprintf(buf, "%u\n", fnode_conn->snack_req_en);
6286 break;
6287 case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
6288 rc = sprintf(buf, "%u\n", fnode_sess->discovery_logout_en);
6289 break;
6290 case ISCSI_FLASHNODE_BIDI_CHAP_EN:
6291 rc = sprintf(buf, "%u\n", fnode_sess->bidi_chap_en);
6292 break;
6293 case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
6294 rc = sprintf(buf, "%u\n", fnode_sess->discovery_auth_optional);
6295 break;
6296 case ISCSI_FLASHNODE_ERL:
6297 rc = sprintf(buf, "%u\n", fnode_sess->erl);
6298 break;
6299 case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
6300 rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_stat);
6301 break;
6302 case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
6303 rc = sprintf(buf, "%u\n", fnode_conn->tcp_nagle_disable);
6304 break;
6305 case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
6306 rc = sprintf(buf, "%u\n", fnode_conn->tcp_wsf_disable);
6307 break;
6308 case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
6309 rc = sprintf(buf, "%u\n", fnode_conn->tcp_timer_scale);
6310 break;
6311 case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
6312 rc = sprintf(buf, "%u\n", fnode_conn->tcp_timestamp_en);
6313 break;
6314 case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
6315 rc = sprintf(buf, "%u\n", fnode_conn->fragment_disable);
6316 break;
6317 case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
6318 rc = sprintf(buf, "%u\n", fnode_conn->max_recv_dlength);
6319 break;
6320 case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
6321 rc = sprintf(buf, "%u\n", fnode_conn->max_xmit_dlength);
6322 break;
6323 case ISCSI_FLASHNODE_FIRST_BURST:
6324 rc = sprintf(buf, "%u\n", fnode_sess->first_burst);
6325 break;
6326 case ISCSI_FLASHNODE_DEF_TIME2WAIT:
6327 rc = sprintf(buf, "%u\n", fnode_sess->time2wait);
6328 break;
6329 case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
6330 rc = sprintf(buf, "%u\n", fnode_sess->time2retain);
6331 break;
6332 case ISCSI_FLASHNODE_MAX_R2T:
6333 rc = sprintf(buf, "%u\n", fnode_sess->max_r2t);
6334 break;
6335 case ISCSI_FLASHNODE_KEEPALIVE_TMO:
6336 rc = sprintf(buf, "%u\n", fnode_conn->keepalive_timeout);
6337 break;
6338 case ISCSI_FLASHNODE_ISID:
6339 rc = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
6340 fnode_sess->isid[0], fnode_sess->isid[1],
6341 fnode_sess->isid[2], fnode_sess->isid[3],
6342 fnode_sess->isid[4], fnode_sess->isid[5]);
6343 break;
6344 case ISCSI_FLASHNODE_TSID:
6345 rc = sprintf(buf, "%u\n", fnode_sess->tsid);
6346 break;
6347 case ISCSI_FLASHNODE_PORT:
6348 rc = sprintf(buf, "%d\n", fnode_conn->port);
6349 break;
6350 case ISCSI_FLASHNODE_MAX_BURST:
6351 rc = sprintf(buf, "%u\n", fnode_sess->max_burst);
6352 break;
6353 case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
6354 rc = sprintf(buf, "%u\n",
6355 fnode_sess->default_taskmgmt_timeout);
6356 break;
6357 case ISCSI_FLASHNODE_IPADDR:
Adheer Chandravanshic962c182013-03-25 08:08:32 -04006358 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006359 rc = sprintf(buf, "%pI6\n", fnode_conn->ipaddress);
6360 else
6361 rc = sprintf(buf, "%pI4\n", fnode_conn->ipaddress);
6362 break;
6363 case ISCSI_FLASHNODE_ALIAS:
6364 if (fnode_sess->targetalias)
6365 rc = sprintf(buf, "%s\n", fnode_sess->targetalias);
6366 else
6367 rc = sprintf(buf, "\n");
6368 break;
6369 case ISCSI_FLASHNODE_REDIRECT_IPADDR:
Adheer Chandravanshic962c182013-03-25 08:08:32 -04006370 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006371 rc = sprintf(buf, "%pI6\n",
6372 fnode_conn->redirect_ipaddr);
6373 else
6374 rc = sprintf(buf, "%pI4\n",
6375 fnode_conn->redirect_ipaddr);
6376 break;
6377 case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
6378 rc = sprintf(buf, "%u\n", fnode_conn->max_segment_size);
6379 break;
6380 case ISCSI_FLASHNODE_LOCAL_PORT:
6381 rc = sprintf(buf, "%u\n", fnode_conn->local_port);
6382 break;
6383 case ISCSI_FLASHNODE_IPV4_TOS:
6384 rc = sprintf(buf, "%u\n", fnode_conn->ipv4_tos);
6385 break;
6386 case ISCSI_FLASHNODE_IPV6_TC:
Adheer Chandravanshic962c182013-03-25 08:08:32 -04006387 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006388 rc = sprintf(buf, "%u\n",
6389 fnode_conn->ipv6_traffic_class);
6390 else
6391 rc = sprintf(buf, "\n");
6392 break;
6393 case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
6394 rc = sprintf(buf, "%u\n", fnode_conn->ipv6_flow_label);
6395 break;
6396 case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
Adheer Chandravanshic962c182013-03-25 08:08:32 -04006397 if (!strncmp(fnode_sess->portal_type, PORTAL_TYPE_IPV6, 4))
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006398 rc = sprintf(buf, "%pI6\n",
6399 fnode_conn->link_local_ipv6_addr);
6400 else
6401 rc = sprintf(buf, "\n");
6402 break;
6403 case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
Vikas Chaudharyb6130ce2013-04-17 05:15:26 -04006404 if (fnode_sess->discovery_parent_idx < MAX_DDB_ENTRIES)
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006405 parent_index = fnode_sess->discovery_parent_idx;
6406
6407 rc = sprintf(buf, "%u\n", parent_index);
6408 break;
6409 case ISCSI_FLASHNODE_DISCOVERY_PARENT_TYPE:
6410 if (fnode_sess->discovery_parent_type == DDB_ISNS)
6411 parent_type = ISCSI_DISC_PARENT_ISNS;
6412 else if (fnode_sess->discovery_parent_type == DDB_NO_LINK)
6413 parent_type = ISCSI_DISC_PARENT_UNKNOWN;
Vikas Chaudharyb6130ce2013-04-17 05:15:26 -04006414 else if (fnode_sess->discovery_parent_type < MAX_DDB_ENTRIES)
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006415 parent_type = ISCSI_DISC_PARENT_SENDTGT;
6416 else
6417 parent_type = ISCSI_DISC_PARENT_UNKNOWN;
6418
6419 rc = sprintf(buf, "%s\n",
6420 iscsi_get_discovery_parent_name(parent_type));
6421 break;
6422 case ISCSI_FLASHNODE_NAME:
6423 if (fnode_sess->targetname)
6424 rc = sprintf(buf, "%s\n", fnode_sess->targetname);
6425 else
6426 rc = sprintf(buf, "\n");
6427 break;
6428 case ISCSI_FLASHNODE_TPGT:
6429 rc = sprintf(buf, "%u\n", fnode_sess->tpgt);
6430 break;
6431 case ISCSI_FLASHNODE_TCP_XMIT_WSF:
6432 rc = sprintf(buf, "%u\n", fnode_conn->tcp_xmit_wsf);
6433 break;
6434 case ISCSI_FLASHNODE_TCP_RECV_WSF:
6435 rc = sprintf(buf, "%u\n", fnode_conn->tcp_recv_wsf);
6436 break;
6437 case ISCSI_FLASHNODE_CHAP_OUT_IDX:
6438 rc = sprintf(buf, "%u\n", fnode_sess->chap_out_idx);
6439 break;
6440 case ISCSI_FLASHNODE_USERNAME:
6441 if (fnode_sess->chap_auth_en) {
6442 qla4xxx_get_uni_chap_at_index(ha,
6443 chap_tbl.name,
6444 chap_tbl.secret,
6445 fnode_sess->chap_out_idx);
6446 rc = sprintf(buf, "%s\n", chap_tbl.name);
6447 } else {
6448 rc = sprintf(buf, "\n");
6449 }
6450 break;
6451 case ISCSI_FLASHNODE_PASSWORD:
6452 if (fnode_sess->chap_auth_en) {
6453 qla4xxx_get_uni_chap_at_index(ha,
6454 chap_tbl.name,
6455 chap_tbl.secret,
6456 fnode_sess->chap_out_idx);
6457 rc = sprintf(buf, "%s\n", chap_tbl.secret);
6458 } else {
6459 rc = sprintf(buf, "\n");
6460 }
6461 break;
6462 case ISCSI_FLASHNODE_STATSN:
6463 rc = sprintf(buf, "%u\n", fnode_conn->statsn);
6464 break;
6465 case ISCSI_FLASHNODE_EXP_STATSN:
6466 rc = sprintf(buf, "%u\n", fnode_conn->exp_statsn);
6467 break;
6468 case ISCSI_FLASHNODE_IS_BOOT_TGT:
6469 rc = sprintf(buf, "%u\n", fnode_sess->is_boot_target);
6470 break;
6471 default:
6472 rc = -ENOSYS;
6473 break;
6474 }
Mike Christie8526cb12013-05-06 12:06:56 -05006475
6476 put_device(dev);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006477 return rc;
6478}
6479
6480/**
6481 * qla4xxx_sysfs_ddb_set_param - Set parameter for firmware DDB entry
6482 * @fnode_sess: pointer to session attrs of flash ddb entry
6483 * @fnode_conn: pointer to connection attrs of flash ddb entry
6484 * @data: Parameters and their values to update
6485 * @len: len of data
6486 *
6487 * This sets the parameter of flash ddb entry and writes them to flash
6488 **/
6489static int
6490qla4xxx_sysfs_ddb_set_param(struct iscsi_bus_flash_session *fnode_sess,
6491 struct iscsi_bus_flash_conn *fnode_conn,
6492 void *data, int len)
6493{
6494 struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
6495 struct scsi_qla_host *ha = to_qla_host(shost);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006496 struct iscsi_flashnode_param_info *fnode_param;
6497 struct nlattr *attr;
6498 int rc = QLA_ERROR;
6499 uint32_t rem = len;
6500
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006501 nla_for_each_attr(attr, data, len, rem) {
6502 fnode_param = nla_data(attr);
6503
6504 switch (fnode_param->param) {
6505 case ISCSI_FLASHNODE_IS_FW_ASSIGNED_IPV6:
6506 fnode_conn->is_fw_assigned_ipv6 = fnode_param->value[0];
6507 break;
6508 case ISCSI_FLASHNODE_PORTAL_TYPE:
6509 memcpy(fnode_sess->portal_type, fnode_param->value,
6510 strlen(fnode_sess->portal_type));
6511 break;
6512 case ISCSI_FLASHNODE_AUTO_SND_TGT_DISABLE:
6513 fnode_sess->auto_snd_tgt_disable =
6514 fnode_param->value[0];
6515 break;
6516 case ISCSI_FLASHNODE_DISCOVERY_SESS:
6517 fnode_sess->discovery_sess = fnode_param->value[0];
6518 break;
6519 case ISCSI_FLASHNODE_ENTRY_EN:
6520 fnode_sess->entry_state = fnode_param->value[0];
6521 break;
6522 case ISCSI_FLASHNODE_HDR_DGST_EN:
6523 fnode_conn->hdrdgst_en = fnode_param->value[0];
6524 break;
6525 case ISCSI_FLASHNODE_DATA_DGST_EN:
6526 fnode_conn->datadgst_en = fnode_param->value[0];
6527 break;
6528 case ISCSI_FLASHNODE_IMM_DATA_EN:
6529 fnode_sess->imm_data_en = fnode_param->value[0];
6530 break;
6531 case ISCSI_FLASHNODE_INITIAL_R2T_EN:
6532 fnode_sess->initial_r2t_en = fnode_param->value[0];
6533 break;
6534 case ISCSI_FLASHNODE_DATASEQ_INORDER:
6535 fnode_sess->dataseq_inorder_en = fnode_param->value[0];
6536 break;
6537 case ISCSI_FLASHNODE_PDU_INORDER:
6538 fnode_sess->pdu_inorder_en = fnode_param->value[0];
6539 break;
6540 case ISCSI_FLASHNODE_CHAP_AUTH_EN:
6541 fnode_sess->chap_auth_en = fnode_param->value[0];
6542 break;
6543 case ISCSI_FLASHNODE_SNACK_REQ_EN:
6544 fnode_conn->snack_req_en = fnode_param->value[0];
6545 break;
6546 case ISCSI_FLASHNODE_DISCOVERY_LOGOUT_EN:
6547 fnode_sess->discovery_logout_en = fnode_param->value[0];
6548 break;
6549 case ISCSI_FLASHNODE_BIDI_CHAP_EN:
6550 fnode_sess->bidi_chap_en = fnode_param->value[0];
6551 break;
6552 case ISCSI_FLASHNODE_DISCOVERY_AUTH_OPTIONAL:
6553 fnode_sess->discovery_auth_optional =
6554 fnode_param->value[0];
6555 break;
6556 case ISCSI_FLASHNODE_ERL:
6557 fnode_sess->erl = fnode_param->value[0];
6558 break;
6559 case ISCSI_FLASHNODE_TCP_TIMESTAMP_STAT:
6560 fnode_conn->tcp_timestamp_stat = fnode_param->value[0];
6561 break;
6562 case ISCSI_FLASHNODE_TCP_NAGLE_DISABLE:
6563 fnode_conn->tcp_nagle_disable = fnode_param->value[0];
6564 break;
6565 case ISCSI_FLASHNODE_TCP_WSF_DISABLE:
6566 fnode_conn->tcp_wsf_disable = fnode_param->value[0];
6567 break;
6568 case ISCSI_FLASHNODE_TCP_TIMER_SCALE:
6569 fnode_conn->tcp_timer_scale = fnode_param->value[0];
6570 break;
6571 case ISCSI_FLASHNODE_TCP_TIMESTAMP_EN:
6572 fnode_conn->tcp_timestamp_en = fnode_param->value[0];
6573 break;
6574 case ISCSI_FLASHNODE_IP_FRAG_DISABLE:
6575 fnode_conn->fragment_disable = fnode_param->value[0];
6576 break;
6577 case ISCSI_FLASHNODE_MAX_RECV_DLENGTH:
6578 fnode_conn->max_recv_dlength =
6579 *(unsigned *)fnode_param->value;
6580 break;
6581 case ISCSI_FLASHNODE_MAX_XMIT_DLENGTH:
6582 fnode_conn->max_xmit_dlength =
6583 *(unsigned *)fnode_param->value;
6584 break;
6585 case ISCSI_FLASHNODE_FIRST_BURST:
6586 fnode_sess->first_burst =
6587 *(unsigned *)fnode_param->value;
6588 break;
6589 case ISCSI_FLASHNODE_DEF_TIME2WAIT:
6590 fnode_sess->time2wait = *(uint16_t *)fnode_param->value;
6591 break;
6592 case ISCSI_FLASHNODE_DEF_TIME2RETAIN:
6593 fnode_sess->time2retain =
6594 *(uint16_t *)fnode_param->value;
6595 break;
6596 case ISCSI_FLASHNODE_MAX_R2T:
6597 fnode_sess->max_r2t =
6598 *(uint16_t *)fnode_param->value;
6599 break;
6600 case ISCSI_FLASHNODE_KEEPALIVE_TMO:
6601 fnode_conn->keepalive_timeout =
6602 *(uint16_t *)fnode_param->value;
6603 break;
6604 case ISCSI_FLASHNODE_ISID:
6605 memcpy(fnode_sess->isid, fnode_param->value,
6606 sizeof(fnode_sess->isid));
6607 break;
6608 case ISCSI_FLASHNODE_TSID:
6609 fnode_sess->tsid = *(uint16_t *)fnode_param->value;
6610 break;
6611 case ISCSI_FLASHNODE_PORT:
6612 fnode_conn->port = *(uint16_t *)fnode_param->value;
6613 break;
6614 case ISCSI_FLASHNODE_MAX_BURST:
6615 fnode_sess->max_burst = *(unsigned *)fnode_param->value;
6616 break;
6617 case ISCSI_FLASHNODE_DEF_TASKMGMT_TMO:
6618 fnode_sess->default_taskmgmt_timeout =
6619 *(uint16_t *)fnode_param->value;
6620 break;
6621 case ISCSI_FLASHNODE_IPADDR:
6622 memcpy(fnode_conn->ipaddress, fnode_param->value,
6623 IPv6_ADDR_LEN);
6624 break;
6625 case ISCSI_FLASHNODE_ALIAS:
6626 rc = iscsi_switch_str_param(&fnode_sess->targetalias,
6627 (char *)fnode_param->value);
6628 break;
6629 case ISCSI_FLASHNODE_REDIRECT_IPADDR:
6630 memcpy(fnode_conn->redirect_ipaddr, fnode_param->value,
6631 IPv6_ADDR_LEN);
6632 break;
6633 case ISCSI_FLASHNODE_MAX_SEGMENT_SIZE:
6634 fnode_conn->max_segment_size =
6635 *(unsigned *)fnode_param->value;
6636 break;
6637 case ISCSI_FLASHNODE_LOCAL_PORT:
6638 fnode_conn->local_port =
6639 *(uint16_t *)fnode_param->value;
6640 break;
6641 case ISCSI_FLASHNODE_IPV4_TOS:
6642 fnode_conn->ipv4_tos = fnode_param->value[0];
6643 break;
6644 case ISCSI_FLASHNODE_IPV6_TC:
6645 fnode_conn->ipv6_traffic_class = fnode_param->value[0];
6646 break;
6647 case ISCSI_FLASHNODE_IPV6_FLOW_LABEL:
6648 fnode_conn->ipv6_flow_label = fnode_param->value[0];
6649 break;
6650 case ISCSI_FLASHNODE_NAME:
6651 rc = iscsi_switch_str_param(&fnode_sess->targetname,
6652 (char *)fnode_param->value);
6653 break;
6654 case ISCSI_FLASHNODE_TPGT:
6655 fnode_sess->tpgt = *(uint16_t *)fnode_param->value;
6656 break;
6657 case ISCSI_FLASHNODE_LINK_LOCAL_IPV6:
6658 memcpy(fnode_conn->link_local_ipv6_addr,
6659 fnode_param->value, IPv6_ADDR_LEN);
6660 break;
Adheer Chandravanshi65560162013-07-08 08:33:06 -04006661 case ISCSI_FLASHNODE_DISCOVERY_PARENT_IDX:
6662 fnode_sess->discovery_parent_idx =
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006663 *(uint16_t *)fnode_param->value;
6664 break;
6665 case ISCSI_FLASHNODE_TCP_XMIT_WSF:
6666 fnode_conn->tcp_xmit_wsf =
6667 *(uint8_t *)fnode_param->value;
6668 break;
6669 case ISCSI_FLASHNODE_TCP_RECV_WSF:
6670 fnode_conn->tcp_recv_wsf =
6671 *(uint8_t *)fnode_param->value;
6672 break;
6673 case ISCSI_FLASHNODE_STATSN:
6674 fnode_conn->statsn = *(uint32_t *)fnode_param->value;
6675 break;
6676 case ISCSI_FLASHNODE_EXP_STATSN:
6677 fnode_conn->exp_statsn =
6678 *(uint32_t *)fnode_param->value;
6679 break;
6680 default:
6681 ql4_printk(KERN_ERR, ha,
6682 "%s: No such sysfs attribute\n", __func__);
6683 rc = -ENOSYS;
6684 goto exit_set_param;
6685 }
6686 }
6687
6688 rc = qla4xxx_sysfs_ddb_apply(fnode_sess, fnode_conn);
6689
6690exit_set_param:
6691 return rc;
6692}
6693
6694/**
6695 * qla4xxx_sysfs_ddb_delete - Delete firmware DDB entry
6696 * @fnode_sess: pointer to session attrs of flash ddb entry
6697 *
6698 * This invalidates the flash ddb entry at the given index
6699 **/
6700static int qla4xxx_sysfs_ddb_delete(struct iscsi_bus_flash_session *fnode_sess)
6701{
6702 struct Scsi_Host *shost = iscsi_flash_session_to_shost(fnode_sess);
6703 struct scsi_qla_host *ha = to_qla_host(shost);
6704 uint32_t dev_db_start_offset;
6705 uint32_t dev_db_end_offset;
6706 struct dev_db_entry *fw_ddb_entry = NULL;
6707 dma_addr_t fw_ddb_entry_dma;
6708 uint16_t *ddb_cookie = NULL;
Vikas Chaudharyb6130ce2013-04-17 05:15:26 -04006709 size_t ddb_size = 0;
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006710 void *pddb = NULL;
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006711 int target_id;
6712 int rc = 0;
6713
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006714 if (fnode_sess->is_boot_target) {
6715 rc = -EPERM;
6716 DEBUG2(ql4_printk(KERN_ERR, ha,
6717 "%s: Deletion of boot target entry is not permitted.\n",
6718 __func__));
6719 goto exit_ddb_del;
6720 }
6721
6722 if (fnode_sess->flash_state == DEV_DB_NON_PERSISTENT)
6723 goto sysfs_ddb_del;
6724
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006725 if (is_qla40XX(ha)) {
6726 dev_db_start_offset = FLASH_OFFSET_DB_INFO;
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006727 dev_db_end_offset = FLASH_OFFSET_DB_END;
6728 dev_db_start_offset += (fnode_sess->target_id *
6729 sizeof(*fw_ddb_entry));
6730 ddb_size = sizeof(*fw_ddb_entry);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006731 } else {
6732 dev_db_start_offset = FLASH_RAW_ACCESS_ADDR +
6733 (ha->hw.flt_region_ddb << 2);
6734 /* flt_ddb_size is DDB table size for both ports
6735 * so divide it by 2 to calculate the offset for second port
6736 */
6737 if (ha->port_num == 1)
6738 dev_db_start_offset += (ha->hw.flt_ddb_size / 2);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006739
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006740 dev_db_end_offset = dev_db_start_offset +
6741 (ha->hw.flt_ddb_size / 2);
6742
6743 dev_db_start_offset += (fnode_sess->target_id *
6744 sizeof(*fw_ddb_entry));
Adheer Chandravanshi1bcb5612013-04-17 05:15:28 -04006745 dev_db_start_offset += offsetof(struct dev_db_entry, cookie);
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006746
6747 ddb_size = sizeof(*ddb_cookie);
6748 }
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006749
6750 DEBUG2(ql4_printk(KERN_ERR, ha, "%s: start offset=%u, end offset=%u\n",
6751 __func__, dev_db_start_offset, dev_db_end_offset));
6752
6753 if (dev_db_start_offset > dev_db_end_offset) {
6754 rc = -EIO;
6755 DEBUG2(ql4_printk(KERN_ERR, ha, "%s:Invalid DDB index %u\n",
6756 __func__, fnode_sess->target_id));
6757 goto exit_ddb_del;
6758 }
6759
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006760 pddb = dma_alloc_coherent(&ha->pdev->dev, ddb_size,
6761 &fw_ddb_entry_dma, GFP_KERNEL);
6762 if (!pddb) {
6763 rc = -ENOMEM;
6764 DEBUG2(ql4_printk(KERN_ERR, ha,
6765 "%s: Unable to allocate dma buffer\n",
6766 __func__));
6767 goto exit_ddb_del;
6768 }
6769
6770 if (is_qla40XX(ha)) {
6771 fw_ddb_entry = pddb;
6772 memset(fw_ddb_entry, 0, ddb_size);
6773 ddb_cookie = &fw_ddb_entry->cookie;
6774 } else {
6775 ddb_cookie = pddb;
6776 }
6777
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006778 /* invalidate the cookie */
6779 *ddb_cookie = 0xFFEE;
6780 qla4xxx_set_flash(ha, fw_ddb_entry_dma, dev_db_start_offset,
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006781 ddb_size, FLASH_OPT_RMW_COMMIT);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006782
6783sysfs_ddb_del:
6784 target_id = fnode_sess->target_id;
6785 iscsi_destroy_flashnode_sess(fnode_sess);
6786 ql4_printk(KERN_INFO, ha,
6787 "%s: session and conn entries for flashnode %u of host %lu deleted\n",
6788 __func__, target_id, ha->host_no);
6789exit_ddb_del:
Adheer Chandravanshi039acc12013-04-05 07:06:06 -04006790 if (pddb)
6791 dma_free_coherent(&ha->pdev->dev, ddb_size, pddb,
6792 fw_ddb_entry_dma);
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006793 return rc;
6794}
6795
6796/**
6797 * qla4xxx_sysfs_ddb_export - Create sysfs entries for firmware DDBs
6798 * @ha: pointer to adapter structure
6799 *
6800 * Export the firmware DDB for all send targets and normal targets to sysfs.
6801 **/
6802static int qla4xxx_sysfs_ddb_export(struct scsi_qla_host *ha)
6803{
6804 struct dev_db_entry *fw_ddb_entry = NULL;
6805 dma_addr_t fw_ddb_entry_dma;
6806 uint16_t max_ddbs;
6807 uint16_t idx = 0;
6808 int ret = QLA_SUCCESS;
6809
6810 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev,
6811 sizeof(*fw_ddb_entry),
6812 &fw_ddb_entry_dma, GFP_KERNEL);
6813 if (!fw_ddb_entry) {
6814 DEBUG2(ql4_printk(KERN_ERR, ha,
6815 "%s: Unable to allocate dma buffer\n",
6816 __func__));
6817 return -ENOMEM;
6818 }
6819
Adheer Chandravanshia957a7d2013-04-05 07:06:08 -04006820 max_ddbs = is_qla40XX(ha) ? MAX_PRST_DEV_DB_ENTRIES :
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04006821 MAX_DEV_DB_ENTRIES;
6822
6823 for (idx = 0; idx < max_ddbs; idx++) {
6824 if (qla4xxx_flashdb_by_index(ha, fw_ddb_entry, fw_ddb_entry_dma,
6825 idx))
6826 continue;
6827
6828 ret = qla4xxx_sysfs_ddb_tgt_create(ha, fw_ddb_entry, &idx, 0);
6829 if (ret) {
6830 ret = -EIO;
6831 break;
6832 }
6833 }
6834
6835 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry), fw_ddb_entry,
6836 fw_ddb_entry_dma);
6837
6838 return ret;
6839}
6840
6841static void qla4xxx_sysfs_ddb_remove(struct scsi_qla_host *ha)
6842{
6843 iscsi_destroy_all_flashnode(ha->host);
6844}
6845
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08006846/**
6847 * qla4xxx_build_ddb_list - Build ddb list and setup sessions
6848 * @ha: pointer to adapter structure
6849 * @is_reset: Is this init path or reset path
6850 *
6851 * Create a list of sendtargets (st) from firmware DDBs, issue send targets
6852 * using connection open, then create the list of normal targets (nt)
6853 * from firmware DDBs. Based on the list of nt setup session and connection
6854 * objects.
6855 **/
6856void qla4xxx_build_ddb_list(struct scsi_qla_host *ha, int is_reset)
6857{
6858 uint16_t tmo = 0;
6859 struct list_head list_st, list_nt;
6860 struct qla_ddb_index *st_ddb_idx, *st_ddb_idx_tmp;
6861 unsigned long wtime;
6862
6863 if (!test_bit(AF_LINK_UP, &ha->flags)) {
6864 set_bit(AF_BUILD_DDB_LIST, &ha->flags);
6865 ha->is_reset = is_reset;
6866 return;
6867 }
6868
6869 INIT_LIST_HEAD(&list_st);
6870 INIT_LIST_HEAD(&list_nt);
6871
6872 qla4xxx_build_st_list(ha, &list_st);
6873
Mike Christie13483732011-12-01 21:38:41 -06006874 /* Before issuing conn open mbox, ensure all IPs states are configured
6875 * Note, conn open fails if IPs are not configured
6876 */
6877 qla4xxx_wait_for_ip_configuration(ha);
6878
6879 /* Go thru the STs and fire the sendtargets by issuing conn open mbx */
6880 list_for_each_entry_safe(st_ddb_idx, st_ddb_idx_tmp, &list_st, list) {
6881 qla4xxx_conn_open(ha, st_ddb_idx->fw_ddb_idx);
6882 }
6883
6884 /* Wait to ensure all sendtargets are done for min 12 sec wait */
Nilesh Javalic28eaac2011-12-18 21:40:44 -08006885 tmo = ((ha->def_timeout > LOGIN_TOV) &&
6886 (ha->def_timeout < LOGIN_TOV * 10) ?
6887 ha->def_timeout : LOGIN_TOV);
6888
Mike Christie13483732011-12-01 21:38:41 -06006889 DEBUG2(ql4_printk(KERN_INFO, ha,
6890 "Default time to wait for build ddb %d\n", tmo));
6891
6892 wtime = jiffies + (HZ * tmo);
6893 do {
Nilesh Javalif1f2e602011-12-16 01:58:57 -08006894 if (list_empty(&list_st))
6895 break;
6896
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08006897 qla4xxx_remove_failed_ddb(ha, &list_st);
Mike Christie13483732011-12-01 21:38:41 -06006898 schedule_timeout_uninterruptible(HZ / 10);
6899 } while (time_after(wtime, jiffies));
6900
6901 /* Free up the sendtargets list */
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08006902 qla4xxx_free_ddb_list(&list_st);
Mike Christie13483732011-12-01 21:38:41 -06006903
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08006904 qla4xxx_build_nt_list(ha, &list_nt, is_reset);
Mike Christie13483732011-12-01 21:38:41 -06006905
Lalit Chandivade4a4bc2e2011-12-16 01:58:55 -08006906 qla4xxx_free_ddb_list(&list_nt);
Mike Christie13483732011-12-01 21:38:41 -06006907
6908 qla4xxx_free_ddb_index(ha);
6909}
6910
David Somayajuluafaf5a22006-09-19 10:28:00 -07006911/**
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05006912 * qla4xxx_wait_login_resp_boot_tgt - Wait for iSCSI boot target login
6913 * response.
6914 * @ha: pointer to adapter structure
6915 *
6916 * When the boot entry is normal iSCSI target then DF_BOOT_TGT flag will be
6917 * set in DDB and we will wait for login response of boot targets during
6918 * probe.
6919 **/
6920static void qla4xxx_wait_login_resp_boot_tgt(struct scsi_qla_host *ha)
6921{
6922 struct ddb_entry *ddb_entry;
6923 struct dev_db_entry *fw_ddb_entry = NULL;
6924 dma_addr_t fw_ddb_entry_dma;
6925 unsigned long wtime;
6926 uint32_t ddb_state;
6927 int max_ddbs, idx, ret;
6928
6929 max_ddbs = is_qla40XX(ha) ? MAX_DEV_DB_ENTRIES_40XX :
6930 MAX_DEV_DB_ENTRIES;
6931
6932 fw_ddb_entry = dma_alloc_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6933 &fw_ddb_entry_dma, GFP_KERNEL);
6934 if (!fw_ddb_entry) {
6935 ql4_printk(KERN_ERR, ha,
6936 "%s: Unable to allocate dma buffer\n", __func__);
6937 goto exit_login_resp;
6938 }
6939
6940 wtime = jiffies + (HZ * BOOT_LOGIN_RESP_TOV);
6941
6942 for (idx = 0; idx < max_ddbs; idx++) {
6943 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
6944 if (ddb_entry == NULL)
6945 continue;
6946
6947 if (test_bit(DF_BOOT_TGT, &ddb_entry->flags)) {
6948 DEBUG2(ql4_printk(KERN_INFO, ha,
6949 "%s: DDB index [%d]\n", __func__,
6950 ddb_entry->fw_ddb_index));
6951 do {
6952 ret = qla4xxx_get_fwddb_entry(ha,
6953 ddb_entry->fw_ddb_index,
6954 fw_ddb_entry, fw_ddb_entry_dma,
6955 NULL, NULL, &ddb_state, NULL,
6956 NULL, NULL);
6957 if (ret == QLA_ERROR)
6958 goto exit_login_resp;
6959
6960 if ((ddb_state == DDB_DS_SESSION_ACTIVE) ||
6961 (ddb_state == DDB_DS_SESSION_FAILED))
6962 break;
6963
6964 schedule_timeout_uninterruptible(HZ);
6965
6966 } while ((time_after(wtime, jiffies)));
6967
6968 if (!time_after(wtime, jiffies)) {
6969 DEBUG2(ql4_printk(KERN_INFO, ha,
6970 "%s: Login response wait timer expired\n",
6971 __func__));
6972 goto exit_login_resp;
6973 }
6974 }
6975 }
6976
6977exit_login_resp:
6978 if (fw_ddb_entry)
6979 dma_free_coherent(&ha->pdev->dev, sizeof(*fw_ddb_entry),
6980 fw_ddb_entry, fw_ddb_entry_dma);
6981}
6982
6983/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07006984 * qla4xxx_probe_adapter - callback function to probe HBA
6985 * @pdev: pointer to pci_dev structure
6986 * @pci_device_id: pointer to pci_device entry
6987 *
6988 * This routine will probe for Qlogic 4xxx iSCSI host adapters.
6989 * It returns zero if successful. It also initializes all data necessary for
6990 * the driver.
6991 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006992static int qla4xxx_probe_adapter(struct pci_dev *pdev,
6993 const struct pci_device_id *ent)
David Somayajuluafaf5a22006-09-19 10:28:00 -07006994{
6995 int ret = -ENODEV, status;
6996 struct Scsi_Host *host;
6997 struct scsi_qla_host *ha;
David Somayajuluafaf5a22006-09-19 10:28:00 -07006998 uint8_t init_retry_count = 0;
6999 char buf[34];
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307000 struct qla4_8xxx_legacy_intr_set *nx_legacy_intr;
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07007001 uint32_t dev_state;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007002
7003 if (pci_enable_device(pdev))
7004 return -1;
7005
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007006 host = iscsi_host_alloc(&qla4xxx_driver_template, sizeof(*ha), 0);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007007 if (host == NULL) {
7008 printk(KERN_WARNING
7009 "qla4xxx: Couldn't allocate host from scsi layer!\n");
7010 goto probe_disable_device;
7011 }
7012
7013 /* Clear our data area */
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007014 ha = to_qla_host(host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007015 memset(ha, 0, sizeof(*ha));
7016
7017 /* Save the information from PCI BIOS. */
7018 ha->pdev = pdev;
7019 ha->host = host;
7020 ha->host_no = host->host_no;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007021 ha->func_num = PCI_FUNC(ha->pdev->devfn);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007022
Lalit Chandivade2232be02010-07-30 14:38:47 +05307023 pci_enable_pcie_error_reporting(pdev);
7024
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307025 /* Setup Runtime configurable options */
7026 if (is_qla8022(ha)) {
Vikas Chaudhary7664a1f2012-08-22 07:55:00 -04007027 ha->isp_ops = &qla4_82xx_isp_ops;
Vikas Chaudhary33693c72012-08-22 07:55:04 -04007028 ha->reg_tbl = (uint32_t *) qla4_82xx_reg_tbl;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307029 ha->qdr_sn_window = -1;
7030 ha->ddr_mn_window = -1;
7031 ha->curr_window = 255;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307032 nx_legacy_intr = &legacy_intr[ha->func_num];
7033 ha->nx_legacy_intr.int_vec_bit = nx_legacy_intr->int_vec_bit;
7034 ha->nx_legacy_intr.tgt_status_reg =
7035 nx_legacy_intr->tgt_status_reg;
7036 ha->nx_legacy_intr.tgt_mask_reg = nx_legacy_intr->tgt_mask_reg;
7037 ha->nx_legacy_intr.pci_int_reg = nx_legacy_intr->pci_int_reg;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007038 } else if (is_qla8032(ha)) {
7039 ha->isp_ops = &qla4_83xx_isp_ops;
7040 ha->reg_tbl = (uint32_t *)qla4_83xx_reg_tbl;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307041 } else {
7042 ha->isp_ops = &qla4xxx_isp_ops;
7043 }
7044
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007045 if (is_qla80XX(ha)) {
7046 rwlock_init(&ha->hw_lock);
7047 ha->pf_bit = ha->func_num << 16;
7048 /* Set EEH reset type to fundamental if required by hba */
Lalit Chandivade2232be02010-07-30 14:38:47 +05307049 pdev->needs_freset = 1;
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007050 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05307051
David Somayajuluafaf5a22006-09-19 10:28:00 -07007052 /* Configure PCI I/O space. */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307053 ret = ha->isp_ops->iospace_config(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007054 if (ret)
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307055 goto probe_failed_ioconfig;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007056
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307057 ql4_printk(KERN_INFO, ha, "Found an ISP%04x, irq %d, iobase 0x%p\n",
David Somayajuluafaf5a22006-09-19 10:28:00 -07007058 pdev->device, pdev->irq, ha->reg);
7059
7060 qla4xxx_config_dma_addressing(ha);
7061
7062 /* Initialize lists and spinlocks. */
David Somayajuluafaf5a22006-09-19 10:28:00 -07007063 INIT_LIST_HEAD(&ha->free_srb_q);
7064
7065 mutex_init(&ha->mbox_sem);
Lalit Chandivade45494152011-10-07 16:55:42 -07007066 mutex_init(&ha->chap_sem);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307067 init_completion(&ha->mbx_intr_comp);
Vikas Chaudhary95d31262011-08-12 02:51:29 -07007068 init_completion(&ha->disable_acb_comp);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007069
7070 spin_lock_init(&ha->hardware_lock);
Vikas Chaudhary8e9157c2012-08-22 07:45:24 -04007071 spin_lock_init(&ha->work_lock);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007072
Vikas Chaudharyff884432011-08-29 23:43:02 +05307073 /* Initialize work list */
7074 INIT_LIST_HEAD(&ha->work_list);
7075
David Somayajuluafaf5a22006-09-19 10:28:00 -07007076 /* Allocate dma buffers */
7077 if (qla4xxx_mem_alloc(ha)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307078 ql4_printk(KERN_WARNING, ha,
7079 "[ERROR] Failed to allocate memory for adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07007080
7081 ret = -ENOMEM;
7082 goto probe_failed;
7083 }
7084
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007085 host->cmd_per_lun = 3;
7086 host->max_channel = 0;
7087 host->max_lun = MAX_LUNS - 1;
7088 host->max_id = MAX_TARGETS;
7089 host->max_cmd_len = IOCB_MAX_CDB_LEN;
7090 host->can_queue = MAX_SRBS ;
7091 host->transportt = qla4xxx_scsi_transport;
7092
7093 ret = scsi_init_shared_tag_map(host, MAX_SRBS);
7094 if (ret) {
7095 ql4_printk(KERN_WARNING, ha,
7096 "%s: scsi_init_shared_tag_map failed\n", __func__);
7097 goto probe_failed;
7098 }
7099
7100 pci_set_drvdata(pdev, ha);
7101
7102 ret = scsi_add_host(host, &pdev->dev);
7103 if (ret)
7104 goto probe_failed;
7105
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007106 if (is_qla80XX(ha))
7107 qla4_8xxx_get_flash_info(ha);
7108
7109 if (is_qla8032(ha)) {
7110 qla4_83xx_read_reset_template(ha);
7111 /*
7112 * NOTE: If ql4dontresethba==1, set IDC_CTRL DONTRESET_BIT0.
7113 * If DONRESET_BIT0 is set, drivers should not set dev_state
7114 * to NEED_RESET. But if NEED_RESET is set, drivers should
7115 * should honor the reset.
7116 */
7117 if (ql4xdontresethba == 1)
7118 qla4_83xx_set_idc_dontreset(ha);
7119 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307120
David Somayajuluafaf5a22006-09-19 10:28:00 -07007121 /*
7122 * Initialize the Host adapter request/response queues and
7123 * firmware
7124 * NOTE: interrupts enabled upon successful completion
7125 */
Mike Christie13483732011-12-01 21:38:41 -06007126 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
Poornima Vonti7977f822012-09-20 07:35:07 -04007127
7128 /* Dont retry adapter initialization if IRQ allocation failed */
Nilesh Javali17801c92013-03-22 07:22:42 -04007129 if (is_qla80XX(ha) && !test_bit(AF_IRQ_ATTACHED, &ha->flags)) {
7130 ql4_printk(KERN_WARNING, ha, "%s: Skipping retry of adapter initialization\n",
7131 __func__);
Poornima Vonti7977f822012-09-20 07:35:07 -04007132 goto skip_retry_init;
Nilesh Javali17801c92013-03-22 07:22:42 -04007133 }
Poornima Vonti7977f822012-09-20 07:35:07 -04007134
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307135 while ((!test_bit(AF_ONLINE, &ha->flags)) &&
7136 init_retry_count++ < MAX_INIT_RETRIES) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07007137
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007138 if (is_qla80XX(ha)) {
Vikas Chaudhary33693c72012-08-22 07:55:04 -04007139 ha->isp_ops->idc_lock(ha);
7140 dev_state = qla4_8xxx_rd_direct(ha,
Vikas Chaudharye951aca2012-12-29 02:24:52 -05007141 QLA8XXX_CRB_DEV_STATE);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04007142 ha->isp_ops->idc_unlock(ha);
Vikas Chaudharyde8c72d2012-08-22 09:14:24 -04007143 if (dev_state == QLA8XXX_DEV_FAILED) {
Prasanna Mumbaif9880e72011-03-21 03:34:26 -07007144 ql4_printk(KERN_WARNING, ha, "%s: don't retry "
7145 "initialize adapter. H/W is in failed state\n",
7146 __func__);
7147 break;
7148 }
7149 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07007150 DEBUG2(printk("scsi: %s: retrying adapter initialization "
7151 "(%d)\n", __func__, init_retry_count));
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307152
7153 if (ha->isp_ops->reset_chip(ha) == QLA_ERROR)
7154 continue;
7155
Mike Christie13483732011-12-01 21:38:41 -06007156 status = qla4xxx_initialize_adapter(ha, INIT_ADAPTER);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007157 }
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307158
Poornima Vonti7977f822012-09-20 07:35:07 -04007159skip_retry_init:
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307160 if (!test_bit(AF_ONLINE, &ha->flags)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307161 ql4_printk(KERN_WARNING, ha, "Failed to initialize adapter\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07007162
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007163 if ((is_qla8022(ha) && ql4xdontresethba) ||
7164 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
Lalit Chandivadefe998522010-12-02 22:12:36 -08007165 /* Put the device in failed state. */
7166 DEBUG2(printk(KERN_ERR "HW STATE: FAILED\n"));
Vikas Chaudhary33693c72012-08-22 07:55:04 -04007167 ha->isp_ops->idc_lock(ha);
7168 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
7169 QLA8XXX_DEV_FAILED);
7170 ha->isp_ops->idc_unlock(ha);
Lalit Chandivadefe998522010-12-02 22:12:36 -08007171 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07007172 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007173 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007174 }
7175
David Somayajuluafaf5a22006-09-19 10:28:00 -07007176 /* Startup the kernel thread for this host adapter. */
7177 DEBUG2(printk("scsi: %s: Starting kernel thread for "
7178 "qla4xxx_dpc\n", __func__));
7179 sprintf(buf, "qla4xxx_%lu_dpc", ha->host_no);
7180 ha->dpc_thread = create_singlethread_workqueue(buf);
7181 if (!ha->dpc_thread) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307182 ql4_printk(KERN_WARNING, ha, "Unable to start DPC thread!\n");
David Somayajuluafaf5a22006-09-19 10:28:00 -07007183 ret = -ENODEV;
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007184 goto remove_host;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007185 }
David Howellsc4028952006-11-22 14:57:56 +00007186 INIT_WORK(&ha->dpc_work, qla4xxx_do_dpc);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007187
Kees Cookd8537542013-07-03 15:04:57 -07007188 ha->task_wq = alloc_workqueue("qla4xxx_%lu_task", WQ_MEM_RECLAIM, 1,
7189 ha->host_no);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007190 if (!ha->task_wq) {
7191 ql4_printk(KERN_WARNING, ha, "Unable to start task thread!\n");
7192 ret = -ENODEV;
7193 goto remove_host;
7194 }
7195
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007196 /*
7197 * For ISP-8XXX, request_irqs is called in qla4_8xxx_load_risc
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307198 * (which is called indirectly by qla4xxx_initialize_adapter),
7199 * so that irqs will be registered after crbinit but before
7200 * mbx_intr_enable.
7201 */
Vikas Chaudharyee996a62012-08-22 07:55:05 -04007202 if (is_qla40XX(ha)) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307203 ret = qla4xxx_request_irqs(ha);
7204 if (ret) {
7205 ql4_printk(KERN_WARNING, ha, "Failed to reserve "
7206 "interrupt %d already in use.\n", pdev->irq);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007207 goto remove_host;
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307208 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07007209 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07007210
Lalit Chandivade2232be02010-07-30 14:38:47 +05307211 pci_save_state(ha->pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307212 ha->isp_ops->enable_intrs(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007213
7214 /* Start timer thread. */
7215 qla4xxx_start_timer(ha, qla4xxx_timer, 1);
7216
7217 set_bit(AF_INIT_DONE, &ha->flags);
7218
Tej Parkash068237c82012-05-18 04:41:44 -04007219 qla4_8xxx_alloc_sysfs_attr(ha);
7220
David Somayajuluafaf5a22006-09-19 10:28:00 -07007221 printk(KERN_INFO
7222 " QLogic iSCSI HBA Driver version: %s\n"
7223 " QLogic ISP%04x @ %s, host#=%ld, fw=%02d.%02d.%02d.%02d\n",
7224 qla4xxx_version_str, ha->pdev->device, pci_name(ha->pdev),
7225 ha->host_no, ha->firmware_version[0], ha->firmware_version[1],
7226 ha->patch_number, ha->build_number);
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05007227
Manish Dusanecfb27872012-09-20 07:35:01 -04007228 /* Set the driver version */
7229 if (is_qla80XX(ha))
7230 qla4_8xxx_set_param(ha, SET_DRVR_VERSION);
7231
Manish Rangankar2a991c22011-07-25 13:48:55 -05007232 if (qla4xxx_setup_boot_info(ha))
Vikas Chaudhary3573bfb2012-02-27 03:08:57 -08007233 ql4_printk(KERN_ERR, ha,
7234 "%s: No iSCSI boot target configured\n", __func__);
Manish Rangankar2a991c22011-07-25 13:48:55 -05007235
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04007236 if (qla4xxx_sysfs_ddb_export(ha))
7237 ql4_printk(KERN_ERR, ha,
7238 "%s: Error exporting ddb to sysfs\n", __func__);
7239
Mike Christie13483732011-12-01 21:38:41 -06007240 /* Perform the build ddb list and login to each */
7241 qla4xxx_build_ddb_list(ha, INIT_ADAPTER);
7242 iscsi_host_for_each_session(ha->host, qla4xxx_login_flash_ddb);
Manish Rangankar1dc8ed52013-01-20 23:51:03 -05007243 qla4xxx_wait_login_resp_boot_tgt(ha);
Mike Christie13483732011-12-01 21:38:41 -06007244
7245 qla4xxx_create_chap_list(ha);
7246
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05007247 qla4xxx_create_ifaces(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007248 return 0;
7249
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007250remove_host:
7251 scsi_remove_host(ha->host);
7252
David Somayajuluafaf5a22006-09-19 10:28:00 -07007253probe_failed:
7254 qla4xxx_free_adapter(ha);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307255
7256probe_failed_ioconfig:
Lalit Chandivade2232be02010-07-30 14:38:47 +05307257 pci_disable_pcie_error_reporting(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007258 scsi_host_put(ha->host);
7259
7260probe_disable_device:
7261 pci_disable_device(pdev);
7262
7263 return ret;
7264}
7265
7266/**
Karen Higgins7eece5a2011-03-21 03:34:29 -07007267 * qla4xxx_prevent_other_port_reinit - prevent other port from re-initialize
7268 * @ha: pointer to adapter structure
7269 *
7270 * Mark the other ISP-4xxx port to indicate that the driver is being removed,
7271 * so that the other port will not re-initialize while in the process of
7272 * removing the ha due to driver unload or hba hotplug.
7273 **/
7274static void qla4xxx_prevent_other_port_reinit(struct scsi_qla_host *ha)
7275{
7276 struct scsi_qla_host *other_ha = NULL;
7277 struct pci_dev *other_pdev = NULL;
7278 int fn = ISP4XXX_PCI_FN_2;
7279
7280 /*iscsi function numbers for ISP4xxx is 1 and 3*/
7281 if (PCI_FUNC(ha->pdev->devfn) & BIT_1)
7282 fn = ISP4XXX_PCI_FN_1;
7283
7284 other_pdev =
7285 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
7286 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
7287 fn));
7288
7289 /* Get other_ha if other_pdev is valid and state is enable*/
7290 if (other_pdev) {
7291 if (atomic_read(&other_pdev->enable_cnt)) {
7292 other_ha = pci_get_drvdata(other_pdev);
7293 if (other_ha) {
7294 set_bit(AF_HA_REMOVAL, &other_ha->flags);
7295 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: "
7296 "Prevent %s reinit\n", __func__,
7297 dev_name(&other_ha->pdev->dev)));
7298 }
7299 }
7300 pci_dev_put(other_pdev);
7301 }
7302}
7303
Mike Christie13483732011-12-01 21:38:41 -06007304static void qla4xxx_destroy_fw_ddb_session(struct scsi_qla_host *ha)
7305{
7306 struct ddb_entry *ddb_entry;
7307 int options;
7308 int idx;
7309
7310 for (idx = 0; idx < MAX_DDB_ENTRIES; idx++) {
7311
7312 ddb_entry = qla4xxx_lookup_ddb_by_fw_index(ha, idx);
7313 if ((ddb_entry != NULL) &&
7314 (ddb_entry->ddb_type == FLASH_DDB)) {
7315
7316 options = LOGOUT_OPTION_CLOSE_SESSION;
7317 if (qla4xxx_session_logout_ddb(ha, ddb_entry, options)
7318 == QLA_ERROR)
7319 ql4_printk(KERN_ERR, ha, "%s: Logout failed\n",
7320 __func__);
7321
7322 qla4xxx_clear_ddb_entry(ha, ddb_entry->fw_ddb_index);
7323 /*
7324 * we have decremented the reference count of the driver
7325 * when we setup the session to have the driver unload
7326 * to be seamless without actually destroying the
7327 * session
7328 **/
7329 try_module_get(qla4xxx_iscsi_transport.owner);
7330 iscsi_destroy_endpoint(ddb_entry->conn->ep);
7331 qla4xxx_free_ddb(ha, ddb_entry);
7332 iscsi_session_teardown(ddb_entry->sess);
7333 }
7334 }
7335}
Karen Higgins7eece5a2011-03-21 03:34:29 -07007336/**
Masanari Iida59e13d42012-04-25 00:24:16 +09007337 * qla4xxx_remove_adapter - callback function to remove adapter.
David Somayajuluafaf5a22006-09-19 10:28:00 -07007338 * @pci_dev: PCI device pointer
7339 **/
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007340static void qla4xxx_remove_adapter(struct pci_dev *pdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007341{
7342 struct scsi_qla_host *ha;
7343
Vikas Chaudharyf8b07512012-09-20 07:35:08 -04007344 /*
7345 * If the PCI device is disabled then it means probe_adapter had
7346 * failed and resources already cleaned up on probe_adapter exit.
7347 */
7348 if (!pci_is_enabled(pdev))
7349 return;
7350
David Somayajuluafaf5a22006-09-19 10:28:00 -07007351 ha = pci_get_drvdata(pdev);
7352
Vikas Chaudharyee996a62012-08-22 07:55:05 -04007353 if (is_qla40XX(ha))
Karen Higgins7eece5a2011-03-21 03:34:29 -07007354 qla4xxx_prevent_other_port_reinit(ha);
David C Somayajulubee4fe82007-05-23 18:03:32 -07007355
Vikas Chaudharyed1086e2011-07-25 13:48:41 -05007356 /* destroy iface from sysfs */
7357 qla4xxx_destroy_ifaces(ha);
7358
Mike Christie13483732011-12-01 21:38:41 -06007359 if ((!ql4xdisablesysfsboot) && ha->boot_kset)
Manish Rangankar2a991c22011-07-25 13:48:55 -05007360 iscsi_boot_destroy_kset(ha->boot_kset);
7361
Mike Christie13483732011-12-01 21:38:41 -06007362 qla4xxx_destroy_fw_ddb_session(ha);
Tej Parkash068237c82012-05-18 04:41:44 -04007363 qla4_8xxx_free_sysfs_attr(ha);
Mike Christie13483732011-12-01 21:38:41 -06007364
Adheer Chandravanshi1e9e2be2013-03-22 07:41:31 -04007365 qla4xxx_sysfs_ddb_remove(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007366 scsi_remove_host(ha->host);
7367
7368 qla4xxx_free_adapter(ha);
7369
7370 scsi_host_put(ha->host);
7371
Lalit Chandivade2232be02010-07-30 14:38:47 +05307372 pci_disable_pcie_error_reporting(pdev);
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307373 pci_disable_device(pdev);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007374 pci_set_drvdata(pdev, NULL);
7375}
7376
7377/**
7378 * qla4xxx_config_dma_addressing() - Configure OS DMA addressing method.
7379 * @ha: HA context
7380 *
7381 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
7382 * supported addressing method.
7383 */
Adrian Bunk47975472007-04-26 00:35:16 -07007384static void qla4xxx_config_dma_addressing(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007385{
7386 int retval;
7387
7388 /* Update our PCI device dma_mask for full 64 bit mask */
Yang Hongyang6a355282009-04-06 19:01:13 -07007389 if (pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(64)) == 0) {
7390 if (pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07007391 dev_dbg(&ha->pdev->dev,
7392 "Failed to set 64 bit PCI consistent mask; "
7393 "using 32 bit.\n");
7394 retval = pci_set_consistent_dma_mask(ha->pdev,
Yang Hongyang284901a2009-04-06 19:01:15 -07007395 DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07007396 }
7397 } else
Yang Hongyang284901a2009-04-06 19:01:15 -07007398 retval = pci_set_dma_mask(ha->pdev, DMA_BIT_MASK(32));
David Somayajuluafaf5a22006-09-19 10:28:00 -07007399}
7400
7401static int qla4xxx_slave_alloc(struct scsi_device *sdev)
7402{
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007403 struct iscsi_cls_session *cls_sess;
7404 struct iscsi_session *sess;
7405 struct ddb_entry *ddb;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07007406 int queue_depth = QL4_DEF_QDEPTH;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007407
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007408 cls_sess = starget_to_session(sdev->sdev_target);
7409 sess = cls_sess->dd_data;
7410 ddb = sess->dd_data;
7411
David Somayajuluafaf5a22006-09-19 10:28:00 -07007412 sdev->hostdata = ddb;
7413 sdev->tagged_supported = 1;
Vikas Chaudhary8bb40332011-03-21 03:34:31 -07007414
7415 if (ql4xmaxqdepth != 0 && ql4xmaxqdepth <= 0xffffU)
7416 queue_depth = ql4xmaxqdepth;
7417
7418 scsi_activate_tcq(sdev, queue_depth);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007419 return 0;
7420}
7421
7422static int qla4xxx_slave_configure(struct scsi_device *sdev)
7423{
7424 sdev->tagged_supported = 1;
7425 return 0;
7426}
7427
7428static void qla4xxx_slave_destroy(struct scsi_device *sdev)
7429{
7430 scsi_deactivate_tcq(sdev, 1);
7431}
7432
Tej Parkashf7b4aa62012-04-30 04:12:19 -07007433static int qla4xxx_change_queue_depth(struct scsi_device *sdev, int qdepth,
7434 int reason)
7435{
7436 if (!ql4xqfulltracking)
7437 return -EOPNOTSUPP;
7438
7439 return iscsi_change_queue_depth(sdev, qdepth, reason);
7440}
7441
David Somayajuluafaf5a22006-09-19 10:28:00 -07007442/**
7443 * qla4xxx_del_from_active_array - returns an active srb
7444 * @ha: Pointer to host adapter structure.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02007445 * @index: index into the active_array
David Somayajuluafaf5a22006-09-19 10:28:00 -07007446 *
7447 * This routine removes and returns the srb at the specified index
7448 **/
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307449struct srb *qla4xxx_del_from_active_array(struct scsi_qla_host *ha,
7450 uint32_t index)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007451{
7452 struct srb *srb = NULL;
Vikas Chaudhary53698872010-04-28 11:41:59 +05307453 struct scsi_cmnd *cmd = NULL;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007454
Vikas Chaudhary53698872010-04-28 11:41:59 +05307455 cmd = scsi_host_find_tag(ha->host, index);
7456 if (!cmd)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007457 return srb;
7458
Vikas Chaudhary53698872010-04-28 11:41:59 +05307459 srb = (struct srb *)CMD_SP(cmd);
7460 if (!srb)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007461 return srb;
7462
7463 /* update counters */
7464 if (srb->flags & SRB_DMA_VALID) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07007465 ha->iocb_cnt -= srb->iocb_cnt;
7466 if (srb->cmd)
Vikas Chaudhary53698872010-04-28 11:41:59 +05307467 srb->cmd->host_scribble =
7468 (unsigned char *)(unsigned long) MAX_SRBS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007469 }
7470 return srb;
7471}
7472
7473/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07007474 * qla4xxx_eh_wait_on_command - waits for command to be returned by firmware
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307475 * @ha: Pointer to host adapter structure.
David Somayajuluafaf5a22006-09-19 10:28:00 -07007476 * @cmd: Scsi Command to wait on.
7477 *
7478 * This routine waits for the command to be returned by the Firmware
7479 * for some max time.
7480 **/
7481static int qla4xxx_eh_wait_on_command(struct scsi_qla_host *ha,
7482 struct scsi_cmnd *cmd)
7483{
7484 int done = 0;
7485 struct srb *rp;
7486 uint32_t max_wait_time = EH_WAIT_CMD_TOV;
Lalit Chandivade2232be02010-07-30 14:38:47 +05307487 int ret = SUCCESS;
7488
7489 /* Dont wait on command if PCI error is being handled
7490 * by PCI AER driver
7491 */
7492 if (unlikely(pci_channel_offline(ha->pdev)) ||
7493 (test_bit(AF_EEH_BUSY, &ha->flags))) {
7494 ql4_printk(KERN_WARNING, ha, "scsi%ld: Return from %s\n",
7495 ha->host_no, __func__);
7496 return ret;
7497 }
David Somayajuluafaf5a22006-09-19 10:28:00 -07007498
7499 do {
7500 /* Checking to see if its returned to OS */
Vikas Chaudhary53698872010-04-28 11:41:59 +05307501 rp = (struct srb *) CMD_SP(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007502 if (rp == NULL) {
7503 done++;
7504 break;
7505 }
7506
7507 msleep(2000);
7508 } while (max_wait_time--);
7509
7510 return done;
7511}
7512
7513/**
7514 * qla4xxx_wait_for_hba_online - waits for HBA to come online
7515 * @ha: Pointer to host adapter structure
7516 **/
7517static int qla4xxx_wait_for_hba_online(struct scsi_qla_host *ha)
7518{
7519 unsigned long wait_online;
7520
Vikas Chaudharyf581a3f2010-10-06 22:47:48 -07007521 wait_online = jiffies + (HBA_ONLINE_TOV * HZ);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007522 while (time_before(jiffies, wait_online)) {
7523
7524 if (adapter_up(ha))
7525 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007526
7527 msleep(2000);
7528 }
7529
7530 return QLA_ERROR;
7531}
7532
7533/**
Mike Christiece545032008-02-29 18:25:20 -06007534 * qla4xxx_eh_wait_for_commands - wait for active cmds to finish.
Anand Gadiyarfd589a82009-07-16 17:13:03 +02007535 * @ha: pointer to HBA
David Somayajuluafaf5a22006-09-19 10:28:00 -07007536 * @t: target id
7537 * @l: lun id
7538 *
7539 * This function waits for all outstanding commands to a lun to complete. It
7540 * returns 0 if all pending commands are returned and 1 otherwise.
7541 **/
Mike Christiece545032008-02-29 18:25:20 -06007542static int qla4xxx_eh_wait_for_commands(struct scsi_qla_host *ha,
7543 struct scsi_target *stgt,
7544 struct scsi_device *sdev)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007545{
7546 int cnt;
7547 int status = 0;
7548 struct scsi_cmnd *cmd;
7549
7550 /*
Mike Christiece545032008-02-29 18:25:20 -06007551 * Waiting for all commands for the designated target or dev
7552 * in the active array
David Somayajuluafaf5a22006-09-19 10:28:00 -07007553 */
7554 for (cnt = 0; cnt < ha->host->can_queue; cnt++) {
7555 cmd = scsi_host_find_tag(ha->host, cnt);
Mike Christiece545032008-02-29 18:25:20 -06007556 if (cmd && stgt == scsi_target(cmd->device) &&
7557 (!sdev || sdev == cmd->device)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -07007558 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
7559 status++;
7560 break;
7561 }
7562 }
7563 }
7564 return status;
7565}
7566
7567/**
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307568 * qla4xxx_eh_abort - callback for abort task.
7569 * @cmd: Pointer to Linux's SCSI command structure
7570 *
7571 * This routine is called by the Linux OS to abort the specified
7572 * command.
7573 **/
7574static int qla4xxx_eh_abort(struct scsi_cmnd *cmd)
7575{
7576 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
7577 unsigned int id = cmd->device->id;
7578 unsigned int lun = cmd->device->lun;
Mike Christie92b3e5b2010-10-06 22:51:17 -07007579 unsigned long flags;
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307580 struct srb *srb = NULL;
7581 int ret = SUCCESS;
7582 int wait = 0;
7583
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307584 ql4_printk(KERN_INFO, ha,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04007585 "scsi%ld:%d:%d: Abort command issued cmd=%p\n",
7586 ha->host_no, id, lun, cmd);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307587
Mike Christie92b3e5b2010-10-06 22:51:17 -07007588 spin_lock_irqsave(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307589 srb = (struct srb *) CMD_SP(cmd);
Mike Christie92b3e5b2010-10-06 22:51:17 -07007590 if (!srb) {
7591 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307592 return SUCCESS;
Mike Christie92b3e5b2010-10-06 22:51:17 -07007593 }
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307594 kref_get(&srb->srb_ref);
Mike Christie92b3e5b2010-10-06 22:51:17 -07007595 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307596
7597 if (qla4xxx_abort_task(ha, srb) != QLA_SUCCESS) {
7598 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx failed.\n",
7599 ha->host_no, id, lun));
7600 ret = FAILED;
7601 } else {
7602 DEBUG3(printk("scsi%ld:%d:%d: Abort_task mbx success.\n",
7603 ha->host_no, id, lun));
7604 wait = 1;
7605 }
7606
7607 kref_put(&srb->srb_ref, qla4xxx_srb_compl);
7608
7609 /* Wait for command to complete */
7610 if (wait) {
7611 if (!qla4xxx_eh_wait_on_command(ha, cmd)) {
7612 DEBUG2(printk("scsi%ld:%d:%d: Abort handler timed out\n",
7613 ha->host_no, id, lun));
7614 ret = FAILED;
7615 }
7616 }
7617
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307618 ql4_printk(KERN_INFO, ha,
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307619 "scsi%ld:%d:%d: Abort command - %s\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007620 ha->host_no, id, lun, (ret == SUCCESS) ? "succeeded" : "failed");
Vikas Chaudhary09a0f712010-04-28 11:42:24 +05307621
7622 return ret;
7623}
7624
7625/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07007626 * qla4xxx_eh_device_reset - callback for target reset.
7627 * @cmd: Pointer to Linux's SCSI command structure
7628 *
7629 * This routine is called by the Linux OS to reset all luns on the
7630 * specified target.
7631 **/
7632static int qla4xxx_eh_device_reset(struct scsi_cmnd *cmd)
7633{
7634 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
7635 struct ddb_entry *ddb_entry = cmd->device->hostdata;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007636 int ret = FAILED, stat;
7637
Karen Higgins612f7342009-07-15 15:03:01 -05007638 if (!ddb_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007639 return ret;
7640
Mike Christiec01be6d2010-07-22 16:59:49 +05307641 ret = iscsi_block_scsi_eh(cmd);
7642 if (ret)
7643 return ret;
7644 ret = FAILED;
7645
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307646 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07007647 "scsi%ld:%d:%d:%d: DEVICE RESET ISSUED.\n", ha->host_no,
7648 cmd->device->channel, cmd->device->id, cmd->device->lun);
7649
7650 DEBUG2(printk(KERN_INFO
7651 "scsi%ld: DEVICE_RESET cmd=%p jiffies = 0x%lx, to=%x,"
7652 "dpc_flags=%lx, status=%x allowed=%d\n", ha->host_no,
Jens Axboe242f9dc2008-09-14 05:55:09 -07007653 cmd, jiffies, cmd->request->timeout / HZ,
David Somayajuluafaf5a22006-09-19 10:28:00 -07007654 ha->dpc_flags, cmd->result, cmd->allowed));
7655
7656 /* FIXME: wait for hba to go online */
7657 stat = qla4xxx_reset_lun(ha, ddb_entry, cmd->device->lun);
7658 if (stat != QLA_SUCCESS) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307659 ql4_printk(KERN_INFO, ha, "DEVICE RESET FAILED. %d\n", stat);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007660 goto eh_dev_reset_done;
7661 }
7662
Mike Christiece545032008-02-29 18:25:20 -06007663 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
7664 cmd->device)) {
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307665 ql4_printk(KERN_INFO, ha,
Mike Christiece545032008-02-29 18:25:20 -06007666 "DEVICE RESET FAILED - waiting for "
7667 "commands.\n");
7668 goto eh_dev_reset_done;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007669 }
7670
David C Somayajulu9d562912008-03-19 11:23:03 -07007671 /* Send marker. */
7672 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
7673 MM_LUN_RESET) != QLA_SUCCESS)
7674 goto eh_dev_reset_done;
7675
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307676 ql4_printk(KERN_INFO, ha,
David Somayajuluafaf5a22006-09-19 10:28:00 -07007677 "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
7678 ha->host_no, cmd->device->channel, cmd->device->id,
7679 cmd->device->lun);
7680
7681 ret = SUCCESS;
7682
7683eh_dev_reset_done:
7684
7685 return ret;
7686}
7687
7688/**
Mike Christiece545032008-02-29 18:25:20 -06007689 * qla4xxx_eh_target_reset - callback for target reset.
7690 * @cmd: Pointer to Linux's SCSI command structure
7691 *
7692 * This routine is called by the Linux OS to reset the target.
7693 **/
7694static int qla4xxx_eh_target_reset(struct scsi_cmnd *cmd)
7695{
7696 struct scsi_qla_host *ha = to_qla_host(cmd->device->host);
7697 struct ddb_entry *ddb_entry = cmd->device->hostdata;
Mike Christiec01be6d2010-07-22 16:59:49 +05307698 int stat, ret;
Mike Christiece545032008-02-29 18:25:20 -06007699
7700 if (!ddb_entry)
7701 return FAILED;
7702
Mike Christiec01be6d2010-07-22 16:59:49 +05307703 ret = iscsi_block_scsi_eh(cmd);
7704 if (ret)
7705 return ret;
7706
Mike Christiece545032008-02-29 18:25:20 -06007707 starget_printk(KERN_INFO, scsi_target(cmd->device),
7708 "WARM TARGET RESET ISSUED.\n");
7709
7710 DEBUG2(printk(KERN_INFO
7711 "scsi%ld: TARGET_DEVICE_RESET cmd=%p jiffies = 0x%lx, "
7712 "to=%x,dpc_flags=%lx, status=%x allowed=%d\n",
Jens Axboe242f9dc2008-09-14 05:55:09 -07007713 ha->host_no, cmd, jiffies, cmd->request->timeout / HZ,
Mike Christiece545032008-02-29 18:25:20 -06007714 ha->dpc_flags, cmd->result, cmd->allowed));
7715
7716 stat = qla4xxx_reset_target(ha, ddb_entry);
7717 if (stat != QLA_SUCCESS) {
7718 starget_printk(KERN_INFO, scsi_target(cmd->device),
7719 "WARM TARGET RESET FAILED.\n");
7720 return FAILED;
7721 }
7722
Mike Christiece545032008-02-29 18:25:20 -06007723 if (qla4xxx_eh_wait_for_commands(ha, scsi_target(cmd->device),
7724 NULL)) {
7725 starget_printk(KERN_INFO, scsi_target(cmd->device),
7726 "WARM TARGET DEVICE RESET FAILED - "
7727 "waiting for commands.\n");
7728 return FAILED;
7729 }
7730
David C Somayajulu9d562912008-03-19 11:23:03 -07007731 /* Send marker. */
7732 if (qla4xxx_send_marker_iocb(ha, ddb_entry, cmd->device->lun,
7733 MM_TGT_WARM_RESET) != QLA_SUCCESS) {
7734 starget_printk(KERN_INFO, scsi_target(cmd->device),
7735 "WARM TARGET DEVICE RESET FAILED - "
7736 "marker iocb failed.\n");
7737 return FAILED;
7738 }
7739
Mike Christiece545032008-02-29 18:25:20 -06007740 starget_printk(KERN_INFO, scsi_target(cmd->device),
7741 "WARM TARGET RESET SUCCEEDED.\n");
7742 return SUCCESS;
7743}
7744
7745/**
Sarang Radke8a28896012011-12-06 02:34:10 -08007746 * qla4xxx_is_eh_active - check if error handler is running
7747 * @shost: Pointer to SCSI Host struct
7748 *
7749 * This routine finds that if reset host is called in EH
7750 * scenario or from some application like sg_reset
7751 **/
7752static int qla4xxx_is_eh_active(struct Scsi_Host *shost)
7753{
7754 if (shost->shost_state == SHOST_RECOVERY)
7755 return 1;
7756 return 0;
7757}
7758
7759/**
David Somayajuluafaf5a22006-09-19 10:28:00 -07007760 * qla4xxx_eh_host_reset - kernel callback
7761 * @cmd: Pointer to Linux's SCSI command structure
7762 *
7763 * This routine is invoked by the Linux kernel to perform fatal error
7764 * recovery on the specified adapter.
7765 **/
7766static int qla4xxx_eh_host_reset(struct scsi_cmnd *cmd)
7767{
7768 int return_status = FAILED;
7769 struct scsi_qla_host *ha;
7770
Manish Rangankarb3a271a2011-07-25 13:48:53 -05007771 ha = to_qla_host(cmd->device->host);
David Somayajuluafaf5a22006-09-19 10:28:00 -07007772
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007773 if (is_qla8032(ha) && ql4xdontresethba)
7774 qla4_83xx_set_idc_dontreset(ha);
7775
7776 /*
7777 * For ISP8324, if IDC_CTRL DONTRESET_BIT0 is set by other
7778 * protocol drivers, we should not set device_state to
7779 * NEED_RESET
7780 */
7781 if (ql4xdontresethba ||
7782 (is_qla8032(ha) && qla4_83xx_idc_dontreset(ha))) {
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307783 DEBUG2(printk("scsi%ld: %s: Don't Reset HBA\n",
7784 ha->host_no, __func__));
Sarang Radke8a28896012011-12-06 02:34:10 -08007785
7786 /* Clear outstanding srb in queues */
7787 if (qla4xxx_is_eh_active(cmd->device->host))
7788 qla4xxx_abort_active_cmds(ha, DID_ABORT << 16);
7789
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307790 return FAILED;
7791 }
7792
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307793 ql4_printk(KERN_INFO, ha,
Karen Higginsdca05c42009-07-15 15:03:00 -05007794 "scsi(%ld:%d:%d:%d): HOST RESET ISSUED.\n", ha->host_no,
David Somayajuluafaf5a22006-09-19 10:28:00 -07007795 cmd->device->channel, cmd->device->id, cmd->device->lun);
7796
7797 if (qla4xxx_wait_for_hba_online(ha) != QLA_SUCCESS) {
7798 DEBUG2(printk("scsi%ld:%d: %s: Unable to reset host. Adapter "
7799 "DEAD.\n", ha->host_no, cmd->device->channel,
7800 __func__));
7801
7802 return FAILED;
7803 }
7804
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307805 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007806 if (is_qla80XX(ha))
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307807 set_bit(DPC_RESET_HA_FW_CONTEXT, &ha->dpc_flags);
7808 else
7809 set_bit(DPC_RESET_HA, &ha->dpc_flags);
7810 }
Mike Christie50a29ae2008-03-04 13:26:53 -06007811
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05307812 if (qla4xxx_recover_adapter(ha) == QLA_SUCCESS)
David Somayajuluafaf5a22006-09-19 10:28:00 -07007813 return_status = SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -07007814
Vikas Chaudharyc2660df2010-07-10 14:51:02 +05307815 ql4_printk(KERN_INFO, ha, "HOST RESET %s.\n",
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007816 return_status == FAILED ? "FAILED" : "SUCCEEDED");
David Somayajuluafaf5a22006-09-19 10:28:00 -07007817
7818 return return_status;
7819}
7820
Vikas Chaudhary95d31262011-08-12 02:51:29 -07007821static int qla4xxx_context_reset(struct scsi_qla_host *ha)
7822{
7823 uint32_t mbox_cmd[MBOX_REG_COUNT];
7824 uint32_t mbox_sts[MBOX_REG_COUNT];
7825 struct addr_ctrl_blk_def *acb = NULL;
7826 uint32_t acb_len = sizeof(struct addr_ctrl_blk_def);
7827 int rval = QLA_SUCCESS;
7828 dma_addr_t acb_dma;
7829
7830 acb = dma_alloc_coherent(&ha->pdev->dev,
7831 sizeof(struct addr_ctrl_blk_def),
7832 &acb_dma, GFP_KERNEL);
7833 if (!acb) {
7834 ql4_printk(KERN_ERR, ha, "%s: Unable to alloc acb\n",
7835 __func__);
7836 rval = -ENOMEM;
7837 goto exit_port_reset;
7838 }
7839
7840 memset(acb, 0, acb_len);
7841
7842 rval = qla4xxx_get_acb(ha, acb_dma, PRIMARI_ACB, acb_len);
7843 if (rval != QLA_SUCCESS) {
7844 rval = -EIO;
7845 goto exit_free_acb;
7846 }
7847
7848 rval = qla4xxx_disable_acb(ha);
7849 if (rval != QLA_SUCCESS) {
7850 rval = -EIO;
7851 goto exit_free_acb;
7852 }
7853
7854 wait_for_completion_timeout(&ha->disable_acb_comp,
7855 DISABLE_ACB_TOV * HZ);
7856
7857 rval = qla4xxx_set_acb(ha, &mbox_cmd[0], &mbox_sts[0], acb_dma);
7858 if (rval != QLA_SUCCESS) {
7859 rval = -EIO;
7860 goto exit_free_acb;
7861 }
7862
7863exit_free_acb:
7864 dma_free_coherent(&ha->pdev->dev, sizeof(struct addr_ctrl_blk_def),
7865 acb, acb_dma);
7866exit_port_reset:
7867 DEBUG2(ql4_printk(KERN_INFO, ha, "%s %s\n", __func__,
7868 rval == QLA_SUCCESS ? "SUCCEEDED" : "FAILED"));
7869 return rval;
7870}
7871
7872static int qla4xxx_host_reset(struct Scsi_Host *shost, int reset_type)
7873{
7874 struct scsi_qla_host *ha = to_qla_host(shost);
7875 int rval = QLA_SUCCESS;
Vikas Chaudharyebd777d2013-03-07 05:43:09 -05007876 uint32_t idc_ctrl;
Vikas Chaudhary95d31262011-08-12 02:51:29 -07007877
7878 if (ql4xdontresethba) {
7879 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: Don't Reset HBA\n",
7880 __func__));
7881 rval = -EPERM;
7882 goto exit_host_reset;
7883 }
7884
Vikas Chaudhary95d31262011-08-12 02:51:29 -07007885 if (test_bit(DPC_RESET_HA, &ha->dpc_flags))
7886 goto recover_adapter;
7887
7888 switch (reset_type) {
7889 case SCSI_ADAPTER_RESET:
7890 set_bit(DPC_RESET_HA, &ha->dpc_flags);
7891 break;
7892 case SCSI_FIRMWARE_RESET:
7893 if (!test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04007894 if (is_qla80XX(ha))
Vikas Chaudhary95d31262011-08-12 02:51:29 -07007895 /* set firmware context reset */
7896 set_bit(DPC_RESET_HA_FW_CONTEXT,
7897 &ha->dpc_flags);
7898 else {
7899 rval = qla4xxx_context_reset(ha);
7900 goto exit_host_reset;
7901 }
7902 }
7903 break;
7904 }
7905
7906recover_adapter:
Vikas Chaudharyebd777d2013-03-07 05:43:09 -05007907 /* For ISP83XX set graceful reset bit in IDC_DRV_CTRL if
7908 * reset is issued by application */
7909 if (is_qla8032(ha) && test_bit(DPC_RESET_HA, &ha->dpc_flags)) {
7910 idc_ctrl = qla4_83xx_rd_reg(ha, QLA83XX_IDC_DRV_CTRL);
7911 qla4_83xx_wr_reg(ha, QLA83XX_IDC_DRV_CTRL,
7912 (idc_ctrl | GRACEFUL_RESET_BIT1));
7913 }
7914
Vikas Chaudhary95d31262011-08-12 02:51:29 -07007915 rval = qla4xxx_recover_adapter(ha);
7916 if (rval != QLA_SUCCESS) {
7917 DEBUG2(ql4_printk(KERN_INFO, ha, "%s: recover adapter fail\n",
7918 __func__));
7919 rval = -EIO;
7920 }
7921
7922exit_host_reset:
7923 return rval;
7924}
7925
Lalit Chandivade2232be02010-07-30 14:38:47 +05307926/* PCI AER driver recovers from all correctable errors w/o
7927 * driver intervention. For uncorrectable errors PCI AER
7928 * driver calls the following device driver's callbacks
7929 *
7930 * - Fatal Errors - link_reset
7931 * - Non-Fatal Errors - driver's pci_error_detected() which
7932 * returns CAN_RECOVER, NEED_RESET or DISCONNECT.
7933 *
7934 * PCI AER driver calls
7935 * CAN_RECOVER - driver's pci_mmio_enabled(), mmio_enabled
7936 * returns RECOVERED or NEED_RESET if fw_hung
7937 * NEED_RESET - driver's slot_reset()
7938 * DISCONNECT - device is dead & cannot recover
7939 * RECOVERED - driver's pci_resume()
7940 */
7941static pci_ers_result_t
7942qla4xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7943{
7944 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
7945
7946 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: error detected:state %x\n",
7947 ha->host_no, __func__, state);
7948
7949 if (!is_aer_supported(ha))
7950 return PCI_ERS_RESULT_NONE;
7951
7952 switch (state) {
7953 case pci_channel_io_normal:
7954 clear_bit(AF_EEH_BUSY, &ha->flags);
7955 return PCI_ERS_RESULT_CAN_RECOVER;
7956 case pci_channel_io_frozen:
7957 set_bit(AF_EEH_BUSY, &ha->flags);
7958 qla4xxx_mailbox_premature_completion(ha);
7959 qla4xxx_free_irqs(ha);
7960 pci_disable_device(pdev);
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07007961 /* Return back all IOs */
7962 qla4xxx_abort_active_cmds(ha, DID_RESET << 16);
Lalit Chandivade2232be02010-07-30 14:38:47 +05307963 return PCI_ERS_RESULT_NEED_RESET;
7964 case pci_channel_io_perm_failure:
7965 set_bit(AF_EEH_BUSY, &ha->flags);
7966 set_bit(AF_PCI_CHANNEL_IO_PERM_FAILURE, &ha->flags);
7967 qla4xxx_abort_active_cmds(ha, DID_NO_CONNECT << 16);
7968 return PCI_ERS_RESULT_DISCONNECT;
7969 }
7970 return PCI_ERS_RESULT_NEED_RESET;
7971}
7972
7973/**
7974 * qla4xxx_pci_mmio_enabled() gets called if
7975 * qla4xxx_pci_error_detected() returns PCI_ERS_RESULT_CAN_RECOVER
7976 * and read/write to the device still works.
7977 **/
7978static pci_ers_result_t
7979qla4xxx_pci_mmio_enabled(struct pci_dev *pdev)
7980{
7981 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
7982
7983 if (!is_aer_supported(ha))
7984 return PCI_ERS_RESULT_NONE;
7985
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07007986 return PCI_ERS_RESULT_RECOVERED;
Lalit Chandivade2232be02010-07-30 14:38:47 +05307987}
7988
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07007989static uint32_t qla4_8xxx_error_recovery(struct scsi_qla_host *ha)
Lalit Chandivade2232be02010-07-30 14:38:47 +05307990{
7991 uint32_t rval = QLA_ERROR;
7992 int fn;
7993 struct pci_dev *other_pdev = NULL;
7994
7995 ql4_printk(KERN_WARNING, ha, "scsi%ld: In %s\n", ha->host_no, __func__);
7996
7997 set_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
7998
7999 if (test_bit(AF_ONLINE, &ha->flags)) {
8000 clear_bit(AF_ONLINE, &ha->flags);
Manish Rangankarb3a271a2011-07-25 13:48:53 -05008001 clear_bit(AF_LINK_UP, &ha->flags);
8002 iscsi_host_for_each_session(ha->host, qla4xxx_fail_session);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308003 qla4xxx_process_aen(ha, FLUSH_DDB_CHANGED_AENS);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308004 }
8005
8006 fn = PCI_FUNC(ha->pdev->devfn);
8007 while (fn > 0) {
8008 fn--;
8009 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Finding PCI device at "
8010 "func %x\n", ha->host_no, __func__, fn);
8011 /* Get the pci device given the domain, bus,
8012 * slot/function number */
8013 other_pdev =
8014 pci_get_domain_bus_and_slot(pci_domain_nr(ha->pdev->bus),
8015 ha->pdev->bus->number, PCI_DEVFN(PCI_SLOT(ha->pdev->devfn),
8016 fn));
8017
8018 if (!other_pdev)
8019 continue;
8020
8021 if (atomic_read(&other_pdev->enable_cnt)) {
8022 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: Found PCI "
8023 "func in enabled state%x\n", ha->host_no,
8024 __func__, fn);
8025 pci_dev_put(other_pdev);
8026 break;
8027 }
8028 pci_dev_put(other_pdev);
8029 }
8030
8031 /* The first function on the card, the reset owner will
8032 * start & initialize the firmware. The other functions
8033 * on the card will reset the firmware context
8034 */
8035 if (!fn) {
8036 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn being reset "
8037 "0x%x is the owner\n", ha->host_no, __func__,
8038 ha->pdev->devfn);
8039
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008040 ha->isp_ops->idc_lock(ha);
8041 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8042 QLA8XXX_DEV_COLD);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008043 ha->isp_ops->idc_unlock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308044
Vikas Chaudhary39c95822012-09-20 07:35:05 -04008045 rval = qla4_8xxx_update_idc_reg(ha);
8046 if (rval == QLA_ERROR) {
8047 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: FAILED\n",
8048 ha->host_no, __func__);
8049 ha->isp_ops->idc_lock(ha);
8050 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8051 QLA8XXX_DEV_FAILED);
8052 ha->isp_ops->idc_unlock(ha);
8053 goto exit_error_recovery;
8054 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05308055
Lalit Chandivade2232be02010-07-30 14:38:47 +05308056 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06008057 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308058
8059 if (rval != QLA_SUCCESS) {
8060 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
8061 "FAILED\n", ha->host_no, __func__);
Poornima Vonti82761902012-09-20 07:35:04 -04008062 ha->isp_ops->idc_lock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308063 qla4_8xxx_clear_drv_active(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008064 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8065 QLA8XXX_DEV_FAILED);
Poornima Vonti82761902012-09-20 07:35:04 -04008066 ha->isp_ops->idc_unlock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308067 } else {
8068 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: HW State: "
8069 "READY\n", ha->host_no, __func__);
Poornima Vonti82761902012-09-20 07:35:04 -04008070 ha->isp_ops->idc_lock(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008071 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
8072 QLA8XXX_DEV_READY);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308073 /* Clear driver state register */
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008074 qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DRV_STATE, 0);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308075 qla4_8xxx_set_drv_active(ha);
Poornima Vonti82761902012-09-20 07:35:04 -04008076 ha->isp_ops->idc_unlock(ha);
Poornima Vonti137257d2013-01-20 23:51:01 -05008077 ha->isp_ops->enable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308078 }
Lalit Chandivade2232be02010-07-30 14:38:47 +05308079 } else {
8080 ql4_printk(KERN_INFO, ha, "scsi%ld: %s: devfn 0x%x is not "
8081 "the reset owner\n", ha->host_no, __func__,
8082 ha->pdev->devfn);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008083 if ((qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE) ==
8084 QLA8XXX_DEV_READY)) {
Lalit Chandivade2232be02010-07-30 14:38:47 +05308085 clear_bit(AF_FW_RECOVERY, &ha->flags);
Mike Christie13483732011-12-01 21:38:41 -06008086 rval = qla4xxx_initialize_adapter(ha, RESET_ADAPTER);
Poornima Vonti137257d2013-01-20 23:51:01 -05008087 if (rval == QLA_SUCCESS)
8088 ha->isp_ops->enable_intrs(ha);
8089
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008090 ha->isp_ops->idc_lock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308091 qla4_8xxx_set_drv_active(ha);
Vikas Chaudhary33693c72012-08-22 07:55:04 -04008092 ha->isp_ops->idc_unlock(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308093 }
8094 }
Vikas Chaudhary39c95822012-09-20 07:35:05 -04008095exit_error_recovery:
Lalit Chandivade2232be02010-07-30 14:38:47 +05308096 clear_bit(DPC_RESET_ACTIVE, &ha->dpc_flags);
8097 return rval;
8098}
8099
8100static pci_ers_result_t
8101qla4xxx_pci_slot_reset(struct pci_dev *pdev)
8102{
8103 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
8104 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
8105 int rc;
8106
8107 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: slot_reset\n",
8108 ha->host_no, __func__);
8109
8110 if (!is_aer_supported(ha))
8111 return PCI_ERS_RESULT_NONE;
8112
8113 /* Restore the saved state of PCIe device -
8114 * BAR registers, PCI Config space, PCIX, MSI,
8115 * IOV states
8116 */
8117 pci_restore_state(pdev);
8118
8119 /* pci_restore_state() clears the saved_state flag of the device
8120 * save restored state which resets saved_state flag
8121 */
8122 pci_save_state(pdev);
8123
8124 /* Initialize device or resume if in suspended state */
8125 rc = pci_enable_device(pdev);
8126 if (rc) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008127 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Can't re-enable "
Lalit Chandivade2232be02010-07-30 14:38:47 +05308128 "device after reset\n", ha->host_no, __func__);
8129 goto exit_slot_reset;
8130 }
8131
Vikas Chaudhary7b3595d2010-10-06 22:50:56 -07008132 ha->isp_ops->disable_intrs(ha);
Lalit Chandivade2232be02010-07-30 14:38:47 +05308133
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04008134 if (is_qla80XX(ha)) {
Lalit Chandivade2232be02010-07-30 14:38:47 +05308135 if (qla4_8xxx_error_recovery(ha) == QLA_SUCCESS) {
8136 ret = PCI_ERS_RESULT_RECOVERED;
8137 goto exit_slot_reset;
8138 } else
8139 goto exit_slot_reset;
8140 }
8141
8142exit_slot_reset:
8143 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: Return=%x\n"
8144 "device after reset\n", ha->host_no, __func__, ret);
8145 return ret;
8146}
8147
8148static void
8149qla4xxx_pci_resume(struct pci_dev *pdev)
8150{
8151 struct scsi_qla_host *ha = pci_get_drvdata(pdev);
8152 int ret;
8153
8154 ql4_printk(KERN_WARNING, ha, "scsi%ld: %s: pci_resume\n",
8155 ha->host_no, __func__);
8156
8157 ret = qla4xxx_wait_for_hba_online(ha);
8158 if (ret != QLA_SUCCESS) {
8159 ql4_printk(KERN_ERR, ha, "scsi%ld: %s: the device failed to "
8160 "resume I/O from slot/link_reset\n", ha->host_no,
8161 __func__);
8162 }
8163
8164 pci_cleanup_aer_uncorrect_error_status(pdev);
8165 clear_bit(AF_EEH_BUSY, &ha->flags);
8166}
8167
Stephen Hemmingera55b2d22012-09-07 09:33:16 -07008168static const struct pci_error_handlers qla4xxx_err_handler = {
Lalit Chandivade2232be02010-07-30 14:38:47 +05308169 .error_detected = qla4xxx_pci_error_detected,
8170 .mmio_enabled = qla4xxx_pci_mmio_enabled,
8171 .slot_reset = qla4xxx_pci_slot_reset,
8172 .resume = qla4xxx_pci_resume,
8173};
8174
David Somayajuluafaf5a22006-09-19 10:28:00 -07008175static struct pci_device_id qla4xxx_pci_tbl[] = {
8176 {
8177 .vendor = PCI_VENDOR_ID_QLOGIC,
8178 .device = PCI_DEVICE_ID_QLOGIC_ISP4010,
8179 .subvendor = PCI_ANY_ID,
8180 .subdevice = PCI_ANY_ID,
8181 },
8182 {
8183 .vendor = PCI_VENDOR_ID_QLOGIC,
8184 .device = PCI_DEVICE_ID_QLOGIC_ISP4022,
8185 .subvendor = PCI_ANY_ID,
8186 .subdevice = PCI_ANY_ID,
8187 },
David C Somayajulud9150582006-11-15 17:38:40 -08008188 {
8189 .vendor = PCI_VENDOR_ID_QLOGIC,
8190 .device = PCI_DEVICE_ID_QLOGIC_ISP4032,
8191 .subvendor = PCI_ANY_ID,
8192 .subdevice = PCI_ANY_ID,
8193 },
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +05308194 {
8195 .vendor = PCI_VENDOR_ID_QLOGIC,
8196 .device = PCI_DEVICE_ID_QLOGIC_ISP8022,
8197 .subvendor = PCI_ANY_ID,
8198 .subdevice = PCI_ANY_ID,
8199 },
Vikas Chaudhary6e7b4292012-08-22 07:55:08 -04008200 {
8201 .vendor = PCI_VENDOR_ID_QLOGIC,
8202 .device = PCI_DEVICE_ID_QLOGIC_ISP8324,
8203 .subvendor = PCI_ANY_ID,
8204 .subdevice = PCI_ANY_ID,
8205 },
David Somayajuluafaf5a22006-09-19 10:28:00 -07008206 {0, 0},
8207};
8208MODULE_DEVICE_TABLE(pci, qla4xxx_pci_tbl);
8209
Adrian Bunk47975472007-04-26 00:35:16 -07008210static struct pci_driver qla4xxx_pci_driver = {
David Somayajuluafaf5a22006-09-19 10:28:00 -07008211 .name = DRIVER_NAME,
8212 .id_table = qla4xxx_pci_tbl,
8213 .probe = qla4xxx_probe_adapter,
8214 .remove = qla4xxx_remove_adapter,
Lalit Chandivade2232be02010-07-30 14:38:47 +05308215 .err_handler = &qla4xxx_err_handler,
David Somayajuluafaf5a22006-09-19 10:28:00 -07008216};
8217
8218static int __init qla4xxx_module_init(void)
8219{
8220 int ret;
8221
8222 /* Allocate cache for SRBs. */
8223 srb_cachep = kmem_cache_create("qla4xxx_srbs", sizeof(struct srb), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09008224 SLAB_HWCACHE_ALIGN, NULL);
David Somayajuluafaf5a22006-09-19 10:28:00 -07008225 if (srb_cachep == NULL) {
8226 printk(KERN_ERR
8227 "%s: Unable to allocate SRB cache..."
8228 "Failing load!\n", DRIVER_NAME);
8229 ret = -ENOMEM;
8230 goto no_srp_cache;
8231 }
8232
8233 /* Derive version string. */
8234 strcpy(qla4xxx_version_str, QLA4XXX_DRIVER_VERSION);
Andrew Vasquez11010fe2006-10-06 09:54:59 -07008235 if (ql4xextended_error_logging)
David Somayajuluafaf5a22006-09-19 10:28:00 -07008236 strcat(qla4xxx_version_str, "-debug");
8237
8238 qla4xxx_scsi_transport =
8239 iscsi_register_transport(&qla4xxx_iscsi_transport);
8240 if (!qla4xxx_scsi_transport){
8241 ret = -ENODEV;
8242 goto release_srb_cache;
8243 }
8244
David Somayajuluafaf5a22006-09-19 10:28:00 -07008245 ret = pci_register_driver(&qla4xxx_pci_driver);
8246 if (ret)
8247 goto unregister_transport;
8248
8249 printk(KERN_INFO "QLogic iSCSI HBA Driver\n");
8250 return 0;
Doug Maxey5ae16db2006-10-05 23:50:07 -05008251
David Somayajuluafaf5a22006-09-19 10:28:00 -07008252unregister_transport:
8253 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
8254release_srb_cache:
8255 kmem_cache_destroy(srb_cachep);
8256no_srp_cache:
8257 return ret;
8258}
8259
8260static void __exit qla4xxx_module_exit(void)
8261{
David Somayajuluafaf5a22006-09-19 10:28:00 -07008262 pci_unregister_driver(&qla4xxx_pci_driver);
8263 iscsi_unregister_transport(&qla4xxx_iscsi_transport);
8264 kmem_cache_destroy(srb_cachep);
8265}
8266
8267module_init(qla4xxx_module_init);
8268module_exit(qla4xxx_module_exit);
8269
8270MODULE_AUTHOR("QLogic Corporation");
8271MODULE_DESCRIPTION("QLogic iSCSI HBA Driver");
8272MODULE_LICENSE("GPL");
8273MODULE_VERSION(QLA4XXX_DRIVER_VERSION);