blob: 8f55dd44adaef43386399fc767b7de94b7ef2465 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
Armen Baloyanbd21eaf2014-04-11 16:54:24 -04003 * Copyright (c) 2003-2014 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
Anirban Chakraborty73208df2008-12-09 16:45:39 -08008#include "qla_gbl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Andrew Vasquez0107109e2005-07-06 10:31:37 -070012#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "qla_devtbl.h"
15
David Miller4e08df32007-04-16 12:37:43 -070016#ifdef CONFIG_SPARC
17#include <asm/prom.h>
David Miller4e08df32007-04-16 12:37:43 -070018#endif
19
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040020#include <target/target_core_base.h>
21#include "qla_target.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24* QLogic ISP2x00 Hardware Support Function Prototypes.
25*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int qla2x00_setup_chip(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int qla2x00_fw_ready(scsi_qla_host_t *);
29static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int qla2x00_configure_loop(scsi_qla_host_t *);
31static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int qla2x00_configure_fabric(scsi_qla_host_t *);
Quinn Tran726b8542017-01-19 22:28:00 -080033static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Harihara Kadayam4d4df192008-04-03 13:13:26 -070036static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
37static int qla84xx_init_chip(scsi_qla_host_t *);
Anirban Chakraborty73208df2008-12-09 16:45:39 -080038static int qla25xx_init_queues(struct qla_hw_data *);
Duane Grigsbya5d42f42017-06-21 13:48:41 -070039static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
Quinn Tran726b8542017-01-19 22:28:00 -080040static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41 struct event_arg *);
Duane Grigsbya5d42f42017-06-21 13:48:41 -070042static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
43 struct event_arg *);
Quinn Trana4239942017-12-28 12:33:26 -080044static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
Harihara Kadayam4d4df192008-04-03 13:13:26 -070045
Andrew Vasquezac280b62009-08-20 11:06:05 -070046/* SRB Extensions ---------------------------------------------------------- */
47
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080048void
Kees Cook8e5f4ba2017-09-03 13:23:32 -070049qla2x00_sp_timeout(struct timer_list *t)
Andrew Vasquezac280b62009-08-20 11:06:05 -070050{
Kees Cook8e5f4ba2017-09-03 13:23:32 -070051 srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
Madhuranath Iyengar49163922010-05-04 15:01:28 -070052 struct srb_iocb *iocb;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080053 scsi_qla_host_t *vha = sp->vha;
Andrew Vasquezac280b62009-08-20 11:06:05 -070054 struct req_que *req;
55 unsigned long flags;
56
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080057 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
58 req = vha->hw->req_q_map[0];
Andrew Vasquezac280b62009-08-20 11:06:05 -070059 req->outstanding_cmds[sp->handle] = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080060 iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar49163922010-05-04 15:01:28 -070061 iocb->timeout(sp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080062 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -070063}
64
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080065void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080066qla2x00_sp_free(void *ptr)
Andrew Vasquezac280b62009-08-20 11:06:05 -070067{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080068 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080069 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -070070
Chad Dupuis4d97cc52010-10-15 11:27:41 -070071 del_timer(&iocb->timer);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080072 qla2x00_rel_sp(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -070073}
74
Andrew Vasquezac280b62009-08-20 11:06:05 -070075/* Asynchronous Login/Logout Routines -------------------------------------- */
76
Saurav Kashyapa9b6f722012-08-22 14:21:01 -040077unsigned long
Andrew Vasquez5b914902010-05-28 15:08:30 -070078qla2x00_get_async_timeout(struct scsi_qla_host *vha)
79{
80 unsigned long tmo;
81 struct qla_hw_data *ha = vha->hw;
82
83 /* Firmware should use switch negotiated r_a_tov for timeout. */
84 tmo = ha->r_a_tov / 10 * 2;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040085 if (IS_QLAFX00(ha)) {
86 tmo = FX00_DEF_RATOV * 2;
87 } else if (!IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez5b914902010-05-28 15:08:30 -070088 /*
89 * Except for earlier ISPs where the timeout is seeded from the
90 * initialization control block.
91 */
92 tmo = ha->login_timeout;
93 }
94 return tmo;
95}
Andrew Vasquezac280b62009-08-20 11:06:05 -070096
Quinn Tran726b8542017-01-19 22:28:00 -080097void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080098qla2x00_async_iocb_timeout(void *data)
Andrew Vasquezac280b62009-08-20 11:06:05 -070099{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800100 srb_t *sp = data;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700101 fc_port_t *fcport = sp->fcport;
Quinn Tran726b8542017-01-19 22:28:00 -0800102 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700103
Quinn Tran5c25d452017-12-28 12:33:09 -0800104 if (fcport) {
105 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
106 "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
107 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700108
Quinn Tran6d6749272017-12-28 12:33:41 -0800109 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran5c25d452017-12-28 12:33:09 -0800110 } else {
111 pr_info("Async-%s timeout - hdl=%x.\n",
112 sp->name, sp->handle);
113 }
Quinn Tran726b8542017-01-19 22:28:00 -0800114
115 switch (sp->type) {
116 case SRB_LOGIN_CMD:
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700117 /* Retry as needed. */
118 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
119 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
120 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Traneaf75d12018-02-01 10:33:17 -0800121 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800122 break;
123 case SRB_LOGOUT_CMD:
Quinn Tran726b8542017-01-19 22:28:00 -0800124 case SRB_CT_PTHRU_CMD:
125 case SRB_MB_IOCB:
126 case SRB_NACK_PLOGI:
127 case SRB_NACK_PRLI:
128 case SRB_NACK_LOGO:
Quinn Tran28531922017-12-28 12:33:10 -0800129 case SRB_CTRL_VP:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800130 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800131 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700132 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700133}
134
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700135static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800136qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700137{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800138 srb_t *sp = ptr;
139 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800140 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800141 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700142
Quinn Tran83548fe2017-06-02 09:12:01 -0700143 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800144 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800145
Quinn Tran6d6749272017-12-28 12:33:41 -0800146 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
147
Quinn Tran726b8542017-01-19 22:28:00 -0800148 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
149 memset(&ea, 0, sizeof(ea));
150 ea.event = FCME_PLOGI_DONE;
151 ea.fcport = sp->fcport;
152 ea.data[0] = lio->u.logio.data[0];
153 ea.data[1] = lio->u.logio.data[1];
154 ea.iop[0] = lio->u.logio.iop[0];
155 ea.iop[1] = lio->u.logio.iop[1];
156 ea.sp = sp;
157 qla2x00_fcport_event_handler(vha, &ea);
158 }
159
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800160 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700161}
162
Andrew Vasquezac280b62009-08-20 11:06:05 -0700163int
164qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
165 uint16_t *data)
166{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700167 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700168 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800169 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700170
Quinn Tran726b8542017-01-19 22:28:00 -0800171 if (!vha->flags.online)
172 goto done;
173
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800174 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700175 if (!sp)
176 goto done;
177
Quinn Tran726b8542017-01-19 22:28:00 -0800178 fcport->flags |= FCF_ASYNC_SENT;
179 fcport->logout_completed = 0;
180
Quinn Trana4239942017-12-28 12:33:26 -0800181 fcport->disc_state = DSC_LOGIN_PEND;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800182 sp->type = SRB_LOGIN_CMD;
183 sp->name = "login";
Quinn Trana4239942017-12-28 12:33:26 -0800184 sp->gen1 = fcport->rscn_gen;
185 sp->gen2 = fcport->login_gen;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800186
187 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700188 lio->timeout = qla2x00_async_iocb_timeout;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000189 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
190
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800191 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700192 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700193
194 if (fcport->fc4f_nvme)
195 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
196
Andrew Vasquezac280b62009-08-20 11:06:05 -0700197 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700198 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700199 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500200 if (rval != QLA_SUCCESS) {
Chad Dupuis080c9512016-01-27 12:03:37 -0500201 fcport->flags |= FCF_LOGIN_NEEDED;
202 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700203 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500204 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700205
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700206 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800207 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
208 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800209 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
210 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700211 return rval;
212
213done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800214 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800215 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800216done:
Hannes Reineckefa83e652018-02-22 09:49:37 +0100217 fcport->flags &= ~FCF_ASYNC_ACTIVE;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700218 return rval;
219}
220
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700221static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800222qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700223{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800224 srb_t *sp = ptr;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700225
Quinn Tran6d6749272017-12-28 12:33:41 -0800226 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Traneaf75d12018-02-01 10:33:17 -0800227 sp->fcport->login_gen++;
228 qlt_logo_completion_handler(sp->fcport, res);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800229 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700230}
231
Andrew Vasquezac280b62009-08-20 11:06:05 -0700232int
233qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
234{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700235 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700236 struct srb_iocb *lio;
Quinn Tran3dbec592017-12-28 12:33:40 -0800237 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700238
Quinn Tran3dbec592017-12-28 12:33:40 -0800239 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
240 return rval;
241
Quinn Tran726b8542017-01-19 22:28:00 -0800242 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800243 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700244 if (!sp)
245 goto done;
246
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800247 sp->type = SRB_LOGOUT_CMD;
248 sp->name = "logout";
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800249
250 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700251 lio->timeout = qla2x00_async_iocb_timeout;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000252 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
253
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800254 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700255 rval = qla2x00_start_sp(sp);
256 if (rval != QLA_SUCCESS)
257 goto done_free_sp;
258
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700259 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800260 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800261 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800262 fcport->d_id.b.area, fcport->d_id.b.al_pa,
263 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700264 return rval;
265
266done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800267 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700268done:
Hannes Reineckefa83e652018-02-22 09:49:37 +0100269 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700270 return rval;
271}
Quinn Tran11aea162017-12-28 12:33:20 -0800272
273void
274qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
275 uint16_t *data)
276{
Hannes Reineckefa83e652018-02-22 09:49:37 +0100277 fcport->flags &= ~FCF_ASYNC_ACTIVE;
Quinn Tran11aea162017-12-28 12:33:20 -0800278 /* Don't re-login in target mode */
279 if (!fcport->tgt_session)
280 qla2x00_mark_device_lost(vha, fcport, 1, 0);
281 qlt_logo_completion_handler(fcport, data[0]);
282}
283
284static void
285qla2x00_async_prlo_sp_done(void *s, int res)
286{
287 srb_t *sp = (srb_t *)s;
288 struct srb_iocb *lio = &sp->u.iocb_cmd;
289 struct scsi_qla_host *vha = sp->vha;
290
Hannes Reineckefa83e652018-02-22 09:49:37 +0100291 sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
Quinn Tran11aea162017-12-28 12:33:20 -0800292 if (!test_bit(UNLOADING, &vha->dpc_flags))
293 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
294 lio->u.logio.data);
295 sp->free(sp);
296}
297
298int
299qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
300{
301 srb_t *sp;
302 struct srb_iocb *lio;
303 int rval;
304
305 rval = QLA_FUNCTION_FAILED;
306 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
307 if (!sp)
308 goto done;
309
310 sp->type = SRB_PRLO_CMD;
311 sp->name = "prlo";
Quinn Tran11aea162017-12-28 12:33:20 -0800312
313 lio = &sp->u.iocb_cmd;
314 lio->timeout = qla2x00_async_iocb_timeout;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000315 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
316
Quinn Tran11aea162017-12-28 12:33:20 -0800317 sp->done = qla2x00_async_prlo_sp_done;
318 rval = qla2x00_start_sp(sp);
319 if (rval != QLA_SUCCESS)
320 goto done_free_sp;
321
322 ql_dbg(ql_dbg_disc, vha, 0x2070,
323 "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
324 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
325 fcport->d_id.b.area, fcport->d_id.b.al_pa);
326 return rval;
327
328done_free_sp:
329 sp->free(sp);
330done:
Hannes Reineckefa83e652018-02-22 09:49:37 +0100331 fcport->flags &= ~FCF_ASYNC_ACTIVE;
Quinn Tran11aea162017-12-28 12:33:20 -0800332 return rval;
333}
334
Quinn Tranf13515a2017-12-28 12:33:15 -0800335static
336void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
337{
Quinn Tran0616e962017-12-28 12:33:34 -0800338 struct fc_port *fcport = ea->fcport;
339
340 ql_dbg(ql_dbg_disc, vha, 0x20d2,
341 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
342 __func__, fcport->port_name, fcport->disc_state,
343 fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
344 fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
345
346 if (ea->data[0] != MBS_COMMAND_COMPLETE) {
Quinn Trana4239942017-12-28 12:33:26 -0800347 ql_dbg(ql_dbg_disc, vha, 0x2066,
348 "%s %8phC: adisc fail: post delete\n",
349 __func__, ea->fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800350 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana4239942017-12-28 12:33:26 -0800351 return;
352 }
Quinn Trana4239942017-12-28 12:33:26 -0800353
354 if (ea->fcport->disc_state == DSC_DELETE_PEND)
355 return;
356
357 if (ea->sp->gen2 != ea->fcport->login_gen) {
358 /* target side must have changed it. */
359 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran0616e962017-12-28 12:33:34 -0800360 "%s %8phC generation changed\n",
361 __func__, ea->fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800362 return;
363 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
364 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
365 __func__, __LINE__, ea->fcport->port_name);
366 qla24xx_post_gidpn_work(vha, ea->fcport);
367 return;
368 }
369
370 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800371}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700372
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700373static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800374qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700375{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800376 srb_t *sp = ptr;
377 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800378 struct event_arg ea;
Quinn Tran0616e962017-12-28 12:33:34 -0800379 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700380
Quinn Tranf13515a2017-12-28 12:33:15 -0800381 ql_dbg(ql_dbg_disc, vha, 0x2066,
382 "Async done-%s res %x %8phC\n",
383 sp->name, res, sp->fcport->port_name);
384
Hannes Reineckefa83e652018-02-22 09:49:37 +0100385 sp->fcport->flags &= ~FCF_ASYNC_SENT;
386
Quinn Tranf13515a2017-12-28 12:33:15 -0800387 memset(&ea, 0, sizeof(ea));
388 ea.event = FCME_ADISC_DONE;
389 ea.rc = res;
Quinn Tran0616e962017-12-28 12:33:34 -0800390 ea.data[0] = lio->u.logio.data[0];
391 ea.data[1] = lio->u.logio.data[1];
392 ea.iop[0] = lio->u.logio.iop[0];
393 ea.iop[1] = lio->u.logio.iop[1];
Quinn Tranf13515a2017-12-28 12:33:15 -0800394 ea.fcport = sp->fcport;
395 ea.sp = sp;
396
397 qla2x00_fcport_event_handler(vha, &ea);
398
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800399 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700400}
401
402int
403qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
404 uint16_t *data)
405{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700406 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700407 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700408 int rval;
409
410 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800411 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800412 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700413 if (!sp)
414 goto done;
415
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800416 sp->type = SRB_ADISC_CMD;
417 sp->name = "adisc";
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800418
419 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700420 lio->timeout = qla2x00_async_iocb_timeout;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000421 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
422
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800423 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700424 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700425 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700426 rval = qla2x00_start_sp(sp);
427 if (rval != QLA_SUCCESS)
428 goto done_free_sp;
429
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700430 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800431 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
432 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700433 return rval;
434
435done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800436 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700437done:
Hannes Reineckefa83e652018-02-22 09:49:37 +0100438 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tranf13515a2017-12-28 12:33:15 -0800439 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700440 return rval;
441}
442
Quinn Tran726b8542017-01-19 22:28:00 -0800443static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
444 struct event_arg *ea)
445{
446 fc_port_t *fcport, *conflict_fcport;
447 struct get_name_list_extended *e;
448 u16 i, n, found = 0, loop_id;
449 port_id_t id;
450 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800451 u16 data[2];
452 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800453
454 fcport = ea->fcport;
Quinn Tranf352eeb2017-12-28 12:33:35 -0800455 ql_dbg(ql_dbg_disc, vha, 0xffff,
456 "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
457 __func__, fcport->port_name, fcport->disc_state,
458 fcport->fw_login_state, ea->rc,
459 fcport->login_gen, fcport->last_login_gen,
460 fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800461
Quinn Trana4239942017-12-28 12:33:26 -0800462 if (fcport->disc_state == DSC_DELETE_PEND)
463 return;
464
Quinn Tran726b8542017-01-19 22:28:00 -0800465 if (ea->rc) { /* rval */
466 if (fcport->login_retry == 0) {
467 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700468 ql_dbg(ql_dbg_disc, vha, 0x20de,
469 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
470 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800471 }
472 return;
473 }
474
475 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700476 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800477 "%s %8phC rscn gen changed rscn %d|%d \n",
478 __func__, fcport->port_name,
479 fcport->last_rscn_gen, fcport->rscn_gen);
480 qla24xx_post_gidpn_work(vha, fcport);
481 return;
482 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700483 ql_dbg(ql_dbg_disc, vha, 0x20e0,
Quinn Tranf352eeb2017-12-28 12:33:35 -0800484 "%s %8phC login gen changed\n",
485 __func__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800486 return;
487 }
488
489 n = ea->data[0] / sizeof(struct get_name_list_extended);
490
Quinn Tran83548fe2017-06-02 09:12:01 -0700491 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800492 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
493 __func__, __LINE__, fcport->port_name, n,
494 fcport->d_id.b.domain, fcport->d_id.b.area,
495 fcport->d_id.b.al_pa, fcport->loop_id);
496
497 for (i = 0; i < n; i++) {
498 e = &vha->gnl.l[i];
499 wwn = wwn_to_u64(e->port_name);
500
501 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
502 continue;
503
504 found = 1;
505 id.b.domain = e->port_id[2];
506 id.b.area = e->port_id[1];
507 id.b.al_pa = e->port_id[0];
508 id.b.rsvd_1 = 0;
509
510 loop_id = le16_to_cpu(e->nport_handle);
511 loop_id = (loop_id & 0x7fff);
512
Quinn Tran83548fe2017-06-02 09:12:01 -0700513 ql_dbg(ql_dbg_disc, vha, 0x20e2,
514 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
515 __func__, fcport->port_name,
516 e->current_login_state, fcport->fw_login_state,
517 id.b.domain, id.b.area, id.b.al_pa,
518 fcport->d_id.b.domain, fcport->d_id.b.area,
519 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800520
521 if ((id.b24 != fcport->d_id.b24) ||
522 ((fcport->loop_id != FC_NO_LOOP_ID) &&
523 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700524 ql_dbg(ql_dbg_disc, vha, 0x20e3,
525 "%s %d %8phC post del sess\n",
526 __func__, __LINE__, fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800527 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800528 return;
529 }
530
531 fcport->loop_id = loop_id;
532
533 wwn = wwn_to_u64(fcport->port_name);
534 qlt_find_sess_invalidate_other(vha, wwn,
535 id, loop_id, &conflict_fcport);
536
537 if (conflict_fcport) {
538 /*
539 * Another share fcport share the same loop_id &
540 * nport id. Conflict fcport needs to finish
541 * cleanup before this fcport can proceed to login.
542 */
543 conflict_fcport->conflict = fcport;
544 fcport->login_pause = 1;
545 }
546
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700547 if (fcport->fc4f_nvme)
548 current_login_state = e->current_login_state >> 4;
549 else
550 current_login_state = e->current_login_state & 0xf;
551
552 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800553 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700554 ql_dbg(ql_dbg_disc, vha, 0x20e4,
555 "%s %d %8phC post gpdb\n",
556 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800557
558 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
559 fcport->port_type = FCT_INITIATOR;
560 else
561 fcport->port_type = FCT_TARGET;
562
563 data[0] = data[1] = 0;
564 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800565 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800566 case DSC_LS_PORT_UNAVAIL:
567 default:
568 if (fcport->loop_id == FC_NO_LOOP_ID) {
569 qla2x00_find_new_loop_id(vha, fcport);
570 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
571 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700572 ql_dbg(ql_dbg_disc, vha, 0x20e5,
573 "%s %d %8phC\n",
574 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800575 qla24xx_fcport_handle_login(vha, fcport);
576 break;
577 }
578 }
579
580 if (!found) {
581 /* fw has no record of this port */
Quinn Tran040036b2017-12-28 12:33:38 -0800582 for (i = 0; i < n; i++) {
583 e = &vha->gnl.l[i];
584 id.b.domain = e->port_id[0];
585 id.b.area = e->port_id[1];
586 id.b.al_pa = e->port_id[2];
587 id.b.rsvd_1 = 0;
588 loop_id = le16_to_cpu(e->nport_handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800589
Quinn Tran040036b2017-12-28 12:33:38 -0800590 if (fcport->d_id.b24 == id.b24) {
591 conflict_fcport =
592 qla2x00_find_fcport_by_wwpn(vha,
593 e->port_name, 0);
594 ql_dbg(ql_dbg_disc, vha, 0x20e6,
595 "%s %d %8phC post del sess\n",
596 __func__, __LINE__,
597 conflict_fcport->port_name);
598 qlt_schedule_sess_for_deletion
Quinn Tran94cff6e2017-12-28 12:33:42 -0800599 (conflict_fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800600 }
Quinn Tran040036b2017-12-28 12:33:38 -0800601
602 /* FW already picked this loop id for another fcport */
603 if (fcport->loop_id == loop_id)
604 fcport->loop_id = FC_NO_LOOP_ID;
Quinn Tran726b8542017-01-19 22:28:00 -0800605 }
606 qla24xx_fcport_handle_login(vha, fcport);
607 }
608} /* gnl_event */
609
610static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800611qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800612{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800613 struct srb *sp = s;
614 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800615 unsigned long flags;
616 struct fc_port *fcport = NULL, *tf;
617 u16 i, n = 0, loop_id;
618 struct event_arg ea;
619 struct get_name_list_extended *e;
620 u64 wwn;
621 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800622 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800623
Quinn Tran83548fe2017-06-02 09:12:01 -0700624 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800625 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
626 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
627 sp->u.iocb_cmd.u.mbx.in_mb[2]);
628
629 memset(&ea, 0, sizeof(ea));
630 ea.sp = sp;
631 ea.rc = res;
632 ea.event = FCME_GNL_DONE;
633
634 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
635 sizeof(struct get_name_list_extended)) {
636 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
637 sizeof(struct get_name_list_extended);
638 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
639 }
640
641 for (i = 0; i < n; i++) {
642 e = &vha->gnl.l[i];
643 loop_id = le16_to_cpu(e->nport_handle);
644 /* mask out reserve bit */
645 loop_id = (loop_id & 0x7fff);
646 set_bit(loop_id, vha->hw->loop_id_map);
647 wwn = wwn_to_u64(e->port_name);
648
Quinn Tran83548fe2017-06-02 09:12:01 -0700649 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800650 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
651 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
652 e->port_id[0], e->current_login_state, e->last_login_state,
653 (loop_id & 0x7fff));
654 }
655
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100656 spin_lock_irqsave(&vha->gnl.fcports_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800657
658 INIT_LIST_HEAD(&h);
659 fcport = tf = NULL;
660 if (!list_empty(&vha->gnl.fcports))
661 list_splice_init(&vha->gnl.fcports, &h);
662
663 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
664 list_del_init(&fcport->gnl_entry);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100665 spin_lock(&vha->hw->tgt.sess_lock);
Quinn Tran6d6749272017-12-28 12:33:41 -0800666 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100667 spin_unlock(&vha->hw->tgt.sess_lock);
Quinn Tran726b8542017-01-19 22:28:00 -0800668 ea.fcport = fcport;
669
670 qla2x00_fcport_event_handler(vha, &ea);
671 }
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100672 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800673
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100674 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -0800675 /* create new fcport if fw has knowledge of new sessions */
676 for (i = 0; i < n; i++) {
677 port_id_t id;
678 u64 wwnn;
679
680 e = &vha->gnl.l[i];
681 wwn = wwn_to_u64(e->port_name);
682
683 found = false;
684 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
685 if (!memcmp((u8 *)&wwn, fcport->port_name,
686 WWN_SIZE)) {
687 found = true;
688 break;
689 }
690 }
691
Quinn Trancf055fb2017-12-28 12:33:33 -0800692 id.b.domain = e->port_id[2];
Quinn Trana4239942017-12-28 12:33:26 -0800693 id.b.area = e->port_id[1];
Quinn Trancf055fb2017-12-28 12:33:33 -0800694 id.b.al_pa = e->port_id[0];
Quinn Trana4239942017-12-28 12:33:26 -0800695 id.b.rsvd_1 = 0;
696
697 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
698 ql_dbg(ql_dbg_disc, vha, 0x2065,
Quinn Trancf055fb2017-12-28 12:33:33 -0800699 "%s %d %8phC %06x post new sess\n",
700 __func__, __LINE__, (u8 *)&wwn, id.b24);
Quinn Trana4239942017-12-28 12:33:26 -0800701 wwnn = wwn_to_u64(e->node_name);
702 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
703 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
704 }
705 }
706
Quinn Tran726b8542017-01-19 22:28:00 -0800707 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
708
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800709 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800710}
711
712int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
713{
714 srb_t *sp;
715 struct srb_iocb *mbx;
716 int rval = QLA_FUNCTION_FAILED;
717 unsigned long flags;
718 u16 *mb;
719
Quinn Tran3dbec592017-12-28 12:33:40 -0800720 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
721 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800722
Quinn Tran83548fe2017-06-02 09:12:01 -0700723 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800724 "Async-gnlist WWPN %8phC \n", fcport->port_name);
725
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100726 spin_lock_irqsave(&vha->gnl.fcports_lock, flags);
727 if (!list_empty(&fcport->gnl_entry)) {
728 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
729 rval = QLA_SUCCESS;
730 goto done;
731 }
732
733 spin_lock(&vha->hw->tgt.sess_lock);
Quinn Tran726b8542017-01-19 22:28:00 -0800734 fcport->disc_state = DSC_GNL;
735 fcport->last_rscn_gen = fcport->rscn_gen;
736 fcport->last_login_gen = fcport->login_gen;
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100737 spin_unlock(&vha->hw->tgt.sess_lock);
Quinn Tran726b8542017-01-19 22:28:00 -0800738
739 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100740 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800741
742 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
743 if (!sp)
744 goto done;
Quinn Tran3dbec592017-12-28 12:33:40 -0800745
746 fcport->flags |= FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800747 sp->type = SRB_MB_IOCB;
748 sp->name = "gnlist";
749 sp->gen1 = fcport->rscn_gen;
750 sp->gen2 = fcport->login_gen;
751
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000752 mbx = &sp->u.iocb_cmd;
753 mbx->timeout = qla2x00_async_iocb_timeout;
Quinn Tran726b8542017-01-19 22:28:00 -0800754 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
755
756 mb = sp->u.iocb_cmd.u.mbx.out_mb;
757 mb[0] = MBC_PORT_NODE_NAME_LIST;
758 mb[1] = BIT_2 | BIT_3;
759 mb[2] = MSW(vha->gnl.ldma);
760 mb[3] = LSW(vha->gnl.ldma);
761 mb[6] = MSW(MSD(vha->gnl.ldma));
762 mb[7] = LSW(MSD(vha->gnl.ldma));
763 mb[8] = vha->gnl.size;
764 mb[9] = vha->vp_idx;
765
Quinn Tran726b8542017-01-19 22:28:00 -0800766 sp->done = qla24xx_async_gnl_sp_done;
767
768 rval = qla2x00_start_sp(sp);
769 if (rval != QLA_SUCCESS)
770 goto done_free_sp;
771
Quinn Tran83548fe2017-06-02 09:12:01 -0700772 ql_dbg(ql_dbg_disc, vha, 0x20da,
773 "Async-%s - OUT WWPN %8phC hndl %x\n",
774 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800775
776 return rval;
777
778done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800779 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800780 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800781done:
Quinn Tran726b8542017-01-19 22:28:00 -0800782 return rval;
783}
784
785int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
786{
787 struct qla_work_evt *e;
788
789 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
790 if (!e)
791 return QLA_FUNCTION_FAILED;
792
793 e->u.fcport.fcport = fcport;
Quinn Tran6d6749272017-12-28 12:33:41 -0800794 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800795 return qla2x00_post_work(vha, e);
796}
797
798static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800799void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800800{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800801 struct srb *sp = s;
802 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800803 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800804 fc_port_t *fcport = sp->fcport;
805 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800806 struct event_arg ea;
807
Quinn Tran83548fe2017-06-02 09:12:01 -0700808 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800809 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
810 sp->name, res, fcport->port_name, mb[1], mb[2]);
811
Quinn Tran6d6749272017-12-28 12:33:41 -0800812 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800813
Quinn Tran726b8542017-01-19 22:28:00 -0800814 memset(&ea, 0, sizeof(ea));
815 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800816 ea.fcport = fcport;
817 ea.sp = sp;
818
819 qla2x00_fcport_event_handler(vha, &ea);
820
821 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
822 sp->u.iocb_cmd.u.mbx.in_dma);
823
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800824 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800825}
826
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700827static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
828{
829 struct qla_work_evt *e;
830
831 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
832 if (!e)
833 return QLA_FUNCTION_FAILED;
834
835 e->u.fcport.fcport = fcport;
836
837 return qla2x00_post_work(vha, e);
838}
839
840static void
841qla2x00_async_prli_sp_done(void *ptr, int res)
842{
843 srb_t *sp = ptr;
844 struct scsi_qla_host *vha = sp->vha;
845 struct srb_iocb *lio = &sp->u.iocb_cmd;
846 struct event_arg ea;
847
848 ql_dbg(ql_dbg_disc, vha, 0x2129,
849 "%s %8phC res %d \n", __func__,
850 sp->fcport->port_name, res);
851
852 sp->fcport->flags &= ~FCF_ASYNC_SENT;
853
854 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
855 memset(&ea, 0, sizeof(ea));
856 ea.event = FCME_PRLI_DONE;
857 ea.fcport = sp->fcport;
858 ea.data[0] = lio->u.logio.data[0];
859 ea.data[1] = lio->u.logio.data[1];
860 ea.iop[0] = lio->u.logio.iop[0];
861 ea.iop[1] = lio->u.logio.iop[1];
862 ea.sp = sp;
863
864 qla2x00_fcport_event_handler(vha, &ea);
865 }
866
867 sp->free(sp);
868}
869
870int
871qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
872{
873 srb_t *sp;
874 struct srb_iocb *lio;
875 int rval = QLA_FUNCTION_FAILED;
876
877 if (!vha->flags.online)
878 return rval;
879
880 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700881 fcport->fw_login_state == DSC_LS_PRLI_PEND)
882 return rval;
883
884 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
885 if (!sp)
886 return rval;
887
888 fcport->flags |= FCF_ASYNC_SENT;
889 fcport->logout_completed = 0;
890
891 sp->type = SRB_PRLI_CMD;
892 sp->name = "prli";
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700893
894 lio = &sp->u.iocb_cmd;
895 lio->timeout = qla2x00_async_iocb_timeout;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000896 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
897
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700898 sp->done = qla2x00_async_prli_sp_done;
899 lio->u.logio.flags = 0;
900
901 if (fcport->fc4f_nvme)
902 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
903
904 rval = qla2x00_start_sp(sp);
905 if (rval != QLA_SUCCESS) {
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700906 fcport->flags |= FCF_LOGIN_NEEDED;
907 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
908 goto done_free_sp;
909 }
910
911 ql_dbg(ql_dbg_disc, vha, 0x211b,
912 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
913 fcport->port_name, sp->handle, fcport->loop_id,
914 fcport->d_id.b24, fcport->login_retry);
915
916 return rval;
917
918done_free_sp:
919 sp->free(sp);
920 fcport->flags &= ~FCF_ASYNC_SENT;
921 return rval;
922}
923
Quinn Trana07fc0a2017-08-23 15:05:21 -0700924int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800925{
926 struct qla_work_evt *e;
927
928 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
929 if (!e)
930 return QLA_FUNCTION_FAILED;
931
932 e->u.fcport.fcport = fcport;
933 e->u.fcport.opt = opt;
Quinn Tran6d6749272017-12-28 12:33:41 -0800934 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800935 return qla2x00_post_work(vha, e);
936}
937
938int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
939{
940 srb_t *sp;
941 struct srb_iocb *mbx;
942 int rval = QLA_FUNCTION_FAILED;
943 u16 *mb;
944 dma_addr_t pd_dma;
945 struct port_database_24xx *pd;
946 struct qla_hw_data *ha = vha->hw;
947
Quinn Tran3dbec592017-12-28 12:33:40 -0800948 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
949 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800950
Quinn Tran726b8542017-01-19 22:28:00 -0800951 fcport->disc_state = DSC_GPDB;
952
953 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
954 if (!sp)
955 goto done;
956
Quinn Tran3dbec592017-12-28 12:33:40 -0800957 fcport->flags |= FCF_ASYNC_SENT;
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700958 sp->type = SRB_MB_IOCB;
959 sp->name = "gpdb";
960 sp->gen1 = fcport->rscn_gen;
961 sp->gen2 = fcport->login_gen;
Ben Hutchingse74e7d92018-03-20 21:36:14 +0000962
963 mbx = &sp->u.iocb_cmd;
964 mbx->timeout = qla2x00_async_iocb_timeout;
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700965 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
966
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200967 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800968 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700969 ql_log(ql_log_warn, vha, 0xd043,
970 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800971 goto done_free_sp;
972 }
Quinn Tran726b8542017-01-19 22:28:00 -0800973
Quinn Tran726b8542017-01-19 22:28:00 -0800974 mb = sp->u.iocb_cmd.u.mbx.out_mb;
975 mb[0] = MBC_GET_PORT_DATABASE;
976 mb[1] = fcport->loop_id;
977 mb[2] = MSW(pd_dma);
978 mb[3] = LSW(pd_dma);
979 mb[6] = MSW(MSD(pd_dma));
980 mb[7] = LSW(MSD(pd_dma));
981 mb[9] = vha->vp_idx;
982 mb[10] = opt;
983
Quinn Tran726b8542017-01-19 22:28:00 -0800984 mbx->u.mbx.in = (void *)pd;
985 mbx->u.mbx.in_dma = pd_dma;
986
987 sp->done = qla24xx_async_gpdb_sp_done;
988
989 rval = qla2x00_start_sp(sp);
990 if (rval != QLA_SUCCESS)
991 goto done_free_sp;
992
Quinn Tran83548fe2017-06-02 09:12:01 -0700993 ql_dbg(ql_dbg_disc, vha, 0x20dc,
994 "Async-%s %8phC hndl %x opt %x\n",
995 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800996
997 return rval;
998
999done_free_sp:
1000 if (pd)
1001 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1002
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001003 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -08001004 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -08001005done:
Quinn Tran726b8542017-01-19 22:28:00 -08001006 qla24xx_post_gpdb_work(vha, fcport, opt);
1007 return rval;
1008}
1009
1010static
Quinn Trana4239942017-12-28 12:33:26 -08001011void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001012{
Quinn Tran726b8542017-01-19 22:28:00 -08001013 unsigned long flags;
1014
Quinn Tran726b8542017-01-19 22:28:00 -08001015 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001016 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001017 ea->fcport->deleted = 0;
1018 ea->fcport->logout_on_delete = 1;
1019
1020 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1021 vha->fcport_count++;
1022 ea->fcport->login_succ = 1;
1023
1024 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1025 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001026 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001027 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001028 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001029 vha->fcport_count);
1030
Quinn Trana4239942017-12-28 12:33:26 -08001031 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001032 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001033 if (ea->fcport->id_changed) {
1034 ea->fcport->id_changed = 0;
1035 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1036 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1037 __func__, __LINE__, ea->fcport->port_name,
1038 vha->fcport_count);
1039 qla24xx_post_gfpnid_work(vha, ea->fcport);
1040 } else {
1041 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1042 "%s %d %8phC post gpsc fcp_cnt %d\n",
1043 __func__, __LINE__, ea->fcport->port_name,
1044 vha->fcport_count);
1045 qla24xx_post_gpsc_work(vha, ea->fcport);
1046 }
Quinn Tran726b8542017-01-19 22:28:00 -08001047 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001048 } else if (ea->fcport->login_succ) {
1049 /*
1050 * We have an existing session. A late RSCN delivery
1051 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001052 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001053 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001054 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1055 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001056 __func__, __LINE__, ea->fcport->port_name);
himanshu.madhani@cavium.com8a7eac22018-01-15 20:46:50 -08001057 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001058 }
1059 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001060}
Quinn Tran726b8542017-01-19 22:28:00 -08001061
Quinn Trana4239942017-12-28 12:33:26 -08001062static
1063void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1064{
Quinn Trana4239942017-12-28 12:33:26 -08001065 fc_port_t *fcport = ea->fcport;
1066 struct port_database_24xx *pd;
1067 struct srb *sp = ea->sp;
Darren Trapp2b5b9642018-02-27 16:31:12 -08001068 uint8_t ls;
Quinn Trana4239942017-12-28 12:33:26 -08001069
1070 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1071
1072 fcport->flags &= ~FCF_ASYNC_SENT;
1073
1074 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001075 "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1076 fcport->disc_state, pd->current_login_state, ea->rc);
Quinn Trana4239942017-12-28 12:33:26 -08001077
1078 if (fcport->disc_state == DSC_DELETE_PEND)
1079 return;
1080
Darren Trapp2b5b9642018-02-27 16:31:12 -08001081 if (fcport->fc4f_nvme)
1082 ls = pd->current_login_state >> 4;
1083 else
1084 ls = pd->current_login_state & 0xf;
1085
1086 switch (ls) {
Quinn Trana4239942017-12-28 12:33:26 -08001087 case PDS_PRLI_COMPLETE:
1088 __qla24xx_parse_gpdb(vha, fcport, pd);
1089 break;
1090 case PDS_PLOGI_PENDING:
1091 case PDS_PLOGI_COMPLETE:
1092 case PDS_PRLI_PENDING:
1093 case PDS_PRLI2_PENDING:
1094 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1095 __func__, __LINE__, fcport->port_name);
1096 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1097 return;
1098 case PDS_LOGO_PENDING:
1099 case PDS_PORT_UNAVAILABLE:
1100 default:
1101 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1102 __func__, __LINE__, fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08001103 qlt_schedule_sess_for_deletion(fcport);
Quinn Trana4239942017-12-28 12:33:26 -08001104 return;
1105 }
1106 __qla24xx_handle_gpdb_event(vha, ea);
1107} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001108
1109static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1110{
1111 u8 login = 0;
Quinn Tran040036b2017-12-28 12:33:38 -08001112 int rc;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001113
1114 if (qla_tgt_mode_enabled(vha))
1115 return;
1116
1117 if (qla_dual_mode_enabled(vha)) {
1118 if (N2N_TOPO(vha->hw)) {
1119 u64 mywwn, wwn;
1120
1121 mywwn = wwn_to_u64(vha->port_name);
1122 wwn = wwn_to_u64(fcport->port_name);
1123 if (mywwn > wwn)
1124 login = 1;
1125 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1126 && time_after_eq(jiffies,
1127 fcport->plogi_nack_done_deadline))
1128 login = 1;
1129 } else {
1130 login = 1;
1131 }
1132 } else {
1133 /* initiator mode */
1134 login = 1;
1135 }
1136
1137 if (login) {
Quinn Tran040036b2017-12-28 12:33:38 -08001138 if (fcport->loop_id == FC_NO_LOOP_ID) {
1139 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1140 rc = qla2x00_find_new_loop_id(vha, fcport);
1141 if (rc) {
1142 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1143 "%s %d %8phC post del sess - out of loopid\n",
1144 __func__, __LINE__, fcport->port_name);
1145 fcport->scan_state = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001146 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran040036b2017-12-28 12:33:38 -08001147 return;
1148 }
1149 }
Quinn Tran9cd883f2017-12-28 12:33:24 -08001150 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1151 "%s %d %8phC post login\n",
1152 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001153 qla2x00_post_async_login_work(vha, fcport, NULL);
1154 }
1155}
1156
Quinn Tran726b8542017-01-19 22:28:00 -08001157int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1158{
Quinn Tranf13515a2017-12-28 12:33:15 -08001159 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001160 u64 wwn;
1161
1162 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001163 "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d retry %d lid %d scan %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001164 __func__, fcport->port_name, fcport->disc_state,
1165 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1166 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001167 fcport->login_gen, fcport->login_retry,
Quinn Trana4239942017-12-28 12:33:26 -08001168 fcport->loop_id, fcport->scan_state);
1169
Quinn Tran726b8542017-01-19 22:28:00 -08001170 if (fcport->login_retry == 0)
1171 return 0;
1172
1173 if (fcport->scan_state != QLA_FCPORT_FOUND)
1174 return 0;
1175
Hannes Reinecke07ea4b62018-02-22 09:49:36 +01001176 if ((fcport->loop_id != FC_NO_LOOP_ID) &&
1177 ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1178 (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
Quinn Tran726b8542017-01-19 22:28:00 -08001179 return 0;
1180
Quinn Tran5b334692017-03-15 09:48:48 -07001181 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001182 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1183 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001184 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001185 }
Quinn Tran5b334692017-03-15 09:48:48 -07001186 }
1187
Quinn Tran726b8542017-01-19 22:28:00 -08001188 /* for pure Target Mode. Login will not be initiated */
1189 if (vha->host->active_mode == MODE_TARGET)
1190 return 0;
1191
1192 if (fcport->flags & FCF_ASYNC_SENT) {
1193 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1194 return 0;
1195 }
1196
Quinn Trana4239942017-12-28 12:33:26 -08001197 fcport->login_retry--;
1198
Quinn Tran726b8542017-01-19 22:28:00 -08001199 switch (fcport->disc_state) {
1200 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001201 wwn = wwn_to_u64(fcport->node_name);
1202 if (wwn == 0) {
1203 ql_dbg(ql_dbg_disc, vha, 0xffff,
1204 "%s %d %8phC post GNNID\n",
1205 __func__, __LINE__, fcport->port_name);
1206 qla24xx_post_gnnid_work(vha, fcport);
1207 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001208 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1209 "%s %d %8phC post gnl\n",
1210 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001211 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001212 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001213 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001214 }
1215 break;
1216
1217 case DSC_GNL:
1218 if (fcport->login_pause) {
1219 fcport->last_rscn_gen = fcport->rscn_gen;
1220 fcport->last_login_gen = fcport->login_gen;
1221 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1222 break;
1223 }
1224
Quinn Tran9cd883f2017-12-28 12:33:24 -08001225 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001226 break;
1227
1228 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001229 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1230 "%s %d %8phC post gidpn\n",
1231 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001232 if (N2N_TOPO(vha->hw))
1233 qla_chk_n2n_b4_login(vha, fcport);
1234 else
1235 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001236 break;
1237
1238 case DSC_LOGIN_COMPLETE:
1239 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001240 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001241 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001242 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001243 data[0] = data[1] = 0;
1244 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001245 break;
1246
Darren Trapp1cbc0ef2018-03-20 23:09:37 -07001247 case DSC_LOGIN_PEND:
1248 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1249 qla24xx_post_prli_work(vha, fcport);
1250 break;
1251
Quinn Tran726b8542017-01-19 22:28:00 -08001252 default:
1253 break;
1254 }
1255
1256 return 0;
1257}
1258
1259static
1260void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1261{
1262 fcport->rscn_gen++;
1263
Quinn Tran83548fe2017-06-02 09:12:01 -07001264 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1265 "%s %8phC DS %d LS %d\n",
1266 __func__, fcport->port_name, fcport->disc_state,
1267 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001268
1269 if (fcport->flags & FCF_ASYNC_SENT)
1270 return;
1271
1272 switch (fcport->disc_state) {
1273 case DSC_DELETED:
1274 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001275 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001276 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001277 default:
1278 break;
1279 }
1280}
1281
1282int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001283 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001284{
1285 struct qla_work_evt *e;
1286 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1287 if (!e)
1288 return QLA_FUNCTION_FAILED;
1289
1290 e->u.new_sess.id = *id;
1291 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001292 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001293 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001294 if (node_name)
1295 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001296
1297 return qla2x00_post_work(vha, e);
1298}
1299
1300static
Quinn Tran726b8542017-01-19 22:28:00 -08001301void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1302 struct event_arg *ea)
1303{
1304 fc_port_t *fcport = ea->fcport;
1305
Quinn Tran83548fe2017-06-02 09:12:01 -07001306 ql_dbg(ql_dbg_disc, vha, 0x2102,
1307 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1308 __func__, fcport->port_name, fcport->disc_state,
1309 fcport->fw_login_state, fcport->login_pause,
1310 fcport->deleted, fcport->conflict,
1311 fcport->last_rscn_gen, fcport->rscn_gen,
1312 fcport->last_login_gen, fcport->login_gen,
1313 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001314
1315 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001316 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1317 return;
1318
Quinn Tran5b334692017-03-15 09:48:48 -07001319 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001320 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1321 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001322 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001323 }
Quinn Tran5b334692017-03-15 09:48:48 -07001324 }
1325
Quinn Tran726b8542017-01-19 22:28:00 -08001326 if (fcport->flags & FCF_ASYNC_SENT) {
1327 fcport->login_retry++;
1328 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1329 return;
1330 }
1331
1332 if (fcport->disc_state == DSC_DELETE_PEND) {
1333 fcport->login_retry++;
1334 return;
1335 }
1336
1337 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001338 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001339 __func__, __LINE__, fcport->port_name);
1340
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001341 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001342 return;
1343 }
1344
1345 qla24xx_fcport_handle_login(vha, fcport);
1346}
1347
Quinn Tran41dc5292017-01-19 22:28:03 -08001348void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001349{
Quinn Tranf352eeb2017-12-28 12:33:35 -08001350 fc_port_t *f, *tf;
Quinn Tran41dc5292017-01-19 22:28:03 -08001351 uint32_t id = 0, mask, rid;
Quinn Tranf352eeb2017-12-28 12:33:35 -08001352 unsigned long flags;
Quinn Tran726b8542017-01-19 22:28:00 -08001353
1354 switch (ea->event) {
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001355 case FCME_RSCN:
1356 case FCME_GIDPN_DONE:
1357 case FCME_GPSC_DONE:
1358 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001359 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001360 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1361 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1362 return;
1363 break;
1364 default:
1365 break;
1366 }
1367
1368 switch (ea->event) {
1369 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001370 if (test_bit(UNLOADING, &vha->dpc_flags))
1371 return;
1372
1373 qla24xx_handle_relogin_event(vha, ea);
1374 break;
1375 case FCME_RSCN:
1376 if (test_bit(UNLOADING, &vha->dpc_flags))
1377 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001378 switch (ea->id.b.rsvd_1) {
1379 case RSCN_PORT_ADDR:
Quinn Tranf352eeb2017-12-28 12:33:35 -08001380 spin_lock_irqsave(&vha->work_lock, flags);
1381 if (vha->scan.scan_flags == 0) {
1382 ql_dbg(ql_dbg_disc, vha, 0xffff,
1383 "%s: schedule\n", __func__);
1384 vha->scan.scan_flags |= SF_QUEUED;
1385 schedule_delayed_work(&vha->scan.scan_work, 5);
Quinn Tran726b8542017-01-19 22:28:00 -08001386 }
Quinn Tranf352eeb2017-12-28 12:33:35 -08001387 spin_unlock_irqrestore(&vha->work_lock, flags);
1388
Quinn Tran41dc5292017-01-19 22:28:03 -08001389 break;
1390 case RSCN_AREA_ADDR:
1391 case RSCN_DOM_ADDR:
1392 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1393 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001394 ql_dbg(ql_dbg_async, vha, 0x5044,
1395 "RSCN: Area 0x%06x was affected\n",
1396 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001397 } else {
1398 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001399 ql_dbg(ql_dbg_async, vha, 0x507a,
1400 "RSCN: Domain 0x%06x was affected\n",
1401 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001402 }
1403
1404 rid = ea->id.b24 & mask;
1405 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1406 list) {
1407 id = f->d_id.b24 & mask;
1408 if (rid == id) {
1409 ea->fcport = f;
1410 qla24xx_handle_rscn_event(f, ea);
1411 }
1412 }
1413 break;
1414 case RSCN_FAB_ADDR:
1415 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001416 ql_log(ql_log_warn, vha, 0xd045,
1417 "RSCN: Fabric was affected. Addr format %d\n",
1418 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001419 qla2x00_mark_all_devices_lost(vha, 1);
1420 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1421 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001422 }
1423 break;
1424 case FCME_GIDPN_DONE:
1425 qla24xx_handle_gidpn_event(vha, ea);
1426 break;
1427 case FCME_GNL_DONE:
1428 qla24xx_handle_gnl_done_event(vha, ea);
1429 break;
1430 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001431 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001432 break;
1433 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1434 qla24xx_handle_plogi_done_event(vha, ea);
1435 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001436 case FCME_PRLI_DONE:
1437 qla24xx_handle_prli_done_event(vha, ea);
1438 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001439 case FCME_GPDB_DONE:
1440 qla24xx_handle_gpdb_event(vha, ea);
1441 break;
1442 case FCME_GPNID_DONE:
1443 qla24xx_handle_gpnid_event(vha, ea);
1444 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001445 case FCME_GFFID_DONE:
1446 qla24xx_handle_gffid_event(vha, ea);
1447 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001448 case FCME_ADISC_DONE:
1449 qla24xx_handle_adisc_event(vha, ea);
1450 break;
Quinn Trana4239942017-12-28 12:33:26 -08001451 case FCME_GNNID_DONE:
1452 qla24xx_handle_gnnid_event(vha, ea);
1453 break;
1454 case FCME_GFPNID_DONE:
1455 qla24xx_handle_gfpnid_event(vha, ea);
1456 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001457 default:
1458 BUG_ON(1);
1459 break;
1460 }
1461}
1462
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001463static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001464qla2x00_tmf_iocb_timeout(void *data)
1465{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001466 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001467 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1468
1469 tmf->u.tmf.comp_status = CS_TIMEOUT;
1470 complete(&tmf->u.tmf.comp);
1471}
1472
1473static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001474qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001475{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001476 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001477 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001478
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001479 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001480}
1481
1482int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001483qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001484 uint32_t tag)
1485{
1486 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001487 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001488 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001489 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001490
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001491 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001492 if (!sp)
1493 goto done;
1494
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001495 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001496 sp->type = SRB_TM_CMD;
1497 sp->name = "tmf";
Ben Hutchingse74e7d92018-03-20 21:36:14 +00001498
1499 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1500 init_completion(&tm_iocb->u.tmf.comp);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001501 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
Ben Hutchingse74e7d92018-03-20 21:36:14 +00001502
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001503 tm_iocb->u.tmf.flags = flags;
1504 tm_iocb->u.tmf.lun = lun;
1505 tm_iocb->u.tmf.data = tag;
1506 sp->done = qla2x00_tmf_sp_done;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001507
1508 rval = qla2x00_start_sp(sp);
1509 if (rval != QLA_SUCCESS)
1510 goto done_free_sp;
1511
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001512 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001513 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1514 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1515 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001516
1517 wait_for_completion(&tm_iocb->u.tmf.comp);
1518
1519 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1520 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1521
1522 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1523 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1524 "TM IOCB failed (%x).\n", rval);
1525 }
1526
1527 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1528 flags = tm_iocb->u.tmf.flags;
1529 lun = (uint16_t)tm_iocb->u.tmf.lun;
1530
1531 /* Issue Marker IOCB */
1532 qla2x00_marker(vha, vha->hw->req_q_map[0],
1533 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1534 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1535 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001536
1537done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001538 sp->free(sp);
Quinn Tran6d6749272017-12-28 12:33:41 -08001539 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001540done:
1541 return rval;
1542}
1543
Armen Baloyan4440e462014-02-26 04:15:18 -05001544static void
1545qla24xx_abort_iocb_timeout(void *data)
1546{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001547 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001548 struct srb_iocb *abt = &sp->u.iocb_cmd;
1549
1550 abt->u.abt.comp_status = CS_TIMEOUT;
1551 complete(&abt->u.abt.comp);
1552}
1553
1554static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001555qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001556{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001557 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001558 struct srb_iocb *abt = &sp->u.iocb_cmd;
1559
Ben Hutchings3a9910d2018-03-20 21:05:48 +00001560 if (del_timer(&sp->u.iocb_cmd.timer))
1561 complete(&abt->u.abt.comp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001562}
1563
Quinn Tran15f30a52017-03-15 09:48:52 -07001564int
Armen Baloyan4440e462014-02-26 04:15:18 -05001565qla24xx_async_abort_cmd(srb_t *cmd_sp)
1566{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001567 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001568 fc_port_t *fcport = cmd_sp->fcport;
1569 struct srb_iocb *abt_iocb;
1570 srb_t *sp;
1571 int rval = QLA_FUNCTION_FAILED;
1572
1573 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1574 if (!sp)
1575 goto done;
1576
1577 abt_iocb = &sp->u.iocb_cmd;
1578 sp->type = SRB_ABT_CMD;
1579 sp->name = "abort";
Ben Hutchingse74e7d92018-03-20 21:36:14 +00001580
1581 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1582 init_completion(&abt_iocb->u.abt.comp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001583 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
Ben Hutchingse74e7d92018-03-20 21:36:14 +00001584
Armen Baloyan4440e462014-02-26 04:15:18 -05001585 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
Darren Trappb027a5a2018-01-15 20:46:51 -08001586
1587 if (vha->flags.qpairs_available && cmd_sp->qpair)
1588 abt_iocb->u.abt.req_que_no =
1589 cpu_to_le16(cmd_sp->qpair->req->id);
1590 else
1591 abt_iocb->u.abt.req_que_no = cpu_to_le16(vha->req->id);
1592
Armen Baloyan4440e462014-02-26 04:15:18 -05001593 sp->done = qla24xx_abort_sp_done;
Armen Baloyan4440e462014-02-26 04:15:18 -05001594
1595 rval = qla2x00_start_sp(sp);
1596 if (rval != QLA_SUCCESS)
1597 goto done_free_sp;
1598
1599 ql_dbg(ql_dbg_async, vha, 0x507c,
1600 "Abort command issued - hdl=%x, target_id=%x\n",
1601 cmd_sp->handle, fcport->tgt_id);
1602
1603 wait_for_completion(&abt_iocb->u.abt.comp);
1604
1605 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1606 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1607
1608done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001609 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001610done:
1611 return rval;
1612}
1613
1614int
1615qla24xx_async_abort_command(srb_t *sp)
1616{
1617 unsigned long flags = 0;
1618
1619 uint32_t handle;
1620 fc_port_t *fcport = sp->fcport;
1621 struct scsi_qla_host *vha = fcport->vha;
1622 struct qla_hw_data *ha = vha->hw;
1623 struct req_que *req = vha->req;
1624
Darren Trappb027a5a2018-01-15 20:46:51 -08001625 if (vha->flags.qpairs_available && sp->qpair)
1626 req = sp->qpair->req;
1627
Armen Baloyan4440e462014-02-26 04:15:18 -05001628 spin_lock_irqsave(&ha->hardware_lock, flags);
1629 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1630 if (req->outstanding_cmds[handle] == sp)
1631 break;
1632 }
1633 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1634 if (handle == req->num_outstanding_cmds) {
1635 /* Command not found. */
1636 return QLA_FUNCTION_FAILED;
1637 }
1638 if (sp->type == SRB_FXIOCB_DCMD)
1639 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1640 FXDISC_ABORT_IOCTL);
1641
1642 return qla24xx_async_abort_cmd(sp);
1643}
1644
Quinn Tran726b8542017-01-19 22:28:00 -08001645static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001646qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1647{
1648 switch (ea->data[0]) {
1649 case MBS_COMMAND_COMPLETE:
1650 ql_dbg(ql_dbg_disc, vha, 0x2118,
1651 "%s %d %8phC post gpdb\n",
1652 __func__, __LINE__, ea->fcport->port_name);
1653
1654 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1655 ea->fcport->logout_on_delete = 1;
1656 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1657 break;
1658 default:
Darren Trapp1cbc0ef2018-03-20 23:09:37 -07001659 if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
1660 (ea->iop[1] == 0x50000)) { /* reson 5=busy expl:0x0 */
1661 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1662 ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
1663 break;
1664 }
1665
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001666 if (ea->fcport->n2n_flag) {
1667 ql_dbg(ql_dbg_disc, vha, 0x2118,
1668 "%s %d %8phC post fc4 prli\n",
1669 __func__, __LINE__, ea->fcport->port_name);
1670 ea->fcport->fc4f_nvme = 0;
1671 ea->fcport->n2n_flag = 0;
1672 qla24xx_post_prli_work(vha, ea->fcport);
1673 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001674 ql_dbg(ql_dbg_disc, vha, 0x2119,
1675 "%s %d %8phC unhandle event of %x\n",
1676 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1677 break;
1678 }
1679}
1680
1681static void
Quinn Tran726b8542017-01-19 22:28:00 -08001682qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001683{
Quinn Tran726b8542017-01-19 22:28:00 -08001684 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001685 u16 lid;
1686 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001687 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001688 struct fc_port *fcport = ea->fcport;
1689
Quinn Tranf352eeb2017-12-28 12:33:35 -08001690 ql_dbg(ql_dbg_disc, vha, 0xffff,
1691 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1692 __func__, fcport->port_name, fcport->disc_state,
1693 fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1694 ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1695 ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1696
Quinn Trana4239942017-12-28 12:33:26 -08001697 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1698 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1699 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1700 "%s %d %8phC Remote is trying to login\n",
1701 __func__, __LINE__, fcport->port_name);
1702 return;
1703 }
1704
1705 if (fcport->disc_state == DSC_DELETE_PEND)
1706 return;
1707
1708 if (ea->sp->gen2 != fcport->login_gen) {
1709 /* target side must have changed it. */
1710 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001711 "%s %8phC generation changed\n",
1712 __func__, fcport->port_name);
1713 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Trana4239942017-12-28 12:33:26 -08001714 return;
1715 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1716 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1717 __func__, __LINE__, fcport->port_name);
1718 qla24xx_post_gidpn_work(vha, fcport);
1719 return;
1720 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001721
Quinn Tran726b8542017-01-19 22:28:00 -08001722 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001723 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001724 /*
1725 * Driver must validate login state - If PRLI not complete,
1726 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1727 * requests.
1728 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001729 if (ea->fcport->fc4f_nvme) {
1730 ql_dbg(ql_dbg_disc, vha, 0x2117,
1731 "%s %d %8phC post prli\n",
1732 __func__, __LINE__, ea->fcport->port_name);
1733 qla24xx_post_prli_work(vha, ea->fcport);
1734 } else {
1735 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001736 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1737 __func__, __LINE__, ea->fcport->port_name,
1738 ea->fcport->loop_id, ea->fcport->d_id.b24);
1739
1740 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001741 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001742 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1743 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001744 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001745 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1746 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1747
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001748 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1749 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001750 break;
1751 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001752 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001753 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1754
1755 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1756 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1757 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001758 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1759 else
Quinn Tran726b8542017-01-19 22:28:00 -08001760 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001761 break;
1762 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001763 /* data[1] = IO PARAM 1 = nport ID */
1764 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1765 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1766 cid.b.al_pa = ea->iop[1] & 0xff;
1767 cid.b.rsvd_1 = 0;
1768
Quinn Tran83548fe2017-06-02 09:12:01 -07001769 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1770 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1771 __func__, __LINE__, ea->fcport->port_name,
1772 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001773
1774 if (IS_SW_RESV_ADDR(cid)) {
1775 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1776 ea->fcport->loop_id = FC_NO_LOOP_ID;
1777 } else {
1778 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001779 }
Quinn Tran726b8542017-01-19 22:28:00 -08001780 qla24xx_post_gnl_work(vha, ea->fcport);
1781 break;
1782 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001783 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1784 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1785 __func__, __LINE__, ea->fcport->port_name,
1786 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1787 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001788
Quinn Trana084fd62017-12-04 14:45:00 -08001789 lid = ea->iop[1] & 0xffff;
1790 qlt_find_sess_invalidate_other(vha,
1791 wwn_to_u64(ea->fcport->port_name),
1792 ea->fcport->d_id, lid, &conflict_fcport);
1793
1794 if (conflict_fcport) {
1795 /*
1796 * Another fcport share the same loop_id/nport id.
1797 * Conflict fcport needs to finish cleanup before this
1798 * fcport can proceed to login.
1799 */
1800 conflict_fcport->conflict = ea->fcport;
1801 ea->fcport->login_pause = 1;
1802
1803 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1804 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1805 __func__, __LINE__, ea->fcport->port_name,
1806 ea->fcport->d_id.b24, lid);
1807 qla2x00_clear_loop_id(ea->fcport);
1808 qla24xx_post_gidpn_work(vha, ea->fcport);
1809 } else {
1810 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1811 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1812 __func__, __LINE__, ea->fcport->port_name,
1813 ea->fcport->d_id.b24, lid);
1814
1815 qla2x00_clear_loop_id(ea->fcport);
1816 set_bit(lid, vha->hw->loop_id_map);
1817 ea->fcport->loop_id = lid;
1818 ea->fcport->keep_nport_handle = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001819 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana084fd62017-12-04 14:45:00 -08001820 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001821 break;
1822 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001823 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001824}
1825
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001826void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001827qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1828 uint16_t *data)
1829{
Quinn Tran726b8542017-01-19 22:28:00 -08001830 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001831 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001832 fcport->login_gen++;
Hannes Reineckefa83e652018-02-22 09:49:37 +01001833 fcport->flags &= ~FCF_ASYNC_ACTIVE;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001834 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001835}
1836
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001837void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001838qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1839 uint16_t *data)
1840{
Hannes Reineckefa83e652018-02-22 09:49:37 +01001841 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001842 if (data[0] == MBS_COMMAND_COMPLETE) {
1843 qla2x00_update_fcport(vha, fcport);
1844
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001845 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001846 }
1847
1848 /* Retry login. */
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001849 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1850 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1851 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001852 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001853
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001854 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857/****************************************************************************/
1858/* QLogic ISP2x00 Hardware Support Functions. */
1859/****************************************************************************/
1860
Saurav Kashyapfa492632012-11-21 02:40:29 -05001861static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001862qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1863{
1864 int rval = QLA_SUCCESS;
1865 struct qla_hw_data *ha = vha->hw;
1866 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001867 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001868
1869 qla83xx_idc_lock(vha, 0);
1870
1871 /* SV: TODO: Assign initialization timeout from
1872 * flash-info / other param
1873 */
1874 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1875 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1876
1877 /* Set our fcoe function presence */
1878 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1879 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1880 "Error while setting DRV-Presence.\n");
1881 rval = QLA_FUNCTION_FAILED;
1882 goto exit;
1883 }
1884
1885 /* Decide the reset ownership */
1886 qla83xx_reset_ownership(vha);
1887
1888 /*
1889 * On first protocol driver load:
1890 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1891 * register.
1892 * Others: Check compatibility with current IDC Major version.
1893 */
1894 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1895 if (ha->flags.nic_core_reset_owner) {
1896 /* Set IDC Major version */
1897 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1898 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1899
1900 /* Clearing IDC-Lock-Recovery register */
1901 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1902 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1903 /*
1904 * Clear further IDC participation if we are not compatible with
1905 * the current IDC Major Version.
1906 */
1907 ql_log(ql_log_warn, vha, 0xb07d,
1908 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1909 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1910 __qla83xx_clear_drv_presence(vha);
1911 rval = QLA_FUNCTION_FAILED;
1912 goto exit;
1913 }
1914 /* Each function sets its supported Minor version. */
1915 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1916 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1917 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1918
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001919 if (ha->flags.nic_core_reset_owner) {
1920 memset(config, 0, sizeof(config));
1921 if (!qla81xx_get_port_config(vha, config))
1922 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1923 QLA8XXX_DEV_READY);
1924 }
1925
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001926 rval = qla83xx_idc_state_handler(vha);
1927
1928exit:
1929 qla83xx_idc_unlock(vha, 0);
1930
1931 return rval;
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/*
1935* qla2x00_initialize_adapter
1936* Initialize board.
1937*
1938* Input:
1939* ha = adapter block pointer.
1940*
1941* Returns:
1942* 0 = success
1943*/
1944int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001945qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001948 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001949 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001950
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001951 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1952 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001955 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001956 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001957 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001958 ha->flags.pci_channel_io_perm_failure = 0;
1959 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001960 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001961 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1962 atomic_set(&vha->loop_state, LOOP_DOWN);
1963 vha->device_flags = DFLG_NO_CABLE;
1964 vha->dpc_flags = 0;
1965 vha->flags.management_server_logged_in = 0;
1966 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 ha->isp_abort_cnt = 0;
1968 ha->beacon_blink_led = 0;
1969
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001970 set_bit(0, ha->req_qid_map);
1971 set_bit(0, ha->rsp_qid_map);
1972
Chad Dupuiscfb09192011-11-18 09:03:07 -08001973 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001974 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001975 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001977 ql_log(ql_log_warn, vha, 0x0044,
1978 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return (rval);
1980 }
1981
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001982 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001984 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001985 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001986 ql_log(ql_log_fatal, vha, 0x004f,
1987 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001988 return rval;
1989 }
1990
1991 if (IS_QLA8044(ha)) {
1992 qla8044_read_reset_template(vha);
1993
1994 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1995 * If DONRESET_BIT0 is set, drivers should not set dev_state
1996 * to NEED_RESET. But if NEED_RESET is set, drivers should
1997 * should honor the reset. */
1998 if (ql2xdontresethba == 1)
1999 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07002000 }
2001
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002002 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08002003 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002004 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002005
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002006 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07002008 if (ha->flags.disable_serdes) {
2009 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002010 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04002011 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07002012 return QLA_FUNCTION_FAILED;
2013 }
2014
Chad Dupuiscfb09192011-11-18 09:03:07 -08002015 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002016 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002018 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2019 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08002020 if (rval)
2021 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002022 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08002023 if (rval)
2024 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002026
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002027 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002028 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002029 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002030 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002031 "Unable to configure ISP84XX.\n");
2032 return QLA_FUNCTION_FAILED;
2033 }
2034 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002035
Quinn Tranead03852017-01-19 22:28:01 -08002036 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002037 rval = qla2x00_init_rings(vha);
2038
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002039 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002041 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07002042 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002043 rval = qla84xx_init_chip(vha);
2044 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002045 ql_log(ql_log_warn, vha, 0x00d4,
2046 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07002047 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002048 }
2049 }
2050
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002051 /* Load the NIC Core f/w if we are the first protocol driver. */
2052 if (IS_QLA8031(ha)) {
2053 rval = qla83xx_nic_core_fw_load(vha);
2054 if (rval)
2055 ql_log(ql_log_warn, vha, 0x0124,
2056 "Error in initializing NIC Core f/w.\n");
2057 }
2058
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002059 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2060 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002061
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002062 if (IS_P3P_TYPE(ha))
2063 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2064 else
2065 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return (rval);
2068}
2069
2070/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002071 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002072 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
2074 * Returns 0 on success.
2075 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002076int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002077qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002079 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002080 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002081 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002082 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002085 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002088 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2090
Andrew Vasquez737faec2008-10-24 15:13:45 -07002091 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002093 /* Get PCI bus information. */
2094 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002095 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002096 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2097
2098 return QLA_SUCCESS;
2099}
2100
2101/**
2102 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002103 * @vha: HA context
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002104 *
2105 * Returns 0 on success.
2106 */
2107int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002108qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002109{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002110 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002111 unsigned long flags = 0;
2112 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002113 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002114 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002115
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002116 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002117 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002118
2119 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002120 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002121
2122 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2123 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002124 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002125
2126 /*
2127 * If this is a 2300 card and not 2312, reset the
2128 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2129 * the 2310 also reports itself as a 2300 so we need to get the
2130 * fb revision level -- a 6 indicates it really is a 2300 and
2131 * not a 2310.
2132 */
2133 if (IS_QLA2300(ha)) {
2134 spin_lock_irqsave(&ha->hardware_lock, flags);
2135
2136 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002137 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002138 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002139 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002140 break;
2141
2142 udelay(10);
2143 }
2144
2145 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002146 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2147 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002148
2149 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002150 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002151
2152 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002153 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002154
2155 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002156 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2157 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002158
2159 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002160 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002161 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002162 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002163 break;
2164
2165 udelay(10);
2166 }
2167
2168 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2169 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002170
2171 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2172
Andrew Vasquez737faec2008-10-24 15:13:45 -07002173 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002174
2175 /* Get PCI bus information. */
2176 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002177 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002178 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2179
2180 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002184 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002185 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002186 *
2187 * Returns 0 on success.
2188 */
2189int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002190qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002191{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002192 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002193 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002194 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002195 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002196
2197 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002198 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002199
2200 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002201 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002202 w &= ~PCI_COMMAND_INTX_DISABLE;
2203 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2204
2205 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2206
2207 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002208 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2209 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002210
2211 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002212 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002213 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002214
Andrew Vasquez737faec2008-10-24 15:13:45 -07002215 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002216
Auke Kok44c10132007-06-08 15:46:36 -07002217 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002218
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002219 /* Get PCI bus information. */
2220 spin_lock_irqsave(&ha->hardware_lock, flags);
2221 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2222 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2223
2224 return QLA_SUCCESS;
2225}
2226
2227/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002228 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002229 * @vha: HA context
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002230 *
2231 * Returns 0 on success.
2232 */
2233int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002234qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002235{
2236 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002237 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002238
2239 pci_set_master(ha->pdev);
2240 pci_try_set_mwi(ha->pdev);
2241
2242 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2243 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2244 w &= ~PCI_COMMAND_INTX_DISABLE;
2245 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2246
2247 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002248 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002249 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002250
Andrew Vasquez737faec2008-10-24 15:13:45 -07002251 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002252
2253 ha->chip_revision = ha->pdev->revision;
2254
2255 return QLA_SUCCESS;
2256}
2257
2258/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 * qla2x00_isp_firmware() - Choose firmware image.
Bart Van Assche2db62282018-01-23 16:33:51 -08002260 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 *
2262 * Returns 0 on success.
2263 */
2264static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002265qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002268 uint16_t loop_id, topo, sw_cap;
2269 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002270 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002273 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002276 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002279 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002280 if (rval == QLA_SUCCESS) {
2281 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002282 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002283 &area, &domain, &topo, &sw_cap);
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002287 if (rval)
2288 ql_dbg(ql_dbg_init, vha, 0x007a,
2289 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 return (rval);
2292}
2293
2294/**
2295 * qla2x00_reset_chip() - Reset ISP chip.
Bart Van Assche2db62282018-01-23 16:33:51 -08002296 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 *
2298 * Returns 0 on success.
2299 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002300void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002301qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002304 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002305 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 uint16_t cmd;
2308
Andrew Vasquez85880802009-12-15 21:29:46 -08002309 if (unlikely(pci_channel_offline(ha->pdev)))
2310 return;
2311
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002312 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 spin_lock_irqsave(&ha->hardware_lock, flags);
2315
2316 /* Turn off master enable */
2317 cmd = 0;
2318 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2319 cmd &= ~PCI_COMMAND_MASTER;
2320 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2321
2322 if (!IS_QLA2100(ha)) {
2323 /* Pause RISC. */
2324 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2325 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2326 for (cnt = 0; cnt < 30000; cnt++) {
2327 if ((RD_REG_WORD(&reg->hccr) &
2328 HCCR_RISC_PAUSE) != 0)
2329 break;
2330 udelay(100);
2331 }
2332 } else {
2333 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2334 udelay(10);
2335 }
2336
2337 /* Select FPM registers. */
2338 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2339 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2340
2341 /* FPM Soft Reset. */
2342 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2343 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2344
2345 /* Toggle Fpm Reset. */
2346 if (!IS_QLA2200(ha)) {
2347 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2348 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2349 }
2350
2351 /* Select frame buffer registers. */
2352 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2353 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2354
2355 /* Reset frame buffer FIFOs. */
2356 if (IS_QLA2200(ha)) {
2357 WRT_FB_CMD_REG(ha, reg, 0xa000);
2358 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2359 } else {
2360 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2361
2362 /* Read back fb_cmd until zero or 3 seconds max */
2363 for (cnt = 0; cnt < 3000; cnt++) {
2364 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2365 break;
2366 udelay(100);
2367 }
2368 }
2369
2370 /* Select RISC module registers. */
2371 WRT_REG_WORD(&reg->ctrl_status, 0);
2372 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2373
2374 /* Reset RISC processor. */
2375 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2376 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2377
2378 /* Release RISC processor. */
2379 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2380 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2381 }
2382
2383 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2384 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2385
2386 /* Reset ISP chip. */
2387 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2388
2389 /* Wait for RISC to recover from reset. */
2390 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2391 /*
2392 * It is necessary to for a delay here since the card doesn't
2393 * respond to PCI reads during a reset. On some architectures
2394 * this will result in an MCA.
2395 */
2396 udelay(20);
2397 for (cnt = 30000; cnt; cnt--) {
2398 if ((RD_REG_WORD(&reg->ctrl_status) &
2399 CSR_ISP_SOFT_RESET) == 0)
2400 break;
2401 udelay(100);
2402 }
2403 } else
2404 udelay(10);
2405
2406 /* Reset RISC processor. */
2407 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2408
2409 WRT_REG_WORD(&reg->semaphore, 0);
2410
2411 /* Release RISC processor. */
2412 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2413 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2414
2415 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2416 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002417 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 udelay(100);
2421 }
2422 } else
2423 udelay(100);
2424
2425 /* Turn on master enable */
2426 cmd |= PCI_COMMAND_MASTER;
2427 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2428
2429 /* Disable RISC pause on FPM parity error. */
2430 if (!IS_QLA2100(ha)) {
2431 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2432 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2433 }
2434
2435 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2436}
2437
2438/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002439 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
Bart Van Assche2db62282018-01-23 16:33:51 -08002440 * @vha: HA context
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002441 *
2442 * Returns 0 on success.
2443 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002444static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002445qla81xx_reset_mpi(scsi_qla_host_t *vha)
2446{
2447 uint16_t mb[4] = {0x1010, 0, 1, 0};
2448
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002449 if (!IS_QLA81XX(vha->hw))
2450 return QLA_SUCCESS;
2451
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002452 return qla81xx_write_mpi_register(vha, mb);
2453}
2454
2455/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002456 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Bart Van Assche2db62282018-01-23 16:33:51 -08002457 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002458 *
2459 * Returns 0 on success.
2460 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002461static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002462qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002463{
2464 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002465 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002466 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002467 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002468 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002469 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002470 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002471
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002472 spin_lock_irqsave(&ha->hardware_lock, flags);
2473
2474 /* Reset RISC. */
2475 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2476 for (cnt = 0; cnt < 30000; cnt++) {
2477 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2478 break;
2479
2480 udelay(10);
2481 }
2482
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002483 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2484 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2485
2486 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2487 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2488 RD_REG_DWORD(&reg->hccr),
2489 RD_REG_DWORD(&reg->ctrl_status),
2490 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2491
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002492 WRT_REG_DWORD(&reg->ctrl_status,
2493 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002494 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002495
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002496 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002497
Andrew Vasquez88c26662005-07-08 17:59:26 -07002498 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002499 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002500 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2501 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002502 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002503 if (cnt)
2504 udelay(5);
2505 else
2506 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002507 }
2508
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002509 if (rval == QLA_SUCCESS)
2510 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2511
2512 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2513 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2514 RD_REG_DWORD(&reg->hccr),
2515 RD_REG_DWORD(&reg->mailbox0));
2516
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002517 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002518 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002519 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002520 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002521 if ((RD_REG_DWORD(&reg->ctrl_status) &
2522 CSRX_ISP_SOFT_RESET) == 0)
2523 break;
2524
2525 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002526 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002527 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2528 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2529
2530 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2531 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2532 RD_REG_DWORD(&reg->hccr),
2533 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002534
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002535 /* If required, do an MPI FW reset now */
2536 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2537 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2538 if (++abts_cnt < 5) {
2539 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2540 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2541 } else {
2542 /*
2543 * We exhausted the ISP abort retries. We have to
2544 * set the board offline.
2545 */
2546 abts_cnt = 0;
2547 vha->flags.online = 0;
2548 }
2549 }
2550 }
2551
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002552 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2553 RD_REG_DWORD(&reg->hccr);
2554
2555 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2556 RD_REG_DWORD(&reg->hccr);
2557
2558 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2559 RD_REG_DWORD(&reg->hccr);
2560
Bart Van Assche52c82822015-07-09 07:23:26 -07002561 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002562 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002563 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002564 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002565 if (cnt)
2566 udelay(5);
2567 else
2568 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002569 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002570 if (rval == QLA_SUCCESS)
2571 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2572
2573 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2574 "Host Risc 0x%x, mailbox0 0x%x\n",
2575 RD_REG_DWORD(&reg->hccr),
2576 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002577
2578 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002579
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002580 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2581 "Driver in %s mode\n",
2582 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2583
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002584 if (IS_NOPOLLING_TYPE(ha))
2585 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002586
2587 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002588}
2589
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002590static void
2591qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2592{
2593 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2594
2595 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2596 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2597
2598}
2599
2600static void
2601qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2602{
2603 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2604
2605 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2606 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2607}
2608
2609static void
2610qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2611{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002612 uint32_t wd32 = 0;
2613 uint delta_msec = 100;
2614 uint elapsed_msec = 0;
2615 uint timeout_msec;
2616 ulong n;
2617
Joe Carnucciocc790762015-08-04 13:37:53 -04002618 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2619 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002620 return;
2621
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002622 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2623 udelay(100);
2624
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002625attempt:
2626 timeout_msec = TIMEOUT_SEMAPHORE;
2627 n = timeout_msec / delta_msec;
2628 while (n--) {
2629 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2630 qla25xx_read_risc_sema_reg(vha, &wd32);
2631 if (wd32 & RISC_SEMAPHORE)
2632 break;
2633 msleep(delta_msec);
2634 elapsed_msec += delta_msec;
2635 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2636 goto force;
2637 }
2638
2639 if (!(wd32 & RISC_SEMAPHORE))
2640 goto force;
2641
2642 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2643 goto acquired;
2644
2645 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2646 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2647 n = timeout_msec / delta_msec;
2648 while (n--) {
2649 qla25xx_read_risc_sema_reg(vha, &wd32);
2650 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2651 break;
2652 msleep(delta_msec);
2653 elapsed_msec += delta_msec;
2654 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2655 goto force;
2656 }
2657
2658 if (wd32 & RISC_SEMAPHORE_FORCE)
2659 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2660
2661 goto attempt;
2662
2663force:
2664 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2665
2666acquired:
2667 return;
2668}
2669
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002670/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002671 * qla24xx_reset_chip() - Reset ISP24xx chip.
Bart Van Assche2db62282018-01-23 16:33:51 -08002672 * @vha: HA context
Andrew Vasquez88c26662005-07-08 17:59:26 -07002673 *
2674 * Returns 0 on success.
2675 */
2676void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002677qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002678{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002679 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002680
2681 if (pci_channel_offline(ha->pdev) &&
2682 ha->flags.pci_channel_io_perm_failure) {
2683 return;
2684 }
2685
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002686 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002687
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002688 qla25xx_manipulate_risc_semaphore(vha);
2689
Andrew Vasquez88c26662005-07-08 17:59:26 -07002690 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002691 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002692}
2693
2694/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 * qla2x00_chip_diag() - Test chip for proper operation.
Bart Van Assche2db62282018-01-23 16:33:51 -08002696 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 *
2698 * Returns 0 on success.
2699 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002700int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002701qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002704 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002705 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 unsigned long flags = 0;
2707 uint16_t data;
2708 uint32_t cnt;
2709 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002710 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 /* Assume a failed state */
2713 rval = QLA_FUNCTION_FAILED;
2714
Bart Van Asscheda4704d2018-01-23 16:33:47 -08002715 ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
2716 &reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 spin_lock_irqsave(&ha->hardware_lock, flags);
2719
2720 /* Reset ISP chip. */
2721 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2722
2723 /*
2724 * We need to have a delay here since the card will not respond while
2725 * in reset causing an MCA on some architectures.
2726 */
2727 udelay(20);
2728 data = qla2x00_debounce_register(&reg->ctrl_status);
2729 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2730 udelay(5);
2731 data = RD_REG_WORD(&reg->ctrl_status);
2732 barrier();
2733 }
2734
2735 if (!cnt)
2736 goto chip_diag_failed;
2737
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002738 ql_dbg(ql_dbg_init, vha, 0x007c,
2739 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 /* Reset RISC processor. */
2742 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2743 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2744
2745 /* Workaround for QLA2312 PCI parity error */
2746 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2747 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2748 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2749 udelay(5);
2750 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002751 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 } else
2754 udelay(10);
2755
2756 if (!cnt)
2757 goto chip_diag_failed;
2758
2759 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002760 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2763 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2764 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2765 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2766 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2767 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002768 ql_log(ql_log_warn, vha, 0x0062,
2769 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2770 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 goto chip_diag_failed;
2773 }
2774 ha->product_id[0] = mb[1];
2775 ha->product_id[1] = mb[2];
2776 ha->product_id[2] = mb[3];
2777 ha->product_id[3] = mb[4];
2778
2779 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002780 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2782 else
2783 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002784 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 if (IS_QLA2200(ha) &&
2787 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2788 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002789 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002791 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 ha->fw_transfer_size = 128;
2793 }
2794
2795 /* Wrap Incoming Mailboxes Test. */
2796 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2797
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002798 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002799 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002800 if (rval)
2801 ql_log(ql_log_warn, vha, 0x0080,
2802 "Failed mailbox send register test.\n");
2803 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 /* Flag a successful rval */
2805 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 spin_lock_irqsave(&ha->hardware_lock, flags);
2807
2808chip_diag_failed:
2809 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002810 ql_log(ql_log_info, vha, 0x0081,
2811 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2814
2815 return (rval);
2816}
2817
2818/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002819 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
Bart Van Assche2db62282018-01-23 16:33:51 -08002820 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002821 *
2822 * Returns 0 on success.
2823 */
2824int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002825qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002826{
2827 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002828 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002829 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002830
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002831 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002832 return QLA_SUCCESS;
2833
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002834 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002835
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002836 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002837 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002838 ql_log(ql_log_warn, vha, 0x0082,
2839 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002840 } else {
2841 /* Flag a successful rval */
2842 rval = QLA_SUCCESS;
2843 }
2844
2845 return rval;
2846}
2847
Quinn Tranad0a0b02017-12-28 12:33:14 -08002848static void
2849qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002850{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002851 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002852 dma_addr_t tc_dma;
2853 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002854 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002855
Quinn Tranad0a0b02017-12-28 12:33:14 -08002856 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002857 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002858 "%s: Offload Mem is already allocated.\n",
2859 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002860 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002861 }
2862
Quinn Tranad0a0b02017-12-28 12:33:14 -08002863 if (IS_FWI2_CAPABLE(ha)) {
2864 /* Allocate memory for Fibre Channel Event Buffer. */
2865 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2866 !IS_QLA27XX(ha))
2867 goto try_eft;
2868
2869 if (ha->fce)
2870 dma_free_coherent(&ha->pdev->dev,
2871 FCE_SIZE, ha->fce, ha->fce_dma);
2872
2873 /* Allocate memory for Fibre Channel Event Buffer. */
2874 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2875 GFP_KERNEL);
2876 if (!tc) {
2877 ql_log(ql_log_warn, vha, 0x00be,
2878 "Unable to allocate (%d KB) for FCE.\n",
2879 FCE_SIZE / 1024);
2880 goto try_eft;
2881 }
2882
2883 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2884 ha->fce_mb, &ha->fce_bufs);
2885 if (rval) {
2886 ql_log(ql_log_warn, vha, 0x00bf,
2887 "Unable to initialize FCE (%d).\n", rval);
2888 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2889 tc_dma);
2890 ha->flags.fce_enabled = 0;
2891 goto try_eft;
2892 }
2893 ql_dbg(ql_dbg_init, vha, 0x00c0,
2894 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2895
2896 ha->flags.fce_enabled = 1;
2897 ha->fce_dma = tc_dma;
2898 ha->fce = tc;
2899
2900try_eft:
2901 if (ha->eft)
2902 dma_free_coherent(&ha->pdev->dev,
2903 EFT_SIZE, ha->eft, ha->eft_dma);
2904
2905 /* Allocate memory for Extended Trace Buffer. */
2906 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2907 GFP_KERNEL);
2908 if (!tc) {
2909 ql_log(ql_log_warn, vha, 0x00c1,
2910 "Unable to allocate (%d KB) for EFT.\n",
2911 EFT_SIZE / 1024);
2912 goto eft_err;
2913 }
2914
2915 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2916 if (rval) {
2917 ql_log(ql_log_warn, vha, 0x00c2,
2918 "Unable to initialize EFT (%d).\n", rval);
2919 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2920 tc_dma);
2921 goto eft_err;
2922 }
2923 ql_dbg(ql_dbg_init, vha, 0x00c3,
2924 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2925
2926 ha->eft_dma = tc_dma;
2927 ha->eft = tc;
2928 }
2929
2930eft_err:
2931 return;
2932}
2933
2934void
2935qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2936{
2937 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2938 eft_size, fce_size, mq_size;
2939 struct qla_hw_data *ha = vha->hw;
2940 struct req_que *req = ha->req_q_map[0];
2941 struct rsp_que *rsp = ha->rsp_q_map[0];
2942 struct qla2xxx_fw_dump *fw_dump;
2943
Chad Dupuisf73cb692014-02-26 04:15:06 -05002944 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2945 req_q_size = rsp_q_size = 0;
2946
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002947 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2948 fixed_size = sizeof(struct qla2100_fw_dump);
2949 } else if (IS_QLA23XX(ha)) {
2950 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2951 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2952 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002953 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002954 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002955 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2956 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002957 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2958 else if (IS_QLA25XX(ha))
2959 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2960 else
2961 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002962
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002963 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2964 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002965 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002966 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002967 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002968 /*
2969 * Allocate maximum buffer size for all queues.
2970 * Resizing must be done at end-of-dump processing.
2971 */
2972 mq_size += ha->max_req_queues *
2973 (req->length * sizeof(request_t));
2974 mq_size += ha->max_rsp_queues *
2975 (rsp->length * sizeof(response_t));
2976 }
Arun Easi00876ae2013-03-25 02:21:37 -04002977 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002978 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002979 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002980 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2981 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002982 goto try_eft;
2983
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002984 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002985try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002986 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002987 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002988 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002989 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002990
Chad Dupuisf73cb692014-02-26 04:15:06 -05002991 if (IS_QLA27XX(ha)) {
2992 if (!ha->fw_dump_template) {
2993 ql_log(ql_log_warn, vha, 0x00ba,
2994 "Failed missing fwdump template\n");
2995 return;
2996 }
2997 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2998 ql_dbg(ql_dbg_init, vha, 0x00fa,
2999 "-> allocating fwdump (%x bytes)...\n", dump_size);
3000 goto allocate;
3001 }
3002
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003003 req_q_size = req->length * sizeof(request_t);
3004 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003005 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003006 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08003007 ha->chain_offset = dump_size;
3008 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003009
Quinn Tranb945e772017-06-13 20:47:29 -07003010 if (ha->exchoffld_buf)
3011 dump_size += sizeof(struct qla2xxx_offld_chain) +
3012 ha->exchoffld_size;
3013 if (ha->exlogin_buf)
3014 dump_size += sizeof(struct qla2xxx_offld_chain) +
3015 ha->exlogin_size;
3016
Chad Dupuisf73cb692014-02-26 04:15:06 -05003017allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08003018 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
3019 fw_dump = vmalloc(dump_size);
3020 if (!fw_dump) {
3021 ql_log(ql_log_warn, vha, 0x00c4,
3022 "Unable to allocate (%d KB) for firmware dump.\n",
3023 dump_size / 1024);
3024 } else {
3025 if (ha->fw_dump)
3026 vfree(ha->fw_dump);
3027 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003028
Quinn Tranad0a0b02017-12-28 12:33:14 -08003029 ha->fw_dump_len = dump_size;
3030 ql_dbg(ql_dbg_init, vha, 0x00c5,
3031 "Allocated (%d KB) for firmware dump.\n",
3032 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07003033
Quinn Tranad0a0b02017-12-28 12:33:14 -08003034 if (IS_QLA27XX(ha))
3035 return;
3036
3037 ha->fw_dump->signature[0] = 'Q';
3038 ha->fw_dump->signature[1] = 'L';
3039 ha->fw_dump->signature[2] = 'G';
3040 ha->fw_dump->signature[3] = 'C';
3041 ha->fw_dump->version = htonl(1);
3042
3043 ha->fw_dump->fixed_size = htonl(fixed_size);
3044 ha->fw_dump->mem_size = htonl(mem_size);
3045 ha->fw_dump->req_q_size = htonl(req_q_size);
3046 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3047
3048 ha->fw_dump->eft_size = htonl(eft_size);
3049 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3050 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3051
3052 ha->fw_dump->header_size =
3053 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003054 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003055 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003056}
3057
Andrew Vasquez18e75552009-06-03 09:55:30 -07003058static int
3059qla81xx_mpi_sync(scsi_qla_host_t *vha)
3060{
3061#define MPS_MASK 0xe0
3062 int rval;
3063 uint16_t dc;
3064 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003065
3066 if (!IS_QLA81XX(vha->hw))
3067 return QLA_SUCCESS;
3068
3069 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3070 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003071 ql_log(ql_log_warn, vha, 0x0105,
3072 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003073 goto done;
3074 }
3075
3076 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3077 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3078 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003079 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003080 goto done_release;
3081 }
3082
3083 dc &= MPS_MASK;
3084 if (dc == (dw & MPS_MASK))
3085 goto done_release;
3086
3087 dw &= ~MPS_MASK;
3088 dw |= dc;
3089 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3090 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003091 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003092 }
3093
3094done_release:
3095 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3096 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003097 ql_log(ql_log_warn, vha, 0x006d,
3098 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003099 }
3100
3101done:
3102 return rval;
3103}
3104
Chad Dupuis8d93f552013-01-30 03:34:37 -05003105int
3106qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3107{
3108 /* Don't try to reallocate the array */
3109 if (req->outstanding_cmds)
3110 return QLA_SUCCESS;
3111
Michael Hernandezd7459522016-12-12 14:40:07 -08003112 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003113 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3114 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003115 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3116 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003117 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003118 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003119 }
3120
3121 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3122 req->num_outstanding_cmds, GFP_KERNEL);
3123
3124 if (!req->outstanding_cmds) {
3125 /*
3126 * Try to allocate a minimal size just so we can get through
3127 * initialization.
3128 */
3129 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3130 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3131 req->num_outstanding_cmds, GFP_KERNEL);
3132
3133 if (!req->outstanding_cmds) {
3134 ql_log(ql_log_fatal, NULL, 0x0126,
3135 "Failed to allocate memory for "
3136 "outstanding_cmds for req_que %p.\n", req);
3137 req->num_outstanding_cmds = 0;
3138 return QLA_FUNCTION_FAILED;
3139 }
3140 }
3141
3142 return QLA_SUCCESS;
3143}
3144
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003145#define PRINT_FIELD(_field, _flag, _str) { \
3146 if (a0->_field & _flag) {\
3147 if (p) {\
3148 strcat(ptr, "|");\
3149 ptr++;\
3150 leftover--;\
3151 } \
3152 len = snprintf(ptr, leftover, "%s", _str); \
3153 p = 1;\
3154 leftover -= len;\
3155 ptr += len; \
3156 } \
3157}
3158
3159static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3160{
3161#define STR_LEN 64
3162 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3163 u8 str[STR_LEN], *ptr, p;
3164 int leftover, len;
3165
3166 memset(str, 0, STR_LEN);
3167 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3168 ql_dbg(ql_dbg_init, vha, 0x015a,
3169 "SFP MFG Name: %s\n", str);
3170
3171 memset(str, 0, STR_LEN);
3172 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3173 ql_dbg(ql_dbg_init, vha, 0x015c,
3174 "SFP Part Name: %s\n", str);
3175
3176 /* media */
3177 memset(str, 0, STR_LEN);
3178 ptr = str;
3179 leftover = STR_LEN;
3180 p = len = 0;
3181 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3182 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3183 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3184 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3185 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3186 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3187 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3188 ql_dbg(ql_dbg_init, vha, 0x0160,
3189 "SFP Media: %s\n", str);
3190
3191 /* link length */
3192 memset(str, 0, STR_LEN);
3193 ptr = str;
3194 leftover = STR_LEN;
3195 p = len = 0;
3196 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3197 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3198 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3199 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3200 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3201 ql_dbg(ql_dbg_init, vha, 0x0196,
3202 "SFP Link Length: %s\n", str);
3203
3204 memset(str, 0, STR_LEN);
3205 ptr = str;
3206 leftover = STR_LEN;
3207 p = len = 0;
3208 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3209 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3210 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3211 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3212 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3213 ql_dbg(ql_dbg_init, vha, 0x016e,
3214 "SFP FC Link Tech: %s\n", str);
3215
3216 if (a0->length_km)
3217 ql_dbg(ql_dbg_init, vha, 0x016f,
3218 "SFP Distant: %d km\n", a0->length_km);
3219 if (a0->length_100m)
3220 ql_dbg(ql_dbg_init, vha, 0x0170,
3221 "SFP Distant: %d m\n", a0->length_100m*100);
3222 if (a0->length_50um_10m)
3223 ql_dbg(ql_dbg_init, vha, 0x0189,
3224 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3225 if (a0->length_62um_10m)
3226 ql_dbg(ql_dbg_init, vha, 0x018a,
3227 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3228 if (a0->length_om4_10m)
3229 ql_dbg(ql_dbg_init, vha, 0x0194,
3230 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3231 if (a0->length_om3_10m)
3232 ql_dbg(ql_dbg_init, vha, 0x0195,
3233 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3234}
3235
3236
3237/*
3238 * Return Code:
3239 * QLA_SUCCESS: no action
3240 * QLA_INTERFACE_ERROR: SFP is not there.
3241 * QLA_FUNCTION_FAILED: detected New SFP
3242 */
3243int
3244qla24xx_detect_sfp(scsi_qla_host_t *vha)
3245{
3246 int rc = QLA_SUCCESS;
3247 struct sff_8247_a0 *a;
3248 struct qla_hw_data *ha = vha->hw;
3249
3250 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3251 goto out;
3252
3253 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3254 if (rc)
3255 goto out;
3256
3257 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3258 qla2xxx_print_sfp_info(vha);
3259
3260 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3261 /* long range */
3262 ha->flags.detected_lr_sfp = 1;
3263
3264 if (a->length_km > 5 || a->length_100m > 50)
3265 ha->long_range_distance = LR_DISTANCE_10K;
3266 else
3267 ha->long_range_distance = LR_DISTANCE_5K;
3268
3269 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3270 ql_dbg(ql_dbg_async, vha, 0x507b,
3271 "Detected Long Range SFP.\n");
3272 } else {
3273 /* short range */
3274 ha->flags.detected_lr_sfp = 0;
3275 if (ha->flags.using_lr_setting)
3276 ql_dbg(ql_dbg_async, vha, 0x5084,
3277 "Detected Short Range SFP.\n");
3278 }
3279
3280 if (!vha->flags.init_done)
3281 rc = QLA_SUCCESS;
3282out:
3283 return rc;
3284}
3285
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003286/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 * qla2x00_setup_chip() - Load and start RISC firmware.
Bart Van Assche2db62282018-01-23 16:33:51 -08003288 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 *
3290 * Returns 0 on success.
3291 */
3292static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003293qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003295 int rval;
3296 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003297 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003298 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3299 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003300 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003301
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003302 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003303 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003304 if (rval == QLA_SUCCESS) {
3305 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003306 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003307 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003308 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003309 }
3310
Andrew Vasquez3db06522008-01-31 12:33:49 -08003311 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3312 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3313 spin_lock_irqsave(&ha->hardware_lock, flags);
3314 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3315 RD_REG_WORD(&reg->hccr);
3316 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Andrew Vasquez18e75552009-06-03 09:55:30 -07003319 qla81xx_mpi_sync(vha);
3320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003322 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003323 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003324 ql_dbg(ql_dbg_init, vha, 0x00c9,
3325 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003327 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 if (rval == QLA_SUCCESS) {
3329 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003330 ql_dbg(ql_dbg_init, vha, 0x00ca,
3331 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003333 if (ql2xexlogins)
3334 ha->flags.exlogins_enabled = 1;
3335
Quinn Tran99e1b682017-06-02 09:12:03 -07003336 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003337 ha->flags.exchoffld_enabled = 1;
3338
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003339 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003341 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003342 qla24xx_detect_sfp(vha);
3343
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003344 rval = qla2x00_set_exlogins_buffer(vha);
3345 if (rval != QLA_SUCCESS)
3346 goto failed;
3347
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003348 rval = qla2x00_set_exchoffld_buffer(vha);
3349 if (rval != QLA_SUCCESS)
3350 goto failed;
3351
Giridhar Malavalia9083012010-04-12 17:59:55 -07003352enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003353 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003354 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003355 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003356 else
3357 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003358 if (rval != QLA_SUCCESS)
3359 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003360 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003361 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003362 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003363 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003364 if ((!ha->max_npiv_vports) ||
3365 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003366 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003367 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003368 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003369 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003370 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003371
Chad Dupuis8d93f552013-01-30 03:34:37 -05003372 /*
3373 * Allocate the array of outstanding commands
3374 * now that we know the firmware resources.
3375 */
3376 rval = qla2x00_alloc_outstanding_cmds(ha,
3377 vha->req);
3378 if (rval != QLA_SUCCESS)
3379 goto failed;
3380
Quinn Tranad0a0b02017-12-28 12:33:14 -08003381 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3382 qla2x00_alloc_offload_mem(vha);
3383
3384 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003385 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003386
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003387 } else {
3388 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003391 ql_log(ql_log_fatal, vha, 0x00cd,
3392 "ISP Firmware failed checksum.\n");
3393 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003395 } else
3396 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Andrew Vasquez3db06522008-01-31 12:33:49 -08003398 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3399 /* Enable proper parity. */
3400 spin_lock_irqsave(&ha->hardware_lock, flags);
3401 if (IS_QLA2300(ha))
3402 /* SRAM parity */
3403 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3404 else
3405 /* SRAM, Instruction RAM and GP RAM parity */
3406 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3407 RD_REG_WORD(&reg->hccr);
3408 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3409 }
3410
Chad Dupuisf3982d82014-09-25 05:16:57 -04003411 if (IS_QLA27XX(ha))
3412 ha->flags.fac_supported = 1;
3413 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003414 uint32_t size;
3415
3416 rval = qla81xx_fac_get_sector_size(vha, &size);
3417 if (rval == QLA_SUCCESS) {
3418 ha->flags.fac_supported = 1;
3419 ha->fdt_block_size = size << 2;
3420 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003421 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003422 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3423 ha->fw_major_version, ha->fw_minor_version,
3424 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003425
Chad Dupuisf73cb692014-02-26 04:15:06 -05003426 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003427 ha->flags.fac_supported = 0;
3428 rval = QLA_SUCCESS;
3429 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003430 }
3431 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003432failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003434 ql_log(ql_log_fatal, vha, 0x00cf,
3435 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
3437
3438 return (rval);
3439}
3440
3441/**
3442 * qla2x00_init_response_q_entries() - Initializes response queue entries.
Bart Van Assche2db62282018-01-23 16:33:51 -08003443 * @rsp: response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 *
3445 * Beginning of request ring has initialization control block already built
3446 * by nvram config routine.
3447 *
3448 * Returns 0 on success.
3449 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003450void
3451qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
3453 uint16_t cnt;
3454 response_t *pkt;
3455
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003456 rsp->ring_ptr = rsp->ring;
3457 rsp->ring_index = 0;
3458 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003459 pkt = rsp->ring_ptr;
3460 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 pkt->signature = RESPONSE_PROCESSED;
3462 pkt++;
3463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
3466/**
3467 * qla2x00_update_fw_options() - Read and process firmware options.
Bart Van Assche2db62282018-01-23 16:33:51 -08003468 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 *
3470 * Returns 0 on success.
3471 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003472void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003473qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474{
3475 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003476 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003479 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3482 return;
3483
3484 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003485 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3486 "Serial link options.\n");
3487 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3488 (uint8_t *)&ha->fw_seriallink_options,
3489 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
3491 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3492 if (ha->fw_seriallink_options[3] & BIT_2) {
3493 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3494
3495 /* 1G settings */
3496 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3497 emphasis = (ha->fw_seriallink_options[2] &
3498 (BIT_4 | BIT_3)) >> 3;
3499 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003500 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 rx_sens = (ha->fw_seriallink_options[0] &
3502 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3503 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3504 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3505 if (rx_sens == 0x0)
3506 rx_sens = 0x3;
3507 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3508 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3509 ha->fw_options[10] |= BIT_5 |
3510 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3511 (tx_sens & (BIT_1 | BIT_0));
3512
3513 /* 2G settings */
3514 swing = (ha->fw_seriallink_options[2] &
3515 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3516 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3517 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003518 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 rx_sens = (ha->fw_seriallink_options[1] &
3520 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3521 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3522 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3523 if (rx_sens == 0x0)
3524 rx_sens = 0x3;
3525 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3526 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3527 ha->fw_options[11] |= BIT_5 |
3528 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3529 (tx_sens & (BIT_1 | BIT_0));
3530 }
3531
3532 /* FCP2 options. */
3533 /* Return command IOCBs without waiting for an ABTS to complete. */
3534 ha->fw_options[3] |= BIT_13;
3535
3536 /* LED scheme. */
3537 if (ha->flags.enable_led_scheme)
3538 ha->fw_options[2] |= BIT_12;
3539
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003540 /* Detect ISP6312. */
3541 if (IS_QLA6312(ha))
3542 ha->fw_options[2] |= BIT_13;
3543
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003544 /* Set Retry FLOGI in case of P2P connection */
3545 if (ha->operating_mode == P2P) {
3546 ha->fw_options[2] |= BIT_3;
3547 ql_dbg(ql_dbg_disc, vha, 0x2100,
3548 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3549 __func__, ha->fw_options[2]);
3550 }
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003553 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554}
3555
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003556void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003557qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003558{
3559 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003560 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003561
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003562 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003563 return;
3564
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003565 /* Hold status IOCBs until ABTS response received. */
3566 if (ql2xfwholdabts)
3567 ha->fw_options[3] |= BIT_12;
3568
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003569 /* Set Retry FLOGI in case of P2P connection */
3570 if (ha->operating_mode == P2P) {
3571 ha->fw_options[2] |= BIT_3;
3572 ql_dbg(ql_dbg_disc, vha, 0x2101,
3573 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3574 __func__, ha->fw_options[2]);
3575 }
3576
Quinn Tran41dc5292017-01-19 22:28:03 -08003577 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003578 if (ql2xmvasynctoatio &&
3579 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003580 if (qla_tgt_mode_enabled(vha) ||
3581 qla_dual_mode_enabled(vha))
3582 ha->fw_options[2] |= BIT_11;
3583 else
3584 ha->fw_options[2] &= ~BIT_11;
3585 }
3586
Quinn Tranf7e761f2017-06-02 09:12:02 -07003587 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3588 /*
3589 * Tell FW to track each exchange to prevent
3590 * driver from using stale exchange.
3591 */
3592 if (qla_tgt_mode_enabled(vha) ||
3593 qla_dual_mode_enabled(vha))
3594 ha->fw_options[2] |= BIT_4;
3595 else
3596 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003597
3598 /* Reserve 1/2 of emergency exchanges for ELS.*/
3599 if (qla2xuseresexchforels)
3600 ha->fw_options[2] |= BIT_8;
3601 else
3602 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003603 }
3604
Quinn Tran83548fe2017-06-02 09:12:01 -07003605 ql_dbg(ql_dbg_init, vha, 0x00e8,
3606 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3607 __func__, ha->fw_options[1], ha->fw_options[2],
3608 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003609
3610 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3611 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003612
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003613 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003614 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003615 return;
3616
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003617 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003618 le16_to_cpu(ha->fw_seriallink_options24[1]),
3619 le16_to_cpu(ha->fw_seriallink_options24[2]),
3620 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003621 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003622 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003623 "Unable to update Serial Link options (%x).\n", rval);
3624 }
3625}
3626
3627void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003628qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003629{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003630 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003631 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003632 struct req_que *req = ha->req_q_map[0];
3633 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003634
3635 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003636 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3637 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003638 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3639 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3640 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3641 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3642 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3643 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003644
3645 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3646 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3647 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3648 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3649 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3650}
3651
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003652void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003653qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003654{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003655 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003656 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003657 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3658 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003659 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003660 uint16_t rid = 0;
3661 struct req_que *req = ha->req_q_map[0];
3662 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003663
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003664 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003665 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003666 icb->request_q_outpointer = cpu_to_le16(0);
3667 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003668 icb->request_q_length = cpu_to_le16(req->length);
3669 icb->response_q_length = cpu_to_le16(rsp->length);
3670 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3671 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3672 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3673 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003674
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003675 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003676 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003677 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3678 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3679 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3680
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003681 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003682 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003683
Chad Dupuisf73cb692014-02-26 04:15:06 -05003684 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003685 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3686 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003687 if (ha->flags.msix_enabled) {
3688 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003689 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003690 "Registering vector 0x%x for base que.\n",
3691 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003692 icb->msix = cpu_to_le16(msix->entry);
3693 }
3694 /* Use alternate PCI bus number */
3695 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003696 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003697 /* Use alternate PCI devfn */
3698 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003699 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003700
Anirban Chakraborty31557542009-12-02 10:36:55 -08003701 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003702 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3703 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003704 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003705 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003706 ql_dbg(ql_dbg_init, vha, 0x00fe,
3707 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003708 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003709 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003710 }
Bart Van Asschead950362015-07-09 07:24:08 -07003711 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003712
3713 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3714 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3715 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3716 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3717 } else {
3718 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3719 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3720 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3721 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3722 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003723 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003724
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003725 /* PCI posting */
3726 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003727}
3728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729/**
3730 * qla2x00_init_rings() - Initializes firmware.
Bart Van Assche2db62282018-01-23 16:33:51 -08003731 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 *
3733 * Beginning of request ring has initialization control block already built
3734 * by nvram config routine.
3735 *
3736 * Returns 0 on success.
3737 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003738int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003739qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
3741 int rval;
3742 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003743 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003744 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003745 struct req_que *req;
3746 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003747 struct mid_init_cb_24xx *mid_init_cb =
3748 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 spin_lock_irqsave(&ha->hardware_lock, flags);
3751
3752 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003753 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003754 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003755 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003756 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003757 req->out_ptr = (void *)(req->ring + req->length);
3758 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003759 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003760 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003762 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003763
3764 /* Initialize firmware. */
3765 req->ring_ptr = req->ring;
3766 req->ring_index = 0;
3767 req->cnt = req->length;
3768 }
3769
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003770 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003771 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003772 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003773 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003774 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3775 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003776 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003777 if (IS_QLAFX00(ha))
3778 qlafx00_init_response_q_entries(rsp);
3779 else
3780 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003783 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3784 ha->tgt.atio_ring_index = 0;
3785 /* Initialize ATIO queue entries */
3786 qlt_init_atio_q_entries(vha);
3787
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003788 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3791
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003792 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3793
3794 if (IS_QLAFX00(ha)) {
3795 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3796 goto next_check;
3797 }
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003800 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003802 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003803 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003804 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003805 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003806 }
3807
Andrew Vasquez24a08132009-03-24 09:08:16 -07003808 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003809 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003810 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003811 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003812 ha->flags.dport_enabled =
3813 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3814 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3815 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3816 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003817 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003818 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003819 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003820 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003821 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003822
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003823 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003824next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003826 ql_log(ql_log_fatal, vha, 0x00d2,
3827 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003829 ql_dbg(ql_dbg_init, vha, 0x00d3,
3830 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003831 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 }
3833
3834 return (rval);
3835}
3836
3837/**
3838 * qla2x00_fw_ready() - Waits for firmware ready.
Bart Van Assche2db62282018-01-23 16:33:51 -08003839 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 *
3841 * Returns 0 on success.
3842 */
3843static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003844qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
3846 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003847 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 uint16_t min_wait; /* Minimum wait time if loop is down */
3849 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003850 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003851 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003853 if (IS_QLAFX00(vha->hw))
3854 return qlafx00_fw_ready(vha);
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 rval = QLA_SUCCESS;
3857
Chad Dupuis334614912015-04-09 14:59:57 -04003858 /* Time to wait for loop down */
3859 if (IS_P3P_TYPE(ha))
3860 min_wait = 30;
3861 else
3862 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 /*
3865 * Firmware should take at most one RATOV to login, plus 5 seconds for
3866 * our own processing.
3867 */
3868 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3869 wait_time = min_wait;
3870 }
3871
3872 /* Min wait time if loop down */
3873 mtime = jiffies + (min_wait * HZ);
3874
3875 /* wait time before firmware ready */
3876 wtime = jiffies + (wait_time * HZ);
3877
3878 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003879 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003880 ql_log(ql_log_info, vha, 0x801e,
3881 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003884 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003885 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003887 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003888 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003890 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003891 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3892 "fw_state=%x 84xx=%x.\n", state[0],
3893 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003894 if ((state[2] & FSTATE_LOGGED_IN) &&
3895 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003896 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3897 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003898
3899 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003900 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003901 if (rval != QLA_SUCCESS) {
3902 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003903 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003904 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003905 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003906 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003907
3908 /* Add time taken to initialize. */
3909 cs84xx_time = jiffies - cs84xx_time;
3910 wtime += cs84xx_time;
3911 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003912 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003913 "Increasing wait time by %ld. "
3914 "New time %ld.\n", cs84xx_time,
3915 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003916 }
3917 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003918 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3919 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003921 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 &ha->login_timeout, &ha->r_a_tov);
3923
3924 rval = QLA_SUCCESS;
3925 break;
3926 }
3927
3928 rval = QLA_FUNCTION_FAILED;
3929
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003930 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003931 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003933 * other than Wait for Login.
3934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003936 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 "Cable is unplugged...\n");
3938
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003939 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 break;
3941 }
3942 }
3943 } else {
3944 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003945 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003946 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 break;
3948 }
3949
3950 if (time_after_eq(jiffies, wtime))
3951 break;
3952
3953 /* Delay for a while */
3954 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 } while (1);
3956
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003957 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003958 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3959 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Chad Dupuiscfb09192011-11-18 09:03:07 -08003961 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003962 ql_log(ql_log_warn, vha, 0x803b,
3963 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 }
3965
3966 return (rval);
3967}
3968
3969/*
3970* qla2x00_configure_hba
3971* Setup adapter context.
3972*
3973* Input:
3974* ha = adapter state pointer.
3975*
3976* Returns:
3977* 0 = success
3978*
3979* Context:
3980* Kernel context.
3981*/
3982static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003983qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
3985 int rval;
3986 uint16_t loop_id;
3987 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003988 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 uint8_t al_pa;
3990 uint8_t area;
3991 uint8_t domain;
3992 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003993 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003994 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003995 port_id_t id;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08003996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003999 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004000 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004002 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004003 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08004004 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004005 ql_dbg(ql_dbg_disc, vha, 0x2008,
4006 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08004007 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004008 ql_log(ql_log_warn, vha, 0x2009,
4009 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05004010 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
4011 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
4012 ql_log(ql_log_warn, vha, 0x1151,
4013 "Doing link init.\n");
4014 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
4015 return rval;
4016 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004017 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08004018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return (rval);
4020 }
4021
4022 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004023 ql_log(ql_log_info, vha, 0x200a,
4024 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 return (QLA_FUNCTION_FAILED);
4026 }
4027
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004028 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 /* initialize */
4031 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4032 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004033 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 switch (topo) {
4036 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004037 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 ha->current_topology = ISP_CFG_NL;
4039 strcpy(connect_type, "(Loop)");
4040 break;
4041
4042 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004043 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004044 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 ha->current_topology = ISP_CFG_FL;
4046 strcpy(connect_type, "(FL_Port)");
4047 break;
4048
4049 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004050 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 ha->operating_mode = P2P;
4052 ha->current_topology = ISP_CFG_N;
4053 strcpy(connect_type, "(N_Port-to-N_Port)");
4054 break;
4055
4056 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004057 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004058 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 ha->operating_mode = P2P;
4060 ha->current_topology = ISP_CFG_F;
4061 strcpy(connect_type, "(F_Port)");
4062 break;
4063
4064 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004065 ql_dbg(ql_dbg_disc, vha, 0x200f,
4066 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 ha->current_topology = ISP_CFG_NL;
4068 strcpy(connect_type, "(Loop)");
4069 break;
4070 }
4071
4072 /* Save Host port and loop ID. */
4073 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004074 id.b.domain = domain;
4075 id.b.area = area;
4076 id.b.al_pa = al_pa;
4077 id.b.rsvd_1 = 0;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004078 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran482c9dc2017-03-15 09:48:54 -07004079 qlt_update_host_map(vha, id);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004080 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004081
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004082 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004083 ql_log(ql_log_info, vha, 0x2010,
4084 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004085 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 return(rval);
4088}
4089
Giridhar Malavalia9083012010-04-12 17:59:55 -07004090inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004091qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4092 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004093{
4094 char *st, *en;
4095 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004096 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004097 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004098 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004099
4100 if (memcmp(model, BINZERO, len) != 0) {
4101 strncpy(ha->model_number, model, len);
4102 st = en = ha->model_number;
4103 en += len - 1;
4104 while (en > st) {
4105 if (*en != 0x20 && *en != 0x00)
4106 break;
4107 *en-- = '\0';
4108 }
4109
4110 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004111 if (use_tbl &&
4112 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004113 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004114 strncpy(ha->model_desc,
4115 qla2x00_model_name[index * 2 + 1],
4116 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004117 } else {
4118 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004119 if (use_tbl &&
4120 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004121 index < QLA_MODEL_NAMES) {
4122 strcpy(ha->model_number,
4123 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004124 strncpy(ha->model_desc,
4125 qla2x00_model_name[index * 2 + 1],
4126 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004127 } else {
4128 strcpy(ha->model_number, def);
4129 }
4130 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004131 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004132 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004133 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004134}
4135
David Miller4e08df32007-04-16 12:37:43 -07004136/* On sparc systems, obtain port and node WWN from firmware
4137 * properties.
4138 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004139static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004140{
4141#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004142 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004143 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004144 struct device_node *dp = pci_device_to_OF_node(pdev);
4145 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004146 int len;
4147
4148 val = of_get_property(dp, "port-wwn", &len);
4149 if (val && len >= WWN_SIZE)
4150 memcpy(nv->port_name, val, WWN_SIZE);
4151
4152 val = of_get_property(dp, "node-wwn", &len);
4153 if (val && len >= WWN_SIZE)
4154 memcpy(nv->node_name, val, WWN_SIZE);
4155#endif
4156}
4157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158/*
4159* NVRAM configuration for ISP 2xxx
4160*
4161* Input:
4162* ha = adapter block pointer.
4163*
4164* Output:
4165* initialization control block in response_ring
4166* host adapters parameters in host adapter block
4167*
4168* Returns:
4169* 0 = success.
4170*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004171int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004172qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
David Miller4e08df32007-04-16 12:37:43 -07004174 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004175 uint8_t chksum = 0;
4176 uint16_t cnt;
4177 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004178 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004179 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004180 nvram_t *nv = ha->nvram;
4181 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004182 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
David Miller4e08df32007-04-16 12:37:43 -07004184 rval = QLA_SUCCESS;
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004187 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 ha->nvram_base = 0;
4189 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4190 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4191 ha->nvram_base = 0x80;
4192
4193 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004194 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004195 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4196 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004198 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4199 "Contents of NVRAM.\n");
4200 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4201 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 /* Bad NVRAM data, set defaults parameters. */
4204 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4205 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4206 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004207 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004208 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004209 "detected: checksum=0x%x id=%c version=0x%x.\n",
4210 chksum, nv->id[0], nv->nvram_version);
4211 ql_log(ql_log_warn, vha, 0x0065,
4212 "Falling back to "
4213 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004214
4215 /*
4216 * Set default initialization control block.
4217 */
4218 memset(nv, 0, ha->nvram_size);
4219 nv->parameter_block_version = ICB_VERSION;
4220
4221 if (IS_QLA23XX(ha)) {
4222 nv->firmware_options[0] = BIT_2 | BIT_1;
4223 nv->firmware_options[1] = BIT_7 | BIT_5;
4224 nv->add_firmware_options[0] = BIT_5;
4225 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004226 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004227 nv->special_options[1] = BIT_7;
4228 } else if (IS_QLA2200(ha)) {
4229 nv->firmware_options[0] = BIT_2 | BIT_1;
4230 nv->firmware_options[1] = BIT_7 | BIT_5;
4231 nv->add_firmware_options[0] = BIT_5;
4232 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004233 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004234 } else if (IS_QLA2100(ha)) {
4235 nv->firmware_options[0] = BIT_3 | BIT_1;
4236 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004237 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004238 }
4239
Bart Van Asschead950362015-07-09 07:24:08 -07004240 nv->max_iocb_allocation = cpu_to_le16(256);
4241 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004242 nv->retry_count = 8;
4243 nv->retry_delay = 1;
4244
4245 nv->port_name[0] = 33;
4246 nv->port_name[3] = 224;
4247 nv->port_name[4] = 139;
4248
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004249 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004250
4251 nv->login_timeout = 4;
4252
4253 /*
4254 * Set default host adapter parameters
4255 */
4256 nv->host_p[1] = BIT_2;
4257 nv->reset_delay = 5;
4258 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004259 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004260 nv->link_down_timeout = 60;
4261
4262 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264
4265#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4266 /*
4267 * The SN2 does not provide BIOS emulation which means you can't change
4268 * potentially bogus BIOS settings. Force the use of default settings
4269 * for link rate and frame size. Hope that the rest of the settings
4270 * are valid.
4271 */
4272 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004273 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 if (IS_QLA23XX(ha))
4275 nv->special_options[1] = BIT_7;
4276 }
4277#endif
4278
4279 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004280 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 /*
4283 * Setup driver NVRAM options.
4284 */
4285 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4286 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4287 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4288 nv->firmware_options[1] &= ~BIT_4;
4289
4290 if (IS_QLA23XX(ha)) {
4291 nv->firmware_options[0] |= BIT_2;
4292 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004293 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004294 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296 if (IS_QLA2300(ha)) {
4297 if (ha->fb_rev == FPM_2310) {
4298 strcpy(ha->model_number, "QLA2310");
4299 } else {
4300 strcpy(ha->model_number, "QLA2300");
4301 }
4302 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004303 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004304 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 }
4306 } else if (IS_QLA2200(ha)) {
4307 nv->firmware_options[0] |= BIT_2;
4308 /*
4309 * 'Point-to-point preferred, else loop' is not a safe
4310 * connection mode setting.
4311 */
4312 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4313 (BIT_5 | BIT_4)) {
4314 /* Force 'loop preferred, else point-to-point'. */
4315 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4316 nv->add_firmware_options[0] |= BIT_5;
4317 }
4318 strcpy(ha->model_number, "QLA22xx");
4319 } else /*if (IS_QLA2100(ha))*/ {
4320 strcpy(ha->model_number, "QLA2100");
4321 }
4322
4323 /*
4324 * Copy over NVRAM RISC parameter block to initialization control block.
4325 */
4326 dptr1 = (uint8_t *)icb;
4327 dptr2 = (uint8_t *)&nv->parameter_block_version;
4328 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4329 while (cnt--)
4330 *dptr1++ = *dptr2++;
4331
4332 /* Copy 2nd half. */
4333 dptr1 = (uint8_t *)icb->add_firmware_options;
4334 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4335 while (cnt--)
4336 *dptr1++ = *dptr2++;
4337
Andrew Vasquez5341e862006-05-17 15:09:16 -07004338 /* Use alternate WWN? */
4339 if (nv->host_p[1] & BIT_7) {
4340 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4341 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4342 }
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 /* Prepare nodename */
4345 if ((icb->firmware_options[1] & BIT_6) == 0) {
4346 /*
4347 * Firmware will apply the following mask if the nodename was
4348 * not provided.
4349 */
4350 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4351 icb->node_name[0] &= 0xF0;
4352 }
4353
4354 /*
4355 * Set host adapter parameters.
4356 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004357
4358 /*
4359 * BIT_7 in the host-parameters section allows for modification to
4360 * internal driver logging.
4361 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004362 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004363 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4365 /* Always load RISC code on non ISP2[12]00 chips. */
4366 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4367 ha->flags.disable_risc_code_load = 0;
4368 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4369 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4370 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004371 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004372 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 ha->operating_mode =
4375 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4376
4377 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4378 sizeof(ha->fw_seriallink_options));
4379
4380 /* save HBA serial number */
4381 ha->serial0 = icb->port_name[5];
4382 ha->serial1 = icb->port_name[6];
4383 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004384 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4385 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Bart Van Asschead950362015-07-09 07:24:08 -07004387 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 ha->retry_count = nv->retry_count;
4390
4391 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004392 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 nv->login_timeout = ql2xlogintimeout;
4394 if (nv->login_timeout < 4)
4395 nv->login_timeout = 4;
4396 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004398 /* Set minimum RATOV to 100 tenths of a second. */
4399 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 ha->loop_reset_delay = nv->reset_delay;
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 /* Link Down Timeout = 0:
4404 *
4405 * When Port Down timer expires we will start returning
4406 * I/O's to OS with "DID_NO_CONNECT".
4407 *
4408 * Link Down Timeout != 0:
4409 *
4410 * The driver waits for the link to come up after link down
4411 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 if (nv->link_down_timeout == 0) {
4414 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004415 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 } else {
4417 ha->link_down_timeout = nv->link_down_timeout;
4418 ha->loop_down_abort_time =
4419 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 /*
4423 * Need enough time to try and get the port back.
4424 */
4425 ha->port_down_retry_count = nv->port_down_retry_count;
4426 if (qlport_down_retry)
4427 ha->port_down_retry_count = qlport_down_retry;
4428 /* Set login_retry_count */
4429 ha->login_retry_count = nv->retry_count;
4430 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4431 ha->port_down_retry_count > 3)
4432 ha->login_retry_count = ha->port_down_retry_count;
4433 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4434 ha->login_retry_count = ha->port_down_retry_count;
4435 if (ql2xloginretrycount)
4436 ha->login_retry_count = ql2xloginretrycount;
4437
Bart Van Asschead950362015-07-09 07:24:08 -07004438 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 icb->command_resource_count = 0;
4440 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004441 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4444 /* Enable RIO */
4445 icb->firmware_options[0] &= ~BIT_3;
4446 icb->add_firmware_options[0] &=
4447 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4448 icb->add_firmware_options[0] |= BIT_2;
4449 icb->response_accumulation_timer = 3;
4450 icb->interrupt_delay_timer = 5;
4451
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004452 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004454 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004455 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004456 ha->zio_mode = icb->add_firmware_options[0] &
4457 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4458 ha->zio_timer = icb->interrupt_delay_timer ?
4459 icb->interrupt_delay_timer: 2;
4460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 icb->add_firmware_options[0] &=
4462 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004463 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004464 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004465 ha->zio_mode = QLA_ZIO_MODE_6;
4466
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004467 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004468 "ZIO mode %d enabled; timer delay (%d us).\n",
4469 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004471 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4472 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004473 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 }
4475 }
4476
David Miller4e08df32007-04-16 12:37:43 -07004477 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004478 ql_log(ql_log_warn, vha, 0x0069,
4479 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004480 }
4481 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482}
4483
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004484static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004485qla2x00_rport_del(void *data)
4486{
4487 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004488 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004489 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004490
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004491 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004492 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004493 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004494 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004495 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004496 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4497 "%s %8phN. rport %p roles %x\n",
4498 __func__, fcport->port_name, rport,
4499 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004500
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004501 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004502 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004503}
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505/**
4506 * qla2x00_alloc_fcport() - Allocate a generic fcport.
Bart Van Assche2db62282018-01-23 16:33:51 -08004507 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 * @flags: allocation flags
4509 *
4510 * Returns a pointer to the allocated fcport, or NULL, if none available.
4511 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004512fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004513qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
4515 fc_port_t *fcport;
4516
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004517 fcport = kzalloc(sizeof(fc_port_t), flags);
4518 if (!fcport)
4519 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004522 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 fcport->port_type = FCT_UNKNOWN;
4524 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004525 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004526 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Quinn Tran726b8542017-01-19 22:28:00 -08004528 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4529 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004530 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004531 fcport->disc_state = DSC_DELETED;
4532 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4533 fcport->deleted = QLA_SESS_DELETED;
4534 fcport->login_retry = vha->hw->login_retry_count;
4535 fcport->login_retry = 5;
4536 fcport->logout_on_delete = 1;
4537
4538 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004539 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004540 "Failed to allocate ct_sns request.\n");
4541 kfree(fcport);
4542 fcport = NULL;
4543 }
4544 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4545 INIT_LIST_HEAD(&fcport->gnl_entry);
4546 INIT_LIST_HEAD(&fcport->list);
4547
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004548 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549}
4550
Quinn Tran726b8542017-01-19 22:28:00 -08004551void
4552qla2x00_free_fcport(fc_port_t *fcport)
4553{
4554 if (fcport->ct_desc.ct_sns) {
4555 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4556 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4557 fcport->ct_desc.ct_sns_dma);
4558
4559 fcport->ct_desc.ct_sns = NULL;
4560 }
4561 kfree(fcport);
4562}
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564/*
4565 * qla2x00_configure_loop
4566 * Updates Fibre Channel Device Database with what is actually on loop.
4567 *
4568 * Input:
4569 * ha = adapter block pointer.
4570 *
4571 * Returns:
4572 * 0 = success.
4573 * 1 = error.
4574 * 2 = database was full and device was not configured.
4575 */
4576static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004577qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
4579 int rval;
4580 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004581 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 rval = QLA_SUCCESS;
4583
4584 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004585 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4586 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004588 ql_dbg(ql_dbg_disc, vha, 0x2013,
4589 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return (rval);
4591 }
4592 }
4593
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004594 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004595 ql_dbg(ql_dbg_disc, vha, 0x2014,
4596 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
4598 /*
4599 * If we have both an RSCN and PORT UPDATE pending then handle them
4600 * both at the same time.
4601 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004602 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4603 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Michael Hernandez3064ff32009-12-15 21:29:44 -08004605 qla2x00_get_data_rate(vha);
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 /* Determine what we need to do */
4608 if (ha->current_topology == ISP_CFG_FL &&
4609 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 set_bit(RSCN_UPDATE, &flags);
4612
4613 } else if (ha->current_topology == ISP_CFG_F &&
4614 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 set_bit(RSCN_UPDATE, &flags);
4617 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4618
Andrew Vasquez21333b42006-05-17 15:09:56 -07004619 } else if (ha->current_topology == ISP_CFG_N) {
4620 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004621 if (ha->flags.rida_fmt2) {
4622 /* With Rida Format 2, the login is already triggered.
4623 * We know who is on the other side of the wire.
4624 * No need to login to do login to find out or drop into
4625 * qla2x00_configure_local_loop().
4626 */
4627 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4628 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4629 } else {
4630 if (qla_tgt_mode_enabled(vha)) {
4631 /* allow the other side to start the login */
4632 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4633 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4634 }
4635 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004636 } else if (ha->current_topology == ISP_CFG_NL) {
4637 clear_bit(RSCN_UPDATE, &flags);
4638 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004639 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 set_bit(RSCN_UPDATE, &flags);
4642 set_bit(LOCAL_LOOP_UPDATE, &flags);
4643 }
4644
4645 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004646 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4647 ql_dbg(ql_dbg_disc, vha, 0x2015,
4648 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004650 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004651 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
4653
4654 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004655 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004656 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004657 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004659 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004660 else
4661 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
4663
4664 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004665 if (atomic_read(&vha->loop_down_timer) ||
4666 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 rval = QLA_FUNCTION_FAILED;
4668 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004669 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004670 ql_dbg(ql_dbg_disc, vha, 0x2069,
4671 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004672 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004673
4674 /*
4675 * Process any ATIO queue entries that came in
4676 * while we weren't online.
4677 */
Quinn Tranead03852017-01-19 22:28:01 -08004678 if (qla_tgt_mode_enabled(vha) ||
4679 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004680 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4681 spin_lock_irqsave(&ha->tgt.atio_lock,
4682 flags);
4683 qlt_24xx_process_atio_queue(vha, 0);
4684 spin_unlock_irqrestore(
4685 &ha->tgt.atio_lock, flags);
4686 } else {
4687 spin_lock_irqsave(&ha->hardware_lock,
4688 flags);
4689 qlt_24xx_process_atio_queue(vha, 1);
4690 spin_unlock_irqrestore(
4691 &ha->hardware_lock, flags);
4692 }
4693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 }
4695 }
4696
4697 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004698 ql_dbg(ql_dbg_disc, vha, 0x206a,
4699 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004701 ql_dbg(ql_dbg_disc, vha, 0x206b,
4702 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
4704
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004705 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004706 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004708 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004709 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004710 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
4713
4714 return (rval);
4715}
4716
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004717/*
4718 * N2N Login
4719 * Updates Fibre Channel Device Database with local loop devices.
4720 *
4721 * Input:
4722 * ha = adapter block pointer.
4723 *
4724 * Returns:
4725 */
4726static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4727 fc_port_t *fcport)
4728{
4729 struct qla_hw_data *ha = vha->hw;
4730 int res = QLA_SUCCESS, rval;
4731 int greater_wwpn = 0;
4732 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004734 if (ha->current_topology != ISP_CFG_N)
4735 return res;
4736
4737 if (wwn_to_u64(vha->port_name) >
4738 wwn_to_u64(vha->n2n_port_name)) {
4739 ql_dbg(ql_dbg_disc, vha, 0x2002,
4740 "HBA WWPN is greater %llx > target %llx\n",
4741 wwn_to_u64(vha->port_name),
4742 wwn_to_u64(vha->n2n_port_name));
4743 greater_wwpn = 1;
4744 fcport->d_id.b24 = vha->n2n_id;
4745 }
4746
4747 fcport->loop_id = vha->loop_id;
4748 fcport->fc4f_nvme = 0;
4749 fcport->query = 1;
4750
4751 ql_dbg(ql_dbg_disc, vha, 0x4001,
4752 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4753 fcport->d_id.b24, vha->loop_id);
4754
4755 /* Fill in member data. */
4756 if (!greater_wwpn) {
4757 rval = qla2x00_get_port_database(vha, fcport, 0);
4758 ql_dbg(ql_dbg_disc, vha, 0x1051,
4759 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4760 fcport->current_login_state, fcport->last_login_state,
4761 fcport->d_id.b24, fcport->loop_id, rval);
4762
4763 if (((fcport->current_login_state & 0xf) == 0x4) ||
4764 ((fcport->current_login_state & 0xf) == 0x6))
4765 logged_in = 1;
4766 }
4767
4768 if (logged_in || greater_wwpn) {
4769 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4770 qla_nvme_register_hba(vha);
4771
4772 /* Set connected N_Port d_id */
4773 if (vha->flags.nvme_enabled)
4774 fcport->fc4f_nvme = 1;
4775
4776 fcport->scan_state = QLA_FCPORT_FOUND;
4777 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4778 fcport->disc_state = DSC_GNL;
4779 fcport->n2n_flag = 1;
4780 fcport->flags = 3;
4781 vha->hw->flags.gpsc_supported = 0;
4782
4783 if (greater_wwpn) {
4784 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4785 "%s %d PLOGI ELS %8phC\n",
4786 __func__, __LINE__, fcport->port_name);
4787
4788 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4789 fcport, fcport->d_id);
4790 }
4791
4792 if (res != QLA_SUCCESS) {
4793 ql_log(ql_log_info, vha, 0xd04d,
4794 "PLOGI Failed: portid=%06x - retrying\n",
4795 fcport->d_id.b24);
4796 res = QLA_SUCCESS;
4797 } else {
4798 /* State 0x6 means FCP PRLI complete */
4799 if ((fcport->current_login_state & 0xf) == 0x6) {
4800 ql_dbg(ql_dbg_disc, vha, 0x2118,
4801 "%s %d %8phC post GPDB work\n",
4802 __func__, __LINE__, fcport->port_name);
4803 fcport->chip_reset =
4804 vha->hw->base_qpair->chip_reset;
4805 qla24xx_post_gpdb_work(vha, fcport, 0);
4806 } else {
4807 ql_dbg(ql_dbg_disc, vha, 0x2118,
4808 "%s %d %8phC post NVMe PRLI\n",
4809 __func__, __LINE__, fcport->port_name);
4810 qla24xx_post_prli_work(vha, fcport);
4811 }
4812 }
4813 } else {
4814 /* Wait for next database change */
4815 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4816 }
4817
4818 return res;
4819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
4821/*
4822 * qla2x00_configure_local_loop
4823 * Updates Fibre Channel Device Database with local loop devices.
4824 *
4825 * Input:
4826 * ha = adapter block pointer.
4827 *
4828 * Returns:
4829 * 0 = success.
4830 */
4831static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004832qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
4834 int rval, rval2;
4835 int found_devs;
4836 int found;
4837 fc_port_t *fcport, *new_fcport;
4838
4839 uint16_t index;
4840 uint16_t entries;
4841 char *id_iter;
4842 uint16_t loop_id;
4843 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004844 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004845 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847 found_devs = 0;
4848 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004849 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004852 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004853 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 &entries);
4855 if (rval != QLA_SUCCESS)
4856 goto cleanup_allocation;
4857
Quinn Tran83548fe2017-06-02 09:12:01 -07004858 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004859 "Entries in ID list (%d).\n", entries);
4860 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4861 (uint8_t *)ha->gid_list,
4862 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Quinn Tran9cd883f2017-12-28 12:33:24 -08004864 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4865 fcport->scan_state = QLA_FCPORT_SCAN;
4866 }
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004869 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004871 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004872 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 rval = QLA_MEMORY_ALLOC_FAILED;
4874 goto cleanup_allocation;
4875 }
4876 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4877
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004878 /* Inititae N2N login. */
4879 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4880 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4881 if (rval != QLA_SUCCESS)
4882 goto cleanup_allocation;
4883 return QLA_SUCCESS;
4884 }
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 /* Add devices to port list. */
4887 id_iter = (char *)ha->gid_list;
4888 for (index = 0; index < entries; index++) {
4889 domain = ((struct gid_list_info *)id_iter)->domain;
4890 area = ((struct gid_list_info *)id_iter)->area;
4891 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004892 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 loop_id = (uint16_t)
4894 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004895 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 loop_id = le16_to_cpu(
4897 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004898 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
4900 /* Bypass reserved domain fields. */
4901 if ((domain & 0xf0) == 0xf0)
4902 continue;
4903
4904 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004905 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004906 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 continue;
4908
4909 /* Bypass invalid local loop ID. */
4910 if (loop_id > LAST_LOCAL_LOOP_ID)
4911 continue;
4912
Quinn Tran41dc5292017-01-19 22:28:03 -08004913 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 /* Fill in member data. */
4916 new_fcport->d_id.b.domain = domain;
4917 new_fcport->d_id.b.area = area;
4918 new_fcport->d_id.b.al_pa = al_pa;
4919 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004920 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004921
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004922 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004924 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004925 "Failed to retrieve fcport information "
4926 "-- get_port_database=%x, loop_id=0x%04x.\n",
4927 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004928 /* Skip retry if N2N */
4929 if (ha->current_topology != ISP_CFG_N) {
4930 ql_dbg(ql_dbg_disc, vha, 0x2105,
4931 "Scheduling resync.\n");
4932 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4933 continue;
4934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 }
4936
Quinn Tran41dc5292017-01-19 22:28:03 -08004937 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 /* Check for matching device in port list. */
4939 found = 0;
4940 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004941 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (memcmp(new_fcport->port_name, fcport->port_name,
4943 WWN_SIZE))
4944 continue;
4945
Shyam Sundarddb9b122009-03-24 09:08:10 -07004946 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 fcport->loop_id = new_fcport->loop_id;
4948 fcport->port_type = new_fcport->port_type;
4949 fcport->d_id.b24 = new_fcport->d_id.b24;
4950 memcpy(fcport->node_name, new_fcport->node_name,
4951 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004952 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 found++;
4954 break;
4955 }
4956
4957 if (!found) {
4958 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004959 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 /* Allocate a new replacement fcport. */
4962 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004963
4964 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4965
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004966 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004969 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004970 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 rval = QLA_MEMORY_ALLOC_FAILED;
4972 goto cleanup_allocation;
4973 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004974 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4976 }
4977
Quinn Tran41dc5292017-01-19 22:28:03 -08004978 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4979
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004980 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004981 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 found_devs++;
4984 }
4985
Quinn Tran9cd883f2017-12-28 12:33:24 -08004986 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4987 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4988 break;
4989
4990 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4991 if ((qla_dual_mode_enabled(vha) ||
4992 qla_ini_mode_enabled(vha)) &&
4993 atomic_read(&fcport->state) == FCS_ONLINE) {
4994 qla2x00_mark_device_lost(vha, fcport,
4995 ql2xplogiabsentdevice, 0);
4996 if (fcport->loop_id != FC_NO_LOOP_ID &&
4997 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4998 fcport->port_type != FCT_INITIATOR &&
4999 fcport->port_type != FCT_BROADCAST) {
5000 ql_dbg(ql_dbg_disc, vha, 0x20f0,
5001 "%s %d %8phC post del sess\n",
5002 __func__, __LINE__,
5003 fcport->port_name);
5004
Quinn Trand8630bb2017-12-28 12:33:43 -08005005 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran9cd883f2017-12-28 12:33:24 -08005006 continue;
5007 }
5008 }
5009 }
5010
5011 if (fcport->scan_state == QLA_FCPORT_FOUND)
5012 qla24xx_fcport_handle_login(vha, fcport);
5013 }
5014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08005016 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005019 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005020 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return (rval);
5024}
5025
5026static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005027qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005028{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005029 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04005030 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005031 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005032
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07005033 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005034 return;
5035
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07005036 if (atomic_read(&fcport->state) != FCS_ONLINE)
5037 return;
5038
Andrew Vasquez39bd9622007-09-20 14:07:34 -07005039 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5040 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005041 return;
5042
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005043 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07005044 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005045 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005046 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005047 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5048 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005049 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005050 ql_dbg(ql_dbg_disc, vha, 0x2005,
Quinn Tran33b28352018-03-20 23:09:40 -07005051 "iIDMA adjusted to %s GB/s (%X) on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05005052 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Quinn Tran33b28352018-03-20 23:09:40 -07005053 fcport->fp_speed, fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005054 }
5055}
5056
Quinn Tran726b8542017-01-19 22:28:00 -08005057/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005058static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005059qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005060{
5061 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005062 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005063 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005064
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005065 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5066 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005067 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5068 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5069 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005070 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005071 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005072 ql_log(ql_log_warn, vha, 0x2006,
5073 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005074 return;
5075 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005076
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005077 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005078 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005079 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005080
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005081 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005082
5083 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005084 if (fcport->port_type == FCT_INITIATOR)
5085 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5086 if (fcport->port_type == FCT_TARGET)
5087 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005088
Quinn Tran83548fe2017-06-02 09:12:01 -07005089 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5090 "%s %8phN. rport %p is %s mode\n",
5091 __func__, fcport->port_name, rport,
5092 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005093
Andrew Vasquez77d74142005-07-08 18:00:36 -07005094 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
5097/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005098 * qla2x00_update_fcport
5099 * Updates device on list.
5100 *
5101 * Input:
5102 * ha = adapter block pointer.
5103 * fcport = port structure pointer.
5104 *
5105 * Return:
5106 * 0 - Success
5107 * BIT_0 - error
5108 *
5109 * Context:
5110 * Kernel context.
5111 */
5112void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005113qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005114{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005115 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005116
Quinn Tran726b8542017-01-19 22:28:00 -08005117 if (IS_SW_RESV_ADDR(fcport->d_id))
5118 return;
5119
Quinn Tran83548fe2017-06-02 09:12:01 -07005120 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005121 __func__, fcport->port_name);
5122
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005123 if (IS_QLAFX00(vha->hw)) {
5124 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005125 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005126 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005127 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005128 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005129 fcport->disc_state = DSC_LOGIN_COMPLETE;
5130 fcport->deleted = 0;
5131 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005132
Darren Trappdbe18012018-03-20 23:09:31 -07005133 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5134 qla2x00_iidma_fcport(vha, fcport);
5135
Duane Grigsbye84067d2017-06-21 13:48:43 -07005136 if (fcport->fc4f_nvme) {
5137 qla_nvme_register_remote(vha, fcport);
5138 return;
5139 }
5140
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005141 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005142
5143reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005144 switch (vha->host->active_mode) {
5145 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005146 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005147 break;
5148 case MODE_TARGET:
5149 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5150 !vha->vha_tgt.qla_tgt->tgt_stopped)
5151 qlt_fc_port_added(vha, fcport);
5152 break;
5153 case MODE_DUAL:
5154 qla2x00_reg_remote_port(vha, fcport);
5155 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5156 !vha->vha_tgt.qla_tgt->tgt_stopped)
5157 qlt_fc_port_added(vha, fcport);
5158 break;
5159 default:
5160 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005161 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005162}
5163
5164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 * qla2x00_configure_fabric
5166 * Setup SNS devices with loop ID's.
5167 *
5168 * Input:
5169 * ha = adapter block pointer.
5170 *
5171 * Returns:
5172 * 0 = success.
5173 * BIT_0 = error
5174 */
5175static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005176qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177{
Arun Easib3b02e62012-02-09 11:15:39 -08005178 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005179 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005181 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005183 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005184 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
5186 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005187 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005188 loop_id = NPH_F_PORT;
5189 else
5190 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005191 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005193 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005194 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005196 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 return (QLA_SUCCESS);
5198 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005199 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
Quinn Tran41dc5292017-01-19 22:28:03 -08005201
5202 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5203 rval = qla2x00_send_change_request(vha, 0x3, 0);
5204 if (rval != QLA_SUCCESS)
5205 ql_log(ql_log_warn, vha, 0x121,
5206 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5207 rval);
5208 }
5209
5210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005212 qla2x00_mgmt_svr_login(vha);
5213
Andrew Vasquezcca53352005-08-26 19:08:30 -07005214 /* FDMI support. */
5215 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005216 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5217 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005220 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005221 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5222 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005223 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5224 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5225 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5226 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005227 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005228 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005229 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005230 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5231 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005233 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005234 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005235 if (test_bit(LOOP_RESYNC_NEEDED,
5236 &vha->dpc_flags))
5237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005239 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005241 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005242 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005243 if (test_bit(LOOP_RESYNC_NEEDED,
5244 &vha->dpc_flags))
5245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005247 if (vha->flags.nvme_enabled) {
5248 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5249 ql_dbg(ql_dbg_disc, vha, 0x2049,
5250 "Register NVME FC Type Features failed.\n");
5251 }
5252 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005253 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005255 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005256 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005257 if (test_bit(LOOP_RESYNC_NEEDED,
5258 &vha->dpc_flags))
5259 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005260 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005262 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005263 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005264 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 }
5267 }
5268
Joe Carnuccio827210b2013-02-08 01:57:57 -05005269
Alexei Potashnikdf673272015-07-14 16:00:46 -04005270 /* Mark the time right before querying FW for connected ports.
5271 * This process is long, asynchronous and by the time it's done,
5272 * collected information might not be accurate anymore. E.g.
5273 * disconnected port might have re-connected and a brand new
5274 * session has been created. In this case session's generation
5275 * will be newer than discovery_gen. */
5276 qlt_do_generation_tick(vha, &discovery_gen);
5277
Quinn Trana4239942017-12-28 12:33:26 -08005278 if (USE_ASYNC_SCAN(ha)) {
Quinn Tran33b28352018-03-20 23:09:40 -07005279 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI,
5280 NULL);
Quinn Trana4239942017-12-28 12:33:26 -08005281 if (rval)
5282 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5283 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005284 list_for_each_entry(fcport, &vha->vp_fcports, list)
5285 fcport->scan_state = QLA_FCPORT_SCAN;
5286
Quinn Trana4239942017-12-28 12:33:26 -08005287 rval = qla2x00_find_all_fabric_devs(vha);
5288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 if (rval != QLA_SUCCESS)
5290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 } while (0);
5292
Duane Grigsbye84067d2017-06-21 13:48:43 -07005293 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5294 qla_nvme_register_hba(vha);
5295
Quinn Tran726b8542017-01-19 22:28:00 -08005296 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005297 ql_dbg(ql_dbg_disc, vha, 0x2068,
5298 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 return (rval);
5301}
5302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303/*
5304 * qla2x00_find_all_fabric_devs
5305 *
5306 * Input:
5307 * ha = adapter block pointer.
5308 * dev = database device entry pointer.
5309 *
5310 * Returns:
5311 * 0 = success.
5312 *
5313 * Context:
5314 * Kernel context.
5315 */
5316static int
Quinn Tran726b8542017-01-19 22:28:00 -08005317qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318{
5319 int rval;
5320 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005321 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 int found;
5323
5324 sw_info_t *swl;
5325 int swl_idx;
5326 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005327 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005328 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005329 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005330 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 rval = QLA_SUCCESS;
5333
5334 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005335 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005336 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005337 GFP_KERNEL);
5338 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005339 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005341 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005342 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005344 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005345 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005347 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5348 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005349 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005351 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5352 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005353 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005355 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5356 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005357 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5358 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005359 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5360 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005362
5363 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005364 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005365 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005366 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5367 return rval;
5368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
5370 swl_idx = 0;
5371
5372 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005373 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005375 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005376 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return (QLA_MEMORY_ALLOC_FAILED);
5378 }
5379 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 /* Set start port ID scan at adapter ID. */
5381 first_dev = 1;
5382 last_dev = 0;
5383
5384 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005385 loop_id = ha->min_external_loopid;
5386 for (; loop_id <= ha->max_loop_id; loop_id++) {
5387 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 continue;
5389
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005390 if (ha->current_topology == ISP_CFG_FL &&
5391 (atomic_read(&vha->loop_down_timer) ||
5392 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005393 atomic_set(&vha->loop_down_timer, 0);
5394 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5395 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399 if (swl != NULL) {
5400 if (last_dev) {
5401 wrap.b24 = new_fcport->d_id.b24;
5402 } else {
5403 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5404 memcpy(new_fcport->node_name,
5405 swl[swl_idx].node_name, WWN_SIZE);
5406 memcpy(new_fcport->port_name,
5407 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005408 memcpy(new_fcport->fabric_port_name,
5409 swl[swl_idx].fabric_port_name, WWN_SIZE);
5410 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005411 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005413 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005414 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005415 if (vha->flags.nvme_enabled &&
5416 swl[swl_idx].fc4f_nvme) {
5417 new_fcport->fc4f_nvme =
5418 swl[swl_idx].fc4f_nvme;
5419 ql_log(ql_log_info, vha, 0x2131,
5420 "FOUND: NVME port %8phC as FC Type 28h\n",
5421 new_fcport->port_name);
5422 }
5423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5425 last_dev = 1;
5426 }
5427 swl_idx++;
5428 }
5429 } else {
5430 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005431 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005433 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005434 "SNS scan failed -- assuming "
5435 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 rval = QLA_SUCCESS;
5437 break;
5438 }
5439 }
5440
5441 /* If wrap on switch device list, exit. */
5442 if (first_dev) {
5443 wrap.b24 = new_fcport->d_id.b24;
5444 first_dev = 0;
5445 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005446 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005447 "Device wrap (%02x%02x%02x).\n",
5448 new_fcport->d_id.b.domain,
5449 new_fcport->d_id.b.area,
5450 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 break;
5452 }
5453
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005454 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005455 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 continue;
5457
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005458 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005459 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5460 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005461
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005462 /* Bypass if same domain and area of adapter. */
5463 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005464 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005465 ISP_CFG_FL)
5466 continue;
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /* Bypass reserved domain fields. */
5469 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5470 continue;
5471
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005472 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005473 if (ql2xgffidenable &&
5474 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5475 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005476 continue;
5477
Quinn Tran726b8542017-01-19 22:28:00 -08005478 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 /* Locate matching device in database. */
5481 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005482 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 if (memcmp(new_fcport->port_name, fcport->port_name,
5484 WWN_SIZE))
5485 continue;
5486
Joe Carnuccio827210b2013-02-08 01:57:57 -05005487 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 found++;
5490
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005491 /* Update port state. */
5492 memcpy(fcport->fabric_port_name,
5493 new_fcport->fabric_port_name, WWN_SIZE);
5494 fcport->fp_speed = new_fcport->fp_speed;
5495
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005497 * If address the same and state FCS_ONLINE
5498 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 */
5500 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005501 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005502 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 break;
5504 }
5505
5506 /*
5507 * If device was not a fabric device before.
5508 */
5509 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5510 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005511 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 fcport->flags |= (FCF_FABRIC_DEVICE |
5513 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 break;
5515 }
5516
5517 /*
5518 * Port ID changed or device was marked to be updated;
5519 * Log it out if still logged in and mark it for
5520 * relogin later.
5521 */
Quinn Tran726b8542017-01-19 22:28:00 -08005522 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005523 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5524 "port changed FC ID, %8phC"
5525 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5526 fcport->port_name,
5527 fcport->d_id.b.domain,
5528 fcport->d_id.b.area,
5529 fcport->d_id.b.al_pa,
5530 fcport->loop_id,
5531 new_fcport->d_id.b.domain,
5532 new_fcport->d_id.b.area,
5533 new_fcport->d_id.b.al_pa);
5534 fcport->d_id.b24 = new_fcport->d_id.b24;
5535 break;
5536 }
5537
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 fcport->d_id.b24 = new_fcport->d_id.b24;
5539 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 break;
5541 }
5542
Darren Trapp9dd96862018-03-20 23:09:32 -07005543 if (fcport->fc4f_nvme) {
5544 if (fcport->disc_state == DSC_DELETE_PEND) {
5545 fcport->disc_state = DSC_GNL;
5546 vha->fcport_count--;
5547 fcport->login_succ = 0;
5548 }
5549 }
5550
Quinn Tran726b8542017-01-19 22:28:00 -08005551 if (found) {
5552 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005556 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005557 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5558
5559 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
5562 /* Allocate a new replacement fcport. */
5563 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005564 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005566 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005567 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 return (QLA_MEMORY_ALLOC_FAILED);
5569 }
5570 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5571 new_fcport->d_id.b24 = nxt_d_id.b24;
5572 }
5573
Quinn Tran726b8542017-01-19 22:28:00 -08005574 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Quinn Tran726b8542017-01-19 22:28:00 -08005576 /*
5577 * Logout all previous fabric dev marked lost, except FCP2 devices.
5578 */
5579 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5580 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5581 break;
5582
5583 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5584 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5585 continue;
5586
5587 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5588 if ((qla_dual_mode_enabled(vha) ||
5589 qla_ini_mode_enabled(vha)) &&
5590 atomic_read(&fcport->state) == FCS_ONLINE) {
5591 qla2x00_mark_device_lost(vha, fcport,
5592 ql2xplogiabsentdevice, 0);
5593 if (fcport->loop_id != FC_NO_LOOP_ID &&
5594 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5595 fcport->port_type != FCT_INITIATOR &&
5596 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005597 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005598 "%s %d %8phC post del sess\n",
5599 __func__, __LINE__,
5600 fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08005601 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005602 continue;
5603 }
5604 }
5605 }
5606
5607 if (fcport->scan_state == QLA_FCPORT_FOUND)
5608 qla24xx_fcport_handle_login(vha, fcport);
5609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 return (rval);
5611}
5612
5613/*
5614 * qla2x00_find_new_loop_id
5615 * Scan through our port list and find a new usable loop ID.
5616 *
5617 * Input:
5618 * ha: adapter state pointer.
5619 * dev: port structure pointer.
5620 *
5621 * Returns:
5622 * qla2x00 local function return status code.
5623 *
5624 * Context:
5625 * Kernel context.
5626 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005627int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005628qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629{
5630 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005631 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005632 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 rval = QLA_SUCCESS;
5635
Chad Dupuis5f16b332012-08-22 14:21:00 -04005636 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Chad Dupuis5f16b332012-08-22 14:21:00 -04005638 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5639 LOOPID_MAP_SIZE);
5640 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5641 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5642 dev->loop_id = FC_NO_LOOP_ID;
5643 rval = QLA_FUNCTION_FAILED;
5644 } else
5645 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Chad Dupuis5f16b332012-08-22 14:21:00 -04005647 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648
Chad Dupuis5f16b332012-08-22 14:21:00 -04005649 if (rval == QLA_SUCCESS)
5650 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5651 "Assigning new loopid=%x, portid=%x.\n",
5652 dev->loop_id, dev->d_id.b24);
5653 else
5654 ql_log(ql_log_warn, dev->vha, 0x2087,
5655 "No loop_id's available, portid=%x.\n",
5656 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 return (rval);
5659}
5660
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661
5662/*
5663 * qla2x00_fabric_login
5664 * Issue fabric login command.
5665 *
5666 * Input:
5667 * ha = adapter block pointer.
5668 * device = pointer to FC device type structure.
5669 *
5670 * Returns:
5671 * 0 - Login successfully
5672 * 1 - Login failed
5673 * 2 - Initiator device
5674 * 3 - Fatal error
5675 */
5676int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005677qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 uint16_t *next_loopid)
5679{
5680 int rval;
5681 int retry;
5682 uint16_t tmp_loopid;
5683 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005684 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
5686 retry = 0;
5687 tmp_loopid = 0;
5688
5689 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005690 ql_dbg(ql_dbg_disc, vha, 0x2000,
5691 "Trying Fabric Login w/loop id 0x%04x for port "
5692 "%02x%02x%02x.\n",
5693 fcport->loop_id, fcport->d_id.b.domain,
5694 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005697 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 fcport->d_id.b.domain, fcport->d_id.b.area,
5699 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005700 if (rval != QLA_SUCCESS) {
5701 return rval;
5702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 if (mb[0] == MBS_PORT_ID_USED) {
5704 /*
5705 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005706 * recommends the driver perform an implicit login with
5707 * the specified ID again. The ID we just used is save
5708 * here so we return with an ID that can be tried by
5709 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 */
5711 retry++;
5712 tmp_loopid = fcport->loop_id;
5713 fcport->loop_id = mb[1];
5714
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005715 ql_dbg(ql_dbg_disc, vha, 0x2001,
5716 "Fabric Login: port in use - next loop "
5717 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005719 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5722 /*
5723 * Login succeeded.
5724 */
5725 if (retry) {
5726 /* A retry occurred before. */
5727 *next_loopid = tmp_loopid;
5728 } else {
5729 /*
5730 * No retry occurred before. Just increment the
5731 * ID value for next login.
5732 */
5733 *next_loopid = (fcport->loop_id + 1);
5734 }
5735
5736 if (mb[1] & BIT_0) {
5737 fcport->port_type = FCT_INITIATOR;
5738 } else {
5739 fcport->port_type = FCT_TARGET;
5740 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005741 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 }
5743 }
5744
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005745 if (mb[10] & BIT_0)
5746 fcport->supported_classes |= FC_COS_CLASS2;
5747 if (mb[10] & BIT_1)
5748 fcport->supported_classes |= FC_COS_CLASS3;
5749
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005750 if (IS_FWI2_CAPABLE(ha)) {
5751 if (mb[10] & BIT_7)
5752 fcport->flags |=
5753 FCF_CONF_COMP_SUPPORTED;
5754 }
5755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 rval = QLA_SUCCESS;
5757 break;
5758 } else if (mb[0] == MBS_LOOP_ID_USED) {
5759 /*
5760 * Loop ID already used, try next loop ID.
5761 */
5762 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005763 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 if (rval != QLA_SUCCESS) {
5765 /* Ran out of loop IDs to use */
5766 break;
5767 }
5768 } else if (mb[0] == MBS_COMMAND_ERROR) {
5769 /*
5770 * Firmware possibly timed out during login. If NO
5771 * retries are left to do then the device is declared
5772 * dead.
5773 */
5774 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005775 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005776 fcport->d_id.b.domain, fcport->d_id.b.area,
5777 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005778 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780 rval = 1;
5781 break;
5782 } else {
5783 /*
5784 * unrecoverable / not handled error
5785 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005786 ql_dbg(ql_dbg_disc, vha, 0x2002,
5787 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5788 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5789 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5790 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
5792 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005793 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005794 fcport->d_id.b.domain, fcport->d_id.b.area,
5795 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005796 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005797 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 rval = 3;
5800 break;
5801 }
5802 }
5803
5804 return (rval);
5805}
5806
5807/*
5808 * qla2x00_local_device_login
5809 * Issue local device login command.
5810 *
5811 * Input:
5812 * ha = adapter block pointer.
5813 * loop_id = loop id of device to login to.
5814 *
5815 * Returns (Where's the #define!!!!):
5816 * 0 - Login successfully
5817 * 1 - Login failed
5818 * 3 - Fatal error
5819 */
5820int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005821qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
5823 int rval;
5824 uint16_t mb[MAILBOX_REGISTER_COUNT];
5825
5826 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005827 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (rval == QLA_SUCCESS) {
5829 /* Interrogate mailbox registers for any errors */
5830 if (mb[0] == MBS_COMMAND_ERROR)
5831 rval = 1;
5832 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5833 /* device not in PCB table */
5834 rval = 3;
5835 }
5836
5837 return (rval);
5838}
5839
5840/*
5841 * qla2x00_loop_resync
5842 * Resync with fibre channel devices.
5843 *
5844 * Input:
5845 * ha = adapter block pointer.
5846 *
5847 * Returns:
5848 * 0 = success
5849 */
5850int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005851qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005853 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005855 struct req_que *req;
5856 struct rsp_que *rsp;
5857
Michael Hernandezd7459522016-12-12 14:40:07 -08005858 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005859 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005861 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5862 if (vha->flags.online) {
5863 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5865 wait_time = 256;
5866 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005867 if (!IS_QLAFX00(vha->hw)) {
5868 /*
5869 * Issue a marker after FW becomes
5870 * ready.
5871 */
5872 qla2x00_marker(vha, req, rsp, 0, 0,
5873 MK_SYNC_ALL);
5874 vha->marker_needed = 0;
5875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
5877 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005878 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005880 if (IS_QLAFX00(vha->hw))
5881 qlafx00_configure_devices(vha);
5882 else
5883 qla2x00_configure_loop(vha);
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005886 } while (!atomic_read(&vha->loop_down_timer) &&
5887 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5888 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5889 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 }
5892
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005893 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005896 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005897 ql_dbg(ql_dbg_disc, vha, 0x206c,
5898 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
5900 return (rval);
5901}
5902
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005903/*
5904* qla2x00_perform_loop_resync
5905* Description: This function will set the appropriate flags and call
5906* qla2x00_loop_resync. If successful loop will be resynced
5907* Arguments : scsi_qla_host_t pointer
5908* returm : Success or Failure
5909*/
5910
5911int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5912{
5913 int32_t rval = 0;
5914
5915 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5916 /*Configure the flags so that resync happens properly*/
5917 atomic_set(&ha->loop_down_timer, 0);
5918 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5919 atomic_set(&ha->loop_state, LOOP_UP);
5920 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5921 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5922 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5923
5924 rval = qla2x00_loop_resync(ha);
5925 } else
5926 atomic_set(&ha->loop_state, LOOP_DEAD);
5927
5928 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5929 }
5930
5931 return rval;
5932}
5933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005935qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005936{
5937 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005938 struct scsi_qla_host *vha;
5939 struct qla_hw_data *ha = base_vha->hw;
5940 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005941
Arun Easifeafb7b2010-09-03 14:57:00 -07005942 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005943 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005944 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5945 atomic_inc(&vha->vref_count);
5946 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005947 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005948 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5949 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005950 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005951
Arun Easifeafb7b2010-09-03 14:57:00 -07005952 spin_lock_irqsave(&ha->vport_slock, flags);
5953 }
5954 }
5955 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005956 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005957 }
5958 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005959}
5960
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005961/* Assumes idc_lock always held on entry */
5962void
5963qla83xx_reset_ownership(scsi_qla_host_t *vha)
5964{
5965 struct qla_hw_data *ha = vha->hw;
5966 uint32_t drv_presence, drv_presence_mask;
5967 uint32_t dev_part_info1, dev_part_info2, class_type;
5968 uint32_t class_type_mask = 0x3;
5969 uint16_t fcoe_other_function = 0xffff, i;
5970
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005971 if (IS_QLA8044(ha)) {
5972 drv_presence = qla8044_rd_direct(vha,
5973 QLA8044_CRB_DRV_ACTIVE_INDEX);
5974 dev_part_info1 = qla8044_rd_direct(vha,
5975 QLA8044_CRB_DEV_PART_INFO_INDEX);
5976 dev_part_info2 = qla8044_rd_direct(vha,
5977 QLA8044_CRB_DEV_PART_INFO2);
5978 } else {
5979 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5980 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5981 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5982 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005983 for (i = 0; i < 8; i++) {
5984 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5985 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5986 (i != ha->portnum)) {
5987 fcoe_other_function = i;
5988 break;
5989 }
5990 }
5991 if (fcoe_other_function == 0xffff) {
5992 for (i = 0; i < 8; i++) {
5993 class_type = ((dev_part_info2 >> (i * 4)) &
5994 class_type_mask);
5995 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5996 ((i + 8) != ha->portnum)) {
5997 fcoe_other_function = i + 8;
5998 break;
5999 }
6000 }
6001 }
6002 /*
6003 * Prepare drv-presence mask based on fcoe functions present.
6004 * However consider only valid physical fcoe function numbers (0-15).
6005 */
6006 drv_presence_mask = ~((1 << (ha->portnum)) |
6007 ((fcoe_other_function == 0xffff) ?
6008 0 : (1 << (fcoe_other_function))));
6009
6010 /* We are the reset owner iff:
6011 * - No other protocol drivers present.
6012 * - This is the lowest among fcoe functions. */
6013 if (!(drv_presence & drv_presence_mask) &&
6014 (ha->portnum < fcoe_other_function)) {
6015 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
6016 "This host is Reset owner.\n");
6017 ha->flags.nic_core_reset_owner = 1;
6018 }
6019}
6020
Saurav Kashyapfa492632012-11-21 02:40:29 -05006021static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006022__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6023{
6024 int rval = QLA_SUCCESS;
6025 struct qla_hw_data *ha = vha->hw;
6026 uint32_t drv_ack;
6027
6028 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6029 if (rval == QLA_SUCCESS) {
6030 drv_ack |= (1 << ha->portnum);
6031 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6032 }
6033
6034 return rval;
6035}
6036
Saurav Kashyapfa492632012-11-21 02:40:29 -05006037static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006038__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6039{
6040 int rval = QLA_SUCCESS;
6041 struct qla_hw_data *ha = vha->hw;
6042 uint32_t drv_ack;
6043
6044 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6045 if (rval == QLA_SUCCESS) {
6046 drv_ack &= ~(1 << ha->portnum);
6047 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6048 }
6049
6050 return rval;
6051}
6052
Saurav Kashyapfa492632012-11-21 02:40:29 -05006053static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006054qla83xx_dev_state_to_string(uint32_t dev_state)
6055{
6056 switch (dev_state) {
6057 case QLA8XXX_DEV_COLD:
6058 return "COLD/RE-INIT";
6059 case QLA8XXX_DEV_INITIALIZING:
6060 return "INITIALIZING";
6061 case QLA8XXX_DEV_READY:
6062 return "READY";
6063 case QLA8XXX_DEV_NEED_RESET:
6064 return "NEED RESET";
6065 case QLA8XXX_DEV_NEED_QUIESCENT:
6066 return "NEED QUIESCENT";
6067 case QLA8XXX_DEV_FAILED:
6068 return "FAILED";
6069 case QLA8XXX_DEV_QUIESCENT:
6070 return "QUIESCENT";
6071 default:
6072 return "Unknown";
6073 }
6074}
6075
6076/* Assumes idc-lock always held on entry */
6077void
6078qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6079{
6080 struct qla_hw_data *ha = vha->hw;
6081 uint32_t idc_audit_reg = 0, duration_secs = 0;
6082
6083 switch (audit_type) {
6084 case IDC_AUDIT_TIMESTAMP:
6085 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6086 idc_audit_reg = (ha->portnum) |
6087 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6088 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6089 break;
6090
6091 case IDC_AUDIT_COMPLETION:
6092 duration_secs = ((jiffies_to_msecs(jiffies) -
6093 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6094 idc_audit_reg = (ha->portnum) |
6095 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6096 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6097 break;
6098
6099 default:
6100 ql_log(ql_log_warn, vha, 0xb078,
6101 "Invalid audit type specified.\n");
6102 break;
6103 }
6104}
6105
6106/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006107static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006108qla83xx_initiating_reset(scsi_qla_host_t *vha)
6109{
6110 struct qla_hw_data *ha = vha->hw;
6111 uint32_t idc_control, dev_state;
6112
6113 __qla83xx_get_idc_control(vha, &idc_control);
6114 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6115 ql_log(ql_log_info, vha, 0xb080,
6116 "NIC Core reset has been disabled. idc-control=0x%x\n",
6117 idc_control);
6118 return QLA_FUNCTION_FAILED;
6119 }
6120
6121 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6122 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6123 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6124 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6125 QLA8XXX_DEV_NEED_RESET);
6126 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6127 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6128 } else {
6129 const char *state = qla83xx_dev_state_to_string(dev_state);
6130 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6131
6132 /* SV: XXX: Is timeout required here? */
6133 /* Wait for IDC state change READY -> NEED_RESET */
6134 while (dev_state == QLA8XXX_DEV_READY) {
6135 qla83xx_idc_unlock(vha, 0);
6136 msleep(200);
6137 qla83xx_idc_lock(vha, 0);
6138 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6139 }
6140 }
6141
6142 /* Send IDC ack by writing to drv-ack register */
6143 __qla83xx_set_drv_ack(vha);
6144
6145 return QLA_SUCCESS;
6146}
6147
6148int
6149__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6150{
6151 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6152}
6153
6154int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006155__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6156{
6157 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6158}
6159
Saurav Kashyapfa492632012-11-21 02:40:29 -05006160static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006161qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6162{
6163 uint32_t drv_presence = 0;
6164 struct qla_hw_data *ha = vha->hw;
6165
6166 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6167 if (drv_presence & (1 << ha->portnum))
6168 return QLA_SUCCESS;
6169 else
6170 return QLA_TEST_FAILED;
6171}
6172
6173int
6174qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6175{
6176 int rval = QLA_SUCCESS;
6177 struct qla_hw_data *ha = vha->hw;
6178
6179 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6180 "Entered %s().\n", __func__);
6181
6182 if (vha->device_flags & DFLG_DEV_FAILED) {
6183 ql_log(ql_log_warn, vha, 0xb059,
6184 "Device in unrecoverable FAILED state.\n");
6185 return QLA_FUNCTION_FAILED;
6186 }
6187
6188 qla83xx_idc_lock(vha, 0);
6189
6190 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6191 ql_log(ql_log_warn, vha, 0xb05a,
6192 "Function=0x%x has been removed from IDC participation.\n",
6193 ha->portnum);
6194 rval = QLA_FUNCTION_FAILED;
6195 goto exit;
6196 }
6197
6198 qla83xx_reset_ownership(vha);
6199
6200 rval = qla83xx_initiating_reset(vha);
6201
6202 /*
6203 * Perform reset if we are the reset-owner,
6204 * else wait till IDC state changes to READY/FAILED.
6205 */
6206 if (rval == QLA_SUCCESS) {
6207 rval = qla83xx_idc_state_handler(vha);
6208
6209 if (rval == QLA_SUCCESS)
6210 ha->flags.nic_core_hung = 0;
6211 __qla83xx_clear_drv_ack(vha);
6212 }
6213
6214exit:
6215 qla83xx_idc_unlock(vha, 0);
6216
6217 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6218
6219 return rval;
6220}
6221
Saurav Kashyap81178772012-08-22 14:21:04 -04006222int
6223qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6224{
6225 struct qla_hw_data *ha = vha->hw;
6226 int rval = QLA_FUNCTION_FAILED;
6227
6228 if (!IS_MCTP_CAPABLE(ha)) {
6229 /* This message can be removed from the final version */
6230 ql_log(ql_log_info, vha, 0x506d,
6231 "This board is not MCTP capable\n");
6232 return rval;
6233 }
6234
6235 if (!ha->mctp_dump) {
6236 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6237 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6238
6239 if (!ha->mctp_dump) {
6240 ql_log(ql_log_warn, vha, 0x506e,
6241 "Failed to allocate memory for mctp dump\n");
6242 return rval;
6243 }
6244 }
6245
6246#define MCTP_DUMP_STR_ADDR 0x00000000
6247 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6248 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6249 if (rval != QLA_SUCCESS) {
6250 ql_log(ql_log_warn, vha, 0x506f,
6251 "Failed to capture mctp dump\n");
6252 } else {
6253 ql_log(ql_log_info, vha, 0x5070,
6254 "Mctp dump capture for host (%ld/%p).\n",
6255 vha->host_no, ha->mctp_dump);
6256 ha->mctp_dumped = 1;
6257 }
6258
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006259 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006260 ha->flags.nic_core_reset_hdlr_active = 1;
6261 rval = qla83xx_restart_nic_firmware(vha);
6262 if (rval)
6263 /* NIC Core reset failed. */
6264 ql_log(ql_log_warn, vha, 0x5071,
6265 "Failed to restart nic firmware\n");
6266 else
6267 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6268 "Restarted NIC firmware successfully.\n");
6269 ha->flags.nic_core_reset_hdlr_active = 0;
6270 }
6271
6272 return rval;
6273
6274}
6275
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006276/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006277* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006278* Description: This function will block the new I/Os
6279* Its not aborting any I/Os as context
6280* is not destroyed during quiescence
6281* Arguments: scsi_qla_host_t
6282* return : void
6283*/
6284void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006285qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006286{
6287 struct qla_hw_data *ha = vha->hw;
6288 struct scsi_qla_host *vp;
6289
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006290 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6291 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006292
6293 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6294 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6295 atomic_set(&vha->loop_state, LOOP_DOWN);
6296 qla2x00_mark_all_devices_lost(vha, 0);
6297 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006298 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006299 } else {
6300 if (!atomic_read(&vha->loop_down_timer))
6301 atomic_set(&vha->loop_down_timer,
6302 LOOP_DOWN_TIME);
6303 }
6304 /* Wait for pending cmds to complete */
6305 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6306}
6307
Giridhar Malavalia9083012010-04-12 17:59:55 -07006308void
6309qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6310{
6311 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006312 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006313 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006314 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006315 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006316
Saurav Kashyape46ef002011-02-23 15:27:16 -08006317 /* For ISP82XX, driver waits for completion of the commands.
6318 * online flag should be set.
6319 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006320 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006321 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006322 ha->flags.chip_reset_done = 0;
6323 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006324 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006325
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006326 ql_log(ql_log_info, vha, 0x00af,
6327 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006328
Saurav Kashyape46ef002011-02-23 15:27:16 -08006329 /* For ISP82XX, reset_chip is just disabling interrupts.
6330 * Driver waits for the completion of the commands.
6331 * the interrupts need to be enabled.
6332 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006333 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006334 ha->isp_ops->reset_chip(vha);
6335
Quinn Tran9cd883f2017-12-28 12:33:24 -08006336 SAVE_TOPO(ha);
6337 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006338 ha->flags.n2n_ae = 0;
6339 ha->flags.lip_ae = 0;
6340 ha->current_topology = 0;
6341 ha->flags.fw_started = 0;
6342 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006343 ha->base_qpair->chip_reset++;
6344 for (i = 0; i < ha->max_qpairs; i++) {
6345 if (ha->queue_pair_map[i])
6346 ha->queue_pair_map[i]->chip_reset =
6347 ha->base_qpair->chip_reset;
6348 }
Quinn Tran726b8542017-01-19 22:28:00 -08006349
Giridhar Malavalia9083012010-04-12 17:59:55 -07006350 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6351 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6352 atomic_set(&vha->loop_state, LOOP_DOWN);
6353 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006354
6355 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006356 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006357 atomic_inc(&vp->vref_count);
6358 spin_unlock_irqrestore(&ha->vport_slock, flags);
6359
Giridhar Malavalia9083012010-04-12 17:59:55 -07006360 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006361
6362 spin_lock_irqsave(&ha->vport_slock, flags);
6363 atomic_dec(&vp->vref_count);
6364 }
6365 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006366 } else {
6367 if (!atomic_read(&vha->loop_down_timer))
6368 atomic_set(&vha->loop_down_timer,
6369 LOOP_DOWN_TIME);
6370 }
6371
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006372 /* Clear all async request states across all VPs. */
6373 list_for_each_entry(fcport, &vha->vp_fcports, list)
6374 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6375 spin_lock_irqsave(&ha->vport_slock, flags);
6376 list_for_each_entry(vp, &ha->vp_list, list) {
6377 atomic_inc(&vp->vref_count);
6378 spin_unlock_irqrestore(&ha->vport_slock, flags);
6379
6380 list_for_each_entry(fcport, &vp->vp_fcports, list)
6381 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6382
6383 spin_lock_irqsave(&ha->vport_slock, flags);
6384 atomic_dec(&vp->vref_count);
6385 }
6386 spin_unlock_irqrestore(&ha->vport_slock, flags);
6387
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006388 if (!ha->flags.eeh_busy) {
6389 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006390 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006391 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006392 ql_log(ql_log_info, vha, 0x00b4,
6393 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006394
Saurav Kashyape46ef002011-02-23 15:27:16 -08006395 /* Done waiting for pending commands.
6396 * Reset the online flag.
6397 */
6398 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006401 /* Requeue all commands in outstanding command list. */
6402 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6403 }
Arun Easib6a029e2014-09-25 06:14:52 -04006404 /* memory barrier */
6405 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406}
6407
6408/*
6409* qla2x00_abort_isp
6410* Resets ISP and aborts all outstanding commands.
6411*
6412* Input:
6413* ha = adapter block pointer.
6414*
6415* Returns:
6416* 0 = success
6417*/
6418int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006419qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006421 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006423 struct qla_hw_data *ha = vha->hw;
6424 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006425 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006426 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006428 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006429 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
Santosh Vernekara61712972012-08-22 14:21:13 -04006431 if (IS_QLA8031(ha)) {
6432 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6433 "Clearing fcoe driver presence.\n");
6434 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6435 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6436 "Error while clearing DRV-Presence.\n");
6437 }
6438
Andrew Vasquez85880802009-12-15 21:29:46 -08006439 if (unlikely(pci_channel_offline(ha->pdev) &&
6440 ha->flags.pci_channel_io_perm_failure)) {
6441 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6442 status = 0;
6443 return status;
6444 }
6445
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006446 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006447
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006448 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006450 if (!qla2x00_restart_isp(vha)) {
6451 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006453 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 /*
6455 * Issue marker command only when we are going
6456 * to start the I/O .
6457 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006458 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 }
6460
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006461 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006463 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006465 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006466 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006467
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006468 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6469 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006470 if (ha->fce) {
6471 ha->flags.fce_enabled = 1;
6472 memset(ha->fce, 0,
6473 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006474 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006475 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6476 &ha->fce_bufs);
6477 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006478 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006479 "Unable to reinitialize FCE "
6480 "(%d).\n", rval);
6481 ha->flags.fce_enabled = 0;
6482 }
6483 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006484
6485 if (ha->eft) {
6486 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006487 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006488 ha->eft_dma, EFT_NUM_BUFFERS);
6489 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006490 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006491 "Unable to reinitialize EFT "
6492 "(%d).\n", rval);
6493 }
6494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006496 vha->flags.online = 1;
6497 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006499 ql_log(ql_log_fatal, vha, 0x8035,
6500 "ISP error recover failed - "
6501 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006502 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 * The next call disables the board
6504 * completely.
6505 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006506 ha->isp_ops->reset_adapter(vha);
6507 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006509 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 status = 0;
6511 } else { /* schedule another ISP abort */
6512 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006513 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6514 "ISP abort - retry remaining %d.\n",
6515 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 status = 1;
6517 }
6518 } else {
6519 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006520 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6521 "ISP error recovery - retrying (%d) "
6522 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006523 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 status = 1;
6525 }
6526 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006527
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 }
6529
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006530 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006531 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006532 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006533 spin_lock_irqsave(&ha->vport_slock, flags);
6534 list_for_each_entry(vp, &ha->vp_list, list) {
6535 if (vp->vp_idx) {
6536 atomic_inc(&vp->vref_count);
6537 spin_unlock_irqrestore(&ha->vport_slock, flags);
6538
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006539 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006540
6541 spin_lock_irqsave(&ha->vport_slock, flags);
6542 atomic_dec(&vp->vref_count);
6543 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006544 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006545 spin_unlock_irqrestore(&ha->vport_slock, flags);
6546
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006547 if (IS_QLA8031(ha)) {
6548 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6549 "Setting back fcoe driver presence.\n");
6550 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6551 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6552 "Error while setting DRV-Presence.\n");
6553 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006554 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006555 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6556 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 }
6558
6559 return(status);
6560}
6561
6562/*
6563* qla2x00_restart_isp
6564* restarts the ISP after a reset
6565*
6566* Input:
6567* ha = adapter block pointer.
6568*
6569* Returns:
6570* 0 = success
6571*/
6572static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006573qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006575 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006576 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006577 struct req_que *req = ha->req_q_map[0];
6578 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
6580 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006581 if (qla2x00_isp_firmware(vha)) {
6582 vha->flags.online = 0;
6583 status = ha->isp_ops->chip_diag(vha);
6584 if (!status)
6585 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 }
6587
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006588 if (!status && !(status = qla2x00_init_rings(vha))) {
6589 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006590 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006591
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006592 /* Initialize the queues in use */
6593 qla25xx_init_queues(ha);
6594
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006595 status = qla2x00_fw_ready(vha);
6596 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006597 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006598 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006599 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 }
6601
6602 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006603 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 }
6606 return (status);
6607}
6608
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006609static int
6610qla25xx_init_queues(struct qla_hw_data *ha)
6611{
6612 struct rsp_que *rsp = NULL;
6613 struct req_que *req = NULL;
6614 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6615 int ret = -1;
6616 int i;
6617
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006618 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006619 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006620 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006621 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006622 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006623 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006624 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6625 "%s Rsp que: %d init failed.\n",
6626 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006627 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006628 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6629 "%s Rsp que: %d inited.\n",
6630 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006631 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006632 }
6633 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006634 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006635 if (req && test_bit(i, ha->req_qid_map)) {
6636 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006637 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006638 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006639 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006640 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6641 "%s Req que: %d init failed.\n",
6642 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006643 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006644 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6645 "%s Req que: %d inited.\n",
6646 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006647 }
6648 }
6649 return ret;
6650}
6651
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652/*
6653* qla2x00_reset_adapter
6654* Reset adapter.
6655*
6656* Input:
6657* ha = adapter block pointer.
6658*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006659void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006660qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661{
6662 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006663 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006664 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006666 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006667 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 spin_lock_irqsave(&ha->hardware_lock, flags);
6670 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6671 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6672 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6673 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6674 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6675}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006676
6677void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006678qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006679{
6680 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006681 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006682 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6683
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006684 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006685 return;
6686
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006687 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006688 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006689
6690 spin_lock_irqsave(&ha->hardware_lock, flags);
6691 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6692 RD_REG_DWORD(&reg->hccr);
6693 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6694 RD_REG_DWORD(&reg->hccr);
6695 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006696
6697 if (IS_NOPOLLING_TYPE(ha))
6698 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006699}
6700
David Miller4e08df32007-04-16 12:37:43 -07006701/* On sparc systems, obtain port and node WWN from firmware
6702 * properties.
6703 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006704static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6705 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006706{
6707#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006708 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006709 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006710 struct device_node *dp = pci_device_to_OF_node(pdev);
6711 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006712 int len;
6713
6714 val = of_get_property(dp, "port-wwn", &len);
6715 if (val && len >= WWN_SIZE)
6716 memcpy(nv->port_name, val, WWN_SIZE);
6717
6718 val = of_get_property(dp, "node-wwn", &len);
6719 if (val && len >= WWN_SIZE)
6720 memcpy(nv->node_name, val, WWN_SIZE);
6721#endif
6722}
6723
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006724int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006725qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006726{
David Miller4e08df32007-04-16 12:37:43 -07006727 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006728 struct init_cb_24xx *icb;
6729 struct nvram_24xx *nv;
6730 uint32_t *dptr;
6731 uint8_t *dptr1, *dptr2;
6732 uint32_t chksum;
6733 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006734 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006735
David Miller4e08df32007-04-16 12:37:43 -07006736 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006737 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006738 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006739
6740 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006741 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006742 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6743 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6744 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006745 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006746 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6747 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006748
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006749 ha->nvram_size = sizeof(struct nvram_24xx);
6750 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006751
Seokmann Ju281afe12007-07-26 13:43:34 -07006752 /* Get VPD data into cache */
6753 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006754 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006755 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6756
6757 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006758 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006759 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006760 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006761 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6762 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006763
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006764 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6765 "Contents of NVRAM\n");
6766 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6767 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006768
6769 /* Bad NVRAM data, set defaults parameters. */
6770 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6771 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006772 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006773 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006774 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006775 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006776 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6777 ql_log(ql_log_warn, vha, 0x006c,
6778 "Falling back to functioning (yet invalid -- WWPN) "
6779 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006780
6781 /*
6782 * Set default initialization control block.
6783 */
6784 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006785 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6786 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006787 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006788 nv->execution_throttle = cpu_to_le16(0xFFFF);
6789 nv->exchange_count = cpu_to_le16(0);
6790 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006791 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006792 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006793 nv->port_name[2] = 0x00;
6794 nv->port_name[3] = 0xe0;
6795 nv->port_name[4] = 0x8b;
6796 nv->port_name[5] = 0x1c;
6797 nv->port_name[6] = 0x55;
6798 nv->port_name[7] = 0x86;
6799 nv->node_name[0] = 0x20;
6800 nv->node_name[1] = 0x00;
6801 nv->node_name[2] = 0x00;
6802 nv->node_name[3] = 0xe0;
6803 nv->node_name[4] = 0x8b;
6804 nv->node_name[5] = 0x1c;
6805 nv->node_name[6] = 0x55;
6806 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006807 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006808 nv->login_retry_count = cpu_to_le16(8);
6809 nv->interrupt_delay_timer = cpu_to_le16(0);
6810 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006811 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006812 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6813 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6814 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6815 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6816 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6817 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006818 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006819 nv->max_luns_per_target = cpu_to_le16(128);
6820 nv->port_down_retry_count = cpu_to_le16(30);
6821 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006822
6823 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006824 }
6825
Quinn Tran726b8542017-01-19 22:28:00 -08006826 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006827 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006828 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006829 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006830 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006831 }
6832
6833 qlt_24xx_config_nvram_stage1(vha, nv);
6834
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006835 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006836 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006837
6838 /* Copy 1st segment. */
6839 dptr1 = (uint8_t *)icb;
6840 dptr2 = (uint8_t *)&nv->version;
6841 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6842 while (cnt--)
6843 *dptr1++ = *dptr2++;
6844
6845 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006846 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006847
6848 /* Copy 2nd segment. */
6849 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6850 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6851 cnt = (uint8_t *)&icb->reserved_3 -
6852 (uint8_t *)&icb->interrupt_delay_timer;
6853 while (cnt--)
6854 *dptr1++ = *dptr2++;
6855
6856 /*
6857 * Setup driver NVRAM options.
6858 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006859 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006860 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006861
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006862 qlt_24xx_config_nvram_stage2(vha, icb);
6863
Bart Van Asschead950362015-07-09 07:24:08 -07006864 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006865 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006866 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6867 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6868 }
6869
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006870 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006871 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006872 /*
6873 * Firmware will apply the following mask if the nodename was
6874 * not provided.
6875 */
6876 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6877 icb->node_name[0] &= 0xF0;
6878 }
6879
6880 /* Set host adapter parameters. */
6881 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006882 ha->flags.enable_lip_reset = 0;
6883 ha->flags.enable_lip_full_login =
6884 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6885 ha->flags.enable_target_reset =
6886 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006887 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006888 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006889
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006890 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6891 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006892
6893 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6894 sizeof(ha->fw_seriallink_options24));
6895
6896 /* save HBA serial number */
6897 ha->serial0 = icb->port_name[5];
6898 ha->serial1 = icb->port_name[6];
6899 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006900 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6901 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006902
Bart Van Asschead950362015-07-09 07:24:08 -07006903 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006904
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006905 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6906
6907 /* Set minimum login_timeout to 4 seconds. */
6908 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6909 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6910 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006911 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006912 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006913
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006914 /* Set minimum RATOV to 100 tenths of a second. */
6915 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006916
6917 ha->loop_reset_delay = nv->reset_delay;
6918
6919 /* Link Down Timeout = 0:
6920 *
6921 * When Port Down timer expires we will start returning
6922 * I/O's to OS with "DID_NO_CONNECT".
6923 *
6924 * Link Down Timeout != 0:
6925 *
6926 * The driver waits for the link to come up after link down
6927 * before returning I/Os to OS with "DID_NO_CONNECT".
6928 */
6929 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6930 ha->loop_down_abort_time =
6931 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6932 } else {
6933 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6934 ha->loop_down_abort_time =
6935 (LOOP_DOWN_TIME - ha->link_down_timeout);
6936 }
6937
6938 /* Need enough time to try and get the port back. */
6939 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6940 if (qlport_down_retry)
6941 ha->port_down_retry_count = qlport_down_retry;
6942
6943 /* Set login_retry_count */
6944 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6945 if (ha->port_down_retry_count ==
6946 le16_to_cpu(nv->port_down_retry_count) &&
6947 ha->port_down_retry_count > 3)
6948 ha->login_retry_count = ha->port_down_retry_count;
6949 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6950 ha->login_retry_count = ha->port_down_retry_count;
6951 if (ql2xloginretrycount)
6952 ha->login_retry_count = ql2xloginretrycount;
6953
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006954 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006955 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006956 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6957 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6958 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6959 le16_to_cpu(icb->interrupt_delay_timer): 2;
6960 }
Bart Van Asschead950362015-07-09 07:24:08 -07006961 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006962 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006963 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006964 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006965 ha->zio_mode = QLA_ZIO_MODE_6;
6966
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006967 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006968 "ZIO mode %d enabled; timer delay (%d us).\n",
6969 ha->zio_mode, ha->zio_timer * 100);
6970
6971 icb->firmware_options_2 |= cpu_to_le32(
6972 (uint32_t)ha->zio_mode);
6973 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006974 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006975 }
6976
David Miller4e08df32007-04-16 12:37:43 -07006977 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006978 ql_log(ql_log_warn, vha, 0x0070,
6979 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006980 }
6981 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006982}
6983
Sawan Chandak4243c112016-01-27 12:03:31 -05006984uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6985{
6986 struct qla27xx_image_status pri_image_status, sec_image_status;
6987 uint8_t valid_pri_image, valid_sec_image;
6988 uint32_t *wptr;
6989 uint32_t cnt, chksum, size;
6990 struct qla_hw_data *ha = vha->hw;
6991
6992 valid_pri_image = valid_sec_image = 1;
6993 ha->active_image = 0;
6994 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6995
6996 if (!ha->flt_region_img_status_pri) {
6997 valid_pri_image = 0;
6998 goto check_sec_image;
6999 }
7000
7001 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
7002 ha->flt_region_img_status_pri, size);
7003
7004 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7005 ql_dbg(ql_dbg_init, vha, 0x018b,
7006 "Primary image signature (0x%x) not valid\n",
7007 pri_image_status.signature);
7008 valid_pri_image = 0;
7009 goto check_sec_image;
7010 }
7011
7012 wptr = (uint32_t *)(&pri_image_status);
7013 cnt = size;
7014
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007015 for (chksum = 0; cnt--; wptr++)
7016 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08007017
Sawan Chandak4243c112016-01-27 12:03:31 -05007018 if (chksum) {
7019 ql_dbg(ql_dbg_init, vha, 0x018c,
7020 "Checksum validation failed for primary image (0x%x)\n",
7021 chksum);
7022 valid_pri_image = 0;
7023 }
7024
7025check_sec_image:
7026 if (!ha->flt_region_img_status_sec) {
7027 valid_sec_image = 0;
7028 goto check_valid_image;
7029 }
7030
7031 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7032 ha->flt_region_img_status_sec, size);
7033
7034 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7035 ql_dbg(ql_dbg_init, vha, 0x018d,
7036 "Secondary image signature(0x%x) not valid\n",
7037 sec_image_status.signature);
7038 valid_sec_image = 0;
7039 goto check_valid_image;
7040 }
7041
7042 wptr = (uint32_t *)(&sec_image_status);
7043 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007044 for (chksum = 0; cnt--; wptr++)
7045 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05007046 if (chksum) {
7047 ql_dbg(ql_dbg_init, vha, 0x018e,
7048 "Checksum validation failed for secondary image (0x%x)\n",
7049 chksum);
7050 valid_sec_image = 0;
7051 }
7052
7053check_valid_image:
7054 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7055 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7056 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7057 if (!ha->active_image ||
7058 pri_image_status.generation_number <
7059 sec_image_status.generation_number)
7060 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7061 }
7062
7063 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7064 ha->active_image == 0 ? "default bootld and fw" :
7065 ha->active_image == 1 ? "primary" :
7066 ha->active_image == 2 ? "secondary" :
7067 "Invalid");
7068
7069 return ha->active_image;
7070}
7071
Adrian Bunk413975a2006-06-30 02:33:06 -07007072static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007073qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7074 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007075{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007076 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007077 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007078 uint32_t *dcode, dlen;
7079 uint32_t risc_addr;
7080 uint32_t risc_size;
7081 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007082 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007083 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007084
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007085 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007086 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007087
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007088 rval = QLA_SUCCESS;
7089
7090 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007091 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007092 *srisc_addr = 0;
7093
Sawan Chandak4243c112016-01-27 12:03:31 -05007094 if (IS_QLA27XX(ha) &&
7095 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7096 faddr = ha->flt_region_fw_sec;
7097
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007098 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007099 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007100 for (i = 0; i < 4; i++)
7101 dcode[i] = be32_to_cpu(dcode[i]);
7102 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7103 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7104 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7105 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007106 ql_log(ql_log_fatal, vha, 0x008c,
7107 "Unable to verify the integrity of flash firmware "
7108 "image.\n");
7109 ql_log(ql_log_fatal, vha, 0x008d,
7110 "Firmware data: %08x %08x %08x %08x.\n",
7111 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007112
7113 return QLA_FUNCTION_FAILED;
7114 }
7115
7116 while (segments && rval == QLA_SUCCESS) {
7117 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007118 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007119
7120 risc_addr = be32_to_cpu(dcode[2]);
7121 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7122 risc_size = be32_to_cpu(dcode[3]);
7123
7124 fragment = 0;
7125 while (risc_size > 0 && rval == QLA_SUCCESS) {
7126 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7127 if (dlen > risc_size)
7128 dlen = risc_size;
7129
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007130 ql_dbg(ql_dbg_init, vha, 0x008e,
7131 "Loading risc segment@ risc addr %x "
7132 "number of dwords 0x%x offset 0x%x.\n",
7133 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007134
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007135 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007136 for (i = 0; i < dlen; i++)
7137 dcode[i] = swab32(dcode[i]);
7138
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007139 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007140 dlen);
7141 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007142 ql_log(ql_log_fatal, vha, 0x008f,
7143 "Failed to load segment %d of firmware.\n",
7144 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007145 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007146 }
7147
7148 faddr += dlen;
7149 risc_addr += dlen;
7150 risc_size -= dlen;
7151 fragment++;
7152 }
7153
7154 /* Next segment. */
7155 segments--;
7156 }
7157
Chad Dupuisf73cb692014-02-26 04:15:06 -05007158 if (!IS_QLA27XX(ha))
7159 return rval;
7160
7161 if (ha->fw_dump_template)
7162 vfree(ha->fw_dump_template);
7163 ha->fw_dump_template = NULL;
7164 ha->fw_dump_template_len = 0;
7165
7166 ql_dbg(ql_dbg_init, vha, 0x0161,
7167 "Loading fwdump template from %x\n", faddr);
7168 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7169 risc_size = be32_to_cpu(dcode[2]);
7170 ql_dbg(ql_dbg_init, vha, 0x0162,
7171 "-> array size %x dwords\n", risc_size);
7172 if (risc_size == 0 || risc_size == ~0)
7173 goto default_template;
7174
7175 dlen = (risc_size - 8) * sizeof(*dcode);
7176 ql_dbg(ql_dbg_init, vha, 0x0163,
7177 "-> template allocating %x bytes...\n", dlen);
7178 ha->fw_dump_template = vmalloc(dlen);
7179 if (!ha->fw_dump_template) {
7180 ql_log(ql_log_warn, vha, 0x0164,
7181 "Failed fwdump template allocate %x bytes.\n", risc_size);
7182 goto default_template;
7183 }
7184
7185 faddr += 7;
7186 risc_size -= 8;
7187 dcode = ha->fw_dump_template;
7188 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7189 for (i = 0; i < risc_size; i++)
7190 dcode[i] = le32_to_cpu(dcode[i]);
7191
7192 if (!qla27xx_fwdt_template_valid(dcode)) {
7193 ql_log(ql_log_warn, vha, 0x0165,
7194 "Failed fwdump template validate\n");
7195 goto default_template;
7196 }
7197
7198 dlen = qla27xx_fwdt_template_size(dcode);
7199 ql_dbg(ql_dbg_init, vha, 0x0166,
7200 "-> template size %x bytes\n", dlen);
7201 if (dlen > risc_size * sizeof(*dcode)) {
7202 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007203 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007204 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007205 goto default_template;
7206 }
7207 ha->fw_dump_template_len = dlen;
7208 return rval;
7209
7210default_template:
7211 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7212 if (ha->fw_dump_template)
7213 vfree(ha->fw_dump_template);
7214 ha->fw_dump_template = NULL;
7215 ha->fw_dump_template_len = 0;
7216
7217 dlen = qla27xx_fwdt_template_default_size();
7218 ql_dbg(ql_dbg_init, vha, 0x0169,
7219 "-> template allocating %x bytes...\n", dlen);
7220 ha->fw_dump_template = vmalloc(dlen);
7221 if (!ha->fw_dump_template) {
7222 ql_log(ql_log_warn, vha, 0x016a,
7223 "Failed fwdump template allocate %x bytes.\n", risc_size);
7224 goto failed_template;
7225 }
7226
7227 dcode = ha->fw_dump_template;
7228 risc_size = dlen / sizeof(*dcode);
7229 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7230 for (i = 0; i < risc_size; i++)
7231 dcode[i] = be32_to_cpu(dcode[i]);
7232
7233 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7234 ql_log(ql_log_warn, vha, 0x016b,
7235 "Failed fwdump template validate\n");
7236 goto failed_template;
7237 }
7238
7239 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7240 ql_dbg(ql_dbg_init, vha, 0x016c,
7241 "-> template size %x bytes\n", dlen);
7242 ha->fw_dump_template_len = dlen;
7243 return rval;
7244
7245failed_template:
7246 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7247 if (ha->fw_dump_template)
7248 vfree(ha->fw_dump_template);
7249 ha->fw_dump_template = NULL;
7250 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007251 return rval;
7252}
7253
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007254#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007255
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007256int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007257qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007258{
7259 int rval;
7260 int i, fragment;
7261 uint16_t *wcode, *fwcode;
7262 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7263 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007264 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007265 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007266
7267 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007268 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007269 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007270 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007271 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007272 ql_log(ql_log_info, vha, 0x0084,
7273 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007274 return QLA_FUNCTION_FAILED;
7275 }
7276
7277 rval = QLA_SUCCESS;
7278
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007279 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007280 *srisc_addr = 0;
7281 fwcode = (uint16_t *)blob->fw->data;
7282 fwclen = 0;
7283
7284 /* Validate firmware image by checking version. */
7285 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007286 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007287 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007288 blob->fw->size);
7289 goto fail_fw_integrity;
7290 }
7291 for (i = 0; i < 4; i++)
7292 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7293 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7294 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7295 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007296 ql_log(ql_log_fatal, vha, 0x0086,
7297 "Unable to verify integrity of firmware image.\n");
7298 ql_log(ql_log_fatal, vha, 0x0087,
7299 "Firmware data: %04x %04x %04x %04x.\n",
7300 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007301 goto fail_fw_integrity;
7302 }
7303
7304 seg = blob->segs;
7305 while (*seg && rval == QLA_SUCCESS) {
7306 risc_addr = *seg;
7307 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7308 risc_size = be16_to_cpu(fwcode[3]);
7309
7310 /* Validate firmware image size. */
7311 fwclen += risc_size * sizeof(uint16_t);
7312 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007313 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007314 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007315 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007316 goto fail_fw_integrity;
7317 }
7318
7319 fragment = 0;
7320 while (risc_size > 0 && rval == QLA_SUCCESS) {
7321 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7322 if (wlen > risc_size)
7323 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007324 ql_dbg(ql_dbg_init, vha, 0x0089,
7325 "Loading risc segment@ risc addr %x number of "
7326 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007327
7328 for (i = 0; i < wlen; i++)
7329 wcode[i] = swab16(fwcode[i]);
7330
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007331 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007332 wlen);
7333 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007334 ql_log(ql_log_fatal, vha, 0x008a,
7335 "Failed to load segment %d of firmware.\n",
7336 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007337 break;
7338 }
7339
7340 fwcode += wlen;
7341 risc_addr += wlen;
7342 risc_size -= wlen;
7343 fragment++;
7344 }
7345
7346 /* Next segment. */
7347 seg++;
7348 }
7349 return rval;
7350
7351fail_fw_integrity:
7352 return QLA_FUNCTION_FAILED;
7353}
7354
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007355static int
7356qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007357{
7358 int rval;
7359 int segments, fragment;
7360 uint32_t *dcode, dlen;
7361 uint32_t risc_addr;
7362 uint32_t risc_size;
7363 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007364 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007365 const uint32_t *fwcode;
7366 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007367 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007368 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007369
Andrew Vasquez54333832005-11-09 15:49:04 -08007370 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007371 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007372 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007373 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007374 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007375 ql_log(ql_log_warn, vha, 0x0091,
7376 "Firmware images can be retrieved from: "
7377 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007378
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007379 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007380 }
7381
Chad Dupuiscfb09192011-11-18 09:03:07 -08007382 ql_dbg(ql_dbg_init, vha, 0x0092,
7383 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007384
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007385 rval = QLA_SUCCESS;
7386
7387 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007388 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007389 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007390 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007391 fwclen = 0;
7392
7393 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007394 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007395 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007396 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007397 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007398 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007399 }
7400 for (i = 0; i < 4; i++)
7401 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7402 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7403 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7404 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7405 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007406 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007407 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007408 blob->fw->size);
7409 ql_log(ql_log_fatal, vha, 0x0095,
7410 "Firmware data: %08x %08x %08x %08x.\n",
7411 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007412 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007413 }
7414
7415 while (segments && rval == QLA_SUCCESS) {
7416 risc_addr = be32_to_cpu(fwcode[2]);
7417 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7418 risc_size = be32_to_cpu(fwcode[3]);
7419
7420 /* Validate firmware image size. */
7421 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007422 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007423 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007424 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007425 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007426 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007427 }
7428
7429 fragment = 0;
7430 while (risc_size > 0 && rval == QLA_SUCCESS) {
7431 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7432 if (dlen > risc_size)
7433 dlen = risc_size;
7434
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007435 ql_dbg(ql_dbg_init, vha, 0x0097,
7436 "Loading risc segment@ risc addr %x "
7437 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007438
7439 for (i = 0; i < dlen; i++)
7440 dcode[i] = swab32(fwcode[i]);
7441
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007442 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007443 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007444 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007445 ql_log(ql_log_fatal, vha, 0x0098,
7446 "Failed to load segment %d of firmware.\n",
7447 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007448 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007449 }
7450
7451 fwcode += dlen;
7452 risc_addr += dlen;
7453 risc_size -= dlen;
7454 fragment++;
7455 }
7456
7457 /* Next segment. */
7458 segments--;
7459 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007460
7461 if (!IS_QLA27XX(ha))
7462 return rval;
7463
7464 if (ha->fw_dump_template)
7465 vfree(ha->fw_dump_template);
7466 ha->fw_dump_template = NULL;
7467 ha->fw_dump_template_len = 0;
7468
7469 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007470 "Loading fwdump template from %x\n",
7471 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007472 risc_size = be32_to_cpu(fwcode[2]);
7473 ql_dbg(ql_dbg_init, vha, 0x172,
7474 "-> array size %x dwords\n", risc_size);
7475 if (risc_size == 0 || risc_size == ~0)
7476 goto default_template;
7477
7478 dlen = (risc_size - 8) * sizeof(*fwcode);
7479 ql_dbg(ql_dbg_init, vha, 0x0173,
7480 "-> template allocating %x bytes...\n", dlen);
7481 ha->fw_dump_template = vmalloc(dlen);
7482 if (!ha->fw_dump_template) {
7483 ql_log(ql_log_warn, vha, 0x0174,
7484 "Failed fwdump template allocate %x bytes.\n", risc_size);
7485 goto default_template;
7486 }
7487
7488 fwcode += 7;
7489 risc_size -= 8;
7490 dcode = ha->fw_dump_template;
7491 for (i = 0; i < risc_size; i++)
7492 dcode[i] = le32_to_cpu(fwcode[i]);
7493
7494 if (!qla27xx_fwdt_template_valid(dcode)) {
7495 ql_log(ql_log_warn, vha, 0x0175,
7496 "Failed fwdump template validate\n");
7497 goto default_template;
7498 }
7499
7500 dlen = qla27xx_fwdt_template_size(dcode);
7501 ql_dbg(ql_dbg_init, vha, 0x0176,
7502 "-> template size %x bytes\n", dlen);
7503 if (dlen > risc_size * sizeof(*fwcode)) {
7504 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007505 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007506 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007507 goto default_template;
7508 }
7509 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007510 return rval;
7511
Chad Dupuisf73cb692014-02-26 04:15:06 -05007512default_template:
7513 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7514 if (ha->fw_dump_template)
7515 vfree(ha->fw_dump_template);
7516 ha->fw_dump_template = NULL;
7517 ha->fw_dump_template_len = 0;
7518
7519 dlen = qla27xx_fwdt_template_default_size();
7520 ql_dbg(ql_dbg_init, vha, 0x0179,
7521 "-> template allocating %x bytes...\n", dlen);
7522 ha->fw_dump_template = vmalloc(dlen);
7523 if (!ha->fw_dump_template) {
7524 ql_log(ql_log_warn, vha, 0x017a,
7525 "Failed fwdump template allocate %x bytes.\n", risc_size);
7526 goto failed_template;
7527 }
7528
7529 dcode = ha->fw_dump_template;
7530 risc_size = dlen / sizeof(*fwcode);
7531 fwcode = qla27xx_fwdt_template_default();
7532 for (i = 0; i < risc_size; i++)
7533 dcode[i] = be32_to_cpu(fwcode[i]);
7534
7535 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7536 ql_log(ql_log_warn, vha, 0x017b,
7537 "Failed fwdump template validate\n");
7538 goto failed_template;
7539 }
7540
7541 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7542 ql_dbg(ql_dbg_init, vha, 0x017c,
7543 "-> template size %x bytes\n", dlen);
7544 ha->fw_dump_template_len = dlen;
7545 return rval;
7546
7547failed_template:
7548 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7549 if (ha->fw_dump_template)
7550 vfree(ha->fw_dump_template);
7551 ha->fw_dump_template = NULL;
7552 ha->fw_dump_template_len = 0;
7553 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007554}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007555
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007556int
7557qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7558{
7559 int rval;
7560
Andrew Vasqueze337d902009-04-06 22:33:49 -07007561 if (ql2xfwloadbin == 1)
7562 return qla81xx_load_risc(vha, srisc_addr);
7563
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007564 /*
7565 * FW Load priority:
7566 * 1) Firmware via request-firmware interface (.bin file).
7567 * 2) Firmware residing in flash.
7568 */
7569 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7570 if (rval == QLA_SUCCESS)
7571 return rval;
7572
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007573 return qla24xx_load_risc_flash(vha, srisc_addr,
7574 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007575}
7576
7577int
7578qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7579{
7580 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007581 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007582
Andrew Vasqueze337d902009-04-06 22:33:49 -07007583 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007584 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007585
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007586 /*
7587 * FW Load priority:
7588 * 1) Firmware residing in flash.
7589 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007590 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007591 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007592 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007593 if (rval == QLA_SUCCESS)
7594 return rval;
7595
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007596try_blob_fw:
7597 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7598 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7599 return rval;
7600
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007601 ql_log(ql_log_info, vha, 0x0099,
7602 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007603 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7604 if (rval != QLA_SUCCESS)
7605 return rval;
7606
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007607 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007608 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007609 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007610}
7611
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007612void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007613qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007614{
7615 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007616 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007617
Andrew Vasquez85880802009-12-15 21:29:46 -08007618 if (ha->flags.pci_channel_io_perm_failure)
7619 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007620 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007621 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007622 if (!ha->fw_major_version)
7623 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007624 if (!ha->flags.fw_started)
7625 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007626
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007627 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007628 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007629 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007630 ha->isp_ops->reset_chip(vha);
7631 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007632 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007633 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007634 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007635 ql_log(ql_log_info, vha, 0x8015,
7636 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007637 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007638 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007639
Quinn Tran4b60c822017-06-13 20:47:21 -07007640 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007641 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007642}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007643
7644int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007645qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007646{
7647 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007648 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007649 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007650 struct qla_hw_data *ha = vha->hw;
7651 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007652 struct req_que *req;
7653 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007654
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007655 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007656 return -EINVAL;
7657
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007658 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007659 if (vha->qpair)
7660 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007661 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007662 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007663 rsp = req->rsp;
7664
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007665 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007666 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007667 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007668 }
7669
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007670 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007671
7672 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007673 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7674 BIT_1);
7675 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7676 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7677 ql_dbg(ql_dbg_init, vha, 0x0120,
7678 "Failed SNS login: loop_id=%x, rval2=%d\n",
7679 NPH_SNS, rval2);
7680 else
7681 ql_dbg(ql_dbg_init, vha, 0x0103,
7682 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7683 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7684 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007685 return (QLA_FUNCTION_FAILED);
7686 }
7687
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007688 atomic_set(&vha->loop_down_timer, 0);
7689 atomic_set(&vha->loop_state, LOOP_UP);
7690 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7691 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7692 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007693
7694 return rval;
7695}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007696
7697/* 84XX Support **************************************************************/
7698
7699static LIST_HEAD(qla_cs84xx_list);
7700static DEFINE_MUTEX(qla_cs84xx_mutex);
7701
7702static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007703qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007704{
7705 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007706 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007707
7708 mutex_lock(&qla_cs84xx_mutex);
7709
7710 /* Find any shared 84xx chip. */
7711 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7712 if (cs84xx->bus == ha->pdev->bus) {
7713 kref_get(&cs84xx->kref);
7714 goto done;
7715 }
7716 }
7717
7718 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7719 if (!cs84xx)
7720 goto done;
7721
7722 kref_init(&cs84xx->kref);
7723 spin_lock_init(&cs84xx->access_lock);
7724 mutex_init(&cs84xx->fw_update_mutex);
7725 cs84xx->bus = ha->pdev->bus;
7726
7727 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7728done:
7729 mutex_unlock(&qla_cs84xx_mutex);
7730 return cs84xx;
7731}
7732
7733static void
7734__qla84xx_chip_release(struct kref *kref)
7735{
7736 struct qla_chip_state_84xx *cs84xx =
7737 container_of(kref, struct qla_chip_state_84xx, kref);
7738
7739 mutex_lock(&qla_cs84xx_mutex);
7740 list_del(&cs84xx->list);
7741 mutex_unlock(&qla_cs84xx_mutex);
7742 kfree(cs84xx);
7743}
7744
7745void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007746qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007747{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007748 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007749 if (ha->cs84xx)
7750 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7751}
7752
7753static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007754qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007755{
7756 int rval;
7757 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007758 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007759
7760 mutex_lock(&ha->cs84xx->fw_update_mutex);
7761
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007762 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007763
7764 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7765
7766 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7767 QLA_SUCCESS;
7768}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007769
7770/* 81XX Support **************************************************************/
7771
7772int
7773qla81xx_nvram_config(scsi_qla_host_t *vha)
7774{
7775 int rval;
7776 struct init_cb_81xx *icb;
7777 struct nvram_81xx *nv;
7778 uint32_t *dptr;
7779 uint8_t *dptr1, *dptr2;
7780 uint32_t chksum;
7781 uint16_t cnt;
7782 struct qla_hw_data *ha = vha->hw;
7783
7784 rval = QLA_SUCCESS;
7785 icb = (struct init_cb_81xx *)ha->init_cb;
7786 nv = ha->nvram;
7787
7788 /* Determine NVRAM starting address. */
7789 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007790 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007791 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7792 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007793
7794 /* Get VPD data into cache */
7795 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007796 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7797 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007798
7799 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007800 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007801 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007802 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007803 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7804 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007805
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007806 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7807 "Contents of NVRAM:\n");
7808 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7809 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007810
7811 /* Bad NVRAM data, set defaults parameters. */
7812 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7813 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007814 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007815 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007816 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007817 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007818 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007819 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007820 ql_log(ql_log_info, vha, 0x0074,
7821 "Falling back to functioning (yet invalid -- WWPN) "
7822 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007823
7824 /*
7825 * Set default initialization control block.
7826 */
7827 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007828 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7829 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007830 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007831 nv->execution_throttle = cpu_to_le16(0xFFFF);
7832 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007833 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007834 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007835 nv->port_name[2] = 0x00;
7836 nv->port_name[3] = 0xe0;
7837 nv->port_name[4] = 0x8b;
7838 nv->port_name[5] = 0x1c;
7839 nv->port_name[6] = 0x55;
7840 nv->port_name[7] = 0x86;
7841 nv->node_name[0] = 0x20;
7842 nv->node_name[1] = 0x00;
7843 nv->node_name[2] = 0x00;
7844 nv->node_name[3] = 0xe0;
7845 nv->node_name[4] = 0x8b;
7846 nv->node_name[5] = 0x1c;
7847 nv->node_name[6] = 0x55;
7848 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007849 nv->login_retry_count = cpu_to_le16(8);
7850 nv->interrupt_delay_timer = cpu_to_le16(0);
7851 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007852 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007853 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7854 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7855 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7856 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7857 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7858 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007859 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007860 nv->max_luns_per_target = cpu_to_le16(128);
7861 nv->port_down_retry_count = cpu_to_le16(30);
7862 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007863 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007864 nv->enode_mac[1] = 0xC0;
7865 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007866 nv->enode_mac[3] = 0x04;
7867 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007868 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007869
7870 rval = 1;
7871 }
7872
Arun Easi9e522cd2012-08-22 14:21:31 -04007873 if (IS_T10_PI_CAPABLE(ha))
7874 nv->frame_payload_size &= ~7;
7875
Arun Easiaa230bc2013-01-30 03:34:39 -05007876 qlt_81xx_config_nvram_stage1(vha, nv);
7877
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007878 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007879 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007880
7881 /* Copy 1st segment. */
7882 dptr1 = (uint8_t *)icb;
7883 dptr2 = (uint8_t *)&nv->version;
7884 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7885 while (cnt--)
7886 *dptr1++ = *dptr2++;
7887
7888 icb->login_retry_count = nv->login_retry_count;
7889
7890 /* Copy 2nd segment. */
7891 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7892 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7893 cnt = (uint8_t *)&icb->reserved_5 -
7894 (uint8_t *)&icb->interrupt_delay_timer;
7895 while (cnt--)
7896 *dptr1++ = *dptr2++;
7897
7898 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7899 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7900 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007901 icb->enode_mac[0] = 0x00;
7902 icb->enode_mac[1] = 0xC0;
7903 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007904 icb->enode_mac[3] = 0x04;
7905 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007906 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007907 }
7908
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007909 /* Use extended-initialization control block. */
7910 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7911
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007912 /*
7913 * Setup driver NVRAM options.
7914 */
7915 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007916 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007917
Arun Easiaa230bc2013-01-30 03:34:39 -05007918 qlt_81xx_config_nvram_stage2(vha, icb);
7919
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007920 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007921 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007922 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7923 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7924 }
7925
7926 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007927 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007928 /*
7929 * Firmware will apply the following mask if the nodename was
7930 * not provided.
7931 */
7932 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7933 icb->node_name[0] &= 0xF0;
7934 }
7935
7936 /* Set host adapter parameters. */
7937 ha->flags.disable_risc_code_load = 0;
7938 ha->flags.enable_lip_reset = 0;
7939 ha->flags.enable_lip_full_login =
7940 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7941 ha->flags.enable_target_reset =
7942 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7943 ha->flags.enable_led_scheme = 0;
7944 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7945
7946 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7947 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7948
7949 /* save HBA serial number */
7950 ha->serial0 = icb->port_name[5];
7951 ha->serial1 = icb->port_name[6];
7952 ha->serial2 = icb->port_name[7];
7953 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7954 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7955
Bart Van Asschead950362015-07-09 07:24:08 -07007956 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007957
7958 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7959
7960 /* Set minimum login_timeout to 4 seconds. */
7961 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7962 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7963 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007964 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007965 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007966
7967 /* Set minimum RATOV to 100 tenths of a second. */
7968 ha->r_a_tov = 100;
7969
7970 ha->loop_reset_delay = nv->reset_delay;
7971
7972 /* Link Down Timeout = 0:
7973 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007974 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007975 * I/O's to OS with "DID_NO_CONNECT".
7976 *
7977 * Link Down Timeout != 0:
7978 *
7979 * The driver waits for the link to come up after link down
7980 * before returning I/Os to OS with "DID_NO_CONNECT".
7981 */
7982 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7983 ha->loop_down_abort_time =
7984 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7985 } else {
7986 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7987 ha->loop_down_abort_time =
7988 (LOOP_DOWN_TIME - ha->link_down_timeout);
7989 }
7990
7991 /* Need enough time to try and get the port back. */
7992 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7993 if (qlport_down_retry)
7994 ha->port_down_retry_count = qlport_down_retry;
7995
7996 /* Set login_retry_count */
7997 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7998 if (ha->port_down_retry_count ==
7999 le16_to_cpu(nv->port_down_retry_count) &&
8000 ha->port_down_retry_count > 3)
8001 ha->login_retry_count = ha->port_down_retry_count;
8002 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
8003 ha->login_retry_count = ha->port_down_retry_count;
8004 if (ql2xloginretrycount)
8005 ha->login_retry_count = ql2xloginretrycount;
8006
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08008007 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05008008 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07008009 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08008010
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008011 /* Enable ZIO. */
8012 if (!vha->flags.init_done) {
8013 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
8014 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
8015 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
8016 le16_to_cpu(icb->interrupt_delay_timer): 2;
8017 }
Bart Van Asschead950362015-07-09 07:24:08 -07008018 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008019 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
8020 vha->flags.process_response_queue = 0;
8021 if (ha->zio_mode != QLA_ZIO_DISABLED) {
8022 ha->zio_mode = QLA_ZIO_MODE_6;
8023
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008024 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008025 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008026 ha->zio_mode,
8027 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008028
8029 icb->firmware_options_2 |= cpu_to_le32(
8030 (uint32_t)ha->zio_mode);
8031 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8032 vha->flags.process_response_queue = 1;
8033 }
8034
Quinn Tran41dc5292017-01-19 22:28:03 -08008035 /* enable RIDA Format2 */
8036 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
8037 icb->firmware_options_3 |= BIT_0;
8038
Duane Grigsbyedd05de2017-10-13 09:34:06 -07008039 if (IS_QLA27XX(ha)) {
8040 icb->firmware_options_3 |= BIT_8;
8041 ql_dbg(ql_log_info, vha, 0x0075,
8042 "Enabling direct connection.\n");
8043 }
8044
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008045 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008046 ql_log(ql_log_warn, vha, 0x0076,
8047 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008048 }
8049 return (rval);
8050}
8051
Giridhar Malavalia9083012010-04-12 17:59:55 -07008052int
8053qla82xx_restart_isp(scsi_qla_host_t *vha)
8054{
8055 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008056 struct qla_hw_data *ha = vha->hw;
8057 struct req_que *req = ha->req_q_map[0];
8058 struct rsp_que *rsp = ha->rsp_q_map[0];
8059 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008060 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008061
8062 status = qla2x00_init_rings(vha);
8063 if (!status) {
8064 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8065 ha->flags.chip_reset_done = 1;
8066
8067 status = qla2x00_fw_ready(vha);
8068 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008069 /* Issue a marker after FW becomes ready. */
8070 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008071 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008072 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008073 }
8074
8075 /* if no cable then assume it's good */
8076 if ((vha->device_flags & DFLG_NO_CABLE))
8077 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008078 }
8079
8080 if (!status) {
8081 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8082
8083 if (!atomic_read(&vha->loop_down_timer)) {
8084 /*
8085 * Issue marker command only when we are going
8086 * to start the I/O .
8087 */
8088 vha->marker_needed = 1;
8089 }
8090
Giridhar Malavalia9083012010-04-12 17:59:55 -07008091 ha->isp_ops->enable_intrs(ha);
8092
8093 ha->isp_abort_cnt = 0;
8094 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8095
Saurav Kashyap53296782011-05-10 11:30:06 -07008096 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008097 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008098
Giridhar Malavalia9083012010-04-12 17:59:55 -07008099 if (ha->fce) {
8100 ha->flags.fce_enabled = 1;
8101 memset(ha->fce, 0,
8102 fce_calc_size(ha->fce_bufs));
8103 rval = qla2x00_enable_fce_trace(vha,
8104 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8105 &ha->fce_bufs);
8106 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008107 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008108 "Unable to reinitialize FCE (%d).\n",
8109 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008110 ha->flags.fce_enabled = 0;
8111 }
8112 }
8113
8114 if (ha->eft) {
8115 memset(ha->eft, 0, EFT_SIZE);
8116 rval = qla2x00_enable_eft_trace(vha,
8117 ha->eft_dma, EFT_NUM_BUFFERS);
8118 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008119 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008120 "Unable to reinitialize EFT (%d).\n",
8121 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008122 }
8123 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008124 }
8125
8126 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008127 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008128 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008129
8130 spin_lock_irqsave(&ha->vport_slock, flags);
8131 list_for_each_entry(vp, &ha->vp_list, list) {
8132 if (vp->vp_idx) {
8133 atomic_inc(&vp->vref_count);
8134 spin_unlock_irqrestore(&ha->vport_slock, flags);
8135
Giridhar Malavalia9083012010-04-12 17:59:55 -07008136 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008137
8138 spin_lock_irqsave(&ha->vport_slock, flags);
8139 atomic_dec(&vp->vref_count);
8140 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008141 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008142 spin_unlock_irqrestore(&ha->vport_slock, flags);
8143
Giridhar Malavalia9083012010-04-12 17:59:55 -07008144 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008145 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008146 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008147 }
8148
8149 return status;
8150}
8151
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008152void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008153qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008154{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008155 struct qla_hw_data *ha = vha->hw;
8156
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008157 /* Hold status IOCBs until ABTS response received. */
8158 if (ql2xfwholdabts)
8159 ha->fw_options[3] |= BIT_12;
8160
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008161 /* Set Retry FLOGI in case of P2P connection */
8162 if (ha->operating_mode == P2P) {
8163 ha->fw_options[2] |= BIT_3;
8164 ql_dbg(ql_dbg_disc, vha, 0x2103,
8165 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8166 __func__, ha->fw_options[2]);
8167 }
8168
Quinn Tran41dc5292017-01-19 22:28:03 -08008169 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8170 if (ql2xmvasynctoatio) {
8171 if (qla_tgt_mode_enabled(vha) ||
8172 qla_dual_mode_enabled(vha))
8173 ha->fw_options[2] |= BIT_11;
8174 else
8175 ha->fw_options[2] &= ~BIT_11;
8176 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008177
Quinn Tranf7e761f2017-06-02 09:12:02 -07008178 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008179 qla_dual_mode_enabled(vha)) {
8180 /* FW auto send SCSI status during */
8181 ha->fw_options[1] |= BIT_8;
8182 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8183
8184 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008185 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008186 } else {
8187 ha->fw_options[1] &= ~BIT_8;
8188 ha->fw_options[10] &= 0x00ff;
8189
Quinn Tranf7e761f2017-06-02 09:12:02 -07008190 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008191 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008192
Quinn Tran41dc5292017-01-19 22:28:03 -08008193 if (ql2xetsenable) {
8194 /* Enable ETS Burst. */
8195 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8196 ha->fw_options[2] |= BIT_9;
8197 }
8198
Quinn Tran83548fe2017-06-02 09:12:01 -07008199 ql_dbg(ql_dbg_init, vha, 0x00e9,
8200 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8201 __func__, ha->fw_options[1], ha->fw_options[2],
8202 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008203
Andrew Vasquezae97c912010-02-18 10:07:28 -08008204 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008205}
Sarang Radke09ff7012010-03-19 17:03:59 -07008206
8207/*
8208 * qla24xx_get_fcp_prio
8209 * Gets the fcp cmd priority value for the logged in port.
8210 * Looks for a match of the port descriptors within
8211 * each of the fcp prio config entries. If a match is found,
8212 * the tag (priority) value is returned.
8213 *
8214 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008215 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008216 * fcport = port structure pointer.
8217 *
8218 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008219 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008220 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008221 *
8222 * Context:
8223 * Kernel context
8224 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008225static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008226qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8227{
8228 int i, entries;
8229 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008230 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008231 uint32_t pid1, pid2;
8232 uint64_t wwn1, wwn2;
8233 struct qla_fcp_prio_entry *pri_entry;
8234 struct qla_hw_data *ha = vha->hw;
8235
8236 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008237 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008238
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008239 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008240 entries = ha->fcp_prio_cfg->num_entries;
8241 pri_entry = &ha->fcp_prio_cfg->entry[0];
8242
8243 for (i = 0; i < entries; i++) {
8244 pid_match = wwn_match = 0;
8245
8246 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8247 pri_entry++;
8248 continue;
8249 }
8250
8251 /* check source pid for a match */
8252 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8253 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8254 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8255 if (pid1 == INVALID_PORT_ID)
8256 pid_match++;
8257 else if (pid1 == pid2)
8258 pid_match++;
8259 }
8260
8261 /* check destination pid for a match */
8262 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8263 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8264 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8265 if (pid1 == INVALID_PORT_ID)
8266 pid_match++;
8267 else if (pid1 == pid2)
8268 pid_match++;
8269 }
8270
8271 /* check source WWN for a match */
8272 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8273 wwn1 = wwn_to_u64(vha->port_name);
8274 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8275 if (wwn2 == (uint64_t)-1)
8276 wwn_match++;
8277 else if (wwn1 == wwn2)
8278 wwn_match++;
8279 }
8280
8281 /* check destination WWN for a match */
8282 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8283 wwn1 = wwn_to_u64(fcport->port_name);
8284 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8285 if (wwn2 == (uint64_t)-1)
8286 wwn_match++;
8287 else if (wwn1 == wwn2)
8288 wwn_match++;
8289 }
8290
8291 if (pid_match == 2 || wwn_match == 2) {
8292 /* Found a matching entry */
8293 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8294 priority = pri_entry->tag;
8295 break;
8296 }
8297
8298 pri_entry++;
8299 }
8300
8301 return priority;
8302}
8303
8304/*
8305 * qla24xx_update_fcport_fcp_prio
8306 * Activates fcp priority for the logged in fc port
8307 *
8308 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008309 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008310 * fcp = port structure pointer.
8311 *
8312 * Return:
8313 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8314 *
8315 * Context:
8316 * Kernel context.
8317 */
8318int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008319qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008320{
8321 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008322 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008323 uint16_t mb[5];
8324
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008325 if (fcport->port_type != FCT_TARGET ||
8326 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008327 return QLA_FUNCTION_FAILED;
8328
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008329 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008330 if (priority < 0)
8331 return QLA_FUNCTION_FAILED;
8332
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008333 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008334 fcport->fcp_prio = priority & 0xf;
8335 return QLA_SUCCESS;
8336 }
8337
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008338 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008339 if (ret == QLA_SUCCESS) {
8340 if (fcport->fcp_prio != priority)
8341 ql_dbg(ql_dbg_user, vha, 0x709e,
8342 "Updated FCP_CMND priority - value=%d loop_id=%d "
8343 "port_id=%02x%02x%02x.\n", priority,
8344 fcport->loop_id, fcport->d_id.b.domain,
8345 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008346 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008347 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008348 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008349 "Unable to update FCP_CMND priority - ret=0x%x for "
8350 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8351 fcport->d_id.b.domain, fcport->d_id.b.area,
8352 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008353 return ret;
8354}
8355
8356/*
8357 * qla24xx_update_all_fcp_prio
8358 * Activates fcp priority for all the logged in ports
8359 *
8360 * Input:
8361 * ha = adapter block pointer.
8362 *
8363 * Return:
8364 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8365 *
8366 * Context:
8367 * Kernel context.
8368 */
8369int
8370qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8371{
8372 int ret;
8373 fc_port_t *fcport;
8374
8375 ret = QLA_FUNCTION_FAILED;
8376 /* We need to set priority for all logged in ports */
8377 list_for_each_entry(fcport, &vha->vp_fcports, list)
8378 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8379
8380 return ret;
8381}
Michael Hernandezd7459522016-12-12 14:40:07 -08008382
Quinn Tran82de8022017-06-13 20:47:17 -07008383struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8384 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008385{
8386 int rsp_id = 0;
8387 int req_id = 0;
8388 int i;
8389 struct qla_hw_data *ha = vha->hw;
8390 uint16_t qpair_id = 0;
8391 struct qla_qpair *qpair = NULL;
8392 struct qla_msix_entry *msix;
8393
8394 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8395 ql_log(ql_log_warn, vha, 0x00181,
8396 "FW/Driver is not multi-queue capable.\n");
8397 return NULL;
8398 }
8399
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008400 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008401 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8402 if (qpair == NULL) {
8403 ql_log(ql_log_warn, vha, 0x0182,
8404 "Failed to allocate memory for queue pair.\n");
8405 return NULL;
8406 }
8407 memset(qpair, 0, sizeof(struct qla_qpair));
8408
8409 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008410 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008411 qpair->qp_lock_ptr = &qpair->qp_lock;
8412 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008413 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008414
8415 /* Assign available que pair id */
8416 mutex_lock(&ha->mq_lock);
8417 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008418 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008419 mutex_unlock(&ha->mq_lock);
8420 ql_log(ql_log_warn, vha, 0x0183,
8421 "No resources to create additional q pair.\n");
8422 goto fail_qid_map;
8423 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008424 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008425 set_bit(qpair_id, ha->qpair_qid_map);
8426 ha->queue_pair_map[qpair_id] = qpair;
8427 qpair->id = qpair_id;
8428 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008429 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008430 INIT_LIST_HEAD(&qpair->hints_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008431 qpair->chip_reset = ha->base_qpair->chip_reset;
8432 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8433 qpair->enable_explicit_conf =
8434 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008435
8436 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008437 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008438 if (msix->in_use)
8439 continue;
8440 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008441 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008442 "Vector %x selected for qpair\n", msix->vector);
8443 break;
8444 }
8445 if (!qpair->msix) {
8446 ql_log(ql_log_warn, vha, 0x0184,
8447 "Out of MSI-X vectors!.\n");
8448 goto fail_msix;
8449 }
8450
8451 qpair->msix->in_use = 1;
8452 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008453 qpair->pdev = ha->pdev;
8454 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8455 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008456
8457 mutex_unlock(&ha->mq_lock);
8458
8459 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008460 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008461 if (!rsp_id) {
8462 ql_log(ql_log_warn, vha, 0x0185,
8463 "Failed to create response queue.\n");
8464 goto fail_rsp;
8465 }
8466
8467 qpair->rsp = ha->rsp_q_map[rsp_id];
8468
8469 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008470 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8471 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008472 if (!req_id) {
8473 ql_log(ql_log_warn, vha, 0x0186,
8474 "Failed to create request queue.\n");
8475 goto fail_req;
8476 }
8477
8478 qpair->req = ha->req_q_map[req_id];
8479 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008480 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008481 /* init qpair to this cpu. Will adjust at run time. */
8482 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008483
8484 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8485 if (ha->fw_attributes & BIT_4)
8486 qpair->difdix_supported = 1;
8487 }
8488
8489 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8490 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008491 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008492 "Failed to create srb mempool for qpair %d\n",
8493 qpair->id);
8494 goto fail_mempool;
8495 }
8496
8497 /* Mark as online */
8498 qpair->online = 1;
8499
8500 if (!vha->flags.qpairs_available)
8501 vha->flags.qpairs_available = 1;
8502
8503 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8504 "Request/Response queue pair created, id %d\n",
8505 qpair->id);
8506 ql_dbg(ql_dbg_init, vha, 0x0187,
8507 "Request/Response queue pair created, id %d\n",
8508 qpair->id);
8509 }
8510 return qpair;
8511
8512fail_mempool:
8513fail_req:
8514 qla25xx_delete_rsp_que(vha, qpair->rsp);
8515fail_rsp:
8516 mutex_lock(&ha->mq_lock);
8517 qpair->msix->in_use = 0;
8518 list_del(&qpair->qp_list_elem);
8519 if (list_empty(&vha->qp_list))
8520 vha->flags.qpairs_available = 0;
8521fail_msix:
8522 ha->queue_pair_map[qpair_id] = NULL;
8523 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008524 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008525 mutex_unlock(&ha->mq_lock);
8526fail_qid_map:
8527 kfree(qpair);
8528 return NULL;
8529}
8530
8531int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8532{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008533 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008534 struct qla_hw_data *ha = qpair->hw;
8535
8536 qpair->delete_in_progress = 1;
8537 while (atomic_read(&qpair->ref_count))
8538 msleep(500);
8539
8540 ret = qla25xx_delete_req_que(vha, qpair->req);
8541 if (ret != QLA_SUCCESS)
8542 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008543
Michael Hernandezd7459522016-12-12 14:40:07 -08008544 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8545 if (ret != QLA_SUCCESS)
8546 goto fail;
8547
8548 mutex_lock(&ha->mq_lock);
8549 ha->queue_pair_map[qpair->id] = NULL;
8550 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008551 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008552 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008553 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008554 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008555 vha->flags.qpairs_req_created = 0;
8556 vha->flags.qpairs_rsp_created = 0;
8557 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008558 mempool_destroy(qpair->srb_mempool);
8559 kfree(qpair);
8560 mutex_unlock(&ha->mq_lock);
8561
8562 return QLA_SUCCESS;
8563fail:
8564 return ret;
8565}