blob: cacf2ccc081bc7f8c59d238b05e5c004c09efd2e [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 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
187
188 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700189 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800190 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700191 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700192
193 if (fcport->fc4f_nvme)
194 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
195
Andrew Vasquezac280b62009-08-20 11:06:05 -0700196 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700197 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700198 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500199 if (rval != QLA_SUCCESS) {
Chad Dupuis080c9512016-01-27 12:03:37 -0500200 fcport->flags |= FCF_LOGIN_NEEDED;
201 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700202 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500203 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700204
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700205 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800206 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
207 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800208 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
209 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700210 return rval;
211
212done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800213 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800214 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800215done:
Andrew Vasquezac280b62009-08-20 11:06:05 -0700216 return rval;
217}
218
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700219static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800220qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700221{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800222 srb_t *sp = ptr;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700223
Quinn Tran6d6749272017-12-28 12:33:41 -0800224 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Traneaf75d12018-02-01 10:33:17 -0800225 sp->fcport->login_gen++;
226 qlt_logo_completion_handler(sp->fcport, res);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800227 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700228}
229
Andrew Vasquezac280b62009-08-20 11:06:05 -0700230int
231qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
232{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700233 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700234 struct srb_iocb *lio;
Quinn Tran3dbec592017-12-28 12:33:40 -0800235 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700236
Quinn Tran3dbec592017-12-28 12:33:40 -0800237 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
238 return rval;
239
Quinn Tran726b8542017-01-19 22:28:00 -0800240 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800241 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700242 if (!sp)
243 goto done;
244
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800245 sp->type = SRB_LOGOUT_CMD;
246 sp->name = "logout";
247 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
248
249 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700250 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800251 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700252 rval = qla2x00_start_sp(sp);
253 if (rval != QLA_SUCCESS)
254 goto done_free_sp;
255
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700256 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800257 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800258 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800259 fcport->d_id.b.area, fcport->d_id.b.al_pa,
260 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700261 return rval;
262
263done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800264 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700265done:
Quinn Tran726b8542017-01-19 22:28:00 -0800266 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700267 return rval;
268}
Quinn Tran11aea162017-12-28 12:33:20 -0800269
270void
271qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
272 uint16_t *data)
273{
274 /* Don't re-login in target mode */
275 if (!fcport->tgt_session)
276 qla2x00_mark_device_lost(vha, fcport, 1, 0);
277 qlt_logo_completion_handler(fcport, data[0]);
278}
279
280static void
281qla2x00_async_prlo_sp_done(void *s, int res)
282{
283 srb_t *sp = (srb_t *)s;
284 struct srb_iocb *lio = &sp->u.iocb_cmd;
285 struct scsi_qla_host *vha = sp->vha;
286
287 if (!test_bit(UNLOADING, &vha->dpc_flags))
288 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
289 lio->u.logio.data);
290 sp->free(sp);
291}
292
293int
294qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
295{
296 srb_t *sp;
297 struct srb_iocb *lio;
298 int rval;
299
300 rval = QLA_FUNCTION_FAILED;
301 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
302 if (!sp)
303 goto done;
304
305 sp->type = SRB_PRLO_CMD;
306 sp->name = "prlo";
307 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
308
309 lio = &sp->u.iocb_cmd;
310 lio->timeout = qla2x00_async_iocb_timeout;
311 sp->done = qla2x00_async_prlo_sp_done;
312 rval = qla2x00_start_sp(sp);
313 if (rval != QLA_SUCCESS)
314 goto done_free_sp;
315
316 ql_dbg(ql_dbg_disc, vha, 0x2070,
317 "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
318 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
319 fcport->d_id.b.area, fcport->d_id.b.al_pa);
320 return rval;
321
322done_free_sp:
323 sp->free(sp);
324done:
325 return rval;
326}
327
Quinn Tranf13515a2017-12-28 12:33:15 -0800328static
329void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
330{
Quinn Tran0616e962017-12-28 12:33:34 -0800331 struct fc_port *fcport = ea->fcport;
332
333 ql_dbg(ql_dbg_disc, vha, 0x20d2,
334 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
335 __func__, fcport->port_name, fcport->disc_state,
336 fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
337 fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
338
339 if (ea->data[0] != MBS_COMMAND_COMPLETE) {
Quinn Trana4239942017-12-28 12:33:26 -0800340 ql_dbg(ql_dbg_disc, vha, 0x2066,
341 "%s %8phC: adisc fail: post delete\n",
342 __func__, ea->fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800343 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana4239942017-12-28 12:33:26 -0800344 return;
345 }
Quinn Trana4239942017-12-28 12:33:26 -0800346
347 if (ea->fcport->disc_state == DSC_DELETE_PEND)
348 return;
349
350 if (ea->sp->gen2 != ea->fcport->login_gen) {
351 /* target side must have changed it. */
352 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran0616e962017-12-28 12:33:34 -0800353 "%s %8phC generation changed\n",
354 __func__, ea->fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800355 return;
356 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
357 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
358 __func__, __LINE__, ea->fcport->port_name);
359 qla24xx_post_gidpn_work(vha, ea->fcport);
360 return;
361 }
362
363 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800364}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700365
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700366static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800367qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700368{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800369 srb_t *sp = ptr;
370 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800371 struct event_arg ea;
Quinn Tran0616e962017-12-28 12:33:34 -0800372 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700373
Quinn Tranf13515a2017-12-28 12:33:15 -0800374 ql_dbg(ql_dbg_disc, vha, 0x2066,
375 "Async done-%s res %x %8phC\n",
376 sp->name, res, sp->fcport->port_name);
377
378 memset(&ea, 0, sizeof(ea));
379 ea.event = FCME_ADISC_DONE;
380 ea.rc = res;
Quinn Tran0616e962017-12-28 12:33:34 -0800381 ea.data[0] = lio->u.logio.data[0];
382 ea.data[1] = lio->u.logio.data[1];
383 ea.iop[0] = lio->u.logio.iop[0];
384 ea.iop[1] = lio->u.logio.iop[1];
Quinn Tranf13515a2017-12-28 12:33:15 -0800385 ea.fcport = sp->fcport;
386 ea.sp = sp;
387
388 qla2x00_fcport_event_handler(vha, &ea);
389
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800390 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700391}
392
393int
394qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
395 uint16_t *data)
396{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700397 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700398 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700399 int rval;
400
401 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800402 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800403 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700404 if (!sp)
405 goto done;
406
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800407 sp->type = SRB_ADISC_CMD;
408 sp->name = "adisc";
409 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
410
411 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700412 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800413 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700414 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700415 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700416 rval = qla2x00_start_sp(sp);
417 if (rval != QLA_SUCCESS)
418 goto done_free_sp;
419
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700420 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800421 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
422 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700423 return rval;
424
425done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800426 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700427done:
Quinn Tran726b8542017-01-19 22:28:00 -0800428 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tranf13515a2017-12-28 12:33:15 -0800429 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700430 return rval;
431}
432
Quinn Tran726b8542017-01-19 22:28:00 -0800433static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
434 struct event_arg *ea)
435{
436 fc_port_t *fcport, *conflict_fcport;
437 struct get_name_list_extended *e;
438 u16 i, n, found = 0, loop_id;
439 port_id_t id;
440 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800441 u16 data[2];
442 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800443
444 fcport = ea->fcport;
Quinn Tranf352eeb2017-12-28 12:33:35 -0800445 ql_dbg(ql_dbg_disc, vha, 0xffff,
446 "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
447 __func__, fcport->port_name, fcport->disc_state,
448 fcport->fw_login_state, ea->rc,
449 fcport->login_gen, fcport->last_login_gen,
450 fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800451
Quinn Trana4239942017-12-28 12:33:26 -0800452 if (fcport->disc_state == DSC_DELETE_PEND)
453 return;
454
Quinn Tran726b8542017-01-19 22:28:00 -0800455 if (ea->rc) { /* rval */
456 if (fcport->login_retry == 0) {
457 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700458 ql_dbg(ql_dbg_disc, vha, 0x20de,
459 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
460 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800461 }
462 return;
463 }
464
465 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700466 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800467 "%s %8phC rscn gen changed rscn %d|%d \n",
468 __func__, fcport->port_name,
469 fcport->last_rscn_gen, fcport->rscn_gen);
470 qla24xx_post_gidpn_work(vha, fcport);
471 return;
472 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700473 ql_dbg(ql_dbg_disc, vha, 0x20e0,
Quinn Tranf352eeb2017-12-28 12:33:35 -0800474 "%s %8phC login gen changed\n",
475 __func__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800476 return;
477 }
478
479 n = ea->data[0] / sizeof(struct get_name_list_extended);
480
Quinn Tran83548fe2017-06-02 09:12:01 -0700481 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800482 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
483 __func__, __LINE__, fcport->port_name, n,
484 fcport->d_id.b.domain, fcport->d_id.b.area,
485 fcport->d_id.b.al_pa, fcport->loop_id);
486
487 for (i = 0; i < n; i++) {
488 e = &vha->gnl.l[i];
489 wwn = wwn_to_u64(e->port_name);
490
491 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
492 continue;
493
494 found = 1;
495 id.b.domain = e->port_id[2];
496 id.b.area = e->port_id[1];
497 id.b.al_pa = e->port_id[0];
498 id.b.rsvd_1 = 0;
499
500 loop_id = le16_to_cpu(e->nport_handle);
501 loop_id = (loop_id & 0x7fff);
502
Quinn Tran83548fe2017-06-02 09:12:01 -0700503 ql_dbg(ql_dbg_disc, vha, 0x20e2,
504 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
505 __func__, fcport->port_name,
506 e->current_login_state, fcport->fw_login_state,
507 id.b.domain, id.b.area, id.b.al_pa,
508 fcport->d_id.b.domain, fcport->d_id.b.area,
509 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800510
511 if ((id.b24 != fcport->d_id.b24) ||
512 ((fcport->loop_id != FC_NO_LOOP_ID) &&
513 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700514 ql_dbg(ql_dbg_disc, vha, 0x20e3,
515 "%s %d %8phC post del sess\n",
516 __func__, __LINE__, fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800517 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800518 return;
519 }
520
521 fcport->loop_id = loop_id;
522
523 wwn = wwn_to_u64(fcport->port_name);
524 qlt_find_sess_invalidate_other(vha, wwn,
525 id, loop_id, &conflict_fcport);
526
527 if (conflict_fcport) {
528 /*
529 * Another share fcport share the same loop_id &
530 * nport id. Conflict fcport needs to finish
531 * cleanup before this fcport can proceed to login.
532 */
533 conflict_fcport->conflict = fcport;
534 fcport->login_pause = 1;
535 }
536
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700537 if (fcport->fc4f_nvme)
538 current_login_state = e->current_login_state >> 4;
539 else
540 current_login_state = e->current_login_state & 0xf;
541
542 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800543 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700544 ql_dbg(ql_dbg_disc, vha, 0x20e4,
545 "%s %d %8phC post gpdb\n",
546 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800547
548 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
549 fcport->port_type = FCT_INITIATOR;
550 else
551 fcport->port_type = FCT_TARGET;
552
553 data[0] = data[1] = 0;
554 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800555 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800556 case DSC_LS_PORT_UNAVAIL:
557 default:
558 if (fcport->loop_id == FC_NO_LOOP_ID) {
559 qla2x00_find_new_loop_id(vha, fcport);
560 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
561 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700562 ql_dbg(ql_dbg_disc, vha, 0x20e5,
563 "%s %d %8phC\n",
564 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800565 qla24xx_fcport_handle_login(vha, fcport);
566 break;
567 }
568 }
569
570 if (!found) {
571 /* fw has no record of this port */
Quinn Tran040036b2017-12-28 12:33:38 -0800572 for (i = 0; i < n; i++) {
573 e = &vha->gnl.l[i];
574 id.b.domain = e->port_id[0];
575 id.b.area = e->port_id[1];
576 id.b.al_pa = e->port_id[2];
577 id.b.rsvd_1 = 0;
578 loop_id = le16_to_cpu(e->nport_handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800579
Quinn Tran040036b2017-12-28 12:33:38 -0800580 if (fcport->d_id.b24 == id.b24) {
581 conflict_fcport =
582 qla2x00_find_fcport_by_wwpn(vha,
583 e->port_name, 0);
584 ql_dbg(ql_dbg_disc, vha, 0x20e6,
585 "%s %d %8phC post del sess\n",
586 __func__, __LINE__,
587 conflict_fcport->port_name);
588 qlt_schedule_sess_for_deletion
Quinn Tran94cff6e2017-12-28 12:33:42 -0800589 (conflict_fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800590 }
Quinn Tran040036b2017-12-28 12:33:38 -0800591
592 /* FW already picked this loop id for another fcport */
593 if (fcport->loop_id == loop_id)
594 fcport->loop_id = FC_NO_LOOP_ID;
Quinn Tran726b8542017-01-19 22:28:00 -0800595 }
596 qla24xx_fcport_handle_login(vha, fcport);
597 }
598} /* gnl_event */
599
600static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800601qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800602{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800603 struct srb *sp = s;
604 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800605 unsigned long flags;
606 struct fc_port *fcport = NULL, *tf;
607 u16 i, n = 0, loop_id;
608 struct event_arg ea;
609 struct get_name_list_extended *e;
610 u64 wwn;
611 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800612 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800613
Quinn Tran83548fe2017-06-02 09:12:01 -0700614 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800615 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
616 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
617 sp->u.iocb_cmd.u.mbx.in_mb[2]);
618
619 memset(&ea, 0, sizeof(ea));
620 ea.sp = sp;
621 ea.rc = res;
622 ea.event = FCME_GNL_DONE;
623
624 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
625 sizeof(struct get_name_list_extended)) {
626 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
627 sizeof(struct get_name_list_extended);
628 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
629 }
630
631 for (i = 0; i < n; i++) {
632 e = &vha->gnl.l[i];
633 loop_id = le16_to_cpu(e->nport_handle);
634 /* mask out reserve bit */
635 loop_id = (loop_id & 0x7fff);
636 set_bit(loop_id, vha->hw->loop_id_map);
637 wwn = wwn_to_u64(e->port_name);
638
Quinn Tran83548fe2017-06-02 09:12:01 -0700639 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800640 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
641 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
642 e->port_id[0], e->current_login_state, e->last_login_state,
643 (loop_id & 0x7fff));
644 }
645
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100646 spin_lock_irqsave(&vha->gnl.fcports_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800647
648 INIT_LIST_HEAD(&h);
649 fcport = tf = NULL;
650 if (!list_empty(&vha->gnl.fcports))
651 list_splice_init(&vha->gnl.fcports, &h);
652
653 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
654 list_del_init(&fcport->gnl_entry);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100655 spin_lock(&vha->hw->tgt.sess_lock);
Quinn Tran6d6749272017-12-28 12:33:41 -0800656 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100657 spin_unlock(&vha->hw->tgt.sess_lock);
Quinn Tran726b8542017-01-19 22:28:00 -0800658 ea.fcport = fcport;
659
660 qla2x00_fcport_event_handler(vha, &ea);
661 }
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100662 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800663
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100664 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -0800665 /* create new fcport if fw has knowledge of new sessions */
666 for (i = 0; i < n; i++) {
667 port_id_t id;
668 u64 wwnn;
669
670 e = &vha->gnl.l[i];
671 wwn = wwn_to_u64(e->port_name);
672
673 found = false;
674 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
675 if (!memcmp((u8 *)&wwn, fcport->port_name,
676 WWN_SIZE)) {
677 found = true;
678 break;
679 }
680 }
681
Quinn Trancf055fb2017-12-28 12:33:33 -0800682 id.b.domain = e->port_id[2];
Quinn Trana4239942017-12-28 12:33:26 -0800683 id.b.area = e->port_id[1];
Quinn Trancf055fb2017-12-28 12:33:33 -0800684 id.b.al_pa = e->port_id[0];
Quinn Trana4239942017-12-28 12:33:26 -0800685 id.b.rsvd_1 = 0;
686
687 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
688 ql_dbg(ql_dbg_disc, vha, 0x2065,
Quinn Trancf055fb2017-12-28 12:33:33 -0800689 "%s %d %8phC %06x post new sess\n",
690 __func__, __LINE__, (u8 *)&wwn, id.b24);
Quinn Trana4239942017-12-28 12:33:26 -0800691 wwnn = wwn_to_u64(e->node_name);
692 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
693 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
694 }
695 }
696
Quinn Tran726b8542017-01-19 22:28:00 -0800697 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
698
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800699 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800700}
701
702int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
703{
704 srb_t *sp;
705 struct srb_iocb *mbx;
706 int rval = QLA_FUNCTION_FAILED;
707 unsigned long flags;
708 u16 *mb;
709
Quinn Tran3dbec592017-12-28 12:33:40 -0800710 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
711 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800712
Quinn Tran83548fe2017-06-02 09:12:01 -0700713 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800714 "Async-gnlist WWPN %8phC \n", fcport->port_name);
715
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100716 spin_lock_irqsave(&vha->gnl.fcports_lock, flags);
717 if (!list_empty(&fcport->gnl_entry)) {
718 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
719 rval = QLA_SUCCESS;
720 goto done;
721 }
722
723 spin_lock(&vha->hw->tgt.sess_lock);
Quinn Tran726b8542017-01-19 22:28:00 -0800724 fcport->disc_state = DSC_GNL;
725 fcport->last_rscn_gen = fcport->rscn_gen;
726 fcport->last_login_gen = fcport->login_gen;
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100727 spin_unlock(&vha->hw->tgt.sess_lock);
Quinn Tran726b8542017-01-19 22:28:00 -0800728
729 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
Hannes Reinecke1c6cacf2018-02-22 09:49:35 +0100730 spin_unlock_irqrestore(&vha->gnl.fcports_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800731
732 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
733 if (!sp)
734 goto done;
Quinn Tran3dbec592017-12-28 12:33:40 -0800735
736 fcport->flags |= FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800737 sp->type = SRB_MB_IOCB;
738 sp->name = "gnlist";
739 sp->gen1 = fcport->rscn_gen;
740 sp->gen2 = fcport->login_gen;
741
742 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
743
744 mb = sp->u.iocb_cmd.u.mbx.out_mb;
745 mb[0] = MBC_PORT_NODE_NAME_LIST;
746 mb[1] = BIT_2 | BIT_3;
747 mb[2] = MSW(vha->gnl.ldma);
748 mb[3] = LSW(vha->gnl.ldma);
749 mb[6] = MSW(MSD(vha->gnl.ldma));
750 mb[7] = LSW(MSD(vha->gnl.ldma));
751 mb[8] = vha->gnl.size;
752 mb[9] = vha->vp_idx;
753
754 mbx = &sp->u.iocb_cmd;
755 mbx->timeout = qla2x00_async_iocb_timeout;
756
757 sp->done = qla24xx_async_gnl_sp_done;
758
759 rval = qla2x00_start_sp(sp);
760 if (rval != QLA_SUCCESS)
761 goto done_free_sp;
762
Quinn Tran83548fe2017-06-02 09:12:01 -0700763 ql_dbg(ql_dbg_disc, vha, 0x20da,
764 "Async-%s - OUT WWPN %8phC hndl %x\n",
765 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800766
767 return rval;
768
769done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800770 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800771 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800772done:
Quinn Tran726b8542017-01-19 22:28:00 -0800773 return rval;
774}
775
776int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
777{
778 struct qla_work_evt *e;
779
780 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
781 if (!e)
782 return QLA_FUNCTION_FAILED;
783
784 e->u.fcport.fcport = fcport;
Quinn Tran6d6749272017-12-28 12:33:41 -0800785 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800786 return qla2x00_post_work(vha, e);
787}
788
789static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800790void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800791{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800792 struct srb *sp = s;
793 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800794 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800795 fc_port_t *fcport = sp->fcport;
796 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800797 struct event_arg ea;
798
Quinn Tran83548fe2017-06-02 09:12:01 -0700799 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800800 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
801 sp->name, res, fcport->port_name, mb[1], mb[2]);
802
Quinn Tran6d6749272017-12-28 12:33:41 -0800803 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800804
Quinn Tran726b8542017-01-19 22:28:00 -0800805 memset(&ea, 0, sizeof(ea));
806 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800807 ea.fcport = fcport;
808 ea.sp = sp;
809
810 qla2x00_fcport_event_handler(vha, &ea);
811
812 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
813 sp->u.iocb_cmd.u.mbx.in_dma);
814
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800815 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800816}
817
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700818static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
819{
820 struct qla_work_evt *e;
821
822 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
823 if (!e)
824 return QLA_FUNCTION_FAILED;
825
826 e->u.fcport.fcport = fcport;
827
828 return qla2x00_post_work(vha, e);
829}
830
831static void
832qla2x00_async_prli_sp_done(void *ptr, int res)
833{
834 srb_t *sp = ptr;
835 struct scsi_qla_host *vha = sp->vha;
836 struct srb_iocb *lio = &sp->u.iocb_cmd;
837 struct event_arg ea;
838
839 ql_dbg(ql_dbg_disc, vha, 0x2129,
840 "%s %8phC res %d \n", __func__,
841 sp->fcport->port_name, res);
842
843 sp->fcport->flags &= ~FCF_ASYNC_SENT;
844
845 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
846 memset(&ea, 0, sizeof(ea));
847 ea.event = FCME_PRLI_DONE;
848 ea.fcport = sp->fcport;
849 ea.data[0] = lio->u.logio.data[0];
850 ea.data[1] = lio->u.logio.data[1];
851 ea.iop[0] = lio->u.logio.iop[0];
852 ea.iop[1] = lio->u.logio.iop[1];
853 ea.sp = sp;
854
855 qla2x00_fcport_event_handler(vha, &ea);
856 }
857
858 sp->free(sp);
859}
860
861int
862qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
863{
864 srb_t *sp;
865 struct srb_iocb *lio;
866 int rval = QLA_FUNCTION_FAILED;
867
868 if (!vha->flags.online)
869 return rval;
870
871 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
872 fcport->fw_login_state == DSC_LS_PLOGI_COMP ||
873 fcport->fw_login_state == DSC_LS_PRLI_PEND)
874 return rval;
875
876 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
877 if (!sp)
878 return rval;
879
880 fcport->flags |= FCF_ASYNC_SENT;
881 fcport->logout_completed = 0;
882
883 sp->type = SRB_PRLI_CMD;
884 sp->name = "prli";
885 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
886
887 lio = &sp->u.iocb_cmd;
888 lio->timeout = qla2x00_async_iocb_timeout;
889 sp->done = qla2x00_async_prli_sp_done;
890 lio->u.logio.flags = 0;
891
892 if (fcport->fc4f_nvme)
893 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
894
895 rval = qla2x00_start_sp(sp);
896 if (rval != QLA_SUCCESS) {
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700897 fcport->flags |= FCF_LOGIN_NEEDED;
898 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
899 goto done_free_sp;
900 }
901
902 ql_dbg(ql_dbg_disc, vha, 0x211b,
903 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
904 fcport->port_name, sp->handle, fcport->loop_id,
905 fcport->d_id.b24, fcport->login_retry);
906
907 return rval;
908
909done_free_sp:
910 sp->free(sp);
911 fcport->flags &= ~FCF_ASYNC_SENT;
912 return rval;
913}
914
Quinn Trana07fc0a2017-08-23 15:05:21 -0700915int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800916{
917 struct qla_work_evt *e;
918
919 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
920 if (!e)
921 return QLA_FUNCTION_FAILED;
922
923 e->u.fcport.fcport = fcport;
924 e->u.fcport.opt = opt;
Quinn Tran6d6749272017-12-28 12:33:41 -0800925 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800926 return qla2x00_post_work(vha, e);
927}
928
929int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
930{
931 srb_t *sp;
932 struct srb_iocb *mbx;
933 int rval = QLA_FUNCTION_FAILED;
934 u16 *mb;
935 dma_addr_t pd_dma;
936 struct port_database_24xx *pd;
937 struct qla_hw_data *ha = vha->hw;
938
Quinn Tran3dbec592017-12-28 12:33:40 -0800939 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
940 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800941
Quinn Tran726b8542017-01-19 22:28:00 -0800942 fcport->disc_state = DSC_GPDB;
943
944 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
945 if (!sp)
946 goto done;
947
Quinn Tran3dbec592017-12-28 12:33:40 -0800948 fcport->flags |= FCF_ASYNC_SENT;
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700949 sp->type = SRB_MB_IOCB;
950 sp->name = "gpdb";
951 sp->gen1 = fcport->rscn_gen;
952 sp->gen2 = fcport->login_gen;
953 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
954
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200955 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800956 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700957 ql_log(ql_log_warn, vha, 0xd043,
958 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800959 goto done_free_sp;
960 }
Quinn Tran726b8542017-01-19 22:28:00 -0800961
Quinn Tran726b8542017-01-19 22:28:00 -0800962 mb = sp->u.iocb_cmd.u.mbx.out_mb;
963 mb[0] = MBC_GET_PORT_DATABASE;
964 mb[1] = fcport->loop_id;
965 mb[2] = MSW(pd_dma);
966 mb[3] = LSW(pd_dma);
967 mb[6] = MSW(MSD(pd_dma));
968 mb[7] = LSW(MSD(pd_dma));
969 mb[9] = vha->vp_idx;
970 mb[10] = opt;
971
972 mbx = &sp->u.iocb_cmd;
973 mbx->timeout = qla2x00_async_iocb_timeout;
974 mbx->u.mbx.in = (void *)pd;
975 mbx->u.mbx.in_dma = pd_dma;
976
977 sp->done = qla24xx_async_gpdb_sp_done;
978
979 rval = qla2x00_start_sp(sp);
980 if (rval != QLA_SUCCESS)
981 goto done_free_sp;
982
Quinn Tran83548fe2017-06-02 09:12:01 -0700983 ql_dbg(ql_dbg_disc, vha, 0x20dc,
984 "Async-%s %8phC hndl %x opt %x\n",
985 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800986
987 return rval;
988
989done_free_sp:
990 if (pd)
991 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
992
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800993 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800994 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800995done:
Quinn Tran726b8542017-01-19 22:28:00 -0800996 qla24xx_post_gpdb_work(vha, fcport, opt);
997 return rval;
998}
999
1000static
Quinn Trana4239942017-12-28 12:33:26 -08001001void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001002{
Quinn Tran726b8542017-01-19 22:28:00 -08001003 unsigned long flags;
1004
Quinn Tran726b8542017-01-19 22:28:00 -08001005 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001006 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001007 ea->fcport->deleted = 0;
1008 ea->fcport->logout_on_delete = 1;
1009
1010 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1011 vha->fcport_count++;
1012 ea->fcport->login_succ = 1;
1013
1014 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1015 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001016 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001017 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001018 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001019 vha->fcport_count);
1020
Quinn Trana4239942017-12-28 12:33:26 -08001021 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001022 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001023 if (ea->fcport->id_changed) {
1024 ea->fcport->id_changed = 0;
1025 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1026 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1027 __func__, __LINE__, ea->fcport->port_name,
1028 vha->fcport_count);
1029 qla24xx_post_gfpnid_work(vha, ea->fcport);
1030 } else {
1031 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1032 "%s %d %8phC post gpsc fcp_cnt %d\n",
1033 __func__, __LINE__, ea->fcport->port_name,
1034 vha->fcport_count);
1035 qla24xx_post_gpsc_work(vha, ea->fcport);
1036 }
Quinn Tran726b8542017-01-19 22:28:00 -08001037 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001038 } else if (ea->fcport->login_succ) {
1039 /*
1040 * We have an existing session. A late RSCN delivery
1041 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001042 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001043 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001044 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1045 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001046 __func__, __LINE__, ea->fcport->port_name);
himanshu.madhani@cavium.com8a7eac22018-01-15 20:46:50 -08001047 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001048 }
1049 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001050}
Quinn Tran726b8542017-01-19 22:28:00 -08001051
Quinn Trana4239942017-12-28 12:33:26 -08001052static
1053void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1054{
Quinn Trana4239942017-12-28 12:33:26 -08001055 fc_port_t *fcport = ea->fcport;
1056 struct port_database_24xx *pd;
1057 struct srb *sp = ea->sp;
1058
1059 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1060
1061 fcport->flags &= ~FCF_ASYNC_SENT;
1062
1063 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001064 "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1065 fcport->disc_state, pd->current_login_state, ea->rc);
Quinn Trana4239942017-12-28 12:33:26 -08001066
1067 if (fcport->disc_state == DSC_DELETE_PEND)
1068 return;
1069
1070 switch (pd->current_login_state) {
1071 case PDS_PRLI_COMPLETE:
1072 __qla24xx_parse_gpdb(vha, fcport, pd);
1073 break;
1074 case PDS_PLOGI_PENDING:
1075 case PDS_PLOGI_COMPLETE:
1076 case PDS_PRLI_PENDING:
1077 case PDS_PRLI2_PENDING:
1078 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1079 __func__, __LINE__, fcport->port_name);
1080 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1081 return;
1082 case PDS_LOGO_PENDING:
1083 case PDS_PORT_UNAVAILABLE:
1084 default:
1085 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1086 __func__, __LINE__, fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08001087 qlt_schedule_sess_for_deletion(fcport);
Quinn Trana4239942017-12-28 12:33:26 -08001088 return;
1089 }
1090 __qla24xx_handle_gpdb_event(vha, ea);
1091} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001092
1093static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1094{
1095 u8 login = 0;
Quinn Tran040036b2017-12-28 12:33:38 -08001096 int rc;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001097
1098 if (qla_tgt_mode_enabled(vha))
1099 return;
1100
1101 if (qla_dual_mode_enabled(vha)) {
1102 if (N2N_TOPO(vha->hw)) {
1103 u64 mywwn, wwn;
1104
1105 mywwn = wwn_to_u64(vha->port_name);
1106 wwn = wwn_to_u64(fcport->port_name);
1107 if (mywwn > wwn)
1108 login = 1;
1109 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1110 && time_after_eq(jiffies,
1111 fcport->plogi_nack_done_deadline))
1112 login = 1;
1113 } else {
1114 login = 1;
1115 }
1116 } else {
1117 /* initiator mode */
1118 login = 1;
1119 }
1120
1121 if (login) {
Quinn Tran040036b2017-12-28 12:33:38 -08001122 if (fcport->loop_id == FC_NO_LOOP_ID) {
1123 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1124 rc = qla2x00_find_new_loop_id(vha, fcport);
1125 if (rc) {
1126 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1127 "%s %d %8phC post del sess - out of loopid\n",
1128 __func__, __LINE__, fcport->port_name);
1129 fcport->scan_state = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001130 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran040036b2017-12-28 12:33:38 -08001131 return;
1132 }
1133 }
Quinn Tran9cd883f2017-12-28 12:33:24 -08001134 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1135 "%s %d %8phC post login\n",
1136 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001137 qla2x00_post_async_login_work(vha, fcport, NULL);
1138 }
1139}
1140
Quinn Tran726b8542017-01-19 22:28:00 -08001141int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1142{
Quinn Tranf13515a2017-12-28 12:33:15 -08001143 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001144 u64 wwn;
1145
1146 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001147 "%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 -08001148 __func__, fcport->port_name, fcport->disc_state,
1149 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1150 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001151 fcport->login_gen, fcport->login_retry,
Quinn Trana4239942017-12-28 12:33:26 -08001152 fcport->loop_id, fcport->scan_state);
1153
Quinn Tran726b8542017-01-19 22:28:00 -08001154 if (fcport->login_retry == 0)
1155 return 0;
1156
1157 if (fcport->scan_state != QLA_FCPORT_FOUND)
1158 return 0;
1159
Quinn Tran726b8542017-01-19 22:28:00 -08001160 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001161 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1162 return 0;
1163
Quinn Tran5b334692017-03-15 09:48:48 -07001164 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001165 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1166 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001167 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001168 }
Quinn Tran5b334692017-03-15 09:48:48 -07001169 }
1170
Quinn Tran726b8542017-01-19 22:28:00 -08001171 /* for pure Target Mode. Login will not be initiated */
1172 if (vha->host->active_mode == MODE_TARGET)
1173 return 0;
1174
1175 if (fcport->flags & FCF_ASYNC_SENT) {
1176 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1177 return 0;
1178 }
1179
Quinn Trana4239942017-12-28 12:33:26 -08001180 fcport->login_retry--;
1181
Quinn Tran726b8542017-01-19 22:28:00 -08001182 switch (fcport->disc_state) {
1183 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001184 wwn = wwn_to_u64(fcport->node_name);
1185 if (wwn == 0) {
1186 ql_dbg(ql_dbg_disc, vha, 0xffff,
1187 "%s %d %8phC post GNNID\n",
1188 __func__, __LINE__, fcport->port_name);
1189 qla24xx_post_gnnid_work(vha, fcport);
1190 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001191 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1192 "%s %d %8phC post gnl\n",
1193 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001194 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001195 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001196 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001197 }
1198 break;
1199
1200 case DSC_GNL:
1201 if (fcport->login_pause) {
1202 fcport->last_rscn_gen = fcport->rscn_gen;
1203 fcport->last_login_gen = fcport->login_gen;
1204 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1205 break;
1206 }
1207
Quinn Tran9cd883f2017-12-28 12:33:24 -08001208 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001209 break;
1210
1211 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001212 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1213 "%s %d %8phC post gidpn\n",
1214 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001215 if (N2N_TOPO(vha->hw))
1216 qla_chk_n2n_b4_login(vha, fcport);
1217 else
1218 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001219 break;
1220
1221 case DSC_LOGIN_COMPLETE:
1222 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001223 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001224 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001225 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001226 data[0] = data[1] = 0;
1227 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001228 break;
1229
1230 default:
1231 break;
1232 }
1233
1234 return 0;
1235}
1236
1237static
1238void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1239{
1240 fcport->rscn_gen++;
1241
Quinn Tran83548fe2017-06-02 09:12:01 -07001242 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1243 "%s %8phC DS %d LS %d\n",
1244 __func__, fcport->port_name, fcport->disc_state,
1245 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001246
1247 if (fcport->flags & FCF_ASYNC_SENT)
1248 return;
1249
1250 switch (fcport->disc_state) {
1251 case DSC_DELETED:
1252 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001253 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001254 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001255 default:
1256 break;
1257 }
1258}
1259
1260int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001261 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001262{
1263 struct qla_work_evt *e;
1264 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1265 if (!e)
1266 return QLA_FUNCTION_FAILED;
1267
1268 e->u.new_sess.id = *id;
1269 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001270 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001271 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001272 if (node_name)
1273 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001274
1275 return qla2x00_post_work(vha, e);
1276}
1277
1278static
Quinn Tran726b8542017-01-19 22:28:00 -08001279void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1280 struct event_arg *ea)
1281{
1282 fc_port_t *fcport = ea->fcport;
1283
Quinn Tran83548fe2017-06-02 09:12:01 -07001284 ql_dbg(ql_dbg_disc, vha, 0x2102,
1285 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1286 __func__, fcport->port_name, fcport->disc_state,
1287 fcport->fw_login_state, fcport->login_pause,
1288 fcport->deleted, fcport->conflict,
1289 fcport->last_rscn_gen, fcport->rscn_gen,
1290 fcport->last_login_gen, fcport->login_gen,
1291 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001292
1293 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001294 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1295 return;
1296
Quinn Tran5b334692017-03-15 09:48:48 -07001297 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001298 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1299 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001300 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001301 }
Quinn Tran5b334692017-03-15 09:48:48 -07001302 }
1303
Quinn Tran726b8542017-01-19 22:28:00 -08001304 if (fcport->flags & FCF_ASYNC_SENT) {
1305 fcport->login_retry++;
1306 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1307 return;
1308 }
1309
1310 if (fcport->disc_state == DSC_DELETE_PEND) {
1311 fcport->login_retry++;
1312 return;
1313 }
1314
1315 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001316 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001317 __func__, __LINE__, fcport->port_name);
1318
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001319 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001320 return;
1321 }
1322
1323 qla24xx_fcport_handle_login(vha, fcport);
1324}
1325
Quinn Tran41dc5292017-01-19 22:28:03 -08001326void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001327{
Quinn Tranf352eeb2017-12-28 12:33:35 -08001328 fc_port_t *f, *tf;
Quinn Tran41dc5292017-01-19 22:28:03 -08001329 uint32_t id = 0, mask, rid;
Quinn Tranf352eeb2017-12-28 12:33:35 -08001330 unsigned long flags;
Quinn Tran726b8542017-01-19 22:28:00 -08001331
1332 switch (ea->event) {
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001333 case FCME_RSCN:
1334 case FCME_GIDPN_DONE:
1335 case FCME_GPSC_DONE:
1336 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001337 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001338 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1339 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1340 return;
1341 break;
1342 default:
1343 break;
1344 }
1345
1346 switch (ea->event) {
1347 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001348 if (test_bit(UNLOADING, &vha->dpc_flags))
1349 return;
1350
1351 qla24xx_handle_relogin_event(vha, ea);
1352 break;
1353 case FCME_RSCN:
1354 if (test_bit(UNLOADING, &vha->dpc_flags))
1355 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001356 switch (ea->id.b.rsvd_1) {
1357 case RSCN_PORT_ADDR:
Quinn Tranf352eeb2017-12-28 12:33:35 -08001358 spin_lock_irqsave(&vha->work_lock, flags);
1359 if (vha->scan.scan_flags == 0) {
1360 ql_dbg(ql_dbg_disc, vha, 0xffff,
1361 "%s: schedule\n", __func__);
1362 vha->scan.scan_flags |= SF_QUEUED;
1363 schedule_delayed_work(&vha->scan.scan_work, 5);
Quinn Tran726b8542017-01-19 22:28:00 -08001364 }
Quinn Tranf352eeb2017-12-28 12:33:35 -08001365 spin_unlock_irqrestore(&vha->work_lock, flags);
1366
Quinn Tran41dc5292017-01-19 22:28:03 -08001367 break;
1368 case RSCN_AREA_ADDR:
1369 case RSCN_DOM_ADDR:
1370 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1371 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001372 ql_dbg(ql_dbg_async, vha, 0x5044,
1373 "RSCN: Area 0x%06x was affected\n",
1374 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001375 } else {
1376 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001377 ql_dbg(ql_dbg_async, vha, 0x507a,
1378 "RSCN: Domain 0x%06x was affected\n",
1379 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001380 }
1381
1382 rid = ea->id.b24 & mask;
1383 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1384 list) {
1385 id = f->d_id.b24 & mask;
1386 if (rid == id) {
1387 ea->fcport = f;
1388 qla24xx_handle_rscn_event(f, ea);
1389 }
1390 }
1391 break;
1392 case RSCN_FAB_ADDR:
1393 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001394 ql_log(ql_log_warn, vha, 0xd045,
1395 "RSCN: Fabric was affected. Addr format %d\n",
1396 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001397 qla2x00_mark_all_devices_lost(vha, 1);
1398 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1399 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001400 }
1401 break;
1402 case FCME_GIDPN_DONE:
1403 qla24xx_handle_gidpn_event(vha, ea);
1404 break;
1405 case FCME_GNL_DONE:
1406 qla24xx_handle_gnl_done_event(vha, ea);
1407 break;
1408 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001409 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001410 break;
1411 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1412 qla24xx_handle_plogi_done_event(vha, ea);
1413 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001414 case FCME_PRLI_DONE:
1415 qla24xx_handle_prli_done_event(vha, ea);
1416 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001417 case FCME_GPDB_DONE:
1418 qla24xx_handle_gpdb_event(vha, ea);
1419 break;
1420 case FCME_GPNID_DONE:
1421 qla24xx_handle_gpnid_event(vha, ea);
1422 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001423 case FCME_GFFID_DONE:
1424 qla24xx_handle_gffid_event(vha, ea);
1425 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001426 case FCME_ADISC_DONE:
1427 qla24xx_handle_adisc_event(vha, ea);
1428 break;
Quinn Trana4239942017-12-28 12:33:26 -08001429 case FCME_GNNID_DONE:
1430 qla24xx_handle_gnnid_event(vha, ea);
1431 break;
1432 case FCME_GFPNID_DONE:
1433 qla24xx_handle_gfpnid_event(vha, ea);
1434 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001435 default:
1436 BUG_ON(1);
1437 break;
1438 }
1439}
1440
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001441static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001442qla2x00_tmf_iocb_timeout(void *data)
1443{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001444 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001445 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1446
1447 tmf->u.tmf.comp_status = CS_TIMEOUT;
1448 complete(&tmf->u.tmf.comp);
1449}
1450
1451static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001452qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001453{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001454 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001455 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001456
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001457 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001458}
1459
1460int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001461qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001462 uint32_t tag)
1463{
1464 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001465 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001466 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001467 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001468
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001469 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001470 if (!sp)
1471 goto done;
1472
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001473 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001474 sp->type = SRB_TM_CMD;
1475 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001476 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1477 tm_iocb->u.tmf.flags = flags;
1478 tm_iocb->u.tmf.lun = lun;
1479 tm_iocb->u.tmf.data = tag;
1480 sp->done = qla2x00_tmf_sp_done;
1481 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1482 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001483
1484 rval = qla2x00_start_sp(sp);
1485 if (rval != QLA_SUCCESS)
1486 goto done_free_sp;
1487
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001488 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001489 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1490 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1491 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001492
1493 wait_for_completion(&tm_iocb->u.tmf.comp);
1494
1495 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1496 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1497
1498 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1499 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1500 "TM IOCB failed (%x).\n", rval);
1501 }
1502
1503 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1504 flags = tm_iocb->u.tmf.flags;
1505 lun = (uint16_t)tm_iocb->u.tmf.lun;
1506
1507 /* Issue Marker IOCB */
1508 qla2x00_marker(vha, vha->hw->req_q_map[0],
1509 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1510 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1511 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001512
1513done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001514 sp->free(sp);
Quinn Tran6d6749272017-12-28 12:33:41 -08001515 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001516done:
1517 return rval;
1518}
1519
Armen Baloyan4440e462014-02-26 04:15:18 -05001520static void
1521qla24xx_abort_iocb_timeout(void *data)
1522{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001523 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001524 struct srb_iocb *abt = &sp->u.iocb_cmd;
1525
1526 abt->u.abt.comp_status = CS_TIMEOUT;
1527 complete(&abt->u.abt.comp);
1528}
1529
1530static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001531qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001532{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001533 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001534 struct srb_iocb *abt = &sp->u.iocb_cmd;
1535
himanshu.madhani@cavium.com15148392018-02-12 10:28:14 -08001536 del_timer(&sp->u.iocb_cmd.timer);
Armen Baloyan4440e462014-02-26 04:15:18 -05001537 complete(&abt->u.abt.comp);
1538}
1539
Quinn Tran15f30a52017-03-15 09:48:52 -07001540int
Armen Baloyan4440e462014-02-26 04:15:18 -05001541qla24xx_async_abort_cmd(srb_t *cmd_sp)
1542{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001543 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001544 fc_port_t *fcport = cmd_sp->fcport;
1545 struct srb_iocb *abt_iocb;
1546 srb_t *sp;
1547 int rval = QLA_FUNCTION_FAILED;
1548
1549 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1550 if (!sp)
1551 goto done;
1552
1553 abt_iocb = &sp->u.iocb_cmd;
1554 sp->type = SRB_ABT_CMD;
1555 sp->name = "abort";
1556 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1557 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
Darren Trappb027a5a2018-01-15 20:46:51 -08001558
1559 if (vha->flags.qpairs_available && cmd_sp->qpair)
1560 abt_iocb->u.abt.req_que_no =
1561 cpu_to_le16(cmd_sp->qpair->req->id);
1562 else
1563 abt_iocb->u.abt.req_que_no = cpu_to_le16(vha->req->id);
1564
Armen Baloyan4440e462014-02-26 04:15:18 -05001565 sp->done = qla24xx_abort_sp_done;
1566 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1567 init_completion(&abt_iocb->u.abt.comp);
1568
1569 rval = qla2x00_start_sp(sp);
1570 if (rval != QLA_SUCCESS)
1571 goto done_free_sp;
1572
1573 ql_dbg(ql_dbg_async, vha, 0x507c,
1574 "Abort command issued - hdl=%x, target_id=%x\n",
1575 cmd_sp->handle, fcport->tgt_id);
1576
1577 wait_for_completion(&abt_iocb->u.abt.comp);
1578
1579 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1580 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1581
1582done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001583 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001584done:
1585 return rval;
1586}
1587
1588int
1589qla24xx_async_abort_command(srb_t *sp)
1590{
1591 unsigned long flags = 0;
1592
1593 uint32_t handle;
1594 fc_port_t *fcport = sp->fcport;
1595 struct scsi_qla_host *vha = fcport->vha;
1596 struct qla_hw_data *ha = vha->hw;
1597 struct req_que *req = vha->req;
1598
Darren Trappb027a5a2018-01-15 20:46:51 -08001599 if (vha->flags.qpairs_available && sp->qpair)
1600 req = sp->qpair->req;
1601
Armen Baloyan4440e462014-02-26 04:15:18 -05001602 spin_lock_irqsave(&ha->hardware_lock, flags);
1603 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1604 if (req->outstanding_cmds[handle] == sp)
1605 break;
1606 }
1607 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1608 if (handle == req->num_outstanding_cmds) {
1609 /* Command not found. */
1610 return QLA_FUNCTION_FAILED;
1611 }
1612 if (sp->type == SRB_FXIOCB_DCMD)
1613 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1614 FXDISC_ABORT_IOCTL);
1615
1616 return qla24xx_async_abort_cmd(sp);
1617}
1618
Quinn Tran726b8542017-01-19 22:28:00 -08001619static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001620qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1621{
1622 switch (ea->data[0]) {
1623 case MBS_COMMAND_COMPLETE:
1624 ql_dbg(ql_dbg_disc, vha, 0x2118,
1625 "%s %d %8phC post gpdb\n",
1626 __func__, __LINE__, ea->fcport->port_name);
1627
1628 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1629 ea->fcport->logout_on_delete = 1;
1630 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1631 break;
1632 default:
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001633 if (ea->fcport->n2n_flag) {
1634 ql_dbg(ql_dbg_disc, vha, 0x2118,
1635 "%s %d %8phC post fc4 prli\n",
1636 __func__, __LINE__, ea->fcport->port_name);
1637 ea->fcport->fc4f_nvme = 0;
1638 ea->fcport->n2n_flag = 0;
1639 qla24xx_post_prli_work(vha, ea->fcport);
1640 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001641 ql_dbg(ql_dbg_disc, vha, 0x2119,
1642 "%s %d %8phC unhandle event of %x\n",
1643 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1644 break;
1645 }
1646}
1647
1648static void
Quinn Tran726b8542017-01-19 22:28:00 -08001649qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001650{
Quinn Tran726b8542017-01-19 22:28:00 -08001651 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001652 u16 lid;
1653 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001654 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001655 struct fc_port *fcport = ea->fcport;
1656
Quinn Tranf352eeb2017-12-28 12:33:35 -08001657 ql_dbg(ql_dbg_disc, vha, 0xffff,
1658 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1659 __func__, fcport->port_name, fcport->disc_state,
1660 fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1661 ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1662 ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1663
Quinn Trana4239942017-12-28 12:33:26 -08001664 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1665 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1666 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1667 "%s %d %8phC Remote is trying to login\n",
1668 __func__, __LINE__, fcport->port_name);
1669 return;
1670 }
1671
1672 if (fcport->disc_state == DSC_DELETE_PEND)
1673 return;
1674
1675 if (ea->sp->gen2 != fcport->login_gen) {
1676 /* target side must have changed it. */
1677 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001678 "%s %8phC generation changed\n",
1679 __func__, fcport->port_name);
1680 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Trana4239942017-12-28 12:33:26 -08001681 return;
1682 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1683 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1684 __func__, __LINE__, fcport->port_name);
1685 qla24xx_post_gidpn_work(vha, fcport);
1686 return;
1687 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001688
Quinn Tran726b8542017-01-19 22:28:00 -08001689 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001690 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001691 /*
1692 * Driver must validate login state - If PRLI not complete,
1693 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1694 * requests.
1695 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001696 if (ea->fcport->fc4f_nvme) {
1697 ql_dbg(ql_dbg_disc, vha, 0x2117,
1698 "%s %d %8phC post prli\n",
1699 __func__, __LINE__, ea->fcport->port_name);
1700 qla24xx_post_prli_work(vha, ea->fcport);
1701 } else {
1702 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001703 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1704 __func__, __LINE__, ea->fcport->port_name,
1705 ea->fcport->loop_id, ea->fcport->d_id.b24);
1706
1707 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001708 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana084fd62017-12-04 14:45:00 -08001709 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001710 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1711 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001712 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001713 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1714 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1715
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001716 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1717 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001718 break;
1719 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001720 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001721 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1722
1723 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1724 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1725 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001726 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1727 else
Quinn Tran726b8542017-01-19 22:28:00 -08001728 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001729 break;
1730 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001731 /* data[1] = IO PARAM 1 = nport ID */
1732 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1733 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1734 cid.b.al_pa = ea->iop[1] & 0xff;
1735 cid.b.rsvd_1 = 0;
1736
Quinn Tran83548fe2017-06-02 09:12:01 -07001737 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1738 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1739 __func__, __LINE__, ea->fcport->port_name,
1740 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001741
1742 if (IS_SW_RESV_ADDR(cid)) {
1743 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1744 ea->fcport->loop_id = FC_NO_LOOP_ID;
1745 } else {
1746 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001747 }
Quinn Tran726b8542017-01-19 22:28:00 -08001748 qla24xx_post_gnl_work(vha, ea->fcport);
1749 break;
1750 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001751 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1752 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1753 __func__, __LINE__, ea->fcport->port_name,
1754 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1755 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001756
Quinn Trana084fd62017-12-04 14:45:00 -08001757 lid = ea->iop[1] & 0xffff;
1758 qlt_find_sess_invalidate_other(vha,
1759 wwn_to_u64(ea->fcport->port_name),
1760 ea->fcport->d_id, lid, &conflict_fcport);
1761
1762 if (conflict_fcport) {
1763 /*
1764 * Another fcport share the same loop_id/nport id.
1765 * Conflict fcport needs to finish cleanup before this
1766 * fcport can proceed to login.
1767 */
1768 conflict_fcport->conflict = ea->fcport;
1769 ea->fcport->login_pause = 1;
1770
1771 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1772 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1773 __func__, __LINE__, ea->fcport->port_name,
1774 ea->fcport->d_id.b24, lid);
1775 qla2x00_clear_loop_id(ea->fcport);
1776 qla24xx_post_gidpn_work(vha, ea->fcport);
1777 } else {
1778 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1779 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1780 __func__, __LINE__, ea->fcport->port_name,
1781 ea->fcport->d_id.b24, lid);
1782
1783 qla2x00_clear_loop_id(ea->fcport);
1784 set_bit(lid, vha->hw->loop_id_map);
1785 ea->fcport->loop_id = lid;
1786 ea->fcport->keep_nport_handle = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001787 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana084fd62017-12-04 14:45:00 -08001788 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001789 break;
1790 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001791 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001792}
1793
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001794void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001795qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1796 uint16_t *data)
1797{
Quinn Tran726b8542017-01-19 22:28:00 -08001798 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001799 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001800 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001801 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001802}
1803
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001804void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001805qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1806 uint16_t *data)
1807{
1808 if (data[0] == MBS_COMMAND_COMPLETE) {
1809 qla2x00_update_fcport(vha, fcport);
1810
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001811 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001812 }
1813
1814 /* Retry login. */
1815 fcport->flags &= ~FCF_ASYNC_SENT;
1816 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1817 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1818 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001819 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001820
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001821 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001822}
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824/****************************************************************************/
1825/* QLogic ISP2x00 Hardware Support Functions. */
1826/****************************************************************************/
1827
Saurav Kashyapfa492632012-11-21 02:40:29 -05001828static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001829qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1830{
1831 int rval = QLA_SUCCESS;
1832 struct qla_hw_data *ha = vha->hw;
1833 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001834 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001835
1836 qla83xx_idc_lock(vha, 0);
1837
1838 /* SV: TODO: Assign initialization timeout from
1839 * flash-info / other param
1840 */
1841 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1842 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1843
1844 /* Set our fcoe function presence */
1845 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1846 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1847 "Error while setting DRV-Presence.\n");
1848 rval = QLA_FUNCTION_FAILED;
1849 goto exit;
1850 }
1851
1852 /* Decide the reset ownership */
1853 qla83xx_reset_ownership(vha);
1854
1855 /*
1856 * On first protocol driver load:
1857 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1858 * register.
1859 * Others: Check compatibility with current IDC Major version.
1860 */
1861 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1862 if (ha->flags.nic_core_reset_owner) {
1863 /* Set IDC Major version */
1864 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1865 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1866
1867 /* Clearing IDC-Lock-Recovery register */
1868 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1869 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1870 /*
1871 * Clear further IDC participation if we are not compatible with
1872 * the current IDC Major Version.
1873 */
1874 ql_log(ql_log_warn, vha, 0xb07d,
1875 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1876 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1877 __qla83xx_clear_drv_presence(vha);
1878 rval = QLA_FUNCTION_FAILED;
1879 goto exit;
1880 }
1881 /* Each function sets its supported Minor version. */
1882 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1883 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1884 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1885
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001886 if (ha->flags.nic_core_reset_owner) {
1887 memset(config, 0, sizeof(config));
1888 if (!qla81xx_get_port_config(vha, config))
1889 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1890 QLA8XXX_DEV_READY);
1891 }
1892
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001893 rval = qla83xx_idc_state_handler(vha);
1894
1895exit:
1896 qla83xx_idc_unlock(vha, 0);
1897
1898 return rval;
1899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/*
1902* qla2x00_initialize_adapter
1903* Initialize board.
1904*
1905* Input:
1906* ha = adapter block pointer.
1907*
1908* Returns:
1909* 0 = success
1910*/
1911int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001912qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001915 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001916 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001917
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001918 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1919 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001922 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001923 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001924 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001925 ha->flags.pci_channel_io_perm_failure = 0;
1926 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001927 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001928 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1929 atomic_set(&vha->loop_state, LOOP_DOWN);
1930 vha->device_flags = DFLG_NO_CABLE;
1931 vha->dpc_flags = 0;
1932 vha->flags.management_server_logged_in = 0;
1933 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 ha->isp_abort_cnt = 0;
1935 ha->beacon_blink_led = 0;
1936
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001937 set_bit(0, ha->req_qid_map);
1938 set_bit(0, ha->rsp_qid_map);
1939
Chad Dupuiscfb09192011-11-18 09:03:07 -08001940 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001941 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001942 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001944 ql_log(ql_log_warn, vha, 0x0044,
1945 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return (rval);
1947 }
1948
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001949 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001951 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001952 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001953 ql_log(ql_log_fatal, vha, 0x004f,
1954 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001955 return rval;
1956 }
1957
1958 if (IS_QLA8044(ha)) {
1959 qla8044_read_reset_template(vha);
1960
1961 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1962 * If DONRESET_BIT0 is set, drivers should not set dev_state
1963 * to NEED_RESET. But if NEED_RESET is set, drivers should
1964 * should honor the reset. */
1965 if (ql2xdontresethba == 1)
1966 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001967 }
1968
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001969 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001970 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001971 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001972
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001973 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001975 if (ha->flags.disable_serdes) {
1976 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001977 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001978 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001979 return QLA_FUNCTION_FAILED;
1980 }
1981
Chad Dupuiscfb09192011-11-18 09:03:07 -08001982 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001983 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001985 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1986 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001987 if (rval)
1988 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001989 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001990 if (rval)
1991 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001993
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001994 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001995 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001996 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001997 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001998 "Unable to configure ISP84XX.\n");
1999 return QLA_FUNCTION_FAILED;
2000 }
2001 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002002
Quinn Tranead03852017-01-19 22:28:01 -08002003 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002004 rval = qla2x00_init_rings(vha);
2005
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002006 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002008 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07002009 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002010 rval = qla84xx_init_chip(vha);
2011 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002012 ql_log(ql_log_warn, vha, 0x00d4,
2013 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07002014 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002015 }
2016 }
2017
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002018 /* Load the NIC Core f/w if we are the first protocol driver. */
2019 if (IS_QLA8031(ha)) {
2020 rval = qla83xx_nic_core_fw_load(vha);
2021 if (rval)
2022 ql_log(ql_log_warn, vha, 0x0124,
2023 "Error in initializing NIC Core f/w.\n");
2024 }
2025
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002026 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2027 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002028
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002029 if (IS_P3P_TYPE(ha))
2030 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2031 else
2032 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return (rval);
2035}
2036
2037/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002038 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * @ha: HA context
2040 *
2041 * Returns 0 on success.
2042 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002043int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002044qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002046 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002047 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002048 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002049 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002052 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002055 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2057
Andrew Vasquez737faec2008-10-24 15:13:45 -07002058 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002060 /* Get PCI bus information. */
2061 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002062 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002063 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2064
2065 return QLA_SUCCESS;
2066}
2067
2068/**
2069 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2070 * @ha: HA context
2071 *
2072 * Returns 0 on success.
2073 */
2074int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002075qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002076{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002077 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002078 unsigned long flags = 0;
2079 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002080 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002081 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002082
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002083 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002084 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002085
2086 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002087 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002088
2089 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2090 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002091 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002092
2093 /*
2094 * If this is a 2300 card and not 2312, reset the
2095 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2096 * the 2310 also reports itself as a 2300 so we need to get the
2097 * fb revision level -- a 6 indicates it really is a 2300 and
2098 * not a 2310.
2099 */
2100 if (IS_QLA2300(ha)) {
2101 spin_lock_irqsave(&ha->hardware_lock, flags);
2102
2103 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002104 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002105 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002106 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002107 break;
2108
2109 udelay(10);
2110 }
2111
2112 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002113 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2114 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002115
2116 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002117 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002118
2119 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002120 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002121
2122 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002123 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2124 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002125
2126 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002127 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002128 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002129 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002130 break;
2131
2132 udelay(10);
2133 }
2134
2135 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2136 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002137
2138 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2139
Andrew Vasquez737faec2008-10-24 15:13:45 -07002140 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002141
2142 /* Get PCI bus information. */
2143 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002144 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002145 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2146
2147 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002151 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2152 * @ha: HA context
2153 *
2154 * Returns 0 on success.
2155 */
2156int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002157qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002158{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002159 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002160 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002161 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002162 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002163
2164 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002165 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002166
2167 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002168 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002169 w &= ~PCI_COMMAND_INTX_DISABLE;
2170 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2171
2172 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2173
2174 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002175 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2176 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002177
2178 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002179 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002180 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002181
Andrew Vasquez737faec2008-10-24 15:13:45 -07002182 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002183
Auke Kok44c10132007-06-08 15:46:36 -07002184 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002185
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002186 /* Get PCI bus information. */
2187 spin_lock_irqsave(&ha->hardware_lock, flags);
2188 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2189 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2190
2191 return QLA_SUCCESS;
2192}
2193
2194/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002195 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2196 * @ha: HA context
2197 *
2198 * Returns 0 on success.
2199 */
2200int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002201qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002202{
2203 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002204 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002205
2206 pci_set_master(ha->pdev);
2207 pci_try_set_mwi(ha->pdev);
2208
2209 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2210 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2211 w &= ~PCI_COMMAND_INTX_DISABLE;
2212 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2213
2214 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002215 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002216 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002217
Andrew Vasquez737faec2008-10-24 15:13:45 -07002218 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002219
2220 ha->chip_revision = ha->pdev->revision;
2221
2222 return QLA_SUCCESS;
2223}
2224
2225/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 * qla2x00_isp_firmware() - Choose firmware image.
2227 * @ha: HA context
2228 *
2229 * Returns 0 on success.
2230 */
2231static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002232qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002235 uint16_t loop_id, topo, sw_cap;
2236 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002237 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002240 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002243 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002246 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002247 if (rval == QLA_SUCCESS) {
2248 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002249 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002250 &area, &domain, &topo, &sw_cap);
2251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002254 if (rval)
2255 ql_dbg(ql_dbg_init, vha, 0x007a,
2256 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 return (rval);
2259}
2260
2261/**
2262 * qla2x00_reset_chip() - Reset ISP chip.
2263 * @ha: HA context
2264 *
2265 * Returns 0 on success.
2266 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002267void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002268qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002271 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002272 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 uint16_t cmd;
2275
Andrew Vasquez85880802009-12-15 21:29:46 -08002276 if (unlikely(pci_channel_offline(ha->pdev)))
2277 return;
2278
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002279 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 spin_lock_irqsave(&ha->hardware_lock, flags);
2282
2283 /* Turn off master enable */
2284 cmd = 0;
2285 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2286 cmd &= ~PCI_COMMAND_MASTER;
2287 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2288
2289 if (!IS_QLA2100(ha)) {
2290 /* Pause RISC. */
2291 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2292 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2293 for (cnt = 0; cnt < 30000; cnt++) {
2294 if ((RD_REG_WORD(&reg->hccr) &
2295 HCCR_RISC_PAUSE) != 0)
2296 break;
2297 udelay(100);
2298 }
2299 } else {
2300 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2301 udelay(10);
2302 }
2303
2304 /* Select FPM registers. */
2305 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2306 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2307
2308 /* FPM Soft Reset. */
2309 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2310 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2311
2312 /* Toggle Fpm Reset. */
2313 if (!IS_QLA2200(ha)) {
2314 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2315 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2316 }
2317
2318 /* Select frame buffer registers. */
2319 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2320 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2321
2322 /* Reset frame buffer FIFOs. */
2323 if (IS_QLA2200(ha)) {
2324 WRT_FB_CMD_REG(ha, reg, 0xa000);
2325 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2326 } else {
2327 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2328
2329 /* Read back fb_cmd until zero or 3 seconds max */
2330 for (cnt = 0; cnt < 3000; cnt++) {
2331 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2332 break;
2333 udelay(100);
2334 }
2335 }
2336
2337 /* Select RISC module registers. */
2338 WRT_REG_WORD(&reg->ctrl_status, 0);
2339 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2340
2341 /* Reset RISC processor. */
2342 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2343 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2344
2345 /* Release RISC processor. */
2346 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2347 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2348 }
2349
2350 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2351 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2352
2353 /* Reset ISP chip. */
2354 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2355
2356 /* Wait for RISC to recover from reset. */
2357 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2358 /*
2359 * It is necessary to for a delay here since the card doesn't
2360 * respond to PCI reads during a reset. On some architectures
2361 * this will result in an MCA.
2362 */
2363 udelay(20);
2364 for (cnt = 30000; cnt; cnt--) {
2365 if ((RD_REG_WORD(&reg->ctrl_status) &
2366 CSR_ISP_SOFT_RESET) == 0)
2367 break;
2368 udelay(100);
2369 }
2370 } else
2371 udelay(10);
2372
2373 /* Reset RISC processor. */
2374 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2375
2376 WRT_REG_WORD(&reg->semaphore, 0);
2377
2378 /* Release RISC processor. */
2379 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2380 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2381
2382 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2383 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002384 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 udelay(100);
2388 }
2389 } else
2390 udelay(100);
2391
2392 /* Turn on master enable */
2393 cmd |= PCI_COMMAND_MASTER;
2394 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2395
2396 /* Disable RISC pause on FPM parity error. */
2397 if (!IS_QLA2100(ha)) {
2398 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2399 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2400 }
2401
2402 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2403}
2404
2405/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002406 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2407 *
2408 * Returns 0 on success.
2409 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002410static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002411qla81xx_reset_mpi(scsi_qla_host_t *vha)
2412{
2413 uint16_t mb[4] = {0x1010, 0, 1, 0};
2414
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002415 if (!IS_QLA81XX(vha->hw))
2416 return QLA_SUCCESS;
2417
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002418 return qla81xx_write_mpi_register(vha, mb);
2419}
2420
2421/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002422 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002423 * @ha: HA context
2424 *
2425 * Returns 0 on success.
2426 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002427static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002428qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002429{
2430 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002431 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002432 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002433 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002434 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002435 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002436 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002437
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002438 spin_lock_irqsave(&ha->hardware_lock, flags);
2439
2440 /* Reset RISC. */
2441 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2442 for (cnt = 0; cnt < 30000; cnt++) {
2443 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2444 break;
2445
2446 udelay(10);
2447 }
2448
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002449 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2450 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2451
2452 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2453 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2454 RD_REG_DWORD(&reg->hccr),
2455 RD_REG_DWORD(&reg->ctrl_status),
2456 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2457
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002458 WRT_REG_DWORD(&reg->ctrl_status,
2459 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002460 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002461
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002462 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002463
Andrew Vasquez88c26662005-07-08 17:59:26 -07002464 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002465 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002466 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2467 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002468 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002469 if (cnt)
2470 udelay(5);
2471 else
2472 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002473 }
2474
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002475 if (rval == QLA_SUCCESS)
2476 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2477
2478 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2479 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2480 RD_REG_DWORD(&reg->hccr),
2481 RD_REG_DWORD(&reg->mailbox0));
2482
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002483 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002484 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002485 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002486 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002487 if ((RD_REG_DWORD(&reg->ctrl_status) &
2488 CSRX_ISP_SOFT_RESET) == 0)
2489 break;
2490
2491 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002492 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002493 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2494 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2495
2496 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2497 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2498 RD_REG_DWORD(&reg->hccr),
2499 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002500
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002501 /* If required, do an MPI FW reset now */
2502 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2503 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2504 if (++abts_cnt < 5) {
2505 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2506 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2507 } else {
2508 /*
2509 * We exhausted the ISP abort retries. We have to
2510 * set the board offline.
2511 */
2512 abts_cnt = 0;
2513 vha->flags.online = 0;
2514 }
2515 }
2516 }
2517
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002518 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2519 RD_REG_DWORD(&reg->hccr);
2520
2521 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2522 RD_REG_DWORD(&reg->hccr);
2523
2524 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2525 RD_REG_DWORD(&reg->hccr);
2526
Bart Van Assche52c82822015-07-09 07:23:26 -07002527 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002528 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002529 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002530 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002531 if (cnt)
2532 udelay(5);
2533 else
2534 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002535 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002536 if (rval == QLA_SUCCESS)
2537 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2538
2539 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2540 "Host Risc 0x%x, mailbox0 0x%x\n",
2541 RD_REG_DWORD(&reg->hccr),
2542 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002543
2544 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002545
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002546 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2547 "Driver in %s mode\n",
2548 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2549
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002550 if (IS_NOPOLLING_TYPE(ha))
2551 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002552
2553 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002554}
2555
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002556static void
2557qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2558{
2559 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2560
2561 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2562 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2563
2564}
2565
2566static void
2567qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2568{
2569 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2570
2571 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2572 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2573}
2574
2575static void
2576qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2577{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002578 uint32_t wd32 = 0;
2579 uint delta_msec = 100;
2580 uint elapsed_msec = 0;
2581 uint timeout_msec;
2582 ulong n;
2583
Joe Carnucciocc790762015-08-04 13:37:53 -04002584 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2585 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002586 return;
2587
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002588 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2589 udelay(100);
2590
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002591attempt:
2592 timeout_msec = TIMEOUT_SEMAPHORE;
2593 n = timeout_msec / delta_msec;
2594 while (n--) {
2595 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2596 qla25xx_read_risc_sema_reg(vha, &wd32);
2597 if (wd32 & RISC_SEMAPHORE)
2598 break;
2599 msleep(delta_msec);
2600 elapsed_msec += delta_msec;
2601 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2602 goto force;
2603 }
2604
2605 if (!(wd32 & RISC_SEMAPHORE))
2606 goto force;
2607
2608 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2609 goto acquired;
2610
2611 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2612 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2613 n = timeout_msec / delta_msec;
2614 while (n--) {
2615 qla25xx_read_risc_sema_reg(vha, &wd32);
2616 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2617 break;
2618 msleep(delta_msec);
2619 elapsed_msec += delta_msec;
2620 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2621 goto force;
2622 }
2623
2624 if (wd32 & RISC_SEMAPHORE_FORCE)
2625 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2626
2627 goto attempt;
2628
2629force:
2630 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2631
2632acquired:
2633 return;
2634}
2635
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002636/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002637 * qla24xx_reset_chip() - Reset ISP24xx chip.
2638 * @ha: HA context
2639 *
2640 * Returns 0 on success.
2641 */
2642void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002643qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002644{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002645 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002646
2647 if (pci_channel_offline(ha->pdev) &&
2648 ha->flags.pci_channel_io_perm_failure) {
2649 return;
2650 }
2651
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002652 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002653
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002654 qla25xx_manipulate_risc_semaphore(vha);
2655
Andrew Vasquez88c26662005-07-08 17:59:26 -07002656 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002657 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002658}
2659
2660/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 * qla2x00_chip_diag() - Test chip for proper operation.
2662 * @ha: HA context
2663 *
2664 * Returns 0 on success.
2665 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002666int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002667qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002670 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002671 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 unsigned long flags = 0;
2673 uint16_t data;
2674 uint32_t cnt;
2675 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002676 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 /* Assume a failed state */
2679 rval = QLA_FUNCTION_FAILED;
2680
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002681 ql_dbg(ql_dbg_init, vha, 0x007b,
2682 "Testing device at %lx.\n", (u_long)&reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 spin_lock_irqsave(&ha->hardware_lock, flags);
2685
2686 /* Reset ISP chip. */
2687 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2688
2689 /*
2690 * We need to have a delay here since the card will not respond while
2691 * in reset causing an MCA on some architectures.
2692 */
2693 udelay(20);
2694 data = qla2x00_debounce_register(&reg->ctrl_status);
2695 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2696 udelay(5);
2697 data = RD_REG_WORD(&reg->ctrl_status);
2698 barrier();
2699 }
2700
2701 if (!cnt)
2702 goto chip_diag_failed;
2703
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002704 ql_dbg(ql_dbg_init, vha, 0x007c,
2705 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 /* Reset RISC processor. */
2708 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2709 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2710
2711 /* Workaround for QLA2312 PCI parity error */
2712 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2713 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2714 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2715 udelay(5);
2716 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002717 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719 } else
2720 udelay(10);
2721
2722 if (!cnt)
2723 goto chip_diag_failed;
2724
2725 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002726 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2729 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2730 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2731 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2732 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2733 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002734 ql_log(ql_log_warn, vha, 0x0062,
2735 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2736 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 goto chip_diag_failed;
2739 }
2740 ha->product_id[0] = mb[1];
2741 ha->product_id[1] = mb[2];
2742 ha->product_id[2] = mb[3];
2743 ha->product_id[3] = mb[4];
2744
2745 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002746 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2748 else
2749 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002750 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 if (IS_QLA2200(ha) &&
2753 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2754 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002755 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002757 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 ha->fw_transfer_size = 128;
2759 }
2760
2761 /* Wrap Incoming Mailboxes Test. */
2762 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2763
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002764 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002765 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002766 if (rval)
2767 ql_log(ql_log_warn, vha, 0x0080,
2768 "Failed mailbox send register test.\n");
2769 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Flag a successful rval */
2771 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 spin_lock_irqsave(&ha->hardware_lock, flags);
2773
2774chip_diag_failed:
2775 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002776 ql_log(ql_log_info, vha, 0x0081,
2777 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2780
2781 return (rval);
2782}
2783
2784/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002785 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2786 * @ha: HA context
2787 *
2788 * Returns 0 on success.
2789 */
2790int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002791qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002792{
2793 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002794 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002795 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002796
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002797 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002798 return QLA_SUCCESS;
2799
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002800 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002801
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002802 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002803 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002804 ql_log(ql_log_warn, vha, 0x0082,
2805 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002806 } else {
2807 /* Flag a successful rval */
2808 rval = QLA_SUCCESS;
2809 }
2810
2811 return rval;
2812}
2813
Quinn Tranad0a0b02017-12-28 12:33:14 -08002814static void
2815qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002816{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002817 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002818 dma_addr_t tc_dma;
2819 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002820 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002821
Quinn Tranad0a0b02017-12-28 12:33:14 -08002822 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002823 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002824 "%s: Offload Mem is already allocated.\n",
2825 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002826 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002827 }
2828
Quinn Tranad0a0b02017-12-28 12:33:14 -08002829 if (IS_FWI2_CAPABLE(ha)) {
2830 /* Allocate memory for Fibre Channel Event Buffer. */
2831 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2832 !IS_QLA27XX(ha))
2833 goto try_eft;
2834
2835 if (ha->fce)
2836 dma_free_coherent(&ha->pdev->dev,
2837 FCE_SIZE, ha->fce, ha->fce_dma);
2838
2839 /* Allocate memory for Fibre Channel Event Buffer. */
2840 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2841 GFP_KERNEL);
2842 if (!tc) {
2843 ql_log(ql_log_warn, vha, 0x00be,
2844 "Unable to allocate (%d KB) for FCE.\n",
2845 FCE_SIZE / 1024);
2846 goto try_eft;
2847 }
2848
2849 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2850 ha->fce_mb, &ha->fce_bufs);
2851 if (rval) {
2852 ql_log(ql_log_warn, vha, 0x00bf,
2853 "Unable to initialize FCE (%d).\n", rval);
2854 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2855 tc_dma);
2856 ha->flags.fce_enabled = 0;
2857 goto try_eft;
2858 }
2859 ql_dbg(ql_dbg_init, vha, 0x00c0,
2860 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2861
2862 ha->flags.fce_enabled = 1;
2863 ha->fce_dma = tc_dma;
2864 ha->fce = tc;
2865
2866try_eft:
2867 if (ha->eft)
2868 dma_free_coherent(&ha->pdev->dev,
2869 EFT_SIZE, ha->eft, ha->eft_dma);
2870
2871 /* Allocate memory for Extended Trace Buffer. */
2872 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2873 GFP_KERNEL);
2874 if (!tc) {
2875 ql_log(ql_log_warn, vha, 0x00c1,
2876 "Unable to allocate (%d KB) for EFT.\n",
2877 EFT_SIZE / 1024);
2878 goto eft_err;
2879 }
2880
2881 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2882 if (rval) {
2883 ql_log(ql_log_warn, vha, 0x00c2,
2884 "Unable to initialize EFT (%d).\n", rval);
2885 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2886 tc_dma);
2887 goto eft_err;
2888 }
2889 ql_dbg(ql_dbg_init, vha, 0x00c3,
2890 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2891
2892 ha->eft_dma = tc_dma;
2893 ha->eft = tc;
2894 }
2895
2896eft_err:
2897 return;
2898}
2899
2900void
2901qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2902{
2903 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2904 eft_size, fce_size, mq_size;
2905 struct qla_hw_data *ha = vha->hw;
2906 struct req_que *req = ha->req_q_map[0];
2907 struct rsp_que *rsp = ha->rsp_q_map[0];
2908 struct qla2xxx_fw_dump *fw_dump;
2909
Chad Dupuisf73cb692014-02-26 04:15:06 -05002910 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2911 req_q_size = rsp_q_size = 0;
2912
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002913 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2914 fixed_size = sizeof(struct qla2100_fw_dump);
2915 } else if (IS_QLA23XX(ha)) {
2916 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2917 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2918 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002919 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002920 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002921 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2922 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002923 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2924 else if (IS_QLA25XX(ha))
2925 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2926 else
2927 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002928
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002929 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2930 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002931 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002932 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002933 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002934 /*
2935 * Allocate maximum buffer size for all queues.
2936 * Resizing must be done at end-of-dump processing.
2937 */
2938 mq_size += ha->max_req_queues *
2939 (req->length * sizeof(request_t));
2940 mq_size += ha->max_rsp_queues *
2941 (rsp->length * sizeof(response_t));
2942 }
Arun Easi00876ae2013-03-25 02:21:37 -04002943 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002944 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002945 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002946 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2947 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002948 goto try_eft;
2949
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002950 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002951try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002952 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002953 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002954 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002955 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002956
Chad Dupuisf73cb692014-02-26 04:15:06 -05002957 if (IS_QLA27XX(ha)) {
2958 if (!ha->fw_dump_template) {
2959 ql_log(ql_log_warn, vha, 0x00ba,
2960 "Failed missing fwdump template\n");
2961 return;
2962 }
2963 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2964 ql_dbg(ql_dbg_init, vha, 0x00fa,
2965 "-> allocating fwdump (%x bytes)...\n", dump_size);
2966 goto allocate;
2967 }
2968
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002969 req_q_size = req->length * sizeof(request_t);
2970 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002971 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002972 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002973 ha->chain_offset = dump_size;
2974 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002975
Quinn Tranb945e772017-06-13 20:47:29 -07002976 if (ha->exchoffld_buf)
2977 dump_size += sizeof(struct qla2xxx_offld_chain) +
2978 ha->exchoffld_size;
2979 if (ha->exlogin_buf)
2980 dump_size += sizeof(struct qla2xxx_offld_chain) +
2981 ha->exlogin_size;
2982
Chad Dupuisf73cb692014-02-26 04:15:06 -05002983allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08002984 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
2985 fw_dump = vmalloc(dump_size);
2986 if (!fw_dump) {
2987 ql_log(ql_log_warn, vha, 0x00c4,
2988 "Unable to allocate (%d KB) for firmware dump.\n",
2989 dump_size / 1024);
2990 } else {
2991 if (ha->fw_dump)
2992 vfree(ha->fw_dump);
2993 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002994
Quinn Tranad0a0b02017-12-28 12:33:14 -08002995 ha->fw_dump_len = dump_size;
2996 ql_dbg(ql_dbg_init, vha, 0x00c5,
2997 "Allocated (%d KB) for firmware dump.\n",
2998 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002999
Quinn Tranad0a0b02017-12-28 12:33:14 -08003000 if (IS_QLA27XX(ha))
3001 return;
3002
3003 ha->fw_dump->signature[0] = 'Q';
3004 ha->fw_dump->signature[1] = 'L';
3005 ha->fw_dump->signature[2] = 'G';
3006 ha->fw_dump->signature[3] = 'C';
3007 ha->fw_dump->version = htonl(1);
3008
3009 ha->fw_dump->fixed_size = htonl(fixed_size);
3010 ha->fw_dump->mem_size = htonl(mem_size);
3011 ha->fw_dump->req_q_size = htonl(req_q_size);
3012 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3013
3014 ha->fw_dump->eft_size = htonl(eft_size);
3015 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3016 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3017
3018 ha->fw_dump->header_size =
3019 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003020 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003021 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003022}
3023
Andrew Vasquez18e75552009-06-03 09:55:30 -07003024static int
3025qla81xx_mpi_sync(scsi_qla_host_t *vha)
3026{
3027#define MPS_MASK 0xe0
3028 int rval;
3029 uint16_t dc;
3030 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003031
3032 if (!IS_QLA81XX(vha->hw))
3033 return QLA_SUCCESS;
3034
3035 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3036 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003037 ql_log(ql_log_warn, vha, 0x0105,
3038 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003039 goto done;
3040 }
3041
3042 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3043 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3044 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003045 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003046 goto done_release;
3047 }
3048
3049 dc &= MPS_MASK;
3050 if (dc == (dw & MPS_MASK))
3051 goto done_release;
3052
3053 dw &= ~MPS_MASK;
3054 dw |= dc;
3055 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3056 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003057 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003058 }
3059
3060done_release:
3061 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3062 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003063 ql_log(ql_log_warn, vha, 0x006d,
3064 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003065 }
3066
3067done:
3068 return rval;
3069}
3070
Chad Dupuis8d93f552013-01-30 03:34:37 -05003071int
3072qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3073{
3074 /* Don't try to reallocate the array */
3075 if (req->outstanding_cmds)
3076 return QLA_SUCCESS;
3077
Michael Hernandezd7459522016-12-12 14:40:07 -08003078 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003079 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3080 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003081 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3082 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003083 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003084 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003085 }
3086
3087 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3088 req->num_outstanding_cmds, GFP_KERNEL);
3089
3090 if (!req->outstanding_cmds) {
3091 /*
3092 * Try to allocate a minimal size just so we can get through
3093 * initialization.
3094 */
3095 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3096 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3097 req->num_outstanding_cmds, GFP_KERNEL);
3098
3099 if (!req->outstanding_cmds) {
3100 ql_log(ql_log_fatal, NULL, 0x0126,
3101 "Failed to allocate memory for "
3102 "outstanding_cmds for req_que %p.\n", req);
3103 req->num_outstanding_cmds = 0;
3104 return QLA_FUNCTION_FAILED;
3105 }
3106 }
3107
3108 return QLA_SUCCESS;
3109}
3110
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003111#define PRINT_FIELD(_field, _flag, _str) { \
3112 if (a0->_field & _flag) {\
3113 if (p) {\
3114 strcat(ptr, "|");\
3115 ptr++;\
3116 leftover--;\
3117 } \
3118 len = snprintf(ptr, leftover, "%s", _str); \
3119 p = 1;\
3120 leftover -= len;\
3121 ptr += len; \
3122 } \
3123}
3124
3125static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3126{
3127#define STR_LEN 64
3128 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3129 u8 str[STR_LEN], *ptr, p;
3130 int leftover, len;
3131
3132 memset(str, 0, STR_LEN);
3133 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3134 ql_dbg(ql_dbg_init, vha, 0x015a,
3135 "SFP MFG Name: %s\n", str);
3136
3137 memset(str, 0, STR_LEN);
3138 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3139 ql_dbg(ql_dbg_init, vha, 0x015c,
3140 "SFP Part Name: %s\n", str);
3141
3142 /* media */
3143 memset(str, 0, STR_LEN);
3144 ptr = str;
3145 leftover = STR_LEN;
3146 p = len = 0;
3147 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3148 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3149 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3150 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3151 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3152 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3153 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3154 ql_dbg(ql_dbg_init, vha, 0x0160,
3155 "SFP Media: %s\n", str);
3156
3157 /* link length */
3158 memset(str, 0, STR_LEN);
3159 ptr = str;
3160 leftover = STR_LEN;
3161 p = len = 0;
3162 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3163 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3164 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3165 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3166 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3167 ql_dbg(ql_dbg_init, vha, 0x0196,
3168 "SFP Link Length: %s\n", str);
3169
3170 memset(str, 0, STR_LEN);
3171 ptr = str;
3172 leftover = STR_LEN;
3173 p = len = 0;
3174 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3175 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3176 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3177 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3178 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3179 ql_dbg(ql_dbg_init, vha, 0x016e,
3180 "SFP FC Link Tech: %s\n", str);
3181
3182 if (a0->length_km)
3183 ql_dbg(ql_dbg_init, vha, 0x016f,
3184 "SFP Distant: %d km\n", a0->length_km);
3185 if (a0->length_100m)
3186 ql_dbg(ql_dbg_init, vha, 0x0170,
3187 "SFP Distant: %d m\n", a0->length_100m*100);
3188 if (a0->length_50um_10m)
3189 ql_dbg(ql_dbg_init, vha, 0x0189,
3190 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3191 if (a0->length_62um_10m)
3192 ql_dbg(ql_dbg_init, vha, 0x018a,
3193 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3194 if (a0->length_om4_10m)
3195 ql_dbg(ql_dbg_init, vha, 0x0194,
3196 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3197 if (a0->length_om3_10m)
3198 ql_dbg(ql_dbg_init, vha, 0x0195,
3199 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3200}
3201
3202
3203/*
3204 * Return Code:
3205 * QLA_SUCCESS: no action
3206 * QLA_INTERFACE_ERROR: SFP is not there.
3207 * QLA_FUNCTION_FAILED: detected New SFP
3208 */
3209int
3210qla24xx_detect_sfp(scsi_qla_host_t *vha)
3211{
3212 int rc = QLA_SUCCESS;
3213 struct sff_8247_a0 *a;
3214 struct qla_hw_data *ha = vha->hw;
3215
3216 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3217 goto out;
3218
3219 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3220 if (rc)
3221 goto out;
3222
3223 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3224 qla2xxx_print_sfp_info(vha);
3225
3226 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3227 /* long range */
3228 ha->flags.detected_lr_sfp = 1;
3229
3230 if (a->length_km > 5 || a->length_100m > 50)
3231 ha->long_range_distance = LR_DISTANCE_10K;
3232 else
3233 ha->long_range_distance = LR_DISTANCE_5K;
3234
3235 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3236 ql_dbg(ql_dbg_async, vha, 0x507b,
3237 "Detected Long Range SFP.\n");
3238 } else {
3239 /* short range */
3240 ha->flags.detected_lr_sfp = 0;
3241 if (ha->flags.using_lr_setting)
3242 ql_dbg(ql_dbg_async, vha, 0x5084,
3243 "Detected Short Range SFP.\n");
3244 }
3245
3246 if (!vha->flags.init_done)
3247 rc = QLA_SUCCESS;
3248out:
3249 return rc;
3250}
3251
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003252/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 * qla2x00_setup_chip() - Load and start RISC firmware.
3254 * @ha: HA context
3255 *
3256 * Returns 0 on success.
3257 */
3258static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003259qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003261 int rval;
3262 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003263 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003264 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3265 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003266 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003267
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003268 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003269 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003270 if (rval == QLA_SUCCESS) {
3271 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003272 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003273 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003274 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003275 }
3276
Andrew Vasquez3db06522008-01-31 12:33:49 -08003277 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3278 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3279 spin_lock_irqsave(&ha->hardware_lock, flags);
3280 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3281 RD_REG_WORD(&reg->hccr);
3282 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Andrew Vasquez18e75552009-06-03 09:55:30 -07003285 qla81xx_mpi_sync(vha);
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003288 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003289 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003290 ql_dbg(ql_dbg_init, vha, 0x00c9,
3291 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003293 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (rval == QLA_SUCCESS) {
3295 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003296 ql_dbg(ql_dbg_init, vha, 0x00ca,
3297 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003299 if (ql2xexlogins)
3300 ha->flags.exlogins_enabled = 1;
3301
Quinn Tran99e1b682017-06-02 09:12:03 -07003302 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003303 ha->flags.exchoffld_enabled = 1;
3304
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003305 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003307 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003308 qla24xx_detect_sfp(vha);
3309
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003310 rval = qla2x00_set_exlogins_buffer(vha);
3311 if (rval != QLA_SUCCESS)
3312 goto failed;
3313
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003314 rval = qla2x00_set_exchoffld_buffer(vha);
3315 if (rval != QLA_SUCCESS)
3316 goto failed;
3317
Giridhar Malavalia9083012010-04-12 17:59:55 -07003318enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003319 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003320 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003321 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003322 else
3323 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003324 if (rval != QLA_SUCCESS)
3325 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003326 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003327 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003328 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003329 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003330 if ((!ha->max_npiv_vports) ||
3331 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003332 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003333 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003334 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003335 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003336 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003337
Chad Dupuis8d93f552013-01-30 03:34:37 -05003338 /*
3339 * Allocate the array of outstanding commands
3340 * now that we know the firmware resources.
3341 */
3342 rval = qla2x00_alloc_outstanding_cmds(ha,
3343 vha->req);
3344 if (rval != QLA_SUCCESS)
3345 goto failed;
3346
Quinn Tranad0a0b02017-12-28 12:33:14 -08003347 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3348 qla2x00_alloc_offload_mem(vha);
3349
3350 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003351 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003352
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003353 } else {
3354 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003357 ql_log(ql_log_fatal, vha, 0x00cd,
3358 "ISP Firmware failed checksum.\n");
3359 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003361 } else
3362 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Andrew Vasquez3db06522008-01-31 12:33:49 -08003364 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3365 /* Enable proper parity. */
3366 spin_lock_irqsave(&ha->hardware_lock, flags);
3367 if (IS_QLA2300(ha))
3368 /* SRAM parity */
3369 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3370 else
3371 /* SRAM, Instruction RAM and GP RAM parity */
3372 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3373 RD_REG_WORD(&reg->hccr);
3374 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3375 }
3376
Chad Dupuisf3982d82014-09-25 05:16:57 -04003377 if (IS_QLA27XX(ha))
3378 ha->flags.fac_supported = 1;
3379 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003380 uint32_t size;
3381
3382 rval = qla81xx_fac_get_sector_size(vha, &size);
3383 if (rval == QLA_SUCCESS) {
3384 ha->flags.fac_supported = 1;
3385 ha->fdt_block_size = size << 2;
3386 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003387 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003388 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3389 ha->fw_major_version, ha->fw_minor_version,
3390 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003391
Chad Dupuisf73cb692014-02-26 04:15:06 -05003392 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003393 ha->flags.fac_supported = 0;
3394 rval = QLA_SUCCESS;
3395 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003396 }
3397 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003398failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003400 ql_log(ql_log_fatal, vha, 0x00cf,
3401 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
3404 return (rval);
3405}
3406
3407/**
3408 * qla2x00_init_response_q_entries() - Initializes response queue entries.
3409 * @ha: HA context
3410 *
3411 * Beginning of request ring has initialization control block already built
3412 * by nvram config routine.
3413 *
3414 * Returns 0 on success.
3415 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003416void
3417qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418{
3419 uint16_t cnt;
3420 response_t *pkt;
3421
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003422 rsp->ring_ptr = rsp->ring;
3423 rsp->ring_index = 0;
3424 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003425 pkt = rsp->ring_ptr;
3426 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 pkt->signature = RESPONSE_PROCESSED;
3428 pkt++;
3429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
3432/**
3433 * qla2x00_update_fw_options() - Read and process firmware options.
3434 * @ha: HA context
3435 *
3436 * Returns 0 on success.
3437 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003438void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003439qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
3441 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003442 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
3444 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003445 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3448 return;
3449
3450 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003451 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3452 "Serial link options.\n");
3453 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3454 (uint8_t *)&ha->fw_seriallink_options,
3455 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3458 if (ha->fw_seriallink_options[3] & BIT_2) {
3459 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3460
3461 /* 1G settings */
3462 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3463 emphasis = (ha->fw_seriallink_options[2] &
3464 (BIT_4 | BIT_3)) >> 3;
3465 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003466 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 rx_sens = (ha->fw_seriallink_options[0] &
3468 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3469 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3470 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3471 if (rx_sens == 0x0)
3472 rx_sens = 0x3;
3473 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3474 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3475 ha->fw_options[10] |= BIT_5 |
3476 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3477 (tx_sens & (BIT_1 | BIT_0));
3478
3479 /* 2G settings */
3480 swing = (ha->fw_seriallink_options[2] &
3481 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3482 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3483 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003484 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 rx_sens = (ha->fw_seriallink_options[1] &
3486 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3487 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3488 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3489 if (rx_sens == 0x0)
3490 rx_sens = 0x3;
3491 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3492 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3493 ha->fw_options[11] |= BIT_5 |
3494 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3495 (tx_sens & (BIT_1 | BIT_0));
3496 }
3497
3498 /* FCP2 options. */
3499 /* Return command IOCBs without waiting for an ABTS to complete. */
3500 ha->fw_options[3] |= BIT_13;
3501
3502 /* LED scheme. */
3503 if (ha->flags.enable_led_scheme)
3504 ha->fw_options[2] |= BIT_12;
3505
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003506 /* Detect ISP6312. */
3507 if (IS_QLA6312(ha))
3508 ha->fw_options[2] |= BIT_13;
3509
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003510 /* Set Retry FLOGI in case of P2P connection */
3511 if (ha->operating_mode == P2P) {
3512 ha->fw_options[2] |= BIT_3;
3513 ql_dbg(ql_dbg_disc, vha, 0x2100,
3514 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3515 __func__, ha->fw_options[2]);
3516 }
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003519 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003522void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003523qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003524{
3525 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003526 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003527
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003528 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003529 return;
3530
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003531 /* Hold status IOCBs until ABTS response received. */
3532 if (ql2xfwholdabts)
3533 ha->fw_options[3] |= BIT_12;
3534
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003535 /* Set Retry FLOGI in case of P2P connection */
3536 if (ha->operating_mode == P2P) {
3537 ha->fw_options[2] |= BIT_3;
3538 ql_dbg(ql_dbg_disc, vha, 0x2101,
3539 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3540 __func__, ha->fw_options[2]);
3541 }
3542
Quinn Tran41dc5292017-01-19 22:28:03 -08003543 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003544 if (ql2xmvasynctoatio &&
3545 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003546 if (qla_tgt_mode_enabled(vha) ||
3547 qla_dual_mode_enabled(vha))
3548 ha->fw_options[2] |= BIT_11;
3549 else
3550 ha->fw_options[2] &= ~BIT_11;
3551 }
3552
Quinn Tranf7e761f2017-06-02 09:12:02 -07003553 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3554 /*
3555 * Tell FW to track each exchange to prevent
3556 * driver from using stale exchange.
3557 */
3558 if (qla_tgt_mode_enabled(vha) ||
3559 qla_dual_mode_enabled(vha))
3560 ha->fw_options[2] |= BIT_4;
3561 else
3562 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003563
3564 /* Reserve 1/2 of emergency exchanges for ELS.*/
3565 if (qla2xuseresexchforels)
3566 ha->fw_options[2] |= BIT_8;
3567 else
3568 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003569 }
3570
Quinn Tran83548fe2017-06-02 09:12:01 -07003571 ql_dbg(ql_dbg_init, vha, 0x00e8,
3572 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3573 __func__, ha->fw_options[1], ha->fw_options[2],
3574 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003575
3576 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3577 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003578
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003579 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003580 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003581 return;
3582
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003583 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003584 le16_to_cpu(ha->fw_seriallink_options24[1]),
3585 le16_to_cpu(ha->fw_seriallink_options24[2]),
3586 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003587 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003588 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003589 "Unable to update Serial Link options (%x).\n", rval);
3590 }
3591}
3592
3593void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003594qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003595{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003596 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003597 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003598 struct req_que *req = ha->req_q_map[0];
3599 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003600
3601 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003602 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3603 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003604 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3605 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3606 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3607 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3608 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3609 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003610
3611 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3612 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3613 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3614 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3615 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3616}
3617
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003618void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003619qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003620{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003621 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003622 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003623 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3624 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003625 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003626 uint16_t rid = 0;
3627 struct req_que *req = ha->req_q_map[0];
3628 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003629
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003630 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003631 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003632 icb->request_q_outpointer = cpu_to_le16(0);
3633 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003634 icb->request_q_length = cpu_to_le16(req->length);
3635 icb->response_q_length = cpu_to_le16(rsp->length);
3636 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3637 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3638 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3639 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003640
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003641 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003642 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003643 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3644 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3645 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3646
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003647 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003648 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003649
Chad Dupuisf73cb692014-02-26 04:15:06 -05003650 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003651 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3652 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003653 if (ha->flags.msix_enabled) {
3654 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003655 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003656 "Registering vector 0x%x for base que.\n",
3657 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003658 icb->msix = cpu_to_le16(msix->entry);
3659 }
3660 /* Use alternate PCI bus number */
3661 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003662 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003663 /* Use alternate PCI devfn */
3664 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003665 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003666
Anirban Chakraborty31557542009-12-02 10:36:55 -08003667 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003668 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3669 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003670 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003671 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003672 ql_dbg(ql_dbg_init, vha, 0x00fe,
3673 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003674 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003675 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003676 }
Bart Van Asschead950362015-07-09 07:24:08 -07003677 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003678
3679 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3680 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3681 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3682 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3683 } else {
3684 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3685 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3686 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3687 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3688 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003689 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003690
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003691 /* PCI posting */
3692 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003693}
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695/**
3696 * qla2x00_init_rings() - Initializes firmware.
3697 * @ha: HA context
3698 *
3699 * Beginning of request ring has initialization control block already built
3700 * by nvram config routine.
3701 *
3702 * Returns 0 on success.
3703 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003704int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003705qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
3707 int rval;
3708 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003709 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003710 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003711 struct req_que *req;
3712 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003713 struct mid_init_cb_24xx *mid_init_cb =
3714 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
3716 spin_lock_irqsave(&ha->hardware_lock, flags);
3717
3718 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003719 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003720 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003721 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003722 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003723 req->out_ptr = (void *)(req->ring + req->length);
3724 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003725 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003726 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003728 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003729
3730 /* Initialize firmware. */
3731 req->ring_ptr = req->ring;
3732 req->ring_index = 0;
3733 req->cnt = req->length;
3734 }
3735
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003736 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003737 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003738 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003739 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003740 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3741 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003742 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003743 if (IS_QLAFX00(ha))
3744 qlafx00_init_response_q_entries(rsp);
3745 else
3746 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003749 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3750 ha->tgt.atio_ring_index = 0;
3751 /* Initialize ATIO queue entries */
3752 qlt_init_atio_q_entries(vha);
3753
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003754 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3757
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003758 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3759
3760 if (IS_QLAFX00(ha)) {
3761 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3762 goto next_check;
3763 }
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003766 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003768 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003769 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003770 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003771 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003772 }
3773
Andrew Vasquez24a08132009-03-24 09:08:16 -07003774 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003775 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003776 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003777 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003778 ha->flags.dport_enabled =
3779 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3780 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3781 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3782 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003783 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003784 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003785 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003786 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003787 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003788
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003789 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003790next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003792 ql_log(ql_log_fatal, vha, 0x00d2,
3793 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003795 ql_dbg(ql_dbg_init, vha, 0x00d3,
3796 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003797 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
3799
3800 return (rval);
3801}
3802
3803/**
3804 * qla2x00_fw_ready() - Waits for firmware ready.
3805 * @ha: HA context
3806 *
3807 * Returns 0 on success.
3808 */
3809static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003810qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003813 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 uint16_t min_wait; /* Minimum wait time if loop is down */
3815 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003816 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003817 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003819 if (IS_QLAFX00(vha->hw))
3820 return qlafx00_fw_ready(vha);
3821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 rval = QLA_SUCCESS;
3823
Chad Dupuis334614912015-04-09 14:59:57 -04003824 /* Time to wait for loop down */
3825 if (IS_P3P_TYPE(ha))
3826 min_wait = 30;
3827 else
3828 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 /*
3831 * Firmware should take at most one RATOV to login, plus 5 seconds for
3832 * our own processing.
3833 */
3834 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3835 wait_time = min_wait;
3836 }
3837
3838 /* Min wait time if loop down */
3839 mtime = jiffies + (min_wait * HZ);
3840
3841 /* wait time before firmware ready */
3842 wtime = jiffies + (wait_time * HZ);
3843
3844 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003845 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003846 ql_log(ql_log_info, vha, 0x801e,
3847 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
3849 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003850 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003851 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003853 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003854 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003856 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003857 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3858 "fw_state=%x 84xx=%x.\n", state[0],
3859 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003860 if ((state[2] & FSTATE_LOGGED_IN) &&
3861 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003862 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3863 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003864
3865 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003866 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003867 if (rval != QLA_SUCCESS) {
3868 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003869 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003870 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003871 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003872 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003873
3874 /* Add time taken to initialize. */
3875 cs84xx_time = jiffies - cs84xx_time;
3876 wtime += cs84xx_time;
3877 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003878 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003879 "Increasing wait time by %ld. "
3880 "New time %ld.\n", cs84xx_time,
3881 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003882 }
3883 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003884 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3885 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003887 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 &ha->login_timeout, &ha->r_a_tov);
3889
3890 rval = QLA_SUCCESS;
3891 break;
3892 }
3893
3894 rval = QLA_FUNCTION_FAILED;
3895
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003896 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003897 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003899 * other than Wait for Login.
3900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003902 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 "Cable is unplugged...\n");
3904
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003905 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 break;
3907 }
3908 }
3909 } else {
3910 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003911 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003912 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 break;
3914 }
3915
3916 if (time_after_eq(jiffies, wtime))
3917 break;
3918
3919 /* Delay for a while */
3920 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 } while (1);
3922
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003923 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003924 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3925 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Chad Dupuiscfb09192011-11-18 09:03:07 -08003927 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003928 ql_log(ql_log_warn, vha, 0x803b,
3929 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 }
3931
3932 return (rval);
3933}
3934
3935/*
3936* qla2x00_configure_hba
3937* Setup adapter context.
3938*
3939* Input:
3940* ha = adapter state pointer.
3941*
3942* Returns:
3943* 0 = success
3944*
3945* Context:
3946* Kernel context.
3947*/
3948static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003949qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
3951 int rval;
3952 uint16_t loop_id;
3953 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003954 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 uint8_t al_pa;
3956 uint8_t area;
3957 uint8_t domain;
3958 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003959 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003960 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003961 port_id_t id;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08003962 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003965 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003966 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003968 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003969 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003970 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003971 ql_dbg(ql_dbg_disc, vha, 0x2008,
3972 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003973 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003974 ql_log(ql_log_warn, vha, 0x2009,
3975 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003976 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3977 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3978 ql_log(ql_log_warn, vha, 0x1151,
3979 "Doing link init.\n");
3980 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3981 return rval;
3982 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003983 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return (rval);
3986 }
3987
3988 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003989 ql_log(ql_log_info, vha, 0x200a,
3990 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return (QLA_FUNCTION_FAILED);
3992 }
3993
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003994 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 /* initialize */
3997 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3998 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003999 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 switch (topo) {
4002 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004003 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 ha->current_topology = ISP_CFG_NL;
4005 strcpy(connect_type, "(Loop)");
4006 break;
4007
4008 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004009 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004010 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 ha->current_topology = ISP_CFG_FL;
4012 strcpy(connect_type, "(FL_Port)");
4013 break;
4014
4015 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004016 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 ha->operating_mode = P2P;
4018 ha->current_topology = ISP_CFG_N;
4019 strcpy(connect_type, "(N_Port-to-N_Port)");
4020 break;
4021
4022 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004023 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004024 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 ha->operating_mode = P2P;
4026 ha->current_topology = ISP_CFG_F;
4027 strcpy(connect_type, "(F_Port)");
4028 break;
4029
4030 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004031 ql_dbg(ql_dbg_disc, vha, 0x200f,
4032 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 ha->current_topology = ISP_CFG_NL;
4034 strcpy(connect_type, "(Loop)");
4035 break;
4036 }
4037
4038 /* Save Host port and loop ID. */
4039 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004040 id.b.domain = domain;
4041 id.b.area = area;
4042 id.b.al_pa = al_pa;
4043 id.b.rsvd_1 = 0;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004044 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran482c9dc2017-03-15 09:48:54 -07004045 qlt_update_host_map(vha, id);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004046 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004047
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004048 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004049 ql_log(ql_log_info, vha, 0x2010,
4050 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004051 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return(rval);
4054}
4055
Giridhar Malavalia9083012010-04-12 17:59:55 -07004056inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004057qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4058 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004059{
4060 char *st, *en;
4061 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004062 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004063 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004064 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004065
4066 if (memcmp(model, BINZERO, len) != 0) {
4067 strncpy(ha->model_number, model, len);
4068 st = en = ha->model_number;
4069 en += len - 1;
4070 while (en > st) {
4071 if (*en != 0x20 && *en != 0x00)
4072 break;
4073 *en-- = '\0';
4074 }
4075
4076 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004077 if (use_tbl &&
4078 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004079 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004080 strncpy(ha->model_desc,
4081 qla2x00_model_name[index * 2 + 1],
4082 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004083 } else {
4084 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004085 if (use_tbl &&
4086 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004087 index < QLA_MODEL_NAMES) {
4088 strcpy(ha->model_number,
4089 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004090 strncpy(ha->model_desc,
4091 qla2x00_model_name[index * 2 + 1],
4092 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004093 } else {
4094 strcpy(ha->model_number, def);
4095 }
4096 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004097 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004098 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004099 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004100}
4101
David Miller4e08df32007-04-16 12:37:43 -07004102/* On sparc systems, obtain port and node WWN from firmware
4103 * properties.
4104 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004105static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004106{
4107#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004108 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004109 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004110 struct device_node *dp = pci_device_to_OF_node(pdev);
4111 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004112 int len;
4113
4114 val = of_get_property(dp, "port-wwn", &len);
4115 if (val && len >= WWN_SIZE)
4116 memcpy(nv->port_name, val, WWN_SIZE);
4117
4118 val = of_get_property(dp, "node-wwn", &len);
4119 if (val && len >= WWN_SIZE)
4120 memcpy(nv->node_name, val, WWN_SIZE);
4121#endif
4122}
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124/*
4125* NVRAM configuration for ISP 2xxx
4126*
4127* Input:
4128* ha = adapter block pointer.
4129*
4130* Output:
4131* initialization control block in response_ring
4132* host adapters parameters in host adapter block
4133*
4134* Returns:
4135* 0 = success.
4136*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004137int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004138qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
David Miller4e08df32007-04-16 12:37:43 -07004140 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004141 uint8_t chksum = 0;
4142 uint16_t cnt;
4143 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004144 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004145 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004146 nvram_t *nv = ha->nvram;
4147 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004148 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
David Miller4e08df32007-04-16 12:37:43 -07004150 rval = QLA_SUCCESS;
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004153 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 ha->nvram_base = 0;
4155 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4156 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4157 ha->nvram_base = 0x80;
4158
4159 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004160 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004161 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4162 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004164 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4165 "Contents of NVRAM.\n");
4166 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4167 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 /* Bad NVRAM data, set defaults parameters. */
4170 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4171 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4172 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004173 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004174 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004175 "detected: checksum=0x%x id=%c version=0x%x.\n",
4176 chksum, nv->id[0], nv->nvram_version);
4177 ql_log(ql_log_warn, vha, 0x0065,
4178 "Falling back to "
4179 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004180
4181 /*
4182 * Set default initialization control block.
4183 */
4184 memset(nv, 0, ha->nvram_size);
4185 nv->parameter_block_version = ICB_VERSION;
4186
4187 if (IS_QLA23XX(ha)) {
4188 nv->firmware_options[0] = BIT_2 | BIT_1;
4189 nv->firmware_options[1] = BIT_7 | BIT_5;
4190 nv->add_firmware_options[0] = BIT_5;
4191 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004192 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004193 nv->special_options[1] = BIT_7;
4194 } else if (IS_QLA2200(ha)) {
4195 nv->firmware_options[0] = BIT_2 | BIT_1;
4196 nv->firmware_options[1] = BIT_7 | BIT_5;
4197 nv->add_firmware_options[0] = BIT_5;
4198 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004199 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004200 } else if (IS_QLA2100(ha)) {
4201 nv->firmware_options[0] = BIT_3 | BIT_1;
4202 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004203 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004204 }
4205
Bart Van Asschead950362015-07-09 07:24:08 -07004206 nv->max_iocb_allocation = cpu_to_le16(256);
4207 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004208 nv->retry_count = 8;
4209 nv->retry_delay = 1;
4210
4211 nv->port_name[0] = 33;
4212 nv->port_name[3] = 224;
4213 nv->port_name[4] = 139;
4214
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004215 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004216
4217 nv->login_timeout = 4;
4218
4219 /*
4220 * Set default host adapter parameters
4221 */
4222 nv->host_p[1] = BIT_2;
4223 nv->reset_delay = 5;
4224 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004225 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004226 nv->link_down_timeout = 60;
4227
4228 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
4230
4231#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4232 /*
4233 * The SN2 does not provide BIOS emulation which means you can't change
4234 * potentially bogus BIOS settings. Force the use of default settings
4235 * for link rate and frame size. Hope that the rest of the settings
4236 * are valid.
4237 */
4238 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004239 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 if (IS_QLA23XX(ha))
4241 nv->special_options[1] = BIT_7;
4242 }
4243#endif
4244
4245 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004246 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 /*
4249 * Setup driver NVRAM options.
4250 */
4251 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4252 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4253 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4254 nv->firmware_options[1] &= ~BIT_4;
4255
4256 if (IS_QLA23XX(ha)) {
4257 nv->firmware_options[0] |= BIT_2;
4258 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004259 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004260 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 if (IS_QLA2300(ha)) {
4263 if (ha->fb_rev == FPM_2310) {
4264 strcpy(ha->model_number, "QLA2310");
4265 } else {
4266 strcpy(ha->model_number, "QLA2300");
4267 }
4268 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004269 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004270 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 }
4272 } else if (IS_QLA2200(ha)) {
4273 nv->firmware_options[0] |= BIT_2;
4274 /*
4275 * 'Point-to-point preferred, else loop' is not a safe
4276 * connection mode setting.
4277 */
4278 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4279 (BIT_5 | BIT_4)) {
4280 /* Force 'loop preferred, else point-to-point'. */
4281 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4282 nv->add_firmware_options[0] |= BIT_5;
4283 }
4284 strcpy(ha->model_number, "QLA22xx");
4285 } else /*if (IS_QLA2100(ha))*/ {
4286 strcpy(ha->model_number, "QLA2100");
4287 }
4288
4289 /*
4290 * Copy over NVRAM RISC parameter block to initialization control block.
4291 */
4292 dptr1 = (uint8_t *)icb;
4293 dptr2 = (uint8_t *)&nv->parameter_block_version;
4294 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4295 while (cnt--)
4296 *dptr1++ = *dptr2++;
4297
4298 /* Copy 2nd half. */
4299 dptr1 = (uint8_t *)icb->add_firmware_options;
4300 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4301 while (cnt--)
4302 *dptr1++ = *dptr2++;
4303
Andrew Vasquez5341e862006-05-17 15:09:16 -07004304 /* Use alternate WWN? */
4305 if (nv->host_p[1] & BIT_7) {
4306 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4307 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4308 }
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 /* Prepare nodename */
4311 if ((icb->firmware_options[1] & BIT_6) == 0) {
4312 /*
4313 * Firmware will apply the following mask if the nodename was
4314 * not provided.
4315 */
4316 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4317 icb->node_name[0] &= 0xF0;
4318 }
4319
4320 /*
4321 * Set host adapter parameters.
4322 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004323
4324 /*
4325 * BIT_7 in the host-parameters section allows for modification to
4326 * internal driver logging.
4327 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004328 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004329 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4331 /* Always load RISC code on non ISP2[12]00 chips. */
4332 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4333 ha->flags.disable_risc_code_load = 0;
4334 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4335 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4336 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004337 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004338 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 ha->operating_mode =
4341 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4342
4343 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4344 sizeof(ha->fw_seriallink_options));
4345
4346 /* save HBA serial number */
4347 ha->serial0 = icb->port_name[5];
4348 ha->serial1 = icb->port_name[6];
4349 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004350 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4351 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Bart Van Asschead950362015-07-09 07:24:08 -07004353 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
4355 ha->retry_count = nv->retry_count;
4356
4357 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004358 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 nv->login_timeout = ql2xlogintimeout;
4360 if (nv->login_timeout < 4)
4361 nv->login_timeout = 4;
4362 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004364 /* Set minimum RATOV to 100 tenths of a second. */
4365 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 ha->loop_reset_delay = nv->reset_delay;
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 /* Link Down Timeout = 0:
4370 *
4371 * When Port Down timer expires we will start returning
4372 * I/O's to OS with "DID_NO_CONNECT".
4373 *
4374 * Link Down Timeout != 0:
4375 *
4376 * The driver waits for the link to come up after link down
4377 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (nv->link_down_timeout == 0) {
4380 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004381 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 } else {
4383 ha->link_down_timeout = nv->link_down_timeout;
4384 ha->loop_down_abort_time =
4385 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 /*
4389 * Need enough time to try and get the port back.
4390 */
4391 ha->port_down_retry_count = nv->port_down_retry_count;
4392 if (qlport_down_retry)
4393 ha->port_down_retry_count = qlport_down_retry;
4394 /* Set login_retry_count */
4395 ha->login_retry_count = nv->retry_count;
4396 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4397 ha->port_down_retry_count > 3)
4398 ha->login_retry_count = ha->port_down_retry_count;
4399 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4400 ha->login_retry_count = ha->port_down_retry_count;
4401 if (ql2xloginretrycount)
4402 ha->login_retry_count = ql2xloginretrycount;
4403
Bart Van Asschead950362015-07-09 07:24:08 -07004404 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 icb->command_resource_count = 0;
4406 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004407 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4410 /* Enable RIO */
4411 icb->firmware_options[0] &= ~BIT_3;
4412 icb->add_firmware_options[0] &=
4413 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4414 icb->add_firmware_options[0] |= BIT_2;
4415 icb->response_accumulation_timer = 3;
4416 icb->interrupt_delay_timer = 5;
4417
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004418 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004420 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004421 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004422 ha->zio_mode = icb->add_firmware_options[0] &
4423 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4424 ha->zio_timer = icb->interrupt_delay_timer ?
4425 icb->interrupt_delay_timer: 2;
4426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 icb->add_firmware_options[0] &=
4428 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004429 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004430 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004431 ha->zio_mode = QLA_ZIO_MODE_6;
4432
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004433 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004434 "ZIO mode %d enabled; timer delay (%d us).\n",
4435 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004437 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4438 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004439 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 }
4441 }
4442
David Miller4e08df32007-04-16 12:37:43 -07004443 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004444 ql_log(ql_log_warn, vha, 0x0069,
4445 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004446 }
4447 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448}
4449
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004450static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004451qla2x00_rport_del(void *data)
4452{
4453 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004454 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004455 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004456
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004457 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004458 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004459 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004460 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004461 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004462 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4463 "%s %8phN. rport %p roles %x\n",
4464 __func__, fcport->port_name, rport,
4465 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004466
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004467 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004468 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471/**
4472 * qla2x00_alloc_fcport() - Allocate a generic fcport.
4473 * @ha: HA context
4474 * @flags: allocation flags
4475 *
4476 * Returns a pointer to the allocated fcport, or NULL, if none available.
4477 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004478fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004479qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
4481 fc_port_t *fcport;
4482
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004483 fcport = kzalloc(sizeof(fc_port_t), flags);
4484 if (!fcport)
4485 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
4487 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004488 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 fcport->port_type = FCT_UNKNOWN;
4490 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004491 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004492 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Quinn Tran726b8542017-01-19 22:28:00 -08004494 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4495 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004496 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004497 fcport->disc_state = DSC_DELETED;
4498 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4499 fcport->deleted = QLA_SESS_DELETED;
4500 fcport->login_retry = vha->hw->login_retry_count;
4501 fcport->login_retry = 5;
4502 fcport->logout_on_delete = 1;
4503
4504 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004505 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004506 "Failed to allocate ct_sns request.\n");
4507 kfree(fcport);
4508 fcport = NULL;
4509 }
4510 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4511 INIT_LIST_HEAD(&fcport->gnl_entry);
4512 INIT_LIST_HEAD(&fcport->list);
4513
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004514 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Quinn Tran726b8542017-01-19 22:28:00 -08004517void
4518qla2x00_free_fcport(fc_port_t *fcport)
4519{
4520 if (fcport->ct_desc.ct_sns) {
4521 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4522 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4523 fcport->ct_desc.ct_sns_dma);
4524
4525 fcport->ct_desc.ct_sns = NULL;
4526 }
4527 kfree(fcport);
4528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530/*
4531 * qla2x00_configure_loop
4532 * Updates Fibre Channel Device Database with what is actually on loop.
4533 *
4534 * Input:
4535 * ha = adapter block pointer.
4536 *
4537 * Returns:
4538 * 0 = success.
4539 * 1 = error.
4540 * 2 = database was full and device was not configured.
4541 */
4542static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004543qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
4545 int rval;
4546 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004547 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 rval = QLA_SUCCESS;
4549
4550 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004551 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4552 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004554 ql_dbg(ql_dbg_disc, vha, 0x2013,
4555 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return (rval);
4557 }
4558 }
4559
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004560 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004561 ql_dbg(ql_dbg_disc, vha, 0x2014,
4562 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564 /*
4565 * If we have both an RSCN and PORT UPDATE pending then handle them
4566 * both at the same time.
4567 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004568 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4569 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Michael Hernandez3064ff32009-12-15 21:29:44 -08004571 qla2x00_get_data_rate(vha);
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 /* Determine what we need to do */
4574 if (ha->current_topology == ISP_CFG_FL &&
4575 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 set_bit(RSCN_UPDATE, &flags);
4578
4579 } else if (ha->current_topology == ISP_CFG_F &&
4580 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 set_bit(RSCN_UPDATE, &flags);
4583 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4584
Andrew Vasquez21333b42006-05-17 15:09:56 -07004585 } else if (ha->current_topology == ISP_CFG_N) {
4586 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004587 if (ha->flags.rida_fmt2) {
4588 /* With Rida Format 2, the login is already triggered.
4589 * We know who is on the other side of the wire.
4590 * No need to login to do login to find out or drop into
4591 * qla2x00_configure_local_loop().
4592 */
4593 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4594 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4595 } else {
4596 if (qla_tgt_mode_enabled(vha)) {
4597 /* allow the other side to start the login */
4598 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4599 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4600 }
4601 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004602 } else if (ha->current_topology == ISP_CFG_NL) {
4603 clear_bit(RSCN_UPDATE, &flags);
4604 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004605 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 set_bit(RSCN_UPDATE, &flags);
4608 set_bit(LOCAL_LOOP_UPDATE, &flags);
4609 }
4610
4611 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004612 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4613 ql_dbg(ql_dbg_disc, vha, 0x2015,
4614 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004616 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004617 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
4619
4620 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004621 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004622 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004623 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004625 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004626 else
4627 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
4629
4630 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004631 if (atomic_read(&vha->loop_down_timer) ||
4632 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 rval = QLA_FUNCTION_FAILED;
4634 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004635 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004636 ql_dbg(ql_dbg_disc, vha, 0x2069,
4637 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004638 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004639
4640 /*
4641 * Process any ATIO queue entries that came in
4642 * while we weren't online.
4643 */
Quinn Tranead03852017-01-19 22:28:01 -08004644 if (qla_tgt_mode_enabled(vha) ||
4645 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004646 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4647 spin_lock_irqsave(&ha->tgt.atio_lock,
4648 flags);
4649 qlt_24xx_process_atio_queue(vha, 0);
4650 spin_unlock_irqrestore(
4651 &ha->tgt.atio_lock, flags);
4652 } else {
4653 spin_lock_irqsave(&ha->hardware_lock,
4654 flags);
4655 qlt_24xx_process_atio_queue(vha, 1);
4656 spin_unlock_irqrestore(
4657 &ha->hardware_lock, flags);
4658 }
4659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
4661 }
4662
4663 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004664 ql_dbg(ql_dbg_disc, vha, 0x206a,
4665 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004667 ql_dbg(ql_dbg_disc, vha, 0x206b,
4668 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 }
4670
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004671 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004672 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004674 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004675 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004676 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
4679
4680 return (rval);
4681}
4682
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004683/*
4684 * N2N Login
4685 * Updates Fibre Channel Device Database with local loop devices.
4686 *
4687 * Input:
4688 * ha = adapter block pointer.
4689 *
4690 * Returns:
4691 */
4692static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4693 fc_port_t *fcport)
4694{
4695 struct qla_hw_data *ha = vha->hw;
4696 int res = QLA_SUCCESS, rval;
4697 int greater_wwpn = 0;
4698 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004700 if (ha->current_topology != ISP_CFG_N)
4701 return res;
4702
4703 if (wwn_to_u64(vha->port_name) >
4704 wwn_to_u64(vha->n2n_port_name)) {
4705 ql_dbg(ql_dbg_disc, vha, 0x2002,
4706 "HBA WWPN is greater %llx > target %llx\n",
4707 wwn_to_u64(vha->port_name),
4708 wwn_to_u64(vha->n2n_port_name));
4709 greater_wwpn = 1;
4710 fcport->d_id.b24 = vha->n2n_id;
4711 }
4712
4713 fcport->loop_id = vha->loop_id;
4714 fcport->fc4f_nvme = 0;
4715 fcport->query = 1;
4716
4717 ql_dbg(ql_dbg_disc, vha, 0x4001,
4718 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4719 fcport->d_id.b24, vha->loop_id);
4720
4721 /* Fill in member data. */
4722 if (!greater_wwpn) {
4723 rval = qla2x00_get_port_database(vha, fcport, 0);
4724 ql_dbg(ql_dbg_disc, vha, 0x1051,
4725 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4726 fcport->current_login_state, fcport->last_login_state,
4727 fcport->d_id.b24, fcport->loop_id, rval);
4728
4729 if (((fcport->current_login_state & 0xf) == 0x4) ||
4730 ((fcport->current_login_state & 0xf) == 0x6))
4731 logged_in = 1;
4732 }
4733
4734 if (logged_in || greater_wwpn) {
4735 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4736 qla_nvme_register_hba(vha);
4737
4738 /* Set connected N_Port d_id */
4739 if (vha->flags.nvme_enabled)
4740 fcport->fc4f_nvme = 1;
4741
4742 fcport->scan_state = QLA_FCPORT_FOUND;
4743 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4744 fcport->disc_state = DSC_GNL;
4745 fcport->n2n_flag = 1;
4746 fcport->flags = 3;
4747 vha->hw->flags.gpsc_supported = 0;
4748
4749 if (greater_wwpn) {
4750 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4751 "%s %d PLOGI ELS %8phC\n",
4752 __func__, __LINE__, fcport->port_name);
4753
4754 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4755 fcport, fcport->d_id);
4756 }
4757
4758 if (res != QLA_SUCCESS) {
4759 ql_log(ql_log_info, vha, 0xd04d,
4760 "PLOGI Failed: portid=%06x - retrying\n",
4761 fcport->d_id.b24);
4762 res = QLA_SUCCESS;
4763 } else {
4764 /* State 0x6 means FCP PRLI complete */
4765 if ((fcport->current_login_state & 0xf) == 0x6) {
4766 ql_dbg(ql_dbg_disc, vha, 0x2118,
4767 "%s %d %8phC post GPDB work\n",
4768 __func__, __LINE__, fcport->port_name);
4769 fcport->chip_reset =
4770 vha->hw->base_qpair->chip_reset;
4771 qla24xx_post_gpdb_work(vha, fcport, 0);
4772 } else {
4773 ql_dbg(ql_dbg_disc, vha, 0x2118,
4774 "%s %d %8phC post NVMe PRLI\n",
4775 __func__, __LINE__, fcport->port_name);
4776 qla24xx_post_prli_work(vha, fcport);
4777 }
4778 }
4779 } else {
4780 /* Wait for next database change */
4781 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4782 }
4783
4784 return res;
4785}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787/*
4788 * qla2x00_configure_local_loop
4789 * Updates Fibre Channel Device Database with local loop devices.
4790 *
4791 * Input:
4792 * ha = adapter block pointer.
4793 *
4794 * Returns:
4795 * 0 = success.
4796 */
4797static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004798qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
4800 int rval, rval2;
4801 int found_devs;
4802 int found;
4803 fc_port_t *fcport, *new_fcport;
4804
4805 uint16_t index;
4806 uint16_t entries;
4807 char *id_iter;
4808 uint16_t loop_id;
4809 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004810 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004811 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 found_devs = 0;
4814 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004815 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004818 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004819 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 &entries);
4821 if (rval != QLA_SUCCESS)
4822 goto cleanup_allocation;
4823
Quinn Tran83548fe2017-06-02 09:12:01 -07004824 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004825 "Entries in ID list (%d).\n", entries);
4826 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4827 (uint8_t *)ha->gid_list,
4828 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Quinn Tran9cd883f2017-12-28 12:33:24 -08004830 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4831 fcport->scan_state = QLA_FCPORT_SCAN;
4832 }
4833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004835 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004837 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004838 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 rval = QLA_MEMORY_ALLOC_FAILED;
4840 goto cleanup_allocation;
4841 }
4842 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4843
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004844 /* Inititae N2N login. */
4845 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4846 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4847 if (rval != QLA_SUCCESS)
4848 goto cleanup_allocation;
4849 return QLA_SUCCESS;
4850 }
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 /* Add devices to port list. */
4853 id_iter = (char *)ha->gid_list;
4854 for (index = 0; index < entries; index++) {
4855 domain = ((struct gid_list_info *)id_iter)->domain;
4856 area = ((struct gid_list_info *)id_iter)->area;
4857 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004858 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 loop_id = (uint16_t)
4860 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004861 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 loop_id = le16_to_cpu(
4863 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004864 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 /* Bypass reserved domain fields. */
4867 if ((domain & 0xf0) == 0xf0)
4868 continue;
4869
4870 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004871 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004872 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 continue;
4874
4875 /* Bypass invalid local loop ID. */
4876 if (loop_id > LAST_LOCAL_LOOP_ID)
4877 continue;
4878
Quinn Tran41dc5292017-01-19 22:28:03 -08004879 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 /* Fill in member data. */
4882 new_fcport->d_id.b.domain = domain;
4883 new_fcport->d_id.b.area = area;
4884 new_fcport->d_id.b.al_pa = al_pa;
4885 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004886 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004887
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004888 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004890 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004891 "Failed to retrieve fcport information "
4892 "-- get_port_database=%x, loop_id=0x%04x.\n",
4893 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004894 /* Skip retry if N2N */
4895 if (ha->current_topology != ISP_CFG_N) {
4896 ql_dbg(ql_dbg_disc, vha, 0x2105,
4897 "Scheduling resync.\n");
4898 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4899 continue;
4900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 }
4902
Quinn Tran41dc5292017-01-19 22:28:03 -08004903 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 /* Check for matching device in port list. */
4905 found = 0;
4906 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004907 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 if (memcmp(new_fcport->port_name, fcport->port_name,
4909 WWN_SIZE))
4910 continue;
4911
Shyam Sundarddb9b122009-03-24 09:08:10 -07004912 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 fcport->loop_id = new_fcport->loop_id;
4914 fcport->port_type = new_fcport->port_type;
4915 fcport->d_id.b24 = new_fcport->d_id.b24;
4916 memcpy(fcport->node_name, new_fcport->node_name,
4917 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004918 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 found++;
4920 break;
4921 }
4922
4923 if (!found) {
4924 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004925 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 /* Allocate a new replacement fcport. */
4928 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004929
4930 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4931
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004932 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004935 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004936 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 rval = QLA_MEMORY_ALLOC_FAILED;
4938 goto cleanup_allocation;
4939 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004940 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4942 }
4943
Quinn Tran41dc5292017-01-19 22:28:03 -08004944 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4945
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004946 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004947 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 found_devs++;
4950 }
4951
Quinn Tran9cd883f2017-12-28 12:33:24 -08004952 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4953 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4954 break;
4955
4956 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4957 if ((qla_dual_mode_enabled(vha) ||
4958 qla_ini_mode_enabled(vha)) &&
4959 atomic_read(&fcport->state) == FCS_ONLINE) {
4960 qla2x00_mark_device_lost(vha, fcport,
4961 ql2xplogiabsentdevice, 0);
4962 if (fcport->loop_id != FC_NO_LOOP_ID &&
4963 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4964 fcport->port_type != FCT_INITIATOR &&
4965 fcport->port_type != FCT_BROADCAST) {
4966 ql_dbg(ql_dbg_disc, vha, 0x20f0,
4967 "%s %d %8phC post del sess\n",
4968 __func__, __LINE__,
4969 fcport->port_name);
4970
Quinn Trand8630bb2017-12-28 12:33:43 -08004971 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004972 continue;
4973 }
4974 }
4975 }
4976
4977 if (fcport->scan_state == QLA_FCPORT_FOUND)
4978 qla24xx_fcport_handle_login(vha, fcport);
4979 }
4980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004982 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004985 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004986 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 return (rval);
4990}
4991
4992static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004993qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004994{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004995 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004996 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004997 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004998
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004999 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005000 return;
5001
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07005002 if (atomic_read(&fcport->state) != FCS_ONLINE)
5003 return;
5004
Andrew Vasquez39bd9622007-09-20 14:07:34 -07005005 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5006 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005007 return;
5008
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005009 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07005010 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005011 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005012 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005013 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5014 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005015 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005016 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005017 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05005018 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005019 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005020 }
5021}
5022
Quinn Tran726b8542017-01-19 22:28:00 -08005023/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005024static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005025qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005026{
5027 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005028 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005029 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005030
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005031 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5032 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005033 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5034 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5035 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005036 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005037 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005038 ql_log(ql_log_warn, vha, 0x2006,
5039 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005040 return;
5041 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005042
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005043 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005044 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005045 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005046
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005047 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005048
5049 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005050 if (fcport->port_type == FCT_INITIATOR)
5051 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5052 if (fcport->port_type == FCT_TARGET)
5053 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005054
Quinn Tran83548fe2017-06-02 09:12:01 -07005055 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5056 "%s %8phN. rport %p is %s mode\n",
5057 __func__, fcport->port_name, rport,
5058 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005059
Andrew Vasquez77d74142005-07-08 18:00:36 -07005060 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005064 * qla2x00_update_fcport
5065 * Updates device on list.
5066 *
5067 * Input:
5068 * ha = adapter block pointer.
5069 * fcport = port structure pointer.
5070 *
5071 * Return:
5072 * 0 - Success
5073 * BIT_0 - error
5074 *
5075 * Context:
5076 * Kernel context.
5077 */
5078void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005079qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005080{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005081 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005082
Quinn Tran726b8542017-01-19 22:28:00 -08005083 if (IS_SW_RESV_ADDR(fcport->d_id))
5084 return;
5085
Quinn Tran83548fe2017-06-02 09:12:01 -07005086 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005087 __func__, fcport->port_name);
5088
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005089 if (IS_QLAFX00(vha->hw)) {
5090 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005091 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005092 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005093 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005094 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005095 fcport->disc_state = DSC_LOGIN_COMPLETE;
5096 fcport->deleted = 0;
5097 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005098
Duane Grigsbye84067d2017-06-21 13:48:43 -07005099 if (fcport->fc4f_nvme) {
5100 qla_nvme_register_remote(vha, fcport);
5101 return;
5102 }
5103
Joe Carnuccio1f93da522012-11-21 02:40:38 -05005104 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005105 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005106 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005107
5108reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005109 switch (vha->host->active_mode) {
5110 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005111 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005112 break;
5113 case MODE_TARGET:
5114 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5115 !vha->vha_tgt.qla_tgt->tgt_stopped)
5116 qlt_fc_port_added(vha, fcport);
5117 break;
5118 case MODE_DUAL:
5119 qla2x00_reg_remote_port(vha, fcport);
5120 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5121 !vha->vha_tgt.qla_tgt->tgt_stopped)
5122 qlt_fc_port_added(vha, fcport);
5123 break;
5124 default:
5125 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005126 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005127}
5128
5129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 * qla2x00_configure_fabric
5131 * Setup SNS devices with loop ID's.
5132 *
5133 * Input:
5134 * ha = adapter block pointer.
5135 *
5136 * Returns:
5137 * 0 = success.
5138 * BIT_0 = error
5139 */
5140static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005141qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142{
Arun Easib3b02e62012-02-09 11:15:39 -08005143 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005144 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005146 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005148 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005149 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005152 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005153 loop_id = NPH_F_PORT;
5154 else
5155 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005156 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005158 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005159 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005161 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return (QLA_SUCCESS);
5163 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005164 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Quinn Tran41dc5292017-01-19 22:28:03 -08005166
5167 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5168 rval = qla2x00_send_change_request(vha, 0x3, 0);
5169 if (rval != QLA_SUCCESS)
5170 ql_log(ql_log_warn, vha, 0x121,
5171 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5172 rval);
5173 }
5174
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005177 qla2x00_mgmt_svr_login(vha);
5178
Andrew Vasquezcca53352005-08-26 19:08:30 -07005179 /* FDMI support. */
5180 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005181 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5182 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005185 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005186 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5187 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005188 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5189 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5190 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5191 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005192 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005193 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005194 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005195 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5196 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005198 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005199 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005200 if (test_bit(LOOP_RESYNC_NEEDED,
5201 &vha->dpc_flags))
5202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005204 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005206 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005207 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005208 if (test_bit(LOOP_RESYNC_NEEDED,
5209 &vha->dpc_flags))
5210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005212 if (vha->flags.nvme_enabled) {
5213 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5214 ql_dbg(ql_dbg_disc, vha, 0x2049,
5215 "Register NVME FC Type Features failed.\n");
5216 }
5217 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005218 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005220 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005221 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005222 if (test_bit(LOOP_RESYNC_NEEDED,
5223 &vha->dpc_flags))
5224 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005225 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005227 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005228 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005229 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 }
5232 }
5233
Joe Carnuccio827210b2013-02-08 01:57:57 -05005234
Alexei Potashnikdf673272015-07-14 16:00:46 -04005235 /* Mark the time right before querying FW for connected ports.
5236 * This process is long, asynchronous and by the time it's done,
5237 * collected information might not be accurate anymore. E.g.
5238 * disconnected port might have re-connected and a brand new
5239 * session has been created. In this case session's generation
5240 * will be newer than discovery_gen. */
5241 qlt_do_generation_tick(vha, &discovery_gen);
5242
Quinn Trana4239942017-12-28 12:33:26 -08005243 if (USE_ASYNC_SCAN(ha)) {
5244 rval = QLA_SUCCESS;
5245 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5246 if (rval)
5247 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5248 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005249 list_for_each_entry(fcport, &vha->vp_fcports, list)
5250 fcport->scan_state = QLA_FCPORT_SCAN;
5251
Quinn Trana4239942017-12-28 12:33:26 -08005252 rval = qla2x00_find_all_fabric_devs(vha);
5253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 if (rval != QLA_SUCCESS)
5255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 } while (0);
5257
Duane Grigsbye84067d2017-06-21 13:48:43 -07005258 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5259 qla_nvme_register_hba(vha);
5260
Quinn Tran726b8542017-01-19 22:28:00 -08005261 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005262 ql_dbg(ql_dbg_disc, vha, 0x2068,
5263 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
5265 return (rval);
5266}
5267
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268/*
5269 * qla2x00_find_all_fabric_devs
5270 *
5271 * Input:
5272 * ha = adapter block pointer.
5273 * dev = database device entry pointer.
5274 *
5275 * Returns:
5276 * 0 = success.
5277 *
5278 * Context:
5279 * Kernel context.
5280 */
5281static int
Quinn Tran726b8542017-01-19 22:28:00 -08005282qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283{
5284 int rval;
5285 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005286 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 int found;
5288
5289 sw_info_t *swl;
5290 int swl_idx;
5291 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005292 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005293 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005294 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005295 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 rval = QLA_SUCCESS;
5298
5299 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005300 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005301 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005302 GFP_KERNEL);
5303 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005304 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005306 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005307 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005309 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005310 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005312 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5313 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005314 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005316 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5317 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005318 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005320 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5321 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005322 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5323 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005324 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5325 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005327
5328 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005329 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005330 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005331 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5332 return rval;
5333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
5335 swl_idx = 0;
5336
5337 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005338 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005340 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005341 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 return (QLA_MEMORY_ALLOC_FAILED);
5343 }
5344 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 /* Set start port ID scan at adapter ID. */
5346 first_dev = 1;
5347 last_dev = 0;
5348
5349 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005350 loop_id = ha->min_external_loopid;
5351 for (; loop_id <= ha->max_loop_id; loop_id++) {
5352 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 continue;
5354
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005355 if (ha->current_topology == ISP_CFG_FL &&
5356 (atomic_read(&vha->loop_down_timer) ||
5357 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005358 atomic_set(&vha->loop_down_timer, 0);
5359 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5360 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
5364 if (swl != NULL) {
5365 if (last_dev) {
5366 wrap.b24 = new_fcport->d_id.b24;
5367 } else {
5368 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5369 memcpy(new_fcport->node_name,
5370 swl[swl_idx].node_name, WWN_SIZE);
5371 memcpy(new_fcport->port_name,
5372 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005373 memcpy(new_fcport->fabric_port_name,
5374 swl[swl_idx].fabric_port_name, WWN_SIZE);
5375 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005376 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005378 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005379 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005380 if (vha->flags.nvme_enabled &&
5381 swl[swl_idx].fc4f_nvme) {
5382 new_fcport->fc4f_nvme =
5383 swl[swl_idx].fc4f_nvme;
5384 ql_log(ql_log_info, vha, 0x2131,
5385 "FOUND: NVME port %8phC as FC Type 28h\n",
5386 new_fcport->port_name);
5387 }
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5390 last_dev = 1;
5391 }
5392 swl_idx++;
5393 }
5394 } else {
5395 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005396 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005398 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005399 "SNS scan failed -- assuming "
5400 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 rval = QLA_SUCCESS;
5402 break;
5403 }
5404 }
5405
5406 /* If wrap on switch device list, exit. */
5407 if (first_dev) {
5408 wrap.b24 = new_fcport->d_id.b24;
5409 first_dev = 0;
5410 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005411 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005412 "Device wrap (%02x%02x%02x).\n",
5413 new_fcport->d_id.b.domain,
5414 new_fcport->d_id.b.area,
5415 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 break;
5417 }
5418
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005419 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005420 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 continue;
5422
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005423 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005424 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5425 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005426
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005427 /* Bypass if same domain and area of adapter. */
5428 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005429 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005430 ISP_CFG_FL)
5431 continue;
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /* Bypass reserved domain fields. */
5434 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5435 continue;
5436
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005437 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005438 if (ql2xgffidenable &&
5439 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5440 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005441 continue;
5442
Quinn Tran726b8542017-01-19 22:28:00 -08005443 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5444
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 /* Locate matching device in database. */
5446 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005447 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 if (memcmp(new_fcport->port_name, fcport->port_name,
5449 WWN_SIZE))
5450 continue;
5451
Joe Carnuccio827210b2013-02-08 01:57:57 -05005452 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005453
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 found++;
5455
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005456 /* Update port state. */
5457 memcpy(fcport->fabric_port_name,
5458 new_fcport->fabric_port_name, WWN_SIZE);
5459 fcport->fp_speed = new_fcport->fp_speed;
5460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005462 * If address the same and state FCS_ONLINE
5463 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 */
5465 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005466 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005467 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 break;
5469 }
5470
5471 /*
5472 * If device was not a fabric device before.
5473 */
5474 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5475 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005476 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 fcport->flags |= (FCF_FABRIC_DEVICE |
5478 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 break;
5480 }
5481
5482 /*
5483 * Port ID changed or device was marked to be updated;
5484 * Log it out if still logged in and mark it for
5485 * relogin later.
5486 */
Quinn Tran726b8542017-01-19 22:28:00 -08005487 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005488 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5489 "port changed FC ID, %8phC"
5490 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5491 fcport->port_name,
5492 fcport->d_id.b.domain,
5493 fcport->d_id.b.area,
5494 fcport->d_id.b.al_pa,
5495 fcport->loop_id,
5496 new_fcport->d_id.b.domain,
5497 new_fcport->d_id.b.area,
5498 new_fcport->d_id.b.al_pa);
5499 fcport->d_id.b24 = new_fcport->d_id.b24;
5500 break;
5501 }
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 fcport->d_id.b24 = new_fcport->d_id.b24;
5504 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 break;
5506 }
5507
Quinn Tran726b8542017-01-19 22:28:00 -08005508 if (found) {
5509 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005513 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005514 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5515
5516 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
5519 /* Allocate a new replacement fcport. */
5520 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005521 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005523 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005524 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 return (QLA_MEMORY_ALLOC_FAILED);
5526 }
5527 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5528 new_fcport->d_id.b24 = nxt_d_id.b24;
5529 }
5530
Quinn Tran726b8542017-01-19 22:28:00 -08005531 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Quinn Tran726b8542017-01-19 22:28:00 -08005533 /*
5534 * Logout all previous fabric dev marked lost, except FCP2 devices.
5535 */
5536 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5537 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5538 break;
5539
5540 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5541 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5542 continue;
5543
5544 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5545 if ((qla_dual_mode_enabled(vha) ||
5546 qla_ini_mode_enabled(vha)) &&
5547 atomic_read(&fcport->state) == FCS_ONLINE) {
5548 qla2x00_mark_device_lost(vha, fcport,
5549 ql2xplogiabsentdevice, 0);
5550 if (fcport->loop_id != FC_NO_LOOP_ID &&
5551 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5552 fcport->port_type != FCT_INITIATOR &&
5553 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005554 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005555 "%s %d %8phC post del sess\n",
5556 __func__, __LINE__,
5557 fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08005558 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005559 continue;
5560 }
5561 }
5562 }
5563
5564 if (fcport->scan_state == QLA_FCPORT_FOUND)
5565 qla24xx_fcport_handle_login(vha, fcport);
5566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 return (rval);
5568}
5569
5570/*
5571 * qla2x00_find_new_loop_id
5572 * Scan through our port list and find a new usable loop ID.
5573 *
5574 * Input:
5575 * ha: adapter state pointer.
5576 * dev: port structure pointer.
5577 *
5578 * Returns:
5579 * qla2x00 local function return status code.
5580 *
5581 * Context:
5582 * Kernel context.
5583 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005584int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005585qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586{
5587 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005588 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005589 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 rval = QLA_SUCCESS;
5592
Chad Dupuis5f16b332012-08-22 14:21:00 -04005593 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Chad Dupuis5f16b332012-08-22 14:21:00 -04005595 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5596 LOOPID_MAP_SIZE);
5597 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5598 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5599 dev->loop_id = FC_NO_LOOP_ID;
5600 rval = QLA_FUNCTION_FAILED;
5601 } else
5602 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Chad Dupuis5f16b332012-08-22 14:21:00 -04005604 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Chad Dupuis5f16b332012-08-22 14:21:00 -04005606 if (rval == QLA_SUCCESS)
5607 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5608 "Assigning new loopid=%x, portid=%x.\n",
5609 dev->loop_id, dev->d_id.b24);
5610 else
5611 ql_log(ql_log_warn, dev->vha, 0x2087,
5612 "No loop_id's available, portid=%x.\n",
5613 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 return (rval);
5616}
5617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
5619/*
5620 * qla2x00_fabric_login
5621 * Issue fabric login command.
5622 *
5623 * Input:
5624 * ha = adapter block pointer.
5625 * device = pointer to FC device type structure.
5626 *
5627 * Returns:
5628 * 0 - Login successfully
5629 * 1 - Login failed
5630 * 2 - Initiator device
5631 * 3 - Fatal error
5632 */
5633int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005634qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 uint16_t *next_loopid)
5636{
5637 int rval;
5638 int retry;
5639 uint16_t tmp_loopid;
5640 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005641 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 retry = 0;
5644 tmp_loopid = 0;
5645
5646 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005647 ql_dbg(ql_dbg_disc, vha, 0x2000,
5648 "Trying Fabric Login w/loop id 0x%04x for port "
5649 "%02x%02x%02x.\n",
5650 fcport->loop_id, fcport->d_id.b.domain,
5651 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005654 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 fcport->d_id.b.domain, fcport->d_id.b.area,
5656 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005657 if (rval != QLA_SUCCESS) {
5658 return rval;
5659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 if (mb[0] == MBS_PORT_ID_USED) {
5661 /*
5662 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005663 * recommends the driver perform an implicit login with
5664 * the specified ID again. The ID we just used is save
5665 * here so we return with an ID that can be tried by
5666 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 */
5668 retry++;
5669 tmp_loopid = fcport->loop_id;
5670 fcport->loop_id = mb[1];
5671
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005672 ql_dbg(ql_dbg_disc, vha, 0x2001,
5673 "Fabric Login: port in use - next loop "
5674 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005676 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5679 /*
5680 * Login succeeded.
5681 */
5682 if (retry) {
5683 /* A retry occurred before. */
5684 *next_loopid = tmp_loopid;
5685 } else {
5686 /*
5687 * No retry occurred before. Just increment the
5688 * ID value for next login.
5689 */
5690 *next_loopid = (fcport->loop_id + 1);
5691 }
5692
5693 if (mb[1] & BIT_0) {
5694 fcport->port_type = FCT_INITIATOR;
5695 } else {
5696 fcport->port_type = FCT_TARGET;
5697 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005698 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 }
5700 }
5701
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005702 if (mb[10] & BIT_0)
5703 fcport->supported_classes |= FC_COS_CLASS2;
5704 if (mb[10] & BIT_1)
5705 fcport->supported_classes |= FC_COS_CLASS3;
5706
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005707 if (IS_FWI2_CAPABLE(ha)) {
5708 if (mb[10] & BIT_7)
5709 fcport->flags |=
5710 FCF_CONF_COMP_SUPPORTED;
5711 }
5712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 rval = QLA_SUCCESS;
5714 break;
5715 } else if (mb[0] == MBS_LOOP_ID_USED) {
5716 /*
5717 * Loop ID already used, try next loop ID.
5718 */
5719 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005720 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 if (rval != QLA_SUCCESS) {
5722 /* Ran out of loop IDs to use */
5723 break;
5724 }
5725 } else if (mb[0] == MBS_COMMAND_ERROR) {
5726 /*
5727 * Firmware possibly timed out during login. If NO
5728 * retries are left to do then the device is declared
5729 * dead.
5730 */
5731 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005732 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005733 fcport->d_id.b.domain, fcport->d_id.b.area,
5734 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005735 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
5737 rval = 1;
5738 break;
5739 } else {
5740 /*
5741 * unrecoverable / not handled error
5742 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005743 ql_dbg(ql_dbg_disc, vha, 0x2002,
5744 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5745 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5746 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5747 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
5749 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005750 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005751 fcport->d_id.b.domain, fcport->d_id.b.area,
5752 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005753 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005754 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756 rval = 3;
5757 break;
5758 }
5759 }
5760
5761 return (rval);
5762}
5763
5764/*
5765 * qla2x00_local_device_login
5766 * Issue local device login command.
5767 *
5768 * Input:
5769 * ha = adapter block pointer.
5770 * loop_id = loop id of device to login to.
5771 *
5772 * Returns (Where's the #define!!!!):
5773 * 0 - Login successfully
5774 * 1 - Login failed
5775 * 3 - Fatal error
5776 */
5777int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005778qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779{
5780 int rval;
5781 uint16_t mb[MAILBOX_REGISTER_COUNT];
5782
5783 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005784 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 if (rval == QLA_SUCCESS) {
5786 /* Interrogate mailbox registers for any errors */
5787 if (mb[0] == MBS_COMMAND_ERROR)
5788 rval = 1;
5789 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5790 /* device not in PCB table */
5791 rval = 3;
5792 }
5793
5794 return (rval);
5795}
5796
5797/*
5798 * qla2x00_loop_resync
5799 * Resync with fibre channel devices.
5800 *
5801 * Input:
5802 * ha = adapter block pointer.
5803 *
5804 * Returns:
5805 * 0 = success
5806 */
5807int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005808qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005810 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005812 struct req_que *req;
5813 struct rsp_que *rsp;
5814
Michael Hernandezd7459522016-12-12 14:40:07 -08005815 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005816 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005818 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5819 if (vha->flags.online) {
5820 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5822 wait_time = 256;
5823 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005824 if (!IS_QLAFX00(vha->hw)) {
5825 /*
5826 * Issue a marker after FW becomes
5827 * ready.
5828 */
5829 qla2x00_marker(vha, req, rsp, 0, 0,
5830 MK_SYNC_ALL);
5831 vha->marker_needed = 0;
5832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005835 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005837 if (IS_QLAFX00(vha->hw))
5838 qlafx00_configure_devices(vha);
5839 else
5840 qla2x00_configure_loop(vha);
5841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005843 } while (!atomic_read(&vha->loop_down_timer) &&
5844 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5845 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5846 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 }
5849
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005850 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005853 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005854 ql_dbg(ql_dbg_disc, vha, 0x206c,
5855 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 return (rval);
5858}
5859
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005860/*
5861* qla2x00_perform_loop_resync
5862* Description: This function will set the appropriate flags and call
5863* qla2x00_loop_resync. If successful loop will be resynced
5864* Arguments : scsi_qla_host_t pointer
5865* returm : Success or Failure
5866*/
5867
5868int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5869{
5870 int32_t rval = 0;
5871
5872 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5873 /*Configure the flags so that resync happens properly*/
5874 atomic_set(&ha->loop_down_timer, 0);
5875 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5876 atomic_set(&ha->loop_state, LOOP_UP);
5877 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5878 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5879 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5880
5881 rval = qla2x00_loop_resync(ha);
5882 } else
5883 atomic_set(&ha->loop_state, LOOP_DEAD);
5884
5885 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5886 }
5887
5888 return rval;
5889}
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005892qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005893{
5894 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005895 struct scsi_qla_host *vha;
5896 struct qla_hw_data *ha = base_vha->hw;
5897 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005898
Arun Easifeafb7b2010-09-03 14:57:00 -07005899 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005900 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005901 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5902 atomic_inc(&vha->vref_count);
5903 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005904 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005905 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5906 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005907 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005908
Arun Easifeafb7b2010-09-03 14:57:00 -07005909 spin_lock_irqsave(&ha->vport_slock, flags);
5910 }
5911 }
5912 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005913 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005914 }
5915 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005916}
5917
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005918/* Assumes idc_lock always held on entry */
5919void
5920qla83xx_reset_ownership(scsi_qla_host_t *vha)
5921{
5922 struct qla_hw_data *ha = vha->hw;
5923 uint32_t drv_presence, drv_presence_mask;
5924 uint32_t dev_part_info1, dev_part_info2, class_type;
5925 uint32_t class_type_mask = 0x3;
5926 uint16_t fcoe_other_function = 0xffff, i;
5927
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005928 if (IS_QLA8044(ha)) {
5929 drv_presence = qla8044_rd_direct(vha,
5930 QLA8044_CRB_DRV_ACTIVE_INDEX);
5931 dev_part_info1 = qla8044_rd_direct(vha,
5932 QLA8044_CRB_DEV_PART_INFO_INDEX);
5933 dev_part_info2 = qla8044_rd_direct(vha,
5934 QLA8044_CRB_DEV_PART_INFO2);
5935 } else {
5936 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5937 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5938 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5939 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005940 for (i = 0; i < 8; i++) {
5941 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5942 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5943 (i != ha->portnum)) {
5944 fcoe_other_function = i;
5945 break;
5946 }
5947 }
5948 if (fcoe_other_function == 0xffff) {
5949 for (i = 0; i < 8; i++) {
5950 class_type = ((dev_part_info2 >> (i * 4)) &
5951 class_type_mask);
5952 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5953 ((i + 8) != ha->portnum)) {
5954 fcoe_other_function = i + 8;
5955 break;
5956 }
5957 }
5958 }
5959 /*
5960 * Prepare drv-presence mask based on fcoe functions present.
5961 * However consider only valid physical fcoe function numbers (0-15).
5962 */
5963 drv_presence_mask = ~((1 << (ha->portnum)) |
5964 ((fcoe_other_function == 0xffff) ?
5965 0 : (1 << (fcoe_other_function))));
5966
5967 /* We are the reset owner iff:
5968 * - No other protocol drivers present.
5969 * - This is the lowest among fcoe functions. */
5970 if (!(drv_presence & drv_presence_mask) &&
5971 (ha->portnum < fcoe_other_function)) {
5972 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5973 "This host is Reset owner.\n");
5974 ha->flags.nic_core_reset_owner = 1;
5975 }
5976}
5977
Saurav Kashyapfa492632012-11-21 02:40:29 -05005978static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005979__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5980{
5981 int rval = QLA_SUCCESS;
5982 struct qla_hw_data *ha = vha->hw;
5983 uint32_t drv_ack;
5984
5985 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5986 if (rval == QLA_SUCCESS) {
5987 drv_ack |= (1 << ha->portnum);
5988 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5989 }
5990
5991 return rval;
5992}
5993
Saurav Kashyapfa492632012-11-21 02:40:29 -05005994static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005995__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5996{
5997 int rval = QLA_SUCCESS;
5998 struct qla_hw_data *ha = vha->hw;
5999 uint32_t drv_ack;
6000
6001 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6002 if (rval == QLA_SUCCESS) {
6003 drv_ack &= ~(1 << ha->portnum);
6004 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6005 }
6006
6007 return rval;
6008}
6009
Saurav Kashyapfa492632012-11-21 02:40:29 -05006010static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006011qla83xx_dev_state_to_string(uint32_t dev_state)
6012{
6013 switch (dev_state) {
6014 case QLA8XXX_DEV_COLD:
6015 return "COLD/RE-INIT";
6016 case QLA8XXX_DEV_INITIALIZING:
6017 return "INITIALIZING";
6018 case QLA8XXX_DEV_READY:
6019 return "READY";
6020 case QLA8XXX_DEV_NEED_RESET:
6021 return "NEED RESET";
6022 case QLA8XXX_DEV_NEED_QUIESCENT:
6023 return "NEED QUIESCENT";
6024 case QLA8XXX_DEV_FAILED:
6025 return "FAILED";
6026 case QLA8XXX_DEV_QUIESCENT:
6027 return "QUIESCENT";
6028 default:
6029 return "Unknown";
6030 }
6031}
6032
6033/* Assumes idc-lock always held on entry */
6034void
6035qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6036{
6037 struct qla_hw_data *ha = vha->hw;
6038 uint32_t idc_audit_reg = 0, duration_secs = 0;
6039
6040 switch (audit_type) {
6041 case IDC_AUDIT_TIMESTAMP:
6042 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6043 idc_audit_reg = (ha->portnum) |
6044 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6045 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6046 break;
6047
6048 case IDC_AUDIT_COMPLETION:
6049 duration_secs = ((jiffies_to_msecs(jiffies) -
6050 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6051 idc_audit_reg = (ha->portnum) |
6052 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6053 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6054 break;
6055
6056 default:
6057 ql_log(ql_log_warn, vha, 0xb078,
6058 "Invalid audit type specified.\n");
6059 break;
6060 }
6061}
6062
6063/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006064static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006065qla83xx_initiating_reset(scsi_qla_host_t *vha)
6066{
6067 struct qla_hw_data *ha = vha->hw;
6068 uint32_t idc_control, dev_state;
6069
6070 __qla83xx_get_idc_control(vha, &idc_control);
6071 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6072 ql_log(ql_log_info, vha, 0xb080,
6073 "NIC Core reset has been disabled. idc-control=0x%x\n",
6074 idc_control);
6075 return QLA_FUNCTION_FAILED;
6076 }
6077
6078 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6079 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6080 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6081 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6082 QLA8XXX_DEV_NEED_RESET);
6083 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6084 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6085 } else {
6086 const char *state = qla83xx_dev_state_to_string(dev_state);
6087 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6088
6089 /* SV: XXX: Is timeout required here? */
6090 /* Wait for IDC state change READY -> NEED_RESET */
6091 while (dev_state == QLA8XXX_DEV_READY) {
6092 qla83xx_idc_unlock(vha, 0);
6093 msleep(200);
6094 qla83xx_idc_lock(vha, 0);
6095 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6096 }
6097 }
6098
6099 /* Send IDC ack by writing to drv-ack register */
6100 __qla83xx_set_drv_ack(vha);
6101
6102 return QLA_SUCCESS;
6103}
6104
6105int
6106__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6107{
6108 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6109}
6110
6111int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006112__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6113{
6114 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6115}
6116
Saurav Kashyapfa492632012-11-21 02:40:29 -05006117static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006118qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6119{
6120 uint32_t drv_presence = 0;
6121 struct qla_hw_data *ha = vha->hw;
6122
6123 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6124 if (drv_presence & (1 << ha->portnum))
6125 return QLA_SUCCESS;
6126 else
6127 return QLA_TEST_FAILED;
6128}
6129
6130int
6131qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6132{
6133 int rval = QLA_SUCCESS;
6134 struct qla_hw_data *ha = vha->hw;
6135
6136 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6137 "Entered %s().\n", __func__);
6138
6139 if (vha->device_flags & DFLG_DEV_FAILED) {
6140 ql_log(ql_log_warn, vha, 0xb059,
6141 "Device in unrecoverable FAILED state.\n");
6142 return QLA_FUNCTION_FAILED;
6143 }
6144
6145 qla83xx_idc_lock(vha, 0);
6146
6147 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6148 ql_log(ql_log_warn, vha, 0xb05a,
6149 "Function=0x%x has been removed from IDC participation.\n",
6150 ha->portnum);
6151 rval = QLA_FUNCTION_FAILED;
6152 goto exit;
6153 }
6154
6155 qla83xx_reset_ownership(vha);
6156
6157 rval = qla83xx_initiating_reset(vha);
6158
6159 /*
6160 * Perform reset if we are the reset-owner,
6161 * else wait till IDC state changes to READY/FAILED.
6162 */
6163 if (rval == QLA_SUCCESS) {
6164 rval = qla83xx_idc_state_handler(vha);
6165
6166 if (rval == QLA_SUCCESS)
6167 ha->flags.nic_core_hung = 0;
6168 __qla83xx_clear_drv_ack(vha);
6169 }
6170
6171exit:
6172 qla83xx_idc_unlock(vha, 0);
6173
6174 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6175
6176 return rval;
6177}
6178
Saurav Kashyap81178772012-08-22 14:21:04 -04006179int
6180qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6181{
6182 struct qla_hw_data *ha = vha->hw;
6183 int rval = QLA_FUNCTION_FAILED;
6184
6185 if (!IS_MCTP_CAPABLE(ha)) {
6186 /* This message can be removed from the final version */
6187 ql_log(ql_log_info, vha, 0x506d,
6188 "This board is not MCTP capable\n");
6189 return rval;
6190 }
6191
6192 if (!ha->mctp_dump) {
6193 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6194 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6195
6196 if (!ha->mctp_dump) {
6197 ql_log(ql_log_warn, vha, 0x506e,
6198 "Failed to allocate memory for mctp dump\n");
6199 return rval;
6200 }
6201 }
6202
6203#define MCTP_DUMP_STR_ADDR 0x00000000
6204 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6205 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6206 if (rval != QLA_SUCCESS) {
6207 ql_log(ql_log_warn, vha, 0x506f,
6208 "Failed to capture mctp dump\n");
6209 } else {
6210 ql_log(ql_log_info, vha, 0x5070,
6211 "Mctp dump capture for host (%ld/%p).\n",
6212 vha->host_no, ha->mctp_dump);
6213 ha->mctp_dumped = 1;
6214 }
6215
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006216 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006217 ha->flags.nic_core_reset_hdlr_active = 1;
6218 rval = qla83xx_restart_nic_firmware(vha);
6219 if (rval)
6220 /* NIC Core reset failed. */
6221 ql_log(ql_log_warn, vha, 0x5071,
6222 "Failed to restart nic firmware\n");
6223 else
6224 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6225 "Restarted NIC firmware successfully.\n");
6226 ha->flags.nic_core_reset_hdlr_active = 0;
6227 }
6228
6229 return rval;
6230
6231}
6232
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006233/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006234* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006235* Description: This function will block the new I/Os
6236* Its not aborting any I/Os as context
6237* is not destroyed during quiescence
6238* Arguments: scsi_qla_host_t
6239* return : void
6240*/
6241void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006242qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006243{
6244 struct qla_hw_data *ha = vha->hw;
6245 struct scsi_qla_host *vp;
6246
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006247 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6248 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006249
6250 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6251 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6252 atomic_set(&vha->loop_state, LOOP_DOWN);
6253 qla2x00_mark_all_devices_lost(vha, 0);
6254 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006255 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006256 } else {
6257 if (!atomic_read(&vha->loop_down_timer))
6258 atomic_set(&vha->loop_down_timer,
6259 LOOP_DOWN_TIME);
6260 }
6261 /* Wait for pending cmds to complete */
6262 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6263}
6264
Giridhar Malavalia9083012010-04-12 17:59:55 -07006265void
6266qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6267{
6268 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006269 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006270 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006271 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006272 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006273
Saurav Kashyape46ef002011-02-23 15:27:16 -08006274 /* For ISP82XX, driver waits for completion of the commands.
6275 * online flag should be set.
6276 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006277 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006278 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006279 ha->flags.chip_reset_done = 0;
6280 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006281 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006282
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006283 ql_log(ql_log_info, vha, 0x00af,
6284 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006285
Saurav Kashyape46ef002011-02-23 15:27:16 -08006286 /* For ISP82XX, reset_chip is just disabling interrupts.
6287 * Driver waits for the completion of the commands.
6288 * the interrupts need to be enabled.
6289 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006290 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006291 ha->isp_ops->reset_chip(vha);
6292
Quinn Tran9cd883f2017-12-28 12:33:24 -08006293 SAVE_TOPO(ha);
6294 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006295 ha->flags.n2n_ae = 0;
6296 ha->flags.lip_ae = 0;
6297 ha->current_topology = 0;
6298 ha->flags.fw_started = 0;
6299 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006300 ha->base_qpair->chip_reset++;
6301 for (i = 0; i < ha->max_qpairs; i++) {
6302 if (ha->queue_pair_map[i])
6303 ha->queue_pair_map[i]->chip_reset =
6304 ha->base_qpair->chip_reset;
6305 }
Quinn Tran726b8542017-01-19 22:28:00 -08006306
Giridhar Malavalia9083012010-04-12 17:59:55 -07006307 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6308 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6309 atomic_set(&vha->loop_state, LOOP_DOWN);
6310 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006311
6312 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006313 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006314 atomic_inc(&vp->vref_count);
6315 spin_unlock_irqrestore(&ha->vport_slock, flags);
6316
Giridhar Malavalia9083012010-04-12 17:59:55 -07006317 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006318
6319 spin_lock_irqsave(&ha->vport_slock, flags);
6320 atomic_dec(&vp->vref_count);
6321 }
6322 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006323 } else {
6324 if (!atomic_read(&vha->loop_down_timer))
6325 atomic_set(&vha->loop_down_timer,
6326 LOOP_DOWN_TIME);
6327 }
6328
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006329 /* Clear all async request states across all VPs. */
6330 list_for_each_entry(fcport, &vha->vp_fcports, list)
6331 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6332 spin_lock_irqsave(&ha->vport_slock, flags);
6333 list_for_each_entry(vp, &ha->vp_list, list) {
6334 atomic_inc(&vp->vref_count);
6335 spin_unlock_irqrestore(&ha->vport_slock, flags);
6336
6337 list_for_each_entry(fcport, &vp->vp_fcports, list)
6338 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6339
6340 spin_lock_irqsave(&ha->vport_slock, flags);
6341 atomic_dec(&vp->vref_count);
6342 }
6343 spin_unlock_irqrestore(&ha->vport_slock, flags);
6344
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006345 if (!ha->flags.eeh_busy) {
6346 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006347 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006348 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006349 ql_log(ql_log_info, vha, 0x00b4,
6350 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006351
Saurav Kashyape46ef002011-02-23 15:27:16 -08006352 /* Done waiting for pending commands.
6353 * Reset the online flag.
6354 */
6355 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006358 /* Requeue all commands in outstanding command list. */
6359 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6360 }
Arun Easib6a029e2014-09-25 06:14:52 -04006361 /* memory barrier */
6362 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363}
6364
6365/*
6366* qla2x00_abort_isp
6367* Resets ISP and aborts all outstanding commands.
6368*
6369* Input:
6370* ha = adapter block pointer.
6371*
6372* Returns:
6373* 0 = success
6374*/
6375int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006376qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006378 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006380 struct qla_hw_data *ha = vha->hw;
6381 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006382 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006383 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006385 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006386 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
Santosh Vernekara61712972012-08-22 14:21:13 -04006388 if (IS_QLA8031(ha)) {
6389 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6390 "Clearing fcoe driver presence.\n");
6391 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6392 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6393 "Error while clearing DRV-Presence.\n");
6394 }
6395
Andrew Vasquez85880802009-12-15 21:29:46 -08006396 if (unlikely(pci_channel_offline(ha->pdev) &&
6397 ha->flags.pci_channel_io_perm_failure)) {
6398 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6399 status = 0;
6400 return status;
6401 }
6402
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006403 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006404
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006405 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006407 if (!qla2x00_restart_isp(vha)) {
6408 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006410 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 /*
6412 * Issue marker command only when we are going
6413 * to start the I/O .
6414 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006415 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 }
6417
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006418 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006420 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006422 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006423 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006424
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006425 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6426 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006427 if (ha->fce) {
6428 ha->flags.fce_enabled = 1;
6429 memset(ha->fce, 0,
6430 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006431 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006432 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6433 &ha->fce_bufs);
6434 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006435 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006436 "Unable to reinitialize FCE "
6437 "(%d).\n", rval);
6438 ha->flags.fce_enabled = 0;
6439 }
6440 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006441
6442 if (ha->eft) {
6443 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006444 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006445 ha->eft_dma, EFT_NUM_BUFFERS);
6446 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006447 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006448 "Unable to reinitialize EFT "
6449 "(%d).\n", rval);
6450 }
6451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006453 vha->flags.online = 1;
6454 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006456 ql_log(ql_log_fatal, vha, 0x8035,
6457 "ISP error recover failed - "
6458 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006459 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 * The next call disables the board
6461 * completely.
6462 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006463 ha->isp_ops->reset_adapter(vha);
6464 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006466 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 status = 0;
6468 } else { /* schedule another ISP abort */
6469 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006470 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6471 "ISP abort - retry remaining %d.\n",
6472 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 status = 1;
6474 }
6475 } else {
6476 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006477 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6478 "ISP error recovery - retrying (%d) "
6479 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006480 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 status = 1;
6482 }
6483 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006484
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 }
6486
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006487 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006488 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006489 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006490 spin_lock_irqsave(&ha->vport_slock, flags);
6491 list_for_each_entry(vp, &ha->vp_list, list) {
6492 if (vp->vp_idx) {
6493 atomic_inc(&vp->vref_count);
6494 spin_unlock_irqrestore(&ha->vport_slock, flags);
6495
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006496 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006497
6498 spin_lock_irqsave(&ha->vport_slock, flags);
6499 atomic_dec(&vp->vref_count);
6500 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006501 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006502 spin_unlock_irqrestore(&ha->vport_slock, flags);
6503
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006504 if (IS_QLA8031(ha)) {
6505 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6506 "Setting back fcoe driver presence.\n");
6507 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6508 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6509 "Error while setting DRV-Presence.\n");
6510 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006511 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006512 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6513 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 }
6515
6516 return(status);
6517}
6518
6519/*
6520* qla2x00_restart_isp
6521* restarts the ISP after a reset
6522*
6523* Input:
6524* ha = adapter block pointer.
6525*
6526* Returns:
6527* 0 = success
6528*/
6529static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006530qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006532 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006533 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006534 struct req_que *req = ha->req_q_map[0];
6535 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
6537 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006538 if (qla2x00_isp_firmware(vha)) {
6539 vha->flags.online = 0;
6540 status = ha->isp_ops->chip_diag(vha);
6541 if (!status)
6542 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 }
6544
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006545 if (!status && !(status = qla2x00_init_rings(vha))) {
6546 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006547 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006548
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006549 /* Initialize the queues in use */
6550 qla25xx_init_queues(ha);
6551
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006552 status = qla2x00_fw_ready(vha);
6553 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006554 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006555 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006556 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 }
6558
6559 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006560 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 }
6563 return (status);
6564}
6565
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006566static int
6567qla25xx_init_queues(struct qla_hw_data *ha)
6568{
6569 struct rsp_que *rsp = NULL;
6570 struct req_que *req = NULL;
6571 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6572 int ret = -1;
6573 int i;
6574
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006575 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006576 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006577 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006578 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006579 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006580 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006581 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6582 "%s Rsp que: %d init failed.\n",
6583 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006584 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006585 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6586 "%s Rsp que: %d inited.\n",
6587 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006588 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006589 }
6590 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006591 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006592 if (req && test_bit(i, ha->req_qid_map)) {
6593 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006594 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006595 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006596 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006597 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6598 "%s Req que: %d init failed.\n",
6599 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006600 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006601 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6602 "%s Req que: %d inited.\n",
6603 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006604 }
6605 }
6606 return ret;
6607}
6608
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609/*
6610* qla2x00_reset_adapter
6611* Reset adapter.
6612*
6613* Input:
6614* ha = adapter block pointer.
6615*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006616void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006617qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618{
6619 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006620 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006621 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006623 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006624 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 spin_lock_irqsave(&ha->hardware_lock, flags);
6627 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6628 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6629 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6630 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6631 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6632}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006633
6634void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006635qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006636{
6637 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006638 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006639 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6640
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006641 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006642 return;
6643
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006644 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006645 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006646
6647 spin_lock_irqsave(&ha->hardware_lock, flags);
6648 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6649 RD_REG_DWORD(&reg->hccr);
6650 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6651 RD_REG_DWORD(&reg->hccr);
6652 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006653
6654 if (IS_NOPOLLING_TYPE(ha))
6655 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006656}
6657
David Miller4e08df32007-04-16 12:37:43 -07006658/* On sparc systems, obtain port and node WWN from firmware
6659 * properties.
6660 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006661static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6662 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006663{
6664#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006665 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006666 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006667 struct device_node *dp = pci_device_to_OF_node(pdev);
6668 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006669 int len;
6670
6671 val = of_get_property(dp, "port-wwn", &len);
6672 if (val && len >= WWN_SIZE)
6673 memcpy(nv->port_name, val, WWN_SIZE);
6674
6675 val = of_get_property(dp, "node-wwn", &len);
6676 if (val && len >= WWN_SIZE)
6677 memcpy(nv->node_name, val, WWN_SIZE);
6678#endif
6679}
6680
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006681int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006682qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006683{
David Miller4e08df32007-04-16 12:37:43 -07006684 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006685 struct init_cb_24xx *icb;
6686 struct nvram_24xx *nv;
6687 uint32_t *dptr;
6688 uint8_t *dptr1, *dptr2;
6689 uint32_t chksum;
6690 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006691 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006692
David Miller4e08df32007-04-16 12:37:43 -07006693 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006694 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006695 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006696
6697 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006698 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006699 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6700 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6701 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006702 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006703 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6704 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006705
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006706 ha->nvram_size = sizeof(struct nvram_24xx);
6707 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006708
Seokmann Ju281afe12007-07-26 13:43:34 -07006709 /* Get VPD data into cache */
6710 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006711 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006712 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6713
6714 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006715 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006716 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006717 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006718 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6719 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006720
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006721 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6722 "Contents of NVRAM\n");
6723 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6724 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006725
6726 /* Bad NVRAM data, set defaults parameters. */
6727 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6728 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006729 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006730 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006731 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006732 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006733 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6734 ql_log(ql_log_warn, vha, 0x006c,
6735 "Falling back to functioning (yet invalid -- WWPN) "
6736 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006737
6738 /*
6739 * Set default initialization control block.
6740 */
6741 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006742 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6743 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006744 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006745 nv->execution_throttle = cpu_to_le16(0xFFFF);
6746 nv->exchange_count = cpu_to_le16(0);
6747 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006748 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006749 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006750 nv->port_name[2] = 0x00;
6751 nv->port_name[3] = 0xe0;
6752 nv->port_name[4] = 0x8b;
6753 nv->port_name[5] = 0x1c;
6754 nv->port_name[6] = 0x55;
6755 nv->port_name[7] = 0x86;
6756 nv->node_name[0] = 0x20;
6757 nv->node_name[1] = 0x00;
6758 nv->node_name[2] = 0x00;
6759 nv->node_name[3] = 0xe0;
6760 nv->node_name[4] = 0x8b;
6761 nv->node_name[5] = 0x1c;
6762 nv->node_name[6] = 0x55;
6763 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006764 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006765 nv->login_retry_count = cpu_to_le16(8);
6766 nv->interrupt_delay_timer = cpu_to_le16(0);
6767 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006768 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006769 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6770 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6771 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6772 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6773 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6774 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006775 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006776 nv->max_luns_per_target = cpu_to_le16(128);
6777 nv->port_down_retry_count = cpu_to_le16(30);
6778 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006779
6780 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006781 }
6782
Quinn Tran726b8542017-01-19 22:28:00 -08006783 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006784 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006785 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006786 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006787 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006788 }
6789
6790 qlt_24xx_config_nvram_stage1(vha, nv);
6791
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006792 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006793 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006794
6795 /* Copy 1st segment. */
6796 dptr1 = (uint8_t *)icb;
6797 dptr2 = (uint8_t *)&nv->version;
6798 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6799 while (cnt--)
6800 *dptr1++ = *dptr2++;
6801
6802 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006803 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006804
6805 /* Copy 2nd segment. */
6806 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6807 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6808 cnt = (uint8_t *)&icb->reserved_3 -
6809 (uint8_t *)&icb->interrupt_delay_timer;
6810 while (cnt--)
6811 *dptr1++ = *dptr2++;
6812
6813 /*
6814 * Setup driver NVRAM options.
6815 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006816 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006817 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006818
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006819 qlt_24xx_config_nvram_stage2(vha, icb);
6820
Bart Van Asschead950362015-07-09 07:24:08 -07006821 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006822 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006823 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6824 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6825 }
6826
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006827 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006828 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006829 /*
6830 * Firmware will apply the following mask if the nodename was
6831 * not provided.
6832 */
6833 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6834 icb->node_name[0] &= 0xF0;
6835 }
6836
6837 /* Set host adapter parameters. */
6838 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006839 ha->flags.enable_lip_reset = 0;
6840 ha->flags.enable_lip_full_login =
6841 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6842 ha->flags.enable_target_reset =
6843 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006844 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006845 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006846
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006847 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6848 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006849
6850 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6851 sizeof(ha->fw_seriallink_options24));
6852
6853 /* save HBA serial number */
6854 ha->serial0 = icb->port_name[5];
6855 ha->serial1 = icb->port_name[6];
6856 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006857 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6858 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006859
Bart Van Asschead950362015-07-09 07:24:08 -07006860 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006861
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006862 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6863
6864 /* Set minimum login_timeout to 4 seconds. */
6865 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6866 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6867 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006868 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006869 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006870
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006871 /* Set minimum RATOV to 100 tenths of a second. */
6872 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006873
6874 ha->loop_reset_delay = nv->reset_delay;
6875
6876 /* Link Down Timeout = 0:
6877 *
6878 * When Port Down timer expires we will start returning
6879 * I/O's to OS with "DID_NO_CONNECT".
6880 *
6881 * Link Down Timeout != 0:
6882 *
6883 * The driver waits for the link to come up after link down
6884 * before returning I/Os to OS with "DID_NO_CONNECT".
6885 */
6886 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6887 ha->loop_down_abort_time =
6888 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6889 } else {
6890 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6891 ha->loop_down_abort_time =
6892 (LOOP_DOWN_TIME - ha->link_down_timeout);
6893 }
6894
6895 /* Need enough time to try and get the port back. */
6896 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6897 if (qlport_down_retry)
6898 ha->port_down_retry_count = qlport_down_retry;
6899
6900 /* Set login_retry_count */
6901 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6902 if (ha->port_down_retry_count ==
6903 le16_to_cpu(nv->port_down_retry_count) &&
6904 ha->port_down_retry_count > 3)
6905 ha->login_retry_count = ha->port_down_retry_count;
6906 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6907 ha->login_retry_count = ha->port_down_retry_count;
6908 if (ql2xloginretrycount)
6909 ha->login_retry_count = ql2xloginretrycount;
6910
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006911 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006912 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006913 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6914 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6915 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6916 le16_to_cpu(icb->interrupt_delay_timer): 2;
6917 }
Bart Van Asschead950362015-07-09 07:24:08 -07006918 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006919 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006920 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006921 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006922 ha->zio_mode = QLA_ZIO_MODE_6;
6923
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006924 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006925 "ZIO mode %d enabled; timer delay (%d us).\n",
6926 ha->zio_mode, ha->zio_timer * 100);
6927
6928 icb->firmware_options_2 |= cpu_to_le32(
6929 (uint32_t)ha->zio_mode);
6930 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006931 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006932 }
6933
David Miller4e08df32007-04-16 12:37:43 -07006934 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006935 ql_log(ql_log_warn, vha, 0x0070,
6936 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006937 }
6938 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006939}
6940
Sawan Chandak4243c112016-01-27 12:03:31 -05006941uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6942{
6943 struct qla27xx_image_status pri_image_status, sec_image_status;
6944 uint8_t valid_pri_image, valid_sec_image;
6945 uint32_t *wptr;
6946 uint32_t cnt, chksum, size;
6947 struct qla_hw_data *ha = vha->hw;
6948
6949 valid_pri_image = valid_sec_image = 1;
6950 ha->active_image = 0;
6951 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6952
6953 if (!ha->flt_region_img_status_pri) {
6954 valid_pri_image = 0;
6955 goto check_sec_image;
6956 }
6957
6958 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6959 ha->flt_region_img_status_pri, size);
6960
6961 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6962 ql_dbg(ql_dbg_init, vha, 0x018b,
6963 "Primary image signature (0x%x) not valid\n",
6964 pri_image_status.signature);
6965 valid_pri_image = 0;
6966 goto check_sec_image;
6967 }
6968
6969 wptr = (uint32_t *)(&pri_image_status);
6970 cnt = size;
6971
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006972 for (chksum = 0; cnt--; wptr++)
6973 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006974
Sawan Chandak4243c112016-01-27 12:03:31 -05006975 if (chksum) {
6976 ql_dbg(ql_dbg_init, vha, 0x018c,
6977 "Checksum validation failed for primary image (0x%x)\n",
6978 chksum);
6979 valid_pri_image = 0;
6980 }
6981
6982check_sec_image:
6983 if (!ha->flt_region_img_status_sec) {
6984 valid_sec_image = 0;
6985 goto check_valid_image;
6986 }
6987
6988 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6989 ha->flt_region_img_status_sec, size);
6990
6991 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6992 ql_dbg(ql_dbg_init, vha, 0x018d,
6993 "Secondary image signature(0x%x) not valid\n",
6994 sec_image_status.signature);
6995 valid_sec_image = 0;
6996 goto check_valid_image;
6997 }
6998
6999 wptr = (uint32_t *)(&sec_image_status);
7000 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007001 for (chksum = 0; cnt--; wptr++)
7002 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05007003 if (chksum) {
7004 ql_dbg(ql_dbg_init, vha, 0x018e,
7005 "Checksum validation failed for secondary image (0x%x)\n",
7006 chksum);
7007 valid_sec_image = 0;
7008 }
7009
7010check_valid_image:
7011 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7012 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7013 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7014 if (!ha->active_image ||
7015 pri_image_status.generation_number <
7016 sec_image_status.generation_number)
7017 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7018 }
7019
7020 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7021 ha->active_image == 0 ? "default bootld and fw" :
7022 ha->active_image == 1 ? "primary" :
7023 ha->active_image == 2 ? "secondary" :
7024 "Invalid");
7025
7026 return ha->active_image;
7027}
7028
Adrian Bunk413975a2006-06-30 02:33:06 -07007029static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007030qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7031 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007032{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007033 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007034 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007035 uint32_t *dcode, dlen;
7036 uint32_t risc_addr;
7037 uint32_t risc_size;
7038 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007039 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007040 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007041
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007042 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007043 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007044
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007045 rval = QLA_SUCCESS;
7046
7047 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007048 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007049 *srisc_addr = 0;
7050
Sawan Chandak4243c112016-01-27 12:03:31 -05007051 if (IS_QLA27XX(ha) &&
7052 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7053 faddr = ha->flt_region_fw_sec;
7054
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007055 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007056 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007057 for (i = 0; i < 4; i++)
7058 dcode[i] = be32_to_cpu(dcode[i]);
7059 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7060 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7061 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7062 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007063 ql_log(ql_log_fatal, vha, 0x008c,
7064 "Unable to verify the integrity of flash firmware "
7065 "image.\n");
7066 ql_log(ql_log_fatal, vha, 0x008d,
7067 "Firmware data: %08x %08x %08x %08x.\n",
7068 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007069
7070 return QLA_FUNCTION_FAILED;
7071 }
7072
7073 while (segments && rval == QLA_SUCCESS) {
7074 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007075 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007076
7077 risc_addr = be32_to_cpu(dcode[2]);
7078 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7079 risc_size = be32_to_cpu(dcode[3]);
7080
7081 fragment = 0;
7082 while (risc_size > 0 && rval == QLA_SUCCESS) {
7083 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7084 if (dlen > risc_size)
7085 dlen = risc_size;
7086
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007087 ql_dbg(ql_dbg_init, vha, 0x008e,
7088 "Loading risc segment@ risc addr %x "
7089 "number of dwords 0x%x offset 0x%x.\n",
7090 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007091
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007092 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007093 for (i = 0; i < dlen; i++)
7094 dcode[i] = swab32(dcode[i]);
7095
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007096 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007097 dlen);
7098 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007099 ql_log(ql_log_fatal, vha, 0x008f,
7100 "Failed to load segment %d of firmware.\n",
7101 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007102 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007103 }
7104
7105 faddr += dlen;
7106 risc_addr += dlen;
7107 risc_size -= dlen;
7108 fragment++;
7109 }
7110
7111 /* Next segment. */
7112 segments--;
7113 }
7114
Chad Dupuisf73cb692014-02-26 04:15:06 -05007115 if (!IS_QLA27XX(ha))
7116 return rval;
7117
7118 if (ha->fw_dump_template)
7119 vfree(ha->fw_dump_template);
7120 ha->fw_dump_template = NULL;
7121 ha->fw_dump_template_len = 0;
7122
7123 ql_dbg(ql_dbg_init, vha, 0x0161,
7124 "Loading fwdump template from %x\n", faddr);
7125 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7126 risc_size = be32_to_cpu(dcode[2]);
7127 ql_dbg(ql_dbg_init, vha, 0x0162,
7128 "-> array size %x dwords\n", risc_size);
7129 if (risc_size == 0 || risc_size == ~0)
7130 goto default_template;
7131
7132 dlen = (risc_size - 8) * sizeof(*dcode);
7133 ql_dbg(ql_dbg_init, vha, 0x0163,
7134 "-> template allocating %x bytes...\n", dlen);
7135 ha->fw_dump_template = vmalloc(dlen);
7136 if (!ha->fw_dump_template) {
7137 ql_log(ql_log_warn, vha, 0x0164,
7138 "Failed fwdump template allocate %x bytes.\n", risc_size);
7139 goto default_template;
7140 }
7141
7142 faddr += 7;
7143 risc_size -= 8;
7144 dcode = ha->fw_dump_template;
7145 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7146 for (i = 0; i < risc_size; i++)
7147 dcode[i] = le32_to_cpu(dcode[i]);
7148
7149 if (!qla27xx_fwdt_template_valid(dcode)) {
7150 ql_log(ql_log_warn, vha, 0x0165,
7151 "Failed fwdump template validate\n");
7152 goto default_template;
7153 }
7154
7155 dlen = qla27xx_fwdt_template_size(dcode);
7156 ql_dbg(ql_dbg_init, vha, 0x0166,
7157 "-> template size %x bytes\n", dlen);
7158 if (dlen > risc_size * sizeof(*dcode)) {
7159 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007160 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007161 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007162 goto default_template;
7163 }
7164 ha->fw_dump_template_len = dlen;
7165 return rval;
7166
7167default_template:
7168 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7169 if (ha->fw_dump_template)
7170 vfree(ha->fw_dump_template);
7171 ha->fw_dump_template = NULL;
7172 ha->fw_dump_template_len = 0;
7173
7174 dlen = qla27xx_fwdt_template_default_size();
7175 ql_dbg(ql_dbg_init, vha, 0x0169,
7176 "-> template allocating %x bytes...\n", dlen);
7177 ha->fw_dump_template = vmalloc(dlen);
7178 if (!ha->fw_dump_template) {
7179 ql_log(ql_log_warn, vha, 0x016a,
7180 "Failed fwdump template allocate %x bytes.\n", risc_size);
7181 goto failed_template;
7182 }
7183
7184 dcode = ha->fw_dump_template;
7185 risc_size = dlen / sizeof(*dcode);
7186 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7187 for (i = 0; i < risc_size; i++)
7188 dcode[i] = be32_to_cpu(dcode[i]);
7189
7190 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7191 ql_log(ql_log_warn, vha, 0x016b,
7192 "Failed fwdump template validate\n");
7193 goto failed_template;
7194 }
7195
7196 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7197 ql_dbg(ql_dbg_init, vha, 0x016c,
7198 "-> template size %x bytes\n", dlen);
7199 ha->fw_dump_template_len = dlen;
7200 return rval;
7201
7202failed_template:
7203 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7204 if (ha->fw_dump_template)
7205 vfree(ha->fw_dump_template);
7206 ha->fw_dump_template = NULL;
7207 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007208 return rval;
7209}
7210
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007211#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007212
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007213int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007214qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007215{
7216 int rval;
7217 int i, fragment;
7218 uint16_t *wcode, *fwcode;
7219 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7220 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007221 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007222 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007223
7224 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007225 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007226 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007227 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007228 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007229 ql_log(ql_log_info, vha, 0x0084,
7230 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007231 return QLA_FUNCTION_FAILED;
7232 }
7233
7234 rval = QLA_SUCCESS;
7235
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007236 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007237 *srisc_addr = 0;
7238 fwcode = (uint16_t *)blob->fw->data;
7239 fwclen = 0;
7240
7241 /* Validate firmware image by checking version. */
7242 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007243 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007244 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007245 blob->fw->size);
7246 goto fail_fw_integrity;
7247 }
7248 for (i = 0; i < 4; i++)
7249 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7250 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7251 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7252 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007253 ql_log(ql_log_fatal, vha, 0x0086,
7254 "Unable to verify integrity of firmware image.\n");
7255 ql_log(ql_log_fatal, vha, 0x0087,
7256 "Firmware data: %04x %04x %04x %04x.\n",
7257 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007258 goto fail_fw_integrity;
7259 }
7260
7261 seg = blob->segs;
7262 while (*seg && rval == QLA_SUCCESS) {
7263 risc_addr = *seg;
7264 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7265 risc_size = be16_to_cpu(fwcode[3]);
7266
7267 /* Validate firmware image size. */
7268 fwclen += risc_size * sizeof(uint16_t);
7269 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007270 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007271 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007272 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007273 goto fail_fw_integrity;
7274 }
7275
7276 fragment = 0;
7277 while (risc_size > 0 && rval == QLA_SUCCESS) {
7278 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7279 if (wlen > risc_size)
7280 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007281 ql_dbg(ql_dbg_init, vha, 0x0089,
7282 "Loading risc segment@ risc addr %x number of "
7283 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007284
7285 for (i = 0; i < wlen; i++)
7286 wcode[i] = swab16(fwcode[i]);
7287
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007288 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007289 wlen);
7290 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007291 ql_log(ql_log_fatal, vha, 0x008a,
7292 "Failed to load segment %d of firmware.\n",
7293 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007294 break;
7295 }
7296
7297 fwcode += wlen;
7298 risc_addr += wlen;
7299 risc_size -= wlen;
7300 fragment++;
7301 }
7302
7303 /* Next segment. */
7304 seg++;
7305 }
7306 return rval;
7307
7308fail_fw_integrity:
7309 return QLA_FUNCTION_FAILED;
7310}
7311
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007312static int
7313qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007314{
7315 int rval;
7316 int segments, fragment;
7317 uint32_t *dcode, dlen;
7318 uint32_t risc_addr;
7319 uint32_t risc_size;
7320 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007321 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007322 const uint32_t *fwcode;
7323 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007324 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007325 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007326
Andrew Vasquez54333832005-11-09 15:49:04 -08007327 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007328 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007329 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007330 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007331 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007332 ql_log(ql_log_warn, vha, 0x0091,
7333 "Firmware images can be retrieved from: "
7334 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007335
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007336 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007337 }
7338
Chad Dupuiscfb09192011-11-18 09:03:07 -08007339 ql_dbg(ql_dbg_init, vha, 0x0092,
7340 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007341
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007342 rval = QLA_SUCCESS;
7343
7344 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007345 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007346 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007347 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007348 fwclen = 0;
7349
7350 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007351 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007352 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007353 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007354 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007355 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007356 }
7357 for (i = 0; i < 4; i++)
7358 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7359 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7360 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7361 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7362 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007363 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007364 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007365 blob->fw->size);
7366 ql_log(ql_log_fatal, vha, 0x0095,
7367 "Firmware data: %08x %08x %08x %08x.\n",
7368 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007369 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007370 }
7371
7372 while (segments && rval == QLA_SUCCESS) {
7373 risc_addr = be32_to_cpu(fwcode[2]);
7374 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7375 risc_size = be32_to_cpu(fwcode[3]);
7376
7377 /* Validate firmware image size. */
7378 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007379 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007380 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007381 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007382 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007383 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007384 }
7385
7386 fragment = 0;
7387 while (risc_size > 0 && rval == QLA_SUCCESS) {
7388 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7389 if (dlen > risc_size)
7390 dlen = risc_size;
7391
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007392 ql_dbg(ql_dbg_init, vha, 0x0097,
7393 "Loading risc segment@ risc addr %x "
7394 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007395
7396 for (i = 0; i < dlen; i++)
7397 dcode[i] = swab32(fwcode[i]);
7398
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007399 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007400 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007401 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007402 ql_log(ql_log_fatal, vha, 0x0098,
7403 "Failed to load segment %d of firmware.\n",
7404 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007405 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007406 }
7407
7408 fwcode += dlen;
7409 risc_addr += dlen;
7410 risc_size -= dlen;
7411 fragment++;
7412 }
7413
7414 /* Next segment. */
7415 segments--;
7416 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007417
7418 if (!IS_QLA27XX(ha))
7419 return rval;
7420
7421 if (ha->fw_dump_template)
7422 vfree(ha->fw_dump_template);
7423 ha->fw_dump_template = NULL;
7424 ha->fw_dump_template_len = 0;
7425
7426 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007427 "Loading fwdump template from %x\n",
7428 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007429 risc_size = be32_to_cpu(fwcode[2]);
7430 ql_dbg(ql_dbg_init, vha, 0x172,
7431 "-> array size %x dwords\n", risc_size);
7432 if (risc_size == 0 || risc_size == ~0)
7433 goto default_template;
7434
7435 dlen = (risc_size - 8) * sizeof(*fwcode);
7436 ql_dbg(ql_dbg_init, vha, 0x0173,
7437 "-> template allocating %x bytes...\n", dlen);
7438 ha->fw_dump_template = vmalloc(dlen);
7439 if (!ha->fw_dump_template) {
7440 ql_log(ql_log_warn, vha, 0x0174,
7441 "Failed fwdump template allocate %x bytes.\n", risc_size);
7442 goto default_template;
7443 }
7444
7445 fwcode += 7;
7446 risc_size -= 8;
7447 dcode = ha->fw_dump_template;
7448 for (i = 0; i < risc_size; i++)
7449 dcode[i] = le32_to_cpu(fwcode[i]);
7450
7451 if (!qla27xx_fwdt_template_valid(dcode)) {
7452 ql_log(ql_log_warn, vha, 0x0175,
7453 "Failed fwdump template validate\n");
7454 goto default_template;
7455 }
7456
7457 dlen = qla27xx_fwdt_template_size(dcode);
7458 ql_dbg(ql_dbg_init, vha, 0x0176,
7459 "-> template size %x bytes\n", dlen);
7460 if (dlen > risc_size * sizeof(*fwcode)) {
7461 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007462 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007463 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007464 goto default_template;
7465 }
7466 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007467 return rval;
7468
Chad Dupuisf73cb692014-02-26 04:15:06 -05007469default_template:
7470 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7471 if (ha->fw_dump_template)
7472 vfree(ha->fw_dump_template);
7473 ha->fw_dump_template = NULL;
7474 ha->fw_dump_template_len = 0;
7475
7476 dlen = qla27xx_fwdt_template_default_size();
7477 ql_dbg(ql_dbg_init, vha, 0x0179,
7478 "-> template allocating %x bytes...\n", dlen);
7479 ha->fw_dump_template = vmalloc(dlen);
7480 if (!ha->fw_dump_template) {
7481 ql_log(ql_log_warn, vha, 0x017a,
7482 "Failed fwdump template allocate %x bytes.\n", risc_size);
7483 goto failed_template;
7484 }
7485
7486 dcode = ha->fw_dump_template;
7487 risc_size = dlen / sizeof(*fwcode);
7488 fwcode = qla27xx_fwdt_template_default();
7489 for (i = 0; i < risc_size; i++)
7490 dcode[i] = be32_to_cpu(fwcode[i]);
7491
7492 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7493 ql_log(ql_log_warn, vha, 0x017b,
7494 "Failed fwdump template validate\n");
7495 goto failed_template;
7496 }
7497
7498 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7499 ql_dbg(ql_dbg_init, vha, 0x017c,
7500 "-> template size %x bytes\n", dlen);
7501 ha->fw_dump_template_len = dlen;
7502 return rval;
7503
7504failed_template:
7505 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7506 if (ha->fw_dump_template)
7507 vfree(ha->fw_dump_template);
7508 ha->fw_dump_template = NULL;
7509 ha->fw_dump_template_len = 0;
7510 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007511}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007512
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007513int
7514qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7515{
7516 int rval;
7517
Andrew Vasqueze337d902009-04-06 22:33:49 -07007518 if (ql2xfwloadbin == 1)
7519 return qla81xx_load_risc(vha, srisc_addr);
7520
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007521 /*
7522 * FW Load priority:
7523 * 1) Firmware via request-firmware interface (.bin file).
7524 * 2) Firmware residing in flash.
7525 */
7526 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7527 if (rval == QLA_SUCCESS)
7528 return rval;
7529
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007530 return qla24xx_load_risc_flash(vha, srisc_addr,
7531 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007532}
7533
7534int
7535qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7536{
7537 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007538 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007539
Andrew Vasqueze337d902009-04-06 22:33:49 -07007540 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007541 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007542
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007543 /*
7544 * FW Load priority:
7545 * 1) Firmware residing in flash.
7546 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007547 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007548 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007549 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007550 if (rval == QLA_SUCCESS)
7551 return rval;
7552
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007553try_blob_fw:
7554 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7555 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7556 return rval;
7557
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007558 ql_log(ql_log_info, vha, 0x0099,
7559 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007560 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7561 if (rval != QLA_SUCCESS)
7562 return rval;
7563
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007564 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007565 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007566 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007567}
7568
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007569void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007570qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007571{
7572 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007573 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007574
Andrew Vasquez85880802009-12-15 21:29:46 -08007575 if (ha->flags.pci_channel_io_perm_failure)
7576 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007577 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007578 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007579 if (!ha->fw_major_version)
7580 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007581 if (!ha->flags.fw_started)
7582 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007583
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007584 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007585 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007586 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007587 ha->isp_ops->reset_chip(vha);
7588 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007589 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007590 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007591 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007592 ql_log(ql_log_info, vha, 0x8015,
7593 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007594 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007595 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007596
Quinn Tran4b60c822017-06-13 20:47:21 -07007597 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007598 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007599}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007600
7601int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007602qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007603{
7604 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007605 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007606 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007607 struct qla_hw_data *ha = vha->hw;
7608 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007609 struct req_que *req;
7610 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007611
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007612 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007613 return -EINVAL;
7614
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007615 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007616 if (vha->qpair)
7617 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007618 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007619 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007620 rsp = req->rsp;
7621
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007622 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007623 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007624 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007625 }
7626
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007627 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007628
7629 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007630 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7631 BIT_1);
7632 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7633 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7634 ql_dbg(ql_dbg_init, vha, 0x0120,
7635 "Failed SNS login: loop_id=%x, rval2=%d\n",
7636 NPH_SNS, rval2);
7637 else
7638 ql_dbg(ql_dbg_init, vha, 0x0103,
7639 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7640 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7641 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007642 return (QLA_FUNCTION_FAILED);
7643 }
7644
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007645 atomic_set(&vha->loop_down_timer, 0);
7646 atomic_set(&vha->loop_state, LOOP_UP);
7647 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7648 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7649 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007650
7651 return rval;
7652}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007653
7654/* 84XX Support **************************************************************/
7655
7656static LIST_HEAD(qla_cs84xx_list);
7657static DEFINE_MUTEX(qla_cs84xx_mutex);
7658
7659static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007660qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007661{
7662 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007663 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007664
7665 mutex_lock(&qla_cs84xx_mutex);
7666
7667 /* Find any shared 84xx chip. */
7668 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7669 if (cs84xx->bus == ha->pdev->bus) {
7670 kref_get(&cs84xx->kref);
7671 goto done;
7672 }
7673 }
7674
7675 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7676 if (!cs84xx)
7677 goto done;
7678
7679 kref_init(&cs84xx->kref);
7680 spin_lock_init(&cs84xx->access_lock);
7681 mutex_init(&cs84xx->fw_update_mutex);
7682 cs84xx->bus = ha->pdev->bus;
7683
7684 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7685done:
7686 mutex_unlock(&qla_cs84xx_mutex);
7687 return cs84xx;
7688}
7689
7690static void
7691__qla84xx_chip_release(struct kref *kref)
7692{
7693 struct qla_chip_state_84xx *cs84xx =
7694 container_of(kref, struct qla_chip_state_84xx, kref);
7695
7696 mutex_lock(&qla_cs84xx_mutex);
7697 list_del(&cs84xx->list);
7698 mutex_unlock(&qla_cs84xx_mutex);
7699 kfree(cs84xx);
7700}
7701
7702void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007703qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007704{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007705 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007706 if (ha->cs84xx)
7707 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7708}
7709
7710static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007711qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007712{
7713 int rval;
7714 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007715 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007716
7717 mutex_lock(&ha->cs84xx->fw_update_mutex);
7718
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007719 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007720
7721 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7722
7723 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7724 QLA_SUCCESS;
7725}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007726
7727/* 81XX Support **************************************************************/
7728
7729int
7730qla81xx_nvram_config(scsi_qla_host_t *vha)
7731{
7732 int rval;
7733 struct init_cb_81xx *icb;
7734 struct nvram_81xx *nv;
7735 uint32_t *dptr;
7736 uint8_t *dptr1, *dptr2;
7737 uint32_t chksum;
7738 uint16_t cnt;
7739 struct qla_hw_data *ha = vha->hw;
7740
7741 rval = QLA_SUCCESS;
7742 icb = (struct init_cb_81xx *)ha->init_cb;
7743 nv = ha->nvram;
7744
7745 /* Determine NVRAM starting address. */
7746 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007747 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007748 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7749 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007750
7751 /* Get VPD data into cache */
7752 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007753 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7754 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007755
7756 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007757 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007758 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007759 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007760 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7761 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007762
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007763 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7764 "Contents of NVRAM:\n");
7765 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7766 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007767
7768 /* Bad NVRAM data, set defaults parameters. */
7769 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7770 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007771 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007772 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007773 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007774 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007775 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007776 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007777 ql_log(ql_log_info, vha, 0x0074,
7778 "Falling back to functioning (yet invalid -- WWPN) "
7779 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007780
7781 /*
7782 * Set default initialization control block.
7783 */
7784 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007785 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7786 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007787 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007788 nv->execution_throttle = cpu_to_le16(0xFFFF);
7789 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007790 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007791 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007792 nv->port_name[2] = 0x00;
7793 nv->port_name[3] = 0xe0;
7794 nv->port_name[4] = 0x8b;
7795 nv->port_name[5] = 0x1c;
7796 nv->port_name[6] = 0x55;
7797 nv->port_name[7] = 0x86;
7798 nv->node_name[0] = 0x20;
7799 nv->node_name[1] = 0x00;
7800 nv->node_name[2] = 0x00;
7801 nv->node_name[3] = 0xe0;
7802 nv->node_name[4] = 0x8b;
7803 nv->node_name[5] = 0x1c;
7804 nv->node_name[6] = 0x55;
7805 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007806 nv->login_retry_count = cpu_to_le16(8);
7807 nv->interrupt_delay_timer = cpu_to_le16(0);
7808 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007809 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007810 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7811 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7812 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7813 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7814 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7815 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007816 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007817 nv->max_luns_per_target = cpu_to_le16(128);
7818 nv->port_down_retry_count = cpu_to_le16(30);
7819 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007820 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007821 nv->enode_mac[1] = 0xC0;
7822 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007823 nv->enode_mac[3] = 0x04;
7824 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007825 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007826
7827 rval = 1;
7828 }
7829
Arun Easi9e522cd2012-08-22 14:21:31 -04007830 if (IS_T10_PI_CAPABLE(ha))
7831 nv->frame_payload_size &= ~7;
7832
Arun Easiaa230bc2013-01-30 03:34:39 -05007833 qlt_81xx_config_nvram_stage1(vha, nv);
7834
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007835 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007836 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007837
7838 /* Copy 1st segment. */
7839 dptr1 = (uint8_t *)icb;
7840 dptr2 = (uint8_t *)&nv->version;
7841 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7842 while (cnt--)
7843 *dptr1++ = *dptr2++;
7844
7845 icb->login_retry_count = nv->login_retry_count;
7846
7847 /* Copy 2nd segment. */
7848 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7849 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7850 cnt = (uint8_t *)&icb->reserved_5 -
7851 (uint8_t *)&icb->interrupt_delay_timer;
7852 while (cnt--)
7853 *dptr1++ = *dptr2++;
7854
7855 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7856 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7857 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007858 icb->enode_mac[0] = 0x00;
7859 icb->enode_mac[1] = 0xC0;
7860 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007861 icb->enode_mac[3] = 0x04;
7862 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007863 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007864 }
7865
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007866 /* Use extended-initialization control block. */
7867 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7868
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007869 /*
7870 * Setup driver NVRAM options.
7871 */
7872 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007873 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007874
Arun Easiaa230bc2013-01-30 03:34:39 -05007875 qlt_81xx_config_nvram_stage2(vha, icb);
7876
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007877 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007878 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007879 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7880 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7881 }
7882
7883 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007884 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007885 /*
7886 * Firmware will apply the following mask if the nodename was
7887 * not provided.
7888 */
7889 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7890 icb->node_name[0] &= 0xF0;
7891 }
7892
7893 /* Set host adapter parameters. */
7894 ha->flags.disable_risc_code_load = 0;
7895 ha->flags.enable_lip_reset = 0;
7896 ha->flags.enable_lip_full_login =
7897 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7898 ha->flags.enable_target_reset =
7899 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7900 ha->flags.enable_led_scheme = 0;
7901 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7902
7903 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7904 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7905
7906 /* save HBA serial number */
7907 ha->serial0 = icb->port_name[5];
7908 ha->serial1 = icb->port_name[6];
7909 ha->serial2 = icb->port_name[7];
7910 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7911 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7912
Bart Van Asschead950362015-07-09 07:24:08 -07007913 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007914
7915 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7916
7917 /* Set minimum login_timeout to 4 seconds. */
7918 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7919 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7920 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007921 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007922 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007923
7924 /* Set minimum RATOV to 100 tenths of a second. */
7925 ha->r_a_tov = 100;
7926
7927 ha->loop_reset_delay = nv->reset_delay;
7928
7929 /* Link Down Timeout = 0:
7930 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007931 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007932 * I/O's to OS with "DID_NO_CONNECT".
7933 *
7934 * Link Down Timeout != 0:
7935 *
7936 * The driver waits for the link to come up after link down
7937 * before returning I/Os to OS with "DID_NO_CONNECT".
7938 */
7939 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7940 ha->loop_down_abort_time =
7941 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7942 } else {
7943 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7944 ha->loop_down_abort_time =
7945 (LOOP_DOWN_TIME - ha->link_down_timeout);
7946 }
7947
7948 /* Need enough time to try and get the port back. */
7949 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7950 if (qlport_down_retry)
7951 ha->port_down_retry_count = qlport_down_retry;
7952
7953 /* Set login_retry_count */
7954 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7955 if (ha->port_down_retry_count ==
7956 le16_to_cpu(nv->port_down_retry_count) &&
7957 ha->port_down_retry_count > 3)
7958 ha->login_retry_count = ha->port_down_retry_count;
7959 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7960 ha->login_retry_count = ha->port_down_retry_count;
7961 if (ql2xloginretrycount)
7962 ha->login_retry_count = ql2xloginretrycount;
7963
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007964 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007965 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007966 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007967
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007968 /* Enable ZIO. */
7969 if (!vha->flags.init_done) {
7970 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7971 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7972 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7973 le16_to_cpu(icb->interrupt_delay_timer): 2;
7974 }
Bart Van Asschead950362015-07-09 07:24:08 -07007975 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007976 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7977 vha->flags.process_response_queue = 0;
7978 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7979 ha->zio_mode = QLA_ZIO_MODE_6;
7980
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007981 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007982 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007983 ha->zio_mode,
7984 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007985
7986 icb->firmware_options_2 |= cpu_to_le32(
7987 (uint32_t)ha->zio_mode);
7988 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7989 vha->flags.process_response_queue = 1;
7990 }
7991
Quinn Tran41dc5292017-01-19 22:28:03 -08007992 /* enable RIDA Format2 */
7993 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7994 icb->firmware_options_3 |= BIT_0;
7995
Duane Grigsbyedd05de2017-10-13 09:34:06 -07007996 if (IS_QLA27XX(ha)) {
7997 icb->firmware_options_3 |= BIT_8;
7998 ql_dbg(ql_log_info, vha, 0x0075,
7999 "Enabling direct connection.\n");
8000 }
8001
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008002 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008003 ql_log(ql_log_warn, vha, 0x0076,
8004 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008005 }
8006 return (rval);
8007}
8008
Giridhar Malavalia9083012010-04-12 17:59:55 -07008009int
8010qla82xx_restart_isp(scsi_qla_host_t *vha)
8011{
8012 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008013 struct qla_hw_data *ha = vha->hw;
8014 struct req_que *req = ha->req_q_map[0];
8015 struct rsp_que *rsp = ha->rsp_q_map[0];
8016 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008017 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008018
8019 status = qla2x00_init_rings(vha);
8020 if (!status) {
8021 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8022 ha->flags.chip_reset_done = 1;
8023
8024 status = qla2x00_fw_ready(vha);
8025 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008026 /* Issue a marker after FW becomes ready. */
8027 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008028 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008029 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008030 }
8031
8032 /* if no cable then assume it's good */
8033 if ((vha->device_flags & DFLG_NO_CABLE))
8034 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008035 }
8036
8037 if (!status) {
8038 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8039
8040 if (!atomic_read(&vha->loop_down_timer)) {
8041 /*
8042 * Issue marker command only when we are going
8043 * to start the I/O .
8044 */
8045 vha->marker_needed = 1;
8046 }
8047
Giridhar Malavalia9083012010-04-12 17:59:55 -07008048 ha->isp_ops->enable_intrs(ha);
8049
8050 ha->isp_abort_cnt = 0;
8051 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8052
Saurav Kashyap53296782011-05-10 11:30:06 -07008053 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008054 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008055
Giridhar Malavalia9083012010-04-12 17:59:55 -07008056 if (ha->fce) {
8057 ha->flags.fce_enabled = 1;
8058 memset(ha->fce, 0,
8059 fce_calc_size(ha->fce_bufs));
8060 rval = qla2x00_enable_fce_trace(vha,
8061 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8062 &ha->fce_bufs);
8063 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008064 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008065 "Unable to reinitialize FCE (%d).\n",
8066 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008067 ha->flags.fce_enabled = 0;
8068 }
8069 }
8070
8071 if (ha->eft) {
8072 memset(ha->eft, 0, EFT_SIZE);
8073 rval = qla2x00_enable_eft_trace(vha,
8074 ha->eft_dma, EFT_NUM_BUFFERS);
8075 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008076 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008077 "Unable to reinitialize EFT (%d).\n",
8078 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008079 }
8080 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008081 }
8082
8083 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008084 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008085 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008086
8087 spin_lock_irqsave(&ha->vport_slock, flags);
8088 list_for_each_entry(vp, &ha->vp_list, list) {
8089 if (vp->vp_idx) {
8090 atomic_inc(&vp->vref_count);
8091 spin_unlock_irqrestore(&ha->vport_slock, flags);
8092
Giridhar Malavalia9083012010-04-12 17:59:55 -07008093 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008094
8095 spin_lock_irqsave(&ha->vport_slock, flags);
8096 atomic_dec(&vp->vref_count);
8097 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008098 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008099 spin_unlock_irqrestore(&ha->vport_slock, flags);
8100
Giridhar Malavalia9083012010-04-12 17:59:55 -07008101 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008102 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008103 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008104 }
8105
8106 return status;
8107}
8108
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008109void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008110qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008111{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008112 struct qla_hw_data *ha = vha->hw;
8113
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008114 /* Hold status IOCBs until ABTS response received. */
8115 if (ql2xfwholdabts)
8116 ha->fw_options[3] |= BIT_12;
8117
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008118 /* Set Retry FLOGI in case of P2P connection */
8119 if (ha->operating_mode == P2P) {
8120 ha->fw_options[2] |= BIT_3;
8121 ql_dbg(ql_dbg_disc, vha, 0x2103,
8122 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8123 __func__, ha->fw_options[2]);
8124 }
8125
Quinn Tran41dc5292017-01-19 22:28:03 -08008126 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8127 if (ql2xmvasynctoatio) {
8128 if (qla_tgt_mode_enabled(vha) ||
8129 qla_dual_mode_enabled(vha))
8130 ha->fw_options[2] |= BIT_11;
8131 else
8132 ha->fw_options[2] &= ~BIT_11;
8133 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008134
Quinn Tranf7e761f2017-06-02 09:12:02 -07008135 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008136 qla_dual_mode_enabled(vha)) {
8137 /* FW auto send SCSI status during */
8138 ha->fw_options[1] |= BIT_8;
8139 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8140
8141 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008142 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008143 } else {
8144 ha->fw_options[1] &= ~BIT_8;
8145 ha->fw_options[10] &= 0x00ff;
8146
Quinn Tranf7e761f2017-06-02 09:12:02 -07008147 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008148 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008149
Quinn Tran41dc5292017-01-19 22:28:03 -08008150 if (ql2xetsenable) {
8151 /* Enable ETS Burst. */
8152 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8153 ha->fw_options[2] |= BIT_9;
8154 }
8155
Quinn Tran83548fe2017-06-02 09:12:01 -07008156 ql_dbg(ql_dbg_init, vha, 0x00e9,
8157 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8158 __func__, ha->fw_options[1], ha->fw_options[2],
8159 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008160
Andrew Vasquezae97c912010-02-18 10:07:28 -08008161 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008162}
Sarang Radke09ff7012010-03-19 17:03:59 -07008163
8164/*
8165 * qla24xx_get_fcp_prio
8166 * Gets the fcp cmd priority value for the logged in port.
8167 * Looks for a match of the port descriptors within
8168 * each of the fcp prio config entries. If a match is found,
8169 * the tag (priority) value is returned.
8170 *
8171 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008172 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008173 * fcport = port structure pointer.
8174 *
8175 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008176 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008177 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008178 *
8179 * Context:
8180 * Kernel context
8181 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008182static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008183qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8184{
8185 int i, entries;
8186 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008187 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008188 uint32_t pid1, pid2;
8189 uint64_t wwn1, wwn2;
8190 struct qla_fcp_prio_entry *pri_entry;
8191 struct qla_hw_data *ha = vha->hw;
8192
8193 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008194 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008195
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008196 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008197 entries = ha->fcp_prio_cfg->num_entries;
8198 pri_entry = &ha->fcp_prio_cfg->entry[0];
8199
8200 for (i = 0; i < entries; i++) {
8201 pid_match = wwn_match = 0;
8202
8203 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8204 pri_entry++;
8205 continue;
8206 }
8207
8208 /* check source pid for a match */
8209 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8210 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8211 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8212 if (pid1 == INVALID_PORT_ID)
8213 pid_match++;
8214 else if (pid1 == pid2)
8215 pid_match++;
8216 }
8217
8218 /* check destination pid for a match */
8219 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8220 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8221 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8222 if (pid1 == INVALID_PORT_ID)
8223 pid_match++;
8224 else if (pid1 == pid2)
8225 pid_match++;
8226 }
8227
8228 /* check source WWN for a match */
8229 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8230 wwn1 = wwn_to_u64(vha->port_name);
8231 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8232 if (wwn2 == (uint64_t)-1)
8233 wwn_match++;
8234 else if (wwn1 == wwn2)
8235 wwn_match++;
8236 }
8237
8238 /* check destination WWN for a match */
8239 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8240 wwn1 = wwn_to_u64(fcport->port_name);
8241 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8242 if (wwn2 == (uint64_t)-1)
8243 wwn_match++;
8244 else if (wwn1 == wwn2)
8245 wwn_match++;
8246 }
8247
8248 if (pid_match == 2 || wwn_match == 2) {
8249 /* Found a matching entry */
8250 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8251 priority = pri_entry->tag;
8252 break;
8253 }
8254
8255 pri_entry++;
8256 }
8257
8258 return priority;
8259}
8260
8261/*
8262 * qla24xx_update_fcport_fcp_prio
8263 * Activates fcp priority for the logged in fc port
8264 *
8265 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008266 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008267 * fcp = port structure pointer.
8268 *
8269 * Return:
8270 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8271 *
8272 * Context:
8273 * Kernel context.
8274 */
8275int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008276qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008277{
8278 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008279 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008280 uint16_t mb[5];
8281
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008282 if (fcport->port_type != FCT_TARGET ||
8283 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008284 return QLA_FUNCTION_FAILED;
8285
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008286 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008287 if (priority < 0)
8288 return QLA_FUNCTION_FAILED;
8289
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008290 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008291 fcport->fcp_prio = priority & 0xf;
8292 return QLA_SUCCESS;
8293 }
8294
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008295 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008296 if (ret == QLA_SUCCESS) {
8297 if (fcport->fcp_prio != priority)
8298 ql_dbg(ql_dbg_user, vha, 0x709e,
8299 "Updated FCP_CMND priority - value=%d loop_id=%d "
8300 "port_id=%02x%02x%02x.\n", priority,
8301 fcport->loop_id, fcport->d_id.b.domain,
8302 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008303 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008304 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008305 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008306 "Unable to update FCP_CMND priority - ret=0x%x for "
8307 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8308 fcport->d_id.b.domain, fcport->d_id.b.area,
8309 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008310 return ret;
8311}
8312
8313/*
8314 * qla24xx_update_all_fcp_prio
8315 * Activates fcp priority for all the logged in ports
8316 *
8317 * Input:
8318 * ha = adapter block pointer.
8319 *
8320 * Return:
8321 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8322 *
8323 * Context:
8324 * Kernel context.
8325 */
8326int
8327qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8328{
8329 int ret;
8330 fc_port_t *fcport;
8331
8332 ret = QLA_FUNCTION_FAILED;
8333 /* We need to set priority for all logged in ports */
8334 list_for_each_entry(fcport, &vha->vp_fcports, list)
8335 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8336
8337 return ret;
8338}
Michael Hernandezd7459522016-12-12 14:40:07 -08008339
Quinn Tran82de8022017-06-13 20:47:17 -07008340struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8341 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008342{
8343 int rsp_id = 0;
8344 int req_id = 0;
8345 int i;
8346 struct qla_hw_data *ha = vha->hw;
8347 uint16_t qpair_id = 0;
8348 struct qla_qpair *qpair = NULL;
8349 struct qla_msix_entry *msix;
8350
8351 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8352 ql_log(ql_log_warn, vha, 0x00181,
8353 "FW/Driver is not multi-queue capable.\n");
8354 return NULL;
8355 }
8356
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008357 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008358 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8359 if (qpair == NULL) {
8360 ql_log(ql_log_warn, vha, 0x0182,
8361 "Failed to allocate memory for queue pair.\n");
8362 return NULL;
8363 }
8364 memset(qpair, 0, sizeof(struct qla_qpair));
8365
8366 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008367 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008368 qpair->qp_lock_ptr = &qpair->qp_lock;
8369 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008370 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008371
8372 /* Assign available que pair id */
8373 mutex_lock(&ha->mq_lock);
8374 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008375 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008376 mutex_unlock(&ha->mq_lock);
8377 ql_log(ql_log_warn, vha, 0x0183,
8378 "No resources to create additional q pair.\n");
8379 goto fail_qid_map;
8380 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008381 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008382 set_bit(qpair_id, ha->qpair_qid_map);
8383 ha->queue_pair_map[qpair_id] = qpair;
8384 qpair->id = qpair_id;
8385 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008386 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008387 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008388 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008389 qpair->chip_reset = ha->base_qpair->chip_reset;
8390 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8391 qpair->enable_explicit_conf =
8392 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008393
8394 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008395 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008396 if (msix->in_use)
8397 continue;
8398 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008399 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008400 "Vector %x selected for qpair\n", msix->vector);
8401 break;
8402 }
8403 if (!qpair->msix) {
8404 ql_log(ql_log_warn, vha, 0x0184,
8405 "Out of MSI-X vectors!.\n");
8406 goto fail_msix;
8407 }
8408
8409 qpair->msix->in_use = 1;
8410 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008411 qpair->pdev = ha->pdev;
8412 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8413 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008414
8415 mutex_unlock(&ha->mq_lock);
8416
8417 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008418 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008419 if (!rsp_id) {
8420 ql_log(ql_log_warn, vha, 0x0185,
8421 "Failed to create response queue.\n");
8422 goto fail_rsp;
8423 }
8424
8425 qpair->rsp = ha->rsp_q_map[rsp_id];
8426
8427 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008428 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8429 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008430 if (!req_id) {
8431 ql_log(ql_log_warn, vha, 0x0186,
8432 "Failed to create request queue.\n");
8433 goto fail_req;
8434 }
8435
8436 qpair->req = ha->req_q_map[req_id];
8437 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008438 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008439 /* init qpair to this cpu. Will adjust at run time. */
8440 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008441
8442 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8443 if (ha->fw_attributes & BIT_4)
8444 qpair->difdix_supported = 1;
8445 }
8446
8447 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8448 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008449 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008450 "Failed to create srb mempool for qpair %d\n",
8451 qpair->id);
8452 goto fail_mempool;
8453 }
8454
8455 /* Mark as online */
8456 qpair->online = 1;
8457
8458 if (!vha->flags.qpairs_available)
8459 vha->flags.qpairs_available = 1;
8460
8461 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8462 "Request/Response queue pair created, id %d\n",
8463 qpair->id);
8464 ql_dbg(ql_dbg_init, vha, 0x0187,
8465 "Request/Response queue pair created, id %d\n",
8466 qpair->id);
8467 }
8468 return qpair;
8469
8470fail_mempool:
8471fail_req:
8472 qla25xx_delete_rsp_que(vha, qpair->rsp);
8473fail_rsp:
8474 mutex_lock(&ha->mq_lock);
8475 qpair->msix->in_use = 0;
8476 list_del(&qpair->qp_list_elem);
8477 if (list_empty(&vha->qp_list))
8478 vha->flags.qpairs_available = 0;
8479fail_msix:
8480 ha->queue_pair_map[qpair_id] = NULL;
8481 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008482 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008483 mutex_unlock(&ha->mq_lock);
8484fail_qid_map:
8485 kfree(qpair);
8486 return NULL;
8487}
8488
8489int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8490{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008491 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008492 struct qla_hw_data *ha = qpair->hw;
8493
8494 qpair->delete_in_progress = 1;
8495 while (atomic_read(&qpair->ref_count))
8496 msleep(500);
8497
8498 ret = qla25xx_delete_req_que(vha, qpair->req);
8499 if (ret != QLA_SUCCESS)
8500 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008501
Michael Hernandezd7459522016-12-12 14:40:07 -08008502 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8503 if (ret != QLA_SUCCESS)
8504 goto fail;
8505
8506 mutex_lock(&ha->mq_lock);
8507 ha->queue_pair_map[qpair->id] = NULL;
8508 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008509 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008510 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008511 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008512 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008513 vha->flags.qpairs_req_created = 0;
8514 vha->flags.qpairs_rsp_created = 0;
8515 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008516 mempool_destroy(qpair->srb_mempool);
8517 kfree(qpair);
8518 mutex_unlock(&ha->mq_lock);
8519
8520 return QLA_SUCCESS;
8521fail:
8522 return ret;
8523}