blob: 61534b9bef7bf978db3647000aeb055730817786 [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);
Giridhar Malavali045d6ea2017-12-28 12:33:21 -080062 if (sp->type != SRB_ELS_DCMD)
63 sp->free(sp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080064 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -070065}
66
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080067void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080068qla2x00_sp_free(void *ptr)
Andrew Vasquezac280b62009-08-20 11:06:05 -070069{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080070 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080071 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -070072
Chad Dupuis4d97cc52010-10-15 11:27:41 -070073 del_timer(&iocb->timer);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080074 qla2x00_rel_sp(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -070075}
76
Andrew Vasquezac280b62009-08-20 11:06:05 -070077/* Asynchronous Login/Logout Routines -------------------------------------- */
78
Saurav Kashyapa9b6f722012-08-22 14:21:01 -040079unsigned long
Andrew Vasquez5b914902010-05-28 15:08:30 -070080qla2x00_get_async_timeout(struct scsi_qla_host *vha)
81{
82 unsigned long tmo;
83 struct qla_hw_data *ha = vha->hw;
84
85 /* Firmware should use switch negotiated r_a_tov for timeout. */
86 tmo = ha->r_a_tov / 10 * 2;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040087 if (IS_QLAFX00(ha)) {
88 tmo = FX00_DEF_RATOV * 2;
89 } else if (!IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez5b914902010-05-28 15:08:30 -070090 /*
91 * Except for earlier ISPs where the timeout is seeded from the
92 * initialization control block.
93 */
94 tmo = ha->login_timeout;
95 }
96 return tmo;
97}
Andrew Vasquezac280b62009-08-20 11:06:05 -070098
Quinn Tran726b8542017-01-19 22:28:00 -080099void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800100qla2x00_async_iocb_timeout(void *data)
Andrew Vasquezac280b62009-08-20 11:06:05 -0700101{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800102 srb_t *sp = data;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700103 fc_port_t *fcport = sp->fcport;
Quinn Tran726b8542017-01-19 22:28:00 -0800104 struct srb_iocb *lio = &sp->u.iocb_cmd;
105 struct event_arg ea;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700106
Quinn Tran5c25d452017-12-28 12:33:09 -0800107 if (fcport) {
108 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
109 "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
110 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700111
Quinn Tran5c25d452017-12-28 12:33:09 -0800112 fcport->flags &= ~FCF_ASYNC_SENT;
113 } else {
114 pr_info("Async-%s timeout - hdl=%x.\n",
115 sp->name, sp->handle);
116 }
Quinn Tran726b8542017-01-19 22:28:00 -0800117
118 switch (sp->type) {
119 case SRB_LOGIN_CMD:
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700120 /* Retry as needed. */
121 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
122 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
123 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800124 memset(&ea, 0, sizeof(ea));
125 ea.event = FCME_PLOGI_DONE;
126 ea.fcport = sp->fcport;
127 ea.data[0] = lio->u.logio.data[0];
128 ea.data[1] = lio->u.logio.data[1];
129 ea.sp = sp;
130 qla24xx_handle_plogi_done_event(fcport->vha, &ea);
131 break;
132 case SRB_LOGOUT_CMD:
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400133 qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800134 break;
135 case SRB_CT_PTHRU_CMD:
136 case SRB_MB_IOCB:
137 case SRB_NACK_PLOGI:
138 case SRB_NACK_PRLI:
139 case SRB_NACK_LOGO:
Quinn Tran28531922017-12-28 12:33:10 -0800140 case SRB_CTRL_VP:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800141 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800142 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700143 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700144}
145
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700146static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800147qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700148{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800149 srb_t *sp = ptr;
150 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800151 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800152 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700153
Quinn Tran83548fe2017-06-02 09:12:01 -0700154 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800155 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800156
157 sp->fcport->flags &= ~FCF_ASYNC_SENT;
158 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
159 memset(&ea, 0, sizeof(ea));
160 ea.event = FCME_PLOGI_DONE;
161 ea.fcport = sp->fcport;
162 ea.data[0] = lio->u.logio.data[0];
163 ea.data[1] = lio->u.logio.data[1];
164 ea.iop[0] = lio->u.logio.iop[0];
165 ea.iop[1] = lio->u.logio.iop[1];
166 ea.sp = sp;
167 qla2x00_fcport_event_handler(vha, &ea);
168 }
169
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800170 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700171}
172
Andrew Vasquezac280b62009-08-20 11:06:05 -0700173int
174qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
175 uint16_t *data)
176{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700177 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700178 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800179 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700180
Quinn Tran726b8542017-01-19 22:28:00 -0800181 if (!vha->flags.online)
182 goto done;
183
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800184 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700185 if (!sp)
186 goto done;
187
Quinn Tran726b8542017-01-19 22:28:00 -0800188 fcport->flags |= FCF_ASYNC_SENT;
189 fcport->logout_completed = 0;
190
Quinn Trana4239942017-12-28 12:33:26 -0800191 fcport->disc_state = DSC_LOGIN_PEND;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800192 sp->type = SRB_LOGIN_CMD;
193 sp->name = "login";
Quinn Trana4239942017-12-28 12:33:26 -0800194 sp->gen1 = fcport->rscn_gen;
195 sp->gen2 = fcport->login_gen;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800196 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
197
198 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700199 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800200 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700201 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700202
203 if (fcport->fc4f_nvme)
204 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
205
Andrew Vasquezac280b62009-08-20 11:06:05 -0700206 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700207 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700208 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500209 if (rval != QLA_SUCCESS) {
210 fcport->flags &= ~FCF_ASYNC_SENT;
211 fcport->flags |= FCF_LOGIN_NEEDED;
212 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700213 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500214 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700215
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700216 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800217 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
218 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800219 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
220 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700221 return rval;
222
223done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800224 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700225done:
Quinn Tran726b8542017-01-19 22:28:00 -0800226 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700227 return rval;
228}
229
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700230static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800231qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700232{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800233 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800234 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700235
Quinn Tran726b8542017-01-19 22:28:00 -0800236 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800237 if (!test_bit(UNLOADING, &sp->vha->dpc_flags))
238 qla2x00_post_async_logout_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800239 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800240 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700241}
242
Andrew Vasquezac280b62009-08-20 11:06:05 -0700243int
244qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
245{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700246 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700247 struct srb_iocb *lio;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700248 int rval;
249
250 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800251 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800252 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700253 if (!sp)
254 goto done;
255
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800256 sp->type = SRB_LOGOUT_CMD;
257 sp->name = "logout";
258 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
259
260 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700261 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800262 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700263 rval = qla2x00_start_sp(sp);
264 if (rval != QLA_SUCCESS)
265 goto done_free_sp;
266
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700267 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800268 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800269 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800270 fcport->d_id.b.area, fcport->d_id.b.al_pa,
271 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700272 return rval;
273
274done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800275 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700276done:
Quinn Tran726b8542017-01-19 22:28:00 -0800277 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700278 return rval;
279}
Quinn Tran11aea162017-12-28 12:33:20 -0800280
281void
282qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
283 uint16_t *data)
284{
285 /* Don't re-login in target mode */
286 if (!fcport->tgt_session)
287 qla2x00_mark_device_lost(vha, fcport, 1, 0);
288 qlt_logo_completion_handler(fcport, data[0]);
289}
290
291static void
292qla2x00_async_prlo_sp_done(void *s, int res)
293{
294 srb_t *sp = (srb_t *)s;
295 struct srb_iocb *lio = &sp->u.iocb_cmd;
296 struct scsi_qla_host *vha = sp->vha;
297
298 if (!test_bit(UNLOADING, &vha->dpc_flags))
299 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
300 lio->u.logio.data);
301 sp->free(sp);
302}
303
304int
305qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
306{
307 srb_t *sp;
308 struct srb_iocb *lio;
309 int rval;
310
311 rval = QLA_FUNCTION_FAILED;
312 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
313 if (!sp)
314 goto done;
315
316 sp->type = SRB_PRLO_CMD;
317 sp->name = "prlo";
318 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
319
320 lio = &sp->u.iocb_cmd;
321 lio->timeout = qla2x00_async_iocb_timeout;
322 sp->done = qla2x00_async_prlo_sp_done;
323 rval = qla2x00_start_sp(sp);
324 if (rval != QLA_SUCCESS)
325 goto done_free_sp;
326
327 ql_dbg(ql_dbg_disc, vha, 0x2070,
328 "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
329 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
330 fcport->d_id.b.area, fcport->d_id.b.al_pa);
331 return rval;
332
333done_free_sp:
334 sp->free(sp);
335done:
336 return rval;
337}
338
Quinn Tranf13515a2017-12-28 12:33:15 -0800339static
340void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
341{
Quinn Tran0616e962017-12-28 12:33:34 -0800342 struct fc_port *fcport = ea->fcport;
343
344 ql_dbg(ql_dbg_disc, vha, 0x20d2,
345 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
346 __func__, fcport->port_name, fcport->disc_state,
347 fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
348 fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
349
350 if (ea->data[0] != MBS_COMMAND_COMPLETE) {
Quinn Trana4239942017-12-28 12:33:26 -0800351 ql_dbg(ql_dbg_disc, vha, 0x2066,
352 "%s %8phC: adisc fail: post delete\n",
353 __func__, ea->fcport->port_name);
354 qlt_schedule_sess_for_deletion(ea->fcport, 1);
355 return;
356 }
Quinn Trana4239942017-12-28 12:33:26 -0800357
358 if (ea->fcport->disc_state == DSC_DELETE_PEND)
359 return;
360
361 if (ea->sp->gen2 != ea->fcport->login_gen) {
362 /* target side must have changed it. */
363 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran0616e962017-12-28 12:33:34 -0800364 "%s %8phC generation changed\n",
365 __func__, ea->fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800366 return;
367 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
368 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
369 __func__, __LINE__, ea->fcport->port_name);
370 qla24xx_post_gidpn_work(vha, ea->fcport);
371 return;
372 }
373
374 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800375}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700376
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700377static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800378qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700379{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800380 srb_t *sp = ptr;
381 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800382 struct event_arg ea;
Quinn Tran0616e962017-12-28 12:33:34 -0800383 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700384
Quinn Tranf13515a2017-12-28 12:33:15 -0800385 ql_dbg(ql_dbg_disc, vha, 0x2066,
386 "Async done-%s res %x %8phC\n",
387 sp->name, res, sp->fcport->port_name);
388
389 memset(&ea, 0, sizeof(ea));
390 ea.event = FCME_ADISC_DONE;
391 ea.rc = res;
Quinn Tran0616e962017-12-28 12:33:34 -0800392 ea.data[0] = lio->u.logio.data[0];
393 ea.data[1] = lio->u.logio.data[1];
394 ea.iop[0] = lio->u.logio.iop[0];
395 ea.iop[1] = lio->u.logio.iop[1];
Quinn Tranf13515a2017-12-28 12:33:15 -0800396 ea.fcport = sp->fcport;
397 ea.sp = sp;
398
399 qla2x00_fcport_event_handler(vha, &ea);
400
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800401 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700402}
403
404int
405qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
406 uint16_t *data)
407{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700408 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700409 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700410 int rval;
411
412 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800413 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800414 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700415 if (!sp)
416 goto done;
417
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800418 sp->type = SRB_ADISC_CMD;
419 sp->name = "adisc";
420 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
421
422 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700423 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800424 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700425 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700426 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700427 rval = qla2x00_start_sp(sp);
428 if (rval != QLA_SUCCESS)
429 goto done_free_sp;
430
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700431 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800432 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
433 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700434 return rval;
435
436done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800437 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700438done:
Quinn Tran726b8542017-01-19 22:28:00 -0800439 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tranf13515a2017-12-28 12:33:15 -0800440 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700441 return rval;
442}
443
Quinn Tran726b8542017-01-19 22:28:00 -0800444static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
445 struct event_arg *ea)
446{
447 fc_port_t *fcport, *conflict_fcport;
448 struct get_name_list_extended *e;
449 u16 i, n, found = 0, loop_id;
450 port_id_t id;
451 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800452 u16 data[2];
453 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800454
455 fcport = ea->fcport;
Quinn Tranf352eeb2017-12-28 12:33:35 -0800456 ql_dbg(ql_dbg_disc, vha, 0xffff,
457 "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
458 __func__, fcport->port_name, fcport->disc_state,
459 fcport->fw_login_state, ea->rc,
460 fcport->login_gen, fcport->last_login_gen,
461 fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800462
Quinn Trana4239942017-12-28 12:33:26 -0800463 if (fcport->disc_state == DSC_DELETE_PEND)
464 return;
465
Quinn Tran726b8542017-01-19 22:28:00 -0800466 if (ea->rc) { /* rval */
467 if (fcport->login_retry == 0) {
468 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700469 ql_dbg(ql_dbg_disc, vha, 0x20de,
470 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
471 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800472 }
473 return;
474 }
475
476 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700477 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800478 "%s %8phC rscn gen changed rscn %d|%d \n",
479 __func__, fcport->port_name,
480 fcport->last_rscn_gen, fcport->rscn_gen);
481 qla24xx_post_gidpn_work(vha, fcport);
482 return;
483 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700484 ql_dbg(ql_dbg_disc, vha, 0x20e0,
Quinn Tranf352eeb2017-12-28 12:33:35 -0800485 "%s %8phC login gen changed\n",
486 __func__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800487 return;
488 }
489
490 n = ea->data[0] / sizeof(struct get_name_list_extended);
491
Quinn Tran83548fe2017-06-02 09:12:01 -0700492 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800493 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
494 __func__, __LINE__, fcport->port_name, n,
495 fcport->d_id.b.domain, fcport->d_id.b.area,
496 fcport->d_id.b.al_pa, fcport->loop_id);
497
498 for (i = 0; i < n; i++) {
499 e = &vha->gnl.l[i];
500 wwn = wwn_to_u64(e->port_name);
501
502 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
503 continue;
504
505 found = 1;
506 id.b.domain = e->port_id[2];
507 id.b.area = e->port_id[1];
508 id.b.al_pa = e->port_id[0];
509 id.b.rsvd_1 = 0;
510
511 loop_id = le16_to_cpu(e->nport_handle);
512 loop_id = (loop_id & 0x7fff);
513
Quinn Tran83548fe2017-06-02 09:12:01 -0700514 ql_dbg(ql_dbg_disc, vha, 0x20e2,
515 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
516 __func__, fcport->port_name,
517 e->current_login_state, fcport->fw_login_state,
518 id.b.domain, id.b.area, id.b.al_pa,
519 fcport->d_id.b.domain, fcport->d_id.b.area,
520 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800521
522 if ((id.b24 != fcport->d_id.b24) ||
523 ((fcport->loop_id != FC_NO_LOOP_ID) &&
524 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700525 ql_dbg(ql_dbg_disc, vha, 0x20e3,
526 "%s %d %8phC post del sess\n",
527 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800528 qlt_schedule_sess_for_deletion(fcport, 1);
529 return;
530 }
531
532 fcport->loop_id = loop_id;
533
534 wwn = wwn_to_u64(fcport->port_name);
535 qlt_find_sess_invalidate_other(vha, wwn,
536 id, loop_id, &conflict_fcport);
537
538 if (conflict_fcport) {
539 /*
540 * Another share fcport share the same loop_id &
541 * nport id. Conflict fcport needs to finish
542 * cleanup before this fcport can proceed to login.
543 */
544 conflict_fcport->conflict = fcport;
545 fcport->login_pause = 1;
546 }
547
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700548 if (fcport->fc4f_nvme)
549 current_login_state = e->current_login_state >> 4;
550 else
551 current_login_state = e->current_login_state & 0xf;
552
553 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800554 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700555 ql_dbg(ql_dbg_disc, vha, 0x20e4,
556 "%s %d %8phC post gpdb\n",
557 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800558
559 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
560 fcport->port_type = FCT_INITIATOR;
561 else
562 fcport->port_type = FCT_TARGET;
563
564 data[0] = data[1] = 0;
565 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800566 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800567 case DSC_LS_PORT_UNAVAIL:
568 default:
569 if (fcport->loop_id == FC_NO_LOOP_ID) {
570 qla2x00_find_new_loop_id(vha, fcport);
571 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
572 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700573 ql_dbg(ql_dbg_disc, vha, 0x20e5,
574 "%s %d %8phC\n",
575 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800576 qla24xx_fcport_handle_login(vha, fcport);
577 break;
578 }
579 }
580
581 if (!found) {
582 /* fw has no record of this port */
583 if (fcport->loop_id == FC_NO_LOOP_ID) {
584 qla2x00_find_new_loop_id(vha, fcport);
585 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
586 } else {
587 for (i = 0; i < n; i++) {
588 e = &vha->gnl.l[i];
589 id.b.domain = e->port_id[0];
590 id.b.area = e->port_id[1];
591 id.b.al_pa = e->port_id[2];
592 id.b.rsvd_1 = 0;
593 loop_id = le16_to_cpu(e->nport_handle);
594
595 if (fcport->d_id.b24 == id.b24) {
596 conflict_fcport =
597 qla2x00_find_fcport_by_wwpn(vha,
598 e->port_name, 0);
599
Quinn Tran83548fe2017-06-02 09:12:01 -0700600 ql_dbg(ql_dbg_disc, vha, 0x20e6,
Quinn Tran726b8542017-01-19 22:28:00 -0800601 "%s %d %8phC post del sess\n",
602 __func__, __LINE__,
603 conflict_fcport->port_name);
604 qlt_schedule_sess_for_deletion
605 (conflict_fcport, 1);
606 }
607
608 if (fcport->loop_id == loop_id) {
609 /* FW already picked this loop id for another fcport */
610 qla2x00_find_new_loop_id(vha, fcport);
611 }
612 }
613 }
614 qla24xx_fcport_handle_login(vha, fcport);
615 }
616} /* gnl_event */
617
618static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800619qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800620{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800621 struct srb *sp = s;
622 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800623 unsigned long flags;
624 struct fc_port *fcport = NULL, *tf;
625 u16 i, n = 0, loop_id;
626 struct event_arg ea;
627 struct get_name_list_extended *e;
628 u64 wwn;
629 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800630 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800631
Quinn Tran83548fe2017-06-02 09:12:01 -0700632 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800633 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
634 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
635 sp->u.iocb_cmd.u.mbx.in_mb[2]);
636
637 memset(&ea, 0, sizeof(ea));
638 ea.sp = sp;
639 ea.rc = res;
640 ea.event = FCME_GNL_DONE;
641
642 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
643 sizeof(struct get_name_list_extended)) {
644 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
645 sizeof(struct get_name_list_extended);
646 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
647 }
648
649 for (i = 0; i < n; i++) {
650 e = &vha->gnl.l[i];
651 loop_id = le16_to_cpu(e->nport_handle);
652 /* mask out reserve bit */
653 loop_id = (loop_id & 0x7fff);
654 set_bit(loop_id, vha->hw->loop_id_map);
655 wwn = wwn_to_u64(e->port_name);
656
Quinn Tran83548fe2017-06-02 09:12:01 -0700657 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800658 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
659 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
660 e->port_id[0], e->current_login_state, e->last_login_state,
661 (loop_id & 0x7fff));
662 }
663
664 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
665 vha->gnl.sent = 0;
666
667 INIT_LIST_HEAD(&h);
668 fcport = tf = NULL;
669 if (!list_empty(&vha->gnl.fcports))
670 list_splice_init(&vha->gnl.fcports, &h);
671
672 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
673 list_del_init(&fcport->gnl_entry);
674 fcport->flags &= ~FCF_ASYNC_SENT;
675 ea.fcport = fcport;
676
677 qla2x00_fcport_event_handler(vha, &ea);
678 }
679
Quinn Trana4239942017-12-28 12:33:26 -0800680 /* create new fcport if fw has knowledge of new sessions */
681 for (i = 0; i < n; i++) {
682 port_id_t id;
683 u64 wwnn;
684
685 e = &vha->gnl.l[i];
686 wwn = wwn_to_u64(e->port_name);
687
688 found = false;
689 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
690 if (!memcmp((u8 *)&wwn, fcport->port_name,
691 WWN_SIZE)) {
692 found = true;
693 break;
694 }
695 }
696
Quinn Trancf055fb2017-12-28 12:33:33 -0800697 id.b.domain = e->port_id[2];
Quinn Trana4239942017-12-28 12:33:26 -0800698 id.b.area = e->port_id[1];
Quinn Trancf055fb2017-12-28 12:33:33 -0800699 id.b.al_pa = e->port_id[0];
Quinn Trana4239942017-12-28 12:33:26 -0800700 id.b.rsvd_1 = 0;
701
702 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
703 ql_dbg(ql_dbg_disc, vha, 0x2065,
Quinn Trancf055fb2017-12-28 12:33:33 -0800704 "%s %d %8phC %06x post new sess\n",
705 __func__, __LINE__, (u8 *)&wwn, id.b24);
Quinn Trana4239942017-12-28 12:33:26 -0800706 wwnn = wwn_to_u64(e->node_name);
707 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
708 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
709 }
710 }
711
Quinn Tran726b8542017-01-19 22:28:00 -0800712 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
713
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800714 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800715}
716
717int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
718{
719 srb_t *sp;
720 struct srb_iocb *mbx;
721 int rval = QLA_FUNCTION_FAILED;
722 unsigned long flags;
723 u16 *mb;
724
725 if (!vha->flags.online)
726 goto done;
727
Quinn Tran83548fe2017-06-02 09:12:01 -0700728 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800729 "Async-gnlist WWPN %8phC \n", fcport->port_name);
730
731 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
732 fcport->flags |= FCF_ASYNC_SENT;
733 fcport->disc_state = DSC_GNL;
734 fcport->last_rscn_gen = fcport->rscn_gen;
735 fcport->last_login_gen = fcport->login_gen;
736
737 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
738 if (vha->gnl.sent) {
739 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
740 rval = QLA_SUCCESS;
741 goto done;
742 }
743 vha->gnl.sent = 1;
744 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
745
746 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
747 if (!sp)
748 goto done;
749 sp->type = SRB_MB_IOCB;
750 sp->name = "gnlist";
751 sp->gen1 = fcport->rscn_gen;
752 sp->gen2 = fcport->login_gen;
753
754 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
755
756 mb = sp->u.iocb_cmd.u.mbx.out_mb;
757 mb[0] = MBC_PORT_NODE_NAME_LIST;
758 mb[1] = BIT_2 | BIT_3;
759 mb[2] = MSW(vha->gnl.ldma);
760 mb[3] = LSW(vha->gnl.ldma);
761 mb[6] = MSW(MSD(vha->gnl.ldma));
762 mb[7] = LSW(MSD(vha->gnl.ldma));
763 mb[8] = vha->gnl.size;
764 mb[9] = vha->vp_idx;
765
766 mbx = &sp->u.iocb_cmd;
767 mbx->timeout = qla2x00_async_iocb_timeout;
768
769 sp->done = qla24xx_async_gnl_sp_done;
770
771 rval = qla2x00_start_sp(sp);
772 if (rval != QLA_SUCCESS)
773 goto done_free_sp;
774
Quinn Tran83548fe2017-06-02 09:12:01 -0700775 ql_dbg(ql_dbg_disc, vha, 0x20da,
776 "Async-%s - OUT WWPN %8phC hndl %x\n",
777 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800778
779 return rval;
780
781done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800782 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800783done:
784 fcport->flags &= ~FCF_ASYNC_SENT;
785 return rval;
786}
787
788int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
789{
790 struct qla_work_evt *e;
791
792 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
793 if (!e)
794 return QLA_FUNCTION_FAILED;
795
796 e->u.fcport.fcport = fcport;
797 return qla2x00_post_work(vha, e);
798}
799
800static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800801void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800802{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800803 struct srb *sp = s;
804 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800805 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800806 fc_port_t *fcport = sp->fcport;
807 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800808 struct event_arg ea;
809
Quinn Tran83548fe2017-06-02 09:12:01 -0700810 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800811 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
812 sp->name, res, fcport->port_name, mb[1], mb[2]);
813
814 fcport->flags &= ~FCF_ASYNC_SENT;
815
Quinn Tran726b8542017-01-19 22:28:00 -0800816 memset(&ea, 0, sizeof(ea));
817 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800818 ea.fcport = fcport;
819 ea.sp = sp;
820
821 qla2x00_fcport_event_handler(vha, &ea);
822
823 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
824 sp->u.iocb_cmd.u.mbx.in_dma);
825
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800826 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800827}
828
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700829static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
830{
831 struct qla_work_evt *e;
832
833 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
834 if (!e)
835 return QLA_FUNCTION_FAILED;
836
837 e->u.fcport.fcport = fcport;
838
839 return qla2x00_post_work(vha, e);
840}
841
842static void
843qla2x00_async_prli_sp_done(void *ptr, int res)
844{
845 srb_t *sp = ptr;
846 struct scsi_qla_host *vha = sp->vha;
847 struct srb_iocb *lio = &sp->u.iocb_cmd;
848 struct event_arg ea;
849
850 ql_dbg(ql_dbg_disc, vha, 0x2129,
851 "%s %8phC res %d \n", __func__,
852 sp->fcport->port_name, res);
853
854 sp->fcport->flags &= ~FCF_ASYNC_SENT;
855
856 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
857 memset(&ea, 0, sizeof(ea));
858 ea.event = FCME_PRLI_DONE;
859 ea.fcport = sp->fcport;
860 ea.data[0] = lio->u.logio.data[0];
861 ea.data[1] = lio->u.logio.data[1];
862 ea.iop[0] = lio->u.logio.iop[0];
863 ea.iop[1] = lio->u.logio.iop[1];
864 ea.sp = sp;
865
866 qla2x00_fcport_event_handler(vha, &ea);
867 }
868
869 sp->free(sp);
870}
871
872int
873qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
874{
875 srb_t *sp;
876 struct srb_iocb *lio;
877 int rval = QLA_FUNCTION_FAILED;
878
879 if (!vha->flags.online)
880 return rval;
881
882 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
883 fcport->fw_login_state == DSC_LS_PLOGI_COMP ||
884 fcport->fw_login_state == DSC_LS_PRLI_PEND)
885 return rval;
886
887 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
888 if (!sp)
889 return rval;
890
891 fcport->flags |= FCF_ASYNC_SENT;
892 fcport->logout_completed = 0;
893
894 sp->type = SRB_PRLI_CMD;
895 sp->name = "prli";
896 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
897
898 lio = &sp->u.iocb_cmd;
899 lio->timeout = qla2x00_async_iocb_timeout;
900 sp->done = qla2x00_async_prli_sp_done;
901 lio->u.logio.flags = 0;
902
903 if (fcport->fc4f_nvme)
904 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
905
906 rval = qla2x00_start_sp(sp);
907 if (rval != QLA_SUCCESS) {
908 fcport->flags &= ~FCF_ASYNC_SENT;
909 fcport->flags |= FCF_LOGIN_NEEDED;
910 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
911 goto done_free_sp;
912 }
913
914 ql_dbg(ql_dbg_disc, vha, 0x211b,
915 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
916 fcport->port_name, sp->handle, fcport->loop_id,
917 fcport->d_id.b24, fcport->login_retry);
918
919 return rval;
920
921done_free_sp:
922 sp->free(sp);
923 fcport->flags &= ~FCF_ASYNC_SENT;
924 return rval;
925}
926
Quinn Trana07fc0a2017-08-23 15:05:21 -0700927int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800928{
929 struct qla_work_evt *e;
930
931 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
932 if (!e)
933 return QLA_FUNCTION_FAILED;
934
935 e->u.fcport.fcport = fcport;
936 e->u.fcport.opt = opt;
937 return qla2x00_post_work(vha, e);
938}
939
940int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
941{
942 srb_t *sp;
943 struct srb_iocb *mbx;
944 int rval = QLA_FUNCTION_FAILED;
945 u16 *mb;
946 dma_addr_t pd_dma;
947 struct port_database_24xx *pd;
948 struct qla_hw_data *ha = vha->hw;
949
950 if (!vha->flags.online)
951 goto done;
952
953 fcport->flags |= FCF_ASYNC_SENT;
954 fcport->disc_state = DSC_GPDB;
955
956 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
957 if (!sp)
958 goto done;
959
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700960 sp->type = SRB_MB_IOCB;
961 sp->name = "gpdb";
962 sp->gen1 = fcport->rscn_gen;
963 sp->gen2 = fcport->login_gen;
964 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
965
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200966 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800967 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700968 ql_log(ql_log_warn, vha, 0xd043,
969 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800970 goto done_free_sp;
971 }
Quinn Tran726b8542017-01-19 22:28:00 -0800972
Quinn Tran726b8542017-01-19 22:28:00 -0800973 mb = sp->u.iocb_cmd.u.mbx.out_mb;
974 mb[0] = MBC_GET_PORT_DATABASE;
975 mb[1] = fcport->loop_id;
976 mb[2] = MSW(pd_dma);
977 mb[3] = LSW(pd_dma);
978 mb[6] = MSW(MSD(pd_dma));
979 mb[7] = LSW(MSD(pd_dma));
980 mb[9] = vha->vp_idx;
981 mb[10] = opt;
982
983 mbx = &sp->u.iocb_cmd;
984 mbx->timeout = qla2x00_async_iocb_timeout;
985 mbx->u.mbx.in = (void *)pd;
986 mbx->u.mbx.in_dma = pd_dma;
987
988 sp->done = qla24xx_async_gpdb_sp_done;
989
990 rval = qla2x00_start_sp(sp);
991 if (rval != QLA_SUCCESS)
992 goto done_free_sp;
993
Quinn Tran83548fe2017-06-02 09:12:01 -0700994 ql_dbg(ql_dbg_disc, vha, 0x20dc,
995 "Async-%s %8phC hndl %x opt %x\n",
996 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800997
998 return rval;
999
1000done_free_sp:
1001 if (pd)
1002 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1003
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001004 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -08001005done:
1006 fcport->flags &= ~FCF_ASYNC_SENT;
1007 qla24xx_post_gpdb_work(vha, fcport, opt);
1008 return rval;
1009}
1010
1011static
Quinn Trana4239942017-12-28 12:33:26 -08001012void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001013{
Quinn Tran726b8542017-01-19 22:28:00 -08001014 unsigned long flags;
1015
Quinn Tran726b8542017-01-19 22:28:00 -08001016 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001017 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001018 ea->fcport->deleted = 0;
1019 ea->fcport->logout_on_delete = 1;
1020
1021 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1022 vha->fcport_count++;
1023 ea->fcport->login_succ = 1;
1024
1025 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1026 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001027 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001028 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001029 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001030 vha->fcport_count);
1031
Quinn Trana4239942017-12-28 12:33:26 -08001032 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001033 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001034 if (ea->fcport->id_changed) {
1035 ea->fcport->id_changed = 0;
1036 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1037 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1038 __func__, __LINE__, ea->fcport->port_name,
1039 vha->fcport_count);
1040 qla24xx_post_gfpnid_work(vha, ea->fcport);
1041 } else {
1042 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1043 "%s %d %8phC post gpsc fcp_cnt %d\n",
1044 __func__, __LINE__, ea->fcport->port_name,
1045 vha->fcport_count);
1046 qla24xx_post_gpsc_work(vha, ea->fcport);
1047 }
Quinn Tran726b8542017-01-19 22:28:00 -08001048 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001049 } else if (ea->fcport->login_succ) {
1050 /*
1051 * We have an existing session. A late RSCN delivery
1052 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001053 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001054 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001055 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1056 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001057 __func__, __LINE__, ea->fcport->port_name);
1058 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001059 }
1060 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001061}
Quinn Tran726b8542017-01-19 22:28:00 -08001062
Quinn Trana4239942017-12-28 12:33:26 -08001063static
1064void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1065{
Quinn Trana4239942017-12-28 12:33:26 -08001066 fc_port_t *fcport = ea->fcport;
1067 struct port_database_24xx *pd;
1068 struct srb *sp = ea->sp;
1069
1070 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1071
1072 fcport->flags &= ~FCF_ASYNC_SENT;
1073
1074 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001075 "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1076 fcport->disc_state, pd->current_login_state, ea->rc);
Quinn Trana4239942017-12-28 12:33:26 -08001077
1078 if (fcport->disc_state == DSC_DELETE_PEND)
1079 return;
1080
1081 switch (pd->current_login_state) {
1082 case PDS_PRLI_COMPLETE:
1083 __qla24xx_parse_gpdb(vha, fcport, pd);
1084 break;
1085 case PDS_PLOGI_PENDING:
1086 case PDS_PLOGI_COMPLETE:
1087 case PDS_PRLI_PENDING:
1088 case PDS_PRLI2_PENDING:
1089 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1090 __func__, __LINE__, fcport->port_name);
1091 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1092 return;
1093 case PDS_LOGO_PENDING:
1094 case PDS_PORT_UNAVAILABLE:
1095 default:
1096 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1097 __func__, __LINE__, fcport->port_name);
1098 qlt_schedule_sess_for_deletion_lock(fcport);
1099 return;
1100 }
1101 __qla24xx_handle_gpdb_event(vha, ea);
1102} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001103
1104static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1105{
1106 u8 login = 0;
1107
1108 if (qla_tgt_mode_enabled(vha))
1109 return;
1110
1111 if (qla_dual_mode_enabled(vha)) {
1112 if (N2N_TOPO(vha->hw)) {
1113 u64 mywwn, wwn;
1114
1115 mywwn = wwn_to_u64(vha->port_name);
1116 wwn = wwn_to_u64(fcport->port_name);
1117 if (mywwn > wwn)
1118 login = 1;
1119 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1120 && time_after_eq(jiffies,
1121 fcport->plogi_nack_done_deadline))
1122 login = 1;
1123 } else {
1124 login = 1;
1125 }
1126 } else {
1127 /* initiator mode */
1128 login = 1;
1129 }
1130
1131 if (login) {
1132 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1133 "%s %d %8phC post login\n",
1134 __func__, __LINE__, fcport->port_name);
1135 fcport->disc_state = DSC_LOGIN_PEND;
1136 qla2x00_post_async_login_work(vha, fcport, NULL);
1137 }
1138}
1139
Quinn Tran726b8542017-01-19 22:28:00 -08001140int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1141{
Quinn Tranf13515a2017-12-28 12:33:15 -08001142 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001143 u64 wwn;
1144
1145 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001146 "%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 -08001147 __func__, fcport->port_name, fcport->disc_state,
1148 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1149 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001150 fcport->login_gen, fcport->login_retry,
Quinn Trana4239942017-12-28 12:33:26 -08001151 fcport->loop_id, fcport->scan_state);
1152
Quinn Tran726b8542017-01-19 22:28:00 -08001153 if (fcport->login_retry == 0)
1154 return 0;
1155
1156 if (fcport->scan_state != QLA_FCPORT_FOUND)
1157 return 0;
1158
Quinn Tran726b8542017-01-19 22:28:00 -08001159 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001160 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1161 return 0;
1162
Quinn Tran5b334692017-03-15 09:48:48 -07001163 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001164 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1165 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001166 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001167 }
Quinn Tran5b334692017-03-15 09:48:48 -07001168 }
1169
Quinn Tran726b8542017-01-19 22:28:00 -08001170 /* for pure Target Mode. Login will not be initiated */
1171 if (vha->host->active_mode == MODE_TARGET)
1172 return 0;
1173
1174 if (fcport->flags & FCF_ASYNC_SENT) {
1175 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1176 return 0;
1177 }
1178
Quinn Trana4239942017-12-28 12:33:26 -08001179 fcport->login_retry--;
1180
Quinn Tran726b8542017-01-19 22:28:00 -08001181 switch (fcport->disc_state) {
1182 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001183 wwn = wwn_to_u64(fcport->node_name);
1184 if (wwn == 0) {
1185 ql_dbg(ql_dbg_disc, vha, 0xffff,
1186 "%s %d %8phC post GNNID\n",
1187 __func__, __LINE__, fcport->port_name);
1188 qla24xx_post_gnnid_work(vha, fcport);
1189 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001190 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1191 "%s %d %8phC post gnl\n",
1192 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001193 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001194 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001195 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001196 }
1197 break;
1198
1199 case DSC_GNL:
1200 if (fcport->login_pause) {
1201 fcport->last_rscn_gen = fcport->rscn_gen;
1202 fcport->last_login_gen = fcport->login_gen;
1203 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1204 break;
1205 }
1206
Quinn Tran9cd883f2017-12-28 12:33:24 -08001207 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001208 break;
1209
1210 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001211 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1212 "%s %d %8phC post gidpn\n",
1213 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001214 if (N2N_TOPO(vha->hw))
1215 qla_chk_n2n_b4_login(vha, fcport);
1216 else
1217 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001218 break;
1219
1220 case DSC_LOGIN_COMPLETE:
1221 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001222 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001223 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001224 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001225 data[0] = data[1] = 0;
1226 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001227 break;
1228
1229 default:
1230 break;
1231 }
1232
1233 return 0;
1234}
1235
1236static
1237void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1238{
1239 fcport->rscn_gen++;
1240
Quinn Tran83548fe2017-06-02 09:12:01 -07001241 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1242 "%s %8phC DS %d LS %d\n",
1243 __func__, fcport->port_name, fcport->disc_state,
1244 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001245
1246 if (fcport->flags & FCF_ASYNC_SENT)
1247 return;
1248
1249 switch (fcport->disc_state) {
1250 case DSC_DELETED:
1251 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001252 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001253 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001254 default:
1255 break;
1256 }
1257}
1258
1259int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001260 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001261{
1262 struct qla_work_evt *e;
1263 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1264 if (!e)
1265 return QLA_FUNCTION_FAILED;
1266
1267 e->u.new_sess.id = *id;
1268 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001269 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001270 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001271 if (node_name)
1272 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001273
1274 return qla2x00_post_work(vha, e);
1275}
1276
1277static
Quinn Tran726b8542017-01-19 22:28:00 -08001278void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1279 struct event_arg *ea)
1280{
1281 fc_port_t *fcport = ea->fcport;
1282
Quinn Tran83548fe2017-06-02 09:12:01 -07001283 ql_dbg(ql_dbg_disc, vha, 0x2102,
1284 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1285 __func__, fcport->port_name, fcport->disc_state,
1286 fcport->fw_login_state, fcport->login_pause,
1287 fcport->deleted, fcport->conflict,
1288 fcport->last_rscn_gen, fcport->rscn_gen,
1289 fcport->last_login_gen, fcport->login_gen,
1290 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001291
1292 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001293 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1294 return;
1295
Quinn Tran5b334692017-03-15 09:48:48 -07001296 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001297 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1298 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001299 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001300 }
Quinn Tran5b334692017-03-15 09:48:48 -07001301 }
1302
Quinn Tran726b8542017-01-19 22:28:00 -08001303 if (fcport->flags & FCF_ASYNC_SENT) {
1304 fcport->login_retry++;
1305 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1306 return;
1307 }
1308
1309 if (fcport->disc_state == DSC_DELETE_PEND) {
1310 fcport->login_retry++;
1311 return;
1312 }
1313
1314 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001315 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001316 __func__, __LINE__, fcport->port_name);
1317
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001318 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001319 return;
1320 }
1321
1322 qla24xx_fcport_handle_login(vha, fcport);
1323}
1324
Quinn Tran41dc5292017-01-19 22:28:03 -08001325void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001326{
Quinn Tranf352eeb2017-12-28 12:33:35 -08001327 fc_port_t *f, *tf;
Quinn Tran41dc5292017-01-19 22:28:03 -08001328 uint32_t id = 0, mask, rid;
Quinn Tranf352eeb2017-12-28 12:33:35 -08001329 unsigned long flags;
Quinn Tran726b8542017-01-19 22:28:00 -08001330
1331 switch (ea->event) {
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001332 case FCME_RSCN:
1333 case FCME_GIDPN_DONE:
1334 case FCME_GPSC_DONE:
1335 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001336 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001337 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1338 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1339 return;
1340 break;
1341 default:
1342 break;
1343 }
1344
1345 switch (ea->event) {
1346 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001347 if (test_bit(UNLOADING, &vha->dpc_flags))
1348 return;
1349
1350 qla24xx_handle_relogin_event(vha, ea);
1351 break;
1352 case FCME_RSCN:
1353 if (test_bit(UNLOADING, &vha->dpc_flags))
1354 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001355 switch (ea->id.b.rsvd_1) {
1356 case RSCN_PORT_ADDR:
Quinn Tranf352eeb2017-12-28 12:33:35 -08001357 spin_lock_irqsave(&vha->work_lock, flags);
1358 if (vha->scan.scan_flags == 0) {
1359 ql_dbg(ql_dbg_disc, vha, 0xffff,
1360 "%s: schedule\n", __func__);
1361 vha->scan.scan_flags |= SF_QUEUED;
1362 schedule_delayed_work(&vha->scan.scan_work, 5);
Quinn Tran726b8542017-01-19 22:28:00 -08001363 }
Quinn Tranf352eeb2017-12-28 12:33:35 -08001364 spin_unlock_irqrestore(&vha->work_lock, flags);
1365
Quinn Tran41dc5292017-01-19 22:28:03 -08001366 break;
1367 case RSCN_AREA_ADDR:
1368 case RSCN_DOM_ADDR:
1369 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1370 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001371 ql_dbg(ql_dbg_async, vha, 0x5044,
1372 "RSCN: Area 0x%06x was affected\n",
1373 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001374 } else {
1375 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001376 ql_dbg(ql_dbg_async, vha, 0x507a,
1377 "RSCN: Domain 0x%06x was affected\n",
1378 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001379 }
1380
1381 rid = ea->id.b24 & mask;
1382 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1383 list) {
1384 id = f->d_id.b24 & mask;
1385 if (rid == id) {
1386 ea->fcport = f;
1387 qla24xx_handle_rscn_event(f, ea);
1388 }
1389 }
1390 break;
1391 case RSCN_FAB_ADDR:
1392 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001393 ql_log(ql_log_warn, vha, 0xd045,
1394 "RSCN: Fabric was affected. Addr format %d\n",
1395 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001396 qla2x00_mark_all_devices_lost(vha, 1);
1397 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1398 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001399 }
1400 break;
1401 case FCME_GIDPN_DONE:
1402 qla24xx_handle_gidpn_event(vha, ea);
1403 break;
1404 case FCME_GNL_DONE:
1405 qla24xx_handle_gnl_done_event(vha, ea);
1406 break;
1407 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001408 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001409 break;
1410 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1411 qla24xx_handle_plogi_done_event(vha, ea);
1412 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001413 case FCME_PRLI_DONE:
1414 qla24xx_handle_prli_done_event(vha, ea);
1415 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001416 case FCME_GPDB_DONE:
1417 qla24xx_handle_gpdb_event(vha, ea);
1418 break;
1419 case FCME_GPNID_DONE:
1420 qla24xx_handle_gpnid_event(vha, ea);
1421 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001422 case FCME_GFFID_DONE:
1423 qla24xx_handle_gffid_event(vha, ea);
1424 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001425 case FCME_ADISC_DONE:
1426 qla24xx_handle_adisc_event(vha, ea);
1427 break;
Quinn Trana4239942017-12-28 12:33:26 -08001428 case FCME_GNNID_DONE:
1429 qla24xx_handle_gnnid_event(vha, ea);
1430 break;
1431 case FCME_GFPNID_DONE:
1432 qla24xx_handle_gfpnid_event(vha, ea);
1433 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001434 default:
1435 BUG_ON(1);
1436 break;
1437 }
1438}
1439
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001440static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001441qla2x00_tmf_iocb_timeout(void *data)
1442{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001443 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001444 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1445
1446 tmf->u.tmf.comp_status = CS_TIMEOUT;
1447 complete(&tmf->u.tmf.comp);
1448}
1449
1450static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001451qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001452{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001453 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001454 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001455
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001456 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001457}
1458
1459int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001460qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001461 uint32_t tag)
1462{
1463 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001464 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001465 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001466 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001467
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001468 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001469 if (!sp)
1470 goto done;
1471
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001472 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001473 sp->type = SRB_TM_CMD;
1474 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001475 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1476 tm_iocb->u.tmf.flags = flags;
1477 tm_iocb->u.tmf.lun = lun;
1478 tm_iocb->u.tmf.data = tag;
1479 sp->done = qla2x00_tmf_sp_done;
1480 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1481 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001482
1483 rval = qla2x00_start_sp(sp);
1484 if (rval != QLA_SUCCESS)
1485 goto done_free_sp;
1486
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001487 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001488 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1489 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1490 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001491
1492 wait_for_completion(&tm_iocb->u.tmf.comp);
1493
1494 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1495 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1496
1497 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1498 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1499 "TM IOCB failed (%x).\n", rval);
1500 }
1501
1502 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1503 flags = tm_iocb->u.tmf.flags;
1504 lun = (uint16_t)tm_iocb->u.tmf.lun;
1505
1506 /* Issue Marker IOCB */
1507 qla2x00_marker(vha, vha->hw->req_q_map[0],
1508 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1509 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1510 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001511
1512done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001513 sp->free(sp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001514done:
1515 return rval;
1516}
1517
Armen Baloyan4440e462014-02-26 04:15:18 -05001518static void
1519qla24xx_abort_iocb_timeout(void *data)
1520{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001521 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001522 struct srb_iocb *abt = &sp->u.iocb_cmd;
1523
1524 abt->u.abt.comp_status = CS_TIMEOUT;
1525 complete(&abt->u.abt.comp);
1526}
1527
1528static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001529qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001530{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001531 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001532 struct srb_iocb *abt = &sp->u.iocb_cmd;
1533
1534 complete(&abt->u.abt.comp);
1535}
1536
Quinn Tran15f30a52017-03-15 09:48:52 -07001537int
Armen Baloyan4440e462014-02-26 04:15:18 -05001538qla24xx_async_abort_cmd(srb_t *cmd_sp)
1539{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001540 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001541 fc_port_t *fcport = cmd_sp->fcport;
1542 struct srb_iocb *abt_iocb;
1543 srb_t *sp;
1544 int rval = QLA_FUNCTION_FAILED;
1545
1546 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1547 if (!sp)
1548 goto done;
1549
1550 abt_iocb = &sp->u.iocb_cmd;
1551 sp->type = SRB_ABT_CMD;
1552 sp->name = "abort";
1553 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1554 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1555 sp->done = qla24xx_abort_sp_done;
1556 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1557 init_completion(&abt_iocb->u.abt.comp);
1558
1559 rval = qla2x00_start_sp(sp);
1560 if (rval != QLA_SUCCESS)
1561 goto done_free_sp;
1562
1563 ql_dbg(ql_dbg_async, vha, 0x507c,
1564 "Abort command issued - hdl=%x, target_id=%x\n",
1565 cmd_sp->handle, fcport->tgt_id);
1566
1567 wait_for_completion(&abt_iocb->u.abt.comp);
1568
1569 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1570 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1571
1572done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001573 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001574done:
1575 return rval;
1576}
1577
1578int
1579qla24xx_async_abort_command(srb_t *sp)
1580{
1581 unsigned long flags = 0;
1582
1583 uint32_t handle;
1584 fc_port_t *fcport = sp->fcport;
1585 struct scsi_qla_host *vha = fcport->vha;
1586 struct qla_hw_data *ha = vha->hw;
1587 struct req_que *req = vha->req;
1588
1589 spin_lock_irqsave(&ha->hardware_lock, flags);
1590 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1591 if (req->outstanding_cmds[handle] == sp)
1592 break;
1593 }
1594 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1595 if (handle == req->num_outstanding_cmds) {
1596 /* Command not found. */
1597 return QLA_FUNCTION_FAILED;
1598 }
1599 if (sp->type == SRB_FXIOCB_DCMD)
1600 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1601 FXDISC_ABORT_IOCTL);
1602
1603 return qla24xx_async_abort_cmd(sp);
1604}
1605
Quinn Tran726b8542017-01-19 22:28:00 -08001606static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001607qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1608{
1609 switch (ea->data[0]) {
1610 case MBS_COMMAND_COMPLETE:
1611 ql_dbg(ql_dbg_disc, vha, 0x2118,
1612 "%s %d %8phC post gpdb\n",
1613 __func__, __LINE__, ea->fcport->port_name);
1614
1615 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1616 ea->fcport->logout_on_delete = 1;
1617 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1618 break;
1619 default:
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001620 if (ea->fcport->n2n_flag) {
1621 ql_dbg(ql_dbg_disc, vha, 0x2118,
1622 "%s %d %8phC post fc4 prli\n",
1623 __func__, __LINE__, ea->fcport->port_name);
1624 ea->fcport->fc4f_nvme = 0;
1625 ea->fcport->n2n_flag = 0;
1626 qla24xx_post_prli_work(vha, ea->fcport);
1627 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001628 ql_dbg(ql_dbg_disc, vha, 0x2119,
1629 "%s %d %8phC unhandle event of %x\n",
1630 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1631 break;
1632 }
1633}
1634
1635static void
Quinn Tran726b8542017-01-19 22:28:00 -08001636qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001637{
Quinn Tran726b8542017-01-19 22:28:00 -08001638 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001639 u16 lid;
1640 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001641 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001642 struct fc_port *fcport = ea->fcport;
1643
Quinn Tranf352eeb2017-12-28 12:33:35 -08001644 ql_dbg(ql_dbg_disc, vha, 0xffff,
1645 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1646 __func__, fcport->port_name, fcport->disc_state,
1647 fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1648 ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1649 ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1650
Quinn Trana4239942017-12-28 12:33:26 -08001651 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1652 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1653 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1654 "%s %d %8phC Remote is trying to login\n",
1655 __func__, __LINE__, fcport->port_name);
1656 return;
1657 }
1658
1659 if (fcport->disc_state == DSC_DELETE_PEND)
1660 return;
1661
1662 if (ea->sp->gen2 != fcport->login_gen) {
1663 /* target side must have changed it. */
1664 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001665 "%s %8phC generation changed\n",
1666 __func__, fcport->port_name);
1667 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Trana4239942017-12-28 12:33:26 -08001668 return;
1669 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1670 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1671 __func__, __LINE__, fcport->port_name);
1672 qla24xx_post_gidpn_work(vha, fcport);
1673 return;
1674 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001675
Quinn Tran726b8542017-01-19 22:28:00 -08001676 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001677 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001678 /*
1679 * Driver must validate login state - If PRLI not complete,
1680 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1681 * requests.
1682 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001683 if (ea->fcport->fc4f_nvme) {
1684 ql_dbg(ql_dbg_disc, vha, 0x2117,
1685 "%s %d %8phC post prli\n",
1686 __func__, __LINE__, ea->fcport->port_name);
1687 qla24xx_post_prli_work(vha, ea->fcport);
1688 } else {
1689 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001690 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1691 __func__, __LINE__, ea->fcport->port_name,
1692 ea->fcport->loop_id, ea->fcport->d_id.b24);
1693
1694 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001695 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana084fd62017-12-04 14:45:00 -08001696 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001697 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1698 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001699 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001700 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1701 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1702
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001703 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1704 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001705 break;
1706 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001707 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001708 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1709
1710 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1711 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1712 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001713 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1714 else
Quinn Tran726b8542017-01-19 22:28:00 -08001715 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001716 break;
1717 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001718 /* data[1] = IO PARAM 1 = nport ID */
1719 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1720 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1721 cid.b.al_pa = ea->iop[1] & 0xff;
1722 cid.b.rsvd_1 = 0;
1723
Quinn Tran83548fe2017-06-02 09:12:01 -07001724 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1725 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1726 __func__, __LINE__, ea->fcport->port_name,
1727 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001728
1729 if (IS_SW_RESV_ADDR(cid)) {
1730 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1731 ea->fcport->loop_id = FC_NO_LOOP_ID;
1732 } else {
1733 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001734 }
Quinn Tran726b8542017-01-19 22:28:00 -08001735 qla24xx_post_gnl_work(vha, ea->fcport);
1736 break;
1737 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001738 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1739 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1740 __func__, __LINE__, ea->fcport->port_name,
1741 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1742 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001743
Quinn Trana084fd62017-12-04 14:45:00 -08001744 lid = ea->iop[1] & 0xffff;
1745 qlt_find_sess_invalidate_other(vha,
1746 wwn_to_u64(ea->fcport->port_name),
1747 ea->fcport->d_id, lid, &conflict_fcport);
1748
1749 if (conflict_fcport) {
1750 /*
1751 * Another fcport share the same loop_id/nport id.
1752 * Conflict fcport needs to finish cleanup before this
1753 * fcport can proceed to login.
1754 */
1755 conflict_fcport->conflict = ea->fcport;
1756 ea->fcport->login_pause = 1;
1757
1758 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1759 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1760 __func__, __LINE__, ea->fcport->port_name,
1761 ea->fcport->d_id.b24, lid);
1762 qla2x00_clear_loop_id(ea->fcport);
1763 qla24xx_post_gidpn_work(vha, ea->fcport);
1764 } else {
1765 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1766 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1767 __func__, __LINE__, ea->fcport->port_name,
1768 ea->fcport->d_id.b24, lid);
1769
1770 qla2x00_clear_loop_id(ea->fcport);
1771 set_bit(lid, vha->hw->loop_id_map);
1772 ea->fcport->loop_id = lid;
1773 ea->fcport->keep_nport_handle = 0;
1774 qlt_schedule_sess_for_deletion(ea->fcport, false);
1775 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001776 break;
1777 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001778 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001779}
1780
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001781void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001782qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1783 uint16_t *data)
1784{
Quinn Tran726b8542017-01-19 22:28:00 -08001785 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001786 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001787 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001788 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001789}
1790
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001791void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001792qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1793 uint16_t *data)
1794{
1795 if (data[0] == MBS_COMMAND_COMPLETE) {
1796 qla2x00_update_fcport(vha, fcport);
1797
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001798 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001799 }
1800
1801 /* Retry login. */
1802 fcport->flags &= ~FCF_ASYNC_SENT;
1803 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1804 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1805 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001806 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001807
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001808 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/****************************************************************************/
1812/* QLogic ISP2x00 Hardware Support Functions. */
1813/****************************************************************************/
1814
Saurav Kashyapfa492632012-11-21 02:40:29 -05001815static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001816qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1817{
1818 int rval = QLA_SUCCESS;
1819 struct qla_hw_data *ha = vha->hw;
1820 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001821 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001822
1823 qla83xx_idc_lock(vha, 0);
1824
1825 /* SV: TODO: Assign initialization timeout from
1826 * flash-info / other param
1827 */
1828 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1829 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1830
1831 /* Set our fcoe function presence */
1832 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1833 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1834 "Error while setting DRV-Presence.\n");
1835 rval = QLA_FUNCTION_FAILED;
1836 goto exit;
1837 }
1838
1839 /* Decide the reset ownership */
1840 qla83xx_reset_ownership(vha);
1841
1842 /*
1843 * On first protocol driver load:
1844 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1845 * register.
1846 * Others: Check compatibility with current IDC Major version.
1847 */
1848 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1849 if (ha->flags.nic_core_reset_owner) {
1850 /* Set IDC Major version */
1851 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1852 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1853
1854 /* Clearing IDC-Lock-Recovery register */
1855 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1856 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1857 /*
1858 * Clear further IDC participation if we are not compatible with
1859 * the current IDC Major Version.
1860 */
1861 ql_log(ql_log_warn, vha, 0xb07d,
1862 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1863 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1864 __qla83xx_clear_drv_presence(vha);
1865 rval = QLA_FUNCTION_FAILED;
1866 goto exit;
1867 }
1868 /* Each function sets its supported Minor version. */
1869 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1870 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1871 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1872
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001873 if (ha->flags.nic_core_reset_owner) {
1874 memset(config, 0, sizeof(config));
1875 if (!qla81xx_get_port_config(vha, config))
1876 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1877 QLA8XXX_DEV_READY);
1878 }
1879
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001880 rval = qla83xx_idc_state_handler(vha);
1881
1882exit:
1883 qla83xx_idc_unlock(vha, 0);
1884
1885 return rval;
1886}
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888/*
1889* qla2x00_initialize_adapter
1890* Initialize board.
1891*
1892* Input:
1893* ha = adapter block pointer.
1894*
1895* Returns:
1896* 0 = success
1897*/
1898int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001899qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001902 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001903 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001904
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001905 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1906 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001909 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001910 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001911 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001912 ha->flags.pci_channel_io_perm_failure = 0;
1913 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001914 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001915 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1916 atomic_set(&vha->loop_state, LOOP_DOWN);
1917 vha->device_flags = DFLG_NO_CABLE;
1918 vha->dpc_flags = 0;
1919 vha->flags.management_server_logged_in = 0;
1920 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 ha->isp_abort_cnt = 0;
1922 ha->beacon_blink_led = 0;
1923
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001924 set_bit(0, ha->req_qid_map);
1925 set_bit(0, ha->rsp_qid_map);
1926
Chad Dupuiscfb09192011-11-18 09:03:07 -08001927 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001928 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001929 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001931 ql_log(ql_log_warn, vha, 0x0044,
1932 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return (rval);
1934 }
1935
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001936 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001938 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001939 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001940 ql_log(ql_log_fatal, vha, 0x004f,
1941 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001942 return rval;
1943 }
1944
1945 if (IS_QLA8044(ha)) {
1946 qla8044_read_reset_template(vha);
1947
1948 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1949 * If DONRESET_BIT0 is set, drivers should not set dev_state
1950 * to NEED_RESET. But if NEED_RESET is set, drivers should
1951 * should honor the reset. */
1952 if (ql2xdontresethba == 1)
1953 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001954 }
1955
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001956 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001957 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001958 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001959
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001960 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001962 if (ha->flags.disable_serdes) {
1963 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001964 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001965 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001966 return QLA_FUNCTION_FAILED;
1967 }
1968
Chad Dupuiscfb09192011-11-18 09:03:07 -08001969 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001970 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001972 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1973 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001974 if (rval)
1975 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001976 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001977 if (rval)
1978 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001980
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001981 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001982 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001983 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001984 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001985 "Unable to configure ISP84XX.\n");
1986 return QLA_FUNCTION_FAILED;
1987 }
1988 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001989
Quinn Tranead03852017-01-19 22:28:01 -08001990 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001991 rval = qla2x00_init_rings(vha);
1992
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001993 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001995 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001996 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001997 rval = qla84xx_init_chip(vha);
1998 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001999 ql_log(ql_log_warn, vha, 0x00d4,
2000 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07002001 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002002 }
2003 }
2004
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002005 /* Load the NIC Core f/w if we are the first protocol driver. */
2006 if (IS_QLA8031(ha)) {
2007 rval = qla83xx_nic_core_fw_load(vha);
2008 if (rval)
2009 ql_log(ql_log_warn, vha, 0x0124,
2010 "Error in initializing NIC Core f/w.\n");
2011 }
2012
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002013 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2014 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002015
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002016 if (IS_P3P_TYPE(ha))
2017 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2018 else
2019 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return (rval);
2022}
2023
2024/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002025 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 * @ha: HA context
2027 *
2028 * Returns 0 on success.
2029 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002030int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002031qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002033 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002034 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002035 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002036 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002039 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002042 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2044
Andrew Vasquez737faec2008-10-24 15:13:45 -07002045 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002047 /* Get PCI bus information. */
2048 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002049 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002050 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2051
2052 return QLA_SUCCESS;
2053}
2054
2055/**
2056 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2057 * @ha: HA context
2058 *
2059 * Returns 0 on success.
2060 */
2061int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002062qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002063{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002064 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002065 unsigned long flags = 0;
2066 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002067 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002068 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002069
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002070 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002071 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002072
2073 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002074 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002075
2076 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2077 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002078 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002079
2080 /*
2081 * If this is a 2300 card and not 2312, reset the
2082 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2083 * the 2310 also reports itself as a 2300 so we need to get the
2084 * fb revision level -- a 6 indicates it really is a 2300 and
2085 * not a 2310.
2086 */
2087 if (IS_QLA2300(ha)) {
2088 spin_lock_irqsave(&ha->hardware_lock, flags);
2089
2090 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002091 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002092 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002093 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002094 break;
2095
2096 udelay(10);
2097 }
2098
2099 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002100 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2101 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002102
2103 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002104 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002105
2106 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002107 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002108
2109 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002110 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2111 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002112
2113 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002114 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002115 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002116 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002117 break;
2118
2119 udelay(10);
2120 }
2121
2122 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2123 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002124
2125 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2126
Andrew Vasquez737faec2008-10-24 15:13:45 -07002127 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002128
2129 /* Get PCI bus information. */
2130 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002131 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002132 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2133
2134 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002138 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2139 * @ha: HA context
2140 *
2141 * Returns 0 on success.
2142 */
2143int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002144qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002145{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002146 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002147 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002148 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002149 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002150
2151 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002152 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002153
2154 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002155 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002156 w &= ~PCI_COMMAND_INTX_DISABLE;
2157 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2158
2159 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2160
2161 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002162 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2163 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002164
2165 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002166 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002167 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002168
Andrew Vasquez737faec2008-10-24 15:13:45 -07002169 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002170
Auke Kok44c10132007-06-08 15:46:36 -07002171 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002172
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002173 /* Get PCI bus information. */
2174 spin_lock_irqsave(&ha->hardware_lock, flags);
2175 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2176 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2177
2178 return QLA_SUCCESS;
2179}
2180
2181/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002182 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2183 * @ha: HA context
2184 *
2185 * Returns 0 on success.
2186 */
2187int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002188qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002189{
2190 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002191 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002192
2193 pci_set_master(ha->pdev);
2194 pci_try_set_mwi(ha->pdev);
2195
2196 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2197 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2198 w &= ~PCI_COMMAND_INTX_DISABLE;
2199 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2200
2201 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002202 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002203 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002204
Andrew Vasquez737faec2008-10-24 15:13:45 -07002205 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002206
2207 ha->chip_revision = ha->pdev->revision;
2208
2209 return QLA_SUCCESS;
2210}
2211
2212/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 * qla2x00_isp_firmware() - Choose firmware image.
2214 * @ha: HA context
2215 *
2216 * Returns 0 on success.
2217 */
2218static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002219qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002222 uint16_t loop_id, topo, sw_cap;
2223 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002224 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002227 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002230 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002233 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002234 if (rval == QLA_SUCCESS) {
2235 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002236 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002237 &area, &domain, &topo, &sw_cap);
2238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
2240
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002241 if (rval)
2242 ql_dbg(ql_dbg_init, vha, 0x007a,
2243 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 return (rval);
2246}
2247
2248/**
2249 * qla2x00_reset_chip() - Reset ISP chip.
2250 * @ha: HA context
2251 *
2252 * Returns 0 on success.
2253 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002254void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002255qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002258 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002259 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 uint16_t cmd;
2262
Andrew Vasquez85880802009-12-15 21:29:46 -08002263 if (unlikely(pci_channel_offline(ha->pdev)))
2264 return;
2265
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002266 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 spin_lock_irqsave(&ha->hardware_lock, flags);
2269
2270 /* Turn off master enable */
2271 cmd = 0;
2272 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2273 cmd &= ~PCI_COMMAND_MASTER;
2274 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2275
2276 if (!IS_QLA2100(ha)) {
2277 /* Pause RISC. */
2278 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2279 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2280 for (cnt = 0; cnt < 30000; cnt++) {
2281 if ((RD_REG_WORD(&reg->hccr) &
2282 HCCR_RISC_PAUSE) != 0)
2283 break;
2284 udelay(100);
2285 }
2286 } else {
2287 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2288 udelay(10);
2289 }
2290
2291 /* Select FPM registers. */
2292 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2293 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2294
2295 /* FPM Soft Reset. */
2296 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2297 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2298
2299 /* Toggle Fpm Reset. */
2300 if (!IS_QLA2200(ha)) {
2301 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2302 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2303 }
2304
2305 /* Select frame buffer registers. */
2306 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2307 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2308
2309 /* Reset frame buffer FIFOs. */
2310 if (IS_QLA2200(ha)) {
2311 WRT_FB_CMD_REG(ha, reg, 0xa000);
2312 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2313 } else {
2314 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2315
2316 /* Read back fb_cmd until zero or 3 seconds max */
2317 for (cnt = 0; cnt < 3000; cnt++) {
2318 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2319 break;
2320 udelay(100);
2321 }
2322 }
2323
2324 /* Select RISC module registers. */
2325 WRT_REG_WORD(&reg->ctrl_status, 0);
2326 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2327
2328 /* Reset RISC processor. */
2329 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2330 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2331
2332 /* Release RISC processor. */
2333 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2334 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2335 }
2336
2337 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2338 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2339
2340 /* Reset ISP chip. */
2341 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2342
2343 /* Wait for RISC to recover from reset. */
2344 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2345 /*
2346 * It is necessary to for a delay here since the card doesn't
2347 * respond to PCI reads during a reset. On some architectures
2348 * this will result in an MCA.
2349 */
2350 udelay(20);
2351 for (cnt = 30000; cnt; cnt--) {
2352 if ((RD_REG_WORD(&reg->ctrl_status) &
2353 CSR_ISP_SOFT_RESET) == 0)
2354 break;
2355 udelay(100);
2356 }
2357 } else
2358 udelay(10);
2359
2360 /* Reset RISC processor. */
2361 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2362
2363 WRT_REG_WORD(&reg->semaphore, 0);
2364
2365 /* Release RISC processor. */
2366 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2367 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2368
2369 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2370 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002371 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 udelay(100);
2375 }
2376 } else
2377 udelay(100);
2378
2379 /* Turn on master enable */
2380 cmd |= PCI_COMMAND_MASTER;
2381 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2382
2383 /* Disable RISC pause on FPM parity error. */
2384 if (!IS_QLA2100(ha)) {
2385 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2386 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2387 }
2388
2389 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2390}
2391
2392/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002393 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2394 *
2395 * Returns 0 on success.
2396 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002397static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002398qla81xx_reset_mpi(scsi_qla_host_t *vha)
2399{
2400 uint16_t mb[4] = {0x1010, 0, 1, 0};
2401
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002402 if (!IS_QLA81XX(vha->hw))
2403 return QLA_SUCCESS;
2404
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002405 return qla81xx_write_mpi_register(vha, mb);
2406}
2407
2408/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002409 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002410 * @ha: HA context
2411 *
2412 * Returns 0 on success.
2413 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002414static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002415qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002416{
2417 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002418 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002419 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002420 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002421 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002422 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002423 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002424
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002425 spin_lock_irqsave(&ha->hardware_lock, flags);
2426
2427 /* Reset RISC. */
2428 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2429 for (cnt = 0; cnt < 30000; cnt++) {
2430 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2431 break;
2432
2433 udelay(10);
2434 }
2435
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002436 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2437 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2438
2439 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2440 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2441 RD_REG_DWORD(&reg->hccr),
2442 RD_REG_DWORD(&reg->ctrl_status),
2443 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2444
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002445 WRT_REG_DWORD(&reg->ctrl_status,
2446 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002447 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002448
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002449 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002450
Andrew Vasquez88c26662005-07-08 17:59:26 -07002451 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002452 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002453 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2454 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002455 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002456 if (cnt)
2457 udelay(5);
2458 else
2459 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002460 }
2461
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002462 if (rval == QLA_SUCCESS)
2463 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2464
2465 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2466 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2467 RD_REG_DWORD(&reg->hccr),
2468 RD_REG_DWORD(&reg->mailbox0));
2469
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002470 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002471 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002472 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002473 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002474 if ((RD_REG_DWORD(&reg->ctrl_status) &
2475 CSRX_ISP_SOFT_RESET) == 0)
2476 break;
2477
2478 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002479 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002480 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2481 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2482
2483 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2484 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2485 RD_REG_DWORD(&reg->hccr),
2486 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002487
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002488 /* If required, do an MPI FW reset now */
2489 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2490 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2491 if (++abts_cnt < 5) {
2492 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2493 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2494 } else {
2495 /*
2496 * We exhausted the ISP abort retries. We have to
2497 * set the board offline.
2498 */
2499 abts_cnt = 0;
2500 vha->flags.online = 0;
2501 }
2502 }
2503 }
2504
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002505 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2506 RD_REG_DWORD(&reg->hccr);
2507
2508 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2509 RD_REG_DWORD(&reg->hccr);
2510
2511 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2512 RD_REG_DWORD(&reg->hccr);
2513
Bart Van Assche52c82822015-07-09 07:23:26 -07002514 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002515 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002516 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002517 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002518 if (cnt)
2519 udelay(5);
2520 else
2521 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002522 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002523 if (rval == QLA_SUCCESS)
2524 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2525
2526 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2527 "Host Risc 0x%x, mailbox0 0x%x\n",
2528 RD_REG_DWORD(&reg->hccr),
2529 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002530
2531 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002532
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002533 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2534 "Driver in %s mode\n",
2535 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2536
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002537 if (IS_NOPOLLING_TYPE(ha))
2538 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002539
2540 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002541}
2542
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002543static void
2544qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2545{
2546 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2547
2548 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2549 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2550
2551}
2552
2553static void
2554qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2555{
2556 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2557
2558 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2559 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2560}
2561
2562static void
2563qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2564{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002565 uint32_t wd32 = 0;
2566 uint delta_msec = 100;
2567 uint elapsed_msec = 0;
2568 uint timeout_msec;
2569 ulong n;
2570
Joe Carnucciocc790762015-08-04 13:37:53 -04002571 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2572 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002573 return;
2574
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002575 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2576 udelay(100);
2577
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002578attempt:
2579 timeout_msec = TIMEOUT_SEMAPHORE;
2580 n = timeout_msec / delta_msec;
2581 while (n--) {
2582 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2583 qla25xx_read_risc_sema_reg(vha, &wd32);
2584 if (wd32 & RISC_SEMAPHORE)
2585 break;
2586 msleep(delta_msec);
2587 elapsed_msec += delta_msec;
2588 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2589 goto force;
2590 }
2591
2592 if (!(wd32 & RISC_SEMAPHORE))
2593 goto force;
2594
2595 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2596 goto acquired;
2597
2598 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2599 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2600 n = timeout_msec / delta_msec;
2601 while (n--) {
2602 qla25xx_read_risc_sema_reg(vha, &wd32);
2603 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2604 break;
2605 msleep(delta_msec);
2606 elapsed_msec += delta_msec;
2607 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2608 goto force;
2609 }
2610
2611 if (wd32 & RISC_SEMAPHORE_FORCE)
2612 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2613
2614 goto attempt;
2615
2616force:
2617 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2618
2619acquired:
2620 return;
2621}
2622
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002623/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002624 * qla24xx_reset_chip() - Reset ISP24xx chip.
2625 * @ha: HA context
2626 *
2627 * Returns 0 on success.
2628 */
2629void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002630qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002631{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002632 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002633
2634 if (pci_channel_offline(ha->pdev) &&
2635 ha->flags.pci_channel_io_perm_failure) {
2636 return;
2637 }
2638
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002639 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002640
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002641 qla25xx_manipulate_risc_semaphore(vha);
2642
Andrew Vasquez88c26662005-07-08 17:59:26 -07002643 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002644 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002645}
2646
2647/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 * qla2x00_chip_diag() - Test chip for proper operation.
2649 * @ha: HA context
2650 *
2651 * Returns 0 on success.
2652 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002653int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002654qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002657 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002658 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 unsigned long flags = 0;
2660 uint16_t data;
2661 uint32_t cnt;
2662 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002663 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 /* Assume a failed state */
2666 rval = QLA_FUNCTION_FAILED;
2667
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002668 ql_dbg(ql_dbg_init, vha, 0x007b,
2669 "Testing device at %lx.\n", (u_long)&reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 spin_lock_irqsave(&ha->hardware_lock, flags);
2672
2673 /* Reset ISP chip. */
2674 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2675
2676 /*
2677 * We need to have a delay here since the card will not respond while
2678 * in reset causing an MCA on some architectures.
2679 */
2680 udelay(20);
2681 data = qla2x00_debounce_register(&reg->ctrl_status);
2682 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2683 udelay(5);
2684 data = RD_REG_WORD(&reg->ctrl_status);
2685 barrier();
2686 }
2687
2688 if (!cnt)
2689 goto chip_diag_failed;
2690
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002691 ql_dbg(ql_dbg_init, vha, 0x007c,
2692 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 /* Reset RISC processor. */
2695 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2696 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2697
2698 /* Workaround for QLA2312 PCI parity error */
2699 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2700 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2701 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2702 udelay(5);
2703 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002704 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
2706 } else
2707 udelay(10);
2708
2709 if (!cnt)
2710 goto chip_diag_failed;
2711
2712 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002713 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2716 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2717 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2718 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2719 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2720 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002721 ql_log(ql_log_warn, vha, 0x0062,
2722 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2723 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 goto chip_diag_failed;
2726 }
2727 ha->product_id[0] = mb[1];
2728 ha->product_id[1] = mb[2];
2729 ha->product_id[2] = mb[3];
2730 ha->product_id[3] = mb[4];
2731
2732 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002733 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2735 else
2736 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002737 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 if (IS_QLA2200(ha) &&
2740 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2741 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002742 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002744 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 ha->fw_transfer_size = 128;
2746 }
2747
2748 /* Wrap Incoming Mailboxes Test. */
2749 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2750
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002751 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002752 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002753 if (rval)
2754 ql_log(ql_log_warn, vha, 0x0080,
2755 "Failed mailbox send register test.\n");
2756 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /* Flag a successful rval */
2758 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 spin_lock_irqsave(&ha->hardware_lock, flags);
2760
2761chip_diag_failed:
2762 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002763 ql_log(ql_log_info, vha, 0x0081,
2764 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2767
2768 return (rval);
2769}
2770
2771/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002772 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2773 * @ha: HA context
2774 *
2775 * Returns 0 on success.
2776 */
2777int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002778qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002779{
2780 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002781 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002782 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002783
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002784 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002785 return QLA_SUCCESS;
2786
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002787 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002788
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002789 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002790 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002791 ql_log(ql_log_warn, vha, 0x0082,
2792 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002793 } else {
2794 /* Flag a successful rval */
2795 rval = QLA_SUCCESS;
2796 }
2797
2798 return rval;
2799}
2800
Quinn Tranad0a0b02017-12-28 12:33:14 -08002801static void
2802qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002803{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002804 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002805 dma_addr_t tc_dma;
2806 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002807 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002808
Quinn Tranad0a0b02017-12-28 12:33:14 -08002809 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002810 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002811 "%s: Offload Mem is already allocated.\n",
2812 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002813 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002814 }
2815
Quinn Tranad0a0b02017-12-28 12:33:14 -08002816 if (IS_FWI2_CAPABLE(ha)) {
2817 /* Allocate memory for Fibre Channel Event Buffer. */
2818 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2819 !IS_QLA27XX(ha))
2820 goto try_eft;
2821
2822 if (ha->fce)
2823 dma_free_coherent(&ha->pdev->dev,
2824 FCE_SIZE, ha->fce, ha->fce_dma);
2825
2826 /* Allocate memory for Fibre Channel Event Buffer. */
2827 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2828 GFP_KERNEL);
2829 if (!tc) {
2830 ql_log(ql_log_warn, vha, 0x00be,
2831 "Unable to allocate (%d KB) for FCE.\n",
2832 FCE_SIZE / 1024);
2833 goto try_eft;
2834 }
2835
2836 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2837 ha->fce_mb, &ha->fce_bufs);
2838 if (rval) {
2839 ql_log(ql_log_warn, vha, 0x00bf,
2840 "Unable to initialize FCE (%d).\n", rval);
2841 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2842 tc_dma);
2843 ha->flags.fce_enabled = 0;
2844 goto try_eft;
2845 }
2846 ql_dbg(ql_dbg_init, vha, 0x00c0,
2847 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2848
2849 ha->flags.fce_enabled = 1;
2850 ha->fce_dma = tc_dma;
2851 ha->fce = tc;
2852
2853try_eft:
2854 if (ha->eft)
2855 dma_free_coherent(&ha->pdev->dev,
2856 EFT_SIZE, ha->eft, ha->eft_dma);
2857
2858 /* Allocate memory for Extended Trace Buffer. */
2859 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2860 GFP_KERNEL);
2861 if (!tc) {
2862 ql_log(ql_log_warn, vha, 0x00c1,
2863 "Unable to allocate (%d KB) for EFT.\n",
2864 EFT_SIZE / 1024);
2865 goto eft_err;
2866 }
2867
2868 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2869 if (rval) {
2870 ql_log(ql_log_warn, vha, 0x00c2,
2871 "Unable to initialize EFT (%d).\n", rval);
2872 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2873 tc_dma);
2874 goto eft_err;
2875 }
2876 ql_dbg(ql_dbg_init, vha, 0x00c3,
2877 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2878
2879 ha->eft_dma = tc_dma;
2880 ha->eft = tc;
2881 }
2882
2883eft_err:
2884 return;
2885}
2886
2887void
2888qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2889{
2890 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2891 eft_size, fce_size, mq_size;
2892 struct qla_hw_data *ha = vha->hw;
2893 struct req_que *req = ha->req_q_map[0];
2894 struct rsp_que *rsp = ha->rsp_q_map[0];
2895 struct qla2xxx_fw_dump *fw_dump;
2896
Chad Dupuisf73cb692014-02-26 04:15:06 -05002897 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2898 req_q_size = rsp_q_size = 0;
2899
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002900 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2901 fixed_size = sizeof(struct qla2100_fw_dump);
2902 } else if (IS_QLA23XX(ha)) {
2903 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2904 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2905 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002906 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002907 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002908 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2909 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002910 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2911 else if (IS_QLA25XX(ha))
2912 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2913 else
2914 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002915
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002916 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2917 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002918 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002919 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002920 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002921 /*
2922 * Allocate maximum buffer size for all queues.
2923 * Resizing must be done at end-of-dump processing.
2924 */
2925 mq_size += ha->max_req_queues *
2926 (req->length * sizeof(request_t));
2927 mq_size += ha->max_rsp_queues *
2928 (rsp->length * sizeof(response_t));
2929 }
Arun Easi00876ae2013-03-25 02:21:37 -04002930 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002931 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002932 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002933 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2934 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002935 goto try_eft;
2936
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002937 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002938try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002939 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002940 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002941 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002942 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002943
Chad Dupuisf73cb692014-02-26 04:15:06 -05002944 if (IS_QLA27XX(ha)) {
2945 if (!ha->fw_dump_template) {
2946 ql_log(ql_log_warn, vha, 0x00ba,
2947 "Failed missing fwdump template\n");
2948 return;
2949 }
2950 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2951 ql_dbg(ql_dbg_init, vha, 0x00fa,
2952 "-> allocating fwdump (%x bytes)...\n", dump_size);
2953 goto allocate;
2954 }
2955
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002956 req_q_size = req->length * sizeof(request_t);
2957 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002958 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002959 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002960 ha->chain_offset = dump_size;
2961 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002962
Quinn Tranb945e772017-06-13 20:47:29 -07002963 if (ha->exchoffld_buf)
2964 dump_size += sizeof(struct qla2xxx_offld_chain) +
2965 ha->exchoffld_size;
2966 if (ha->exlogin_buf)
2967 dump_size += sizeof(struct qla2xxx_offld_chain) +
2968 ha->exlogin_size;
2969
Chad Dupuisf73cb692014-02-26 04:15:06 -05002970allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08002971 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
2972 fw_dump = vmalloc(dump_size);
2973 if (!fw_dump) {
2974 ql_log(ql_log_warn, vha, 0x00c4,
2975 "Unable to allocate (%d KB) for firmware dump.\n",
2976 dump_size / 1024);
2977 } else {
2978 if (ha->fw_dump)
2979 vfree(ha->fw_dump);
2980 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002981
Quinn Tranad0a0b02017-12-28 12:33:14 -08002982 ha->fw_dump_len = dump_size;
2983 ql_dbg(ql_dbg_init, vha, 0x00c5,
2984 "Allocated (%d KB) for firmware dump.\n",
2985 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002986
Quinn Tranad0a0b02017-12-28 12:33:14 -08002987 if (IS_QLA27XX(ha))
2988 return;
2989
2990 ha->fw_dump->signature[0] = 'Q';
2991 ha->fw_dump->signature[1] = 'L';
2992 ha->fw_dump->signature[2] = 'G';
2993 ha->fw_dump->signature[3] = 'C';
2994 ha->fw_dump->version = htonl(1);
2995
2996 ha->fw_dump->fixed_size = htonl(fixed_size);
2997 ha->fw_dump->mem_size = htonl(mem_size);
2998 ha->fw_dump->req_q_size = htonl(req_q_size);
2999 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3000
3001 ha->fw_dump->eft_size = htonl(eft_size);
3002 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3003 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3004
3005 ha->fw_dump->header_size =
3006 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003007 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003008 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003009}
3010
Andrew Vasquez18e75552009-06-03 09:55:30 -07003011static int
3012qla81xx_mpi_sync(scsi_qla_host_t *vha)
3013{
3014#define MPS_MASK 0xe0
3015 int rval;
3016 uint16_t dc;
3017 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003018
3019 if (!IS_QLA81XX(vha->hw))
3020 return QLA_SUCCESS;
3021
3022 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3023 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003024 ql_log(ql_log_warn, vha, 0x0105,
3025 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003026 goto done;
3027 }
3028
3029 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3030 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3031 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003032 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003033 goto done_release;
3034 }
3035
3036 dc &= MPS_MASK;
3037 if (dc == (dw & MPS_MASK))
3038 goto done_release;
3039
3040 dw &= ~MPS_MASK;
3041 dw |= dc;
3042 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3043 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003044 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003045 }
3046
3047done_release:
3048 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3049 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003050 ql_log(ql_log_warn, vha, 0x006d,
3051 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003052 }
3053
3054done:
3055 return rval;
3056}
3057
Chad Dupuis8d93f552013-01-30 03:34:37 -05003058int
3059qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3060{
3061 /* Don't try to reallocate the array */
3062 if (req->outstanding_cmds)
3063 return QLA_SUCCESS;
3064
Michael Hernandezd7459522016-12-12 14:40:07 -08003065 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003066 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3067 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003068 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3069 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003070 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003071 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003072 }
3073
3074 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3075 req->num_outstanding_cmds, GFP_KERNEL);
3076
3077 if (!req->outstanding_cmds) {
3078 /*
3079 * Try to allocate a minimal size just so we can get through
3080 * initialization.
3081 */
3082 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3083 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3084 req->num_outstanding_cmds, GFP_KERNEL);
3085
3086 if (!req->outstanding_cmds) {
3087 ql_log(ql_log_fatal, NULL, 0x0126,
3088 "Failed to allocate memory for "
3089 "outstanding_cmds for req_que %p.\n", req);
3090 req->num_outstanding_cmds = 0;
3091 return QLA_FUNCTION_FAILED;
3092 }
3093 }
3094
3095 return QLA_SUCCESS;
3096}
3097
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003098#define PRINT_FIELD(_field, _flag, _str) { \
3099 if (a0->_field & _flag) {\
3100 if (p) {\
3101 strcat(ptr, "|");\
3102 ptr++;\
3103 leftover--;\
3104 } \
3105 len = snprintf(ptr, leftover, "%s", _str); \
3106 p = 1;\
3107 leftover -= len;\
3108 ptr += len; \
3109 } \
3110}
3111
3112static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3113{
3114#define STR_LEN 64
3115 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3116 u8 str[STR_LEN], *ptr, p;
3117 int leftover, len;
3118
3119 memset(str, 0, STR_LEN);
3120 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3121 ql_dbg(ql_dbg_init, vha, 0x015a,
3122 "SFP MFG Name: %s\n", str);
3123
3124 memset(str, 0, STR_LEN);
3125 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3126 ql_dbg(ql_dbg_init, vha, 0x015c,
3127 "SFP Part Name: %s\n", str);
3128
3129 /* media */
3130 memset(str, 0, STR_LEN);
3131 ptr = str;
3132 leftover = STR_LEN;
3133 p = len = 0;
3134 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3135 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3136 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3137 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3138 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3139 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3140 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3141 ql_dbg(ql_dbg_init, vha, 0x0160,
3142 "SFP Media: %s\n", str);
3143
3144 /* link length */
3145 memset(str, 0, STR_LEN);
3146 ptr = str;
3147 leftover = STR_LEN;
3148 p = len = 0;
3149 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3150 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3151 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3152 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3153 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3154 ql_dbg(ql_dbg_init, vha, 0x0196,
3155 "SFP Link Length: %s\n", str);
3156
3157 memset(str, 0, STR_LEN);
3158 ptr = str;
3159 leftover = STR_LEN;
3160 p = len = 0;
3161 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3162 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3163 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3164 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3165 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3166 ql_dbg(ql_dbg_init, vha, 0x016e,
3167 "SFP FC Link Tech: %s\n", str);
3168
3169 if (a0->length_km)
3170 ql_dbg(ql_dbg_init, vha, 0x016f,
3171 "SFP Distant: %d km\n", a0->length_km);
3172 if (a0->length_100m)
3173 ql_dbg(ql_dbg_init, vha, 0x0170,
3174 "SFP Distant: %d m\n", a0->length_100m*100);
3175 if (a0->length_50um_10m)
3176 ql_dbg(ql_dbg_init, vha, 0x0189,
3177 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3178 if (a0->length_62um_10m)
3179 ql_dbg(ql_dbg_init, vha, 0x018a,
3180 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3181 if (a0->length_om4_10m)
3182 ql_dbg(ql_dbg_init, vha, 0x0194,
3183 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3184 if (a0->length_om3_10m)
3185 ql_dbg(ql_dbg_init, vha, 0x0195,
3186 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3187}
3188
3189
3190/*
3191 * Return Code:
3192 * QLA_SUCCESS: no action
3193 * QLA_INTERFACE_ERROR: SFP is not there.
3194 * QLA_FUNCTION_FAILED: detected New SFP
3195 */
3196int
3197qla24xx_detect_sfp(scsi_qla_host_t *vha)
3198{
3199 int rc = QLA_SUCCESS;
3200 struct sff_8247_a0 *a;
3201 struct qla_hw_data *ha = vha->hw;
3202
3203 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3204 goto out;
3205
3206 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3207 if (rc)
3208 goto out;
3209
3210 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3211 qla2xxx_print_sfp_info(vha);
3212
3213 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3214 /* long range */
3215 ha->flags.detected_lr_sfp = 1;
3216
3217 if (a->length_km > 5 || a->length_100m > 50)
3218 ha->long_range_distance = LR_DISTANCE_10K;
3219 else
3220 ha->long_range_distance = LR_DISTANCE_5K;
3221
3222 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3223 ql_dbg(ql_dbg_async, vha, 0x507b,
3224 "Detected Long Range SFP.\n");
3225 } else {
3226 /* short range */
3227 ha->flags.detected_lr_sfp = 0;
3228 if (ha->flags.using_lr_setting)
3229 ql_dbg(ql_dbg_async, vha, 0x5084,
3230 "Detected Short Range SFP.\n");
3231 }
3232
3233 if (!vha->flags.init_done)
3234 rc = QLA_SUCCESS;
3235out:
3236 return rc;
3237}
3238
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003239/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 * qla2x00_setup_chip() - Load and start RISC firmware.
3241 * @ha: HA context
3242 *
3243 * Returns 0 on success.
3244 */
3245static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003246qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003248 int rval;
3249 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003250 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003251 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3252 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003253 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003254
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003255 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003256 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003257 if (rval == QLA_SUCCESS) {
3258 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003259 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003260 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003261 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003262 }
3263
Andrew Vasquez3db06522008-01-31 12:33:49 -08003264 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3265 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3266 spin_lock_irqsave(&ha->hardware_lock, flags);
3267 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3268 RD_REG_WORD(&reg->hccr);
3269 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Andrew Vasquez18e75552009-06-03 09:55:30 -07003272 qla81xx_mpi_sync(vha);
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003275 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003276 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003277 ql_dbg(ql_dbg_init, vha, 0x00c9,
3278 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003280 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (rval == QLA_SUCCESS) {
3282 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003283 ql_dbg(ql_dbg_init, vha, 0x00ca,
3284 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003286 if (ql2xexlogins)
3287 ha->flags.exlogins_enabled = 1;
3288
Quinn Tran99e1b682017-06-02 09:12:03 -07003289 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003290 ha->flags.exchoffld_enabled = 1;
3291
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003292 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003294 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003295 qla24xx_detect_sfp(vha);
3296
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003297 rval = qla2x00_set_exlogins_buffer(vha);
3298 if (rval != QLA_SUCCESS)
3299 goto failed;
3300
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003301 rval = qla2x00_set_exchoffld_buffer(vha);
3302 if (rval != QLA_SUCCESS)
3303 goto failed;
3304
Giridhar Malavalia9083012010-04-12 17:59:55 -07003305enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003306 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003307 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003308 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003309 else
3310 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003311 if (rval != QLA_SUCCESS)
3312 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003313 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003314 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003315 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003316 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003317 if ((!ha->max_npiv_vports) ||
3318 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003319 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003320 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003321 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003322 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003323 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003324
Chad Dupuis8d93f552013-01-30 03:34:37 -05003325 /*
3326 * Allocate the array of outstanding commands
3327 * now that we know the firmware resources.
3328 */
3329 rval = qla2x00_alloc_outstanding_cmds(ha,
3330 vha->req);
3331 if (rval != QLA_SUCCESS)
3332 goto failed;
3333
Quinn Tranad0a0b02017-12-28 12:33:14 -08003334 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3335 qla2x00_alloc_offload_mem(vha);
3336
3337 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003338 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003339
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003340 } else {
3341 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003344 ql_log(ql_log_fatal, vha, 0x00cd,
3345 "ISP Firmware failed checksum.\n");
3346 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003348 } else
3349 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350
Andrew Vasquez3db06522008-01-31 12:33:49 -08003351 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3352 /* Enable proper parity. */
3353 spin_lock_irqsave(&ha->hardware_lock, flags);
3354 if (IS_QLA2300(ha))
3355 /* SRAM parity */
3356 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3357 else
3358 /* SRAM, Instruction RAM and GP RAM parity */
3359 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3360 RD_REG_WORD(&reg->hccr);
3361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3362 }
3363
Chad Dupuisf3982d82014-09-25 05:16:57 -04003364 if (IS_QLA27XX(ha))
3365 ha->flags.fac_supported = 1;
3366 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003367 uint32_t size;
3368
3369 rval = qla81xx_fac_get_sector_size(vha, &size);
3370 if (rval == QLA_SUCCESS) {
3371 ha->flags.fac_supported = 1;
3372 ha->fdt_block_size = size << 2;
3373 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003374 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003375 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3376 ha->fw_major_version, ha->fw_minor_version,
3377 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003378
Chad Dupuisf73cb692014-02-26 04:15:06 -05003379 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003380 ha->flags.fac_supported = 0;
3381 rval = QLA_SUCCESS;
3382 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003383 }
3384 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003385failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003387 ql_log(ql_log_fatal, vha, 0x00cf,
3388 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390
3391 return (rval);
3392}
3393
3394/**
3395 * qla2x00_init_response_q_entries() - Initializes response queue entries.
3396 * @ha: HA context
3397 *
3398 * Beginning of request ring has initialization control block already built
3399 * by nvram config routine.
3400 *
3401 * Returns 0 on success.
3402 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003403void
3404qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
3406 uint16_t cnt;
3407 response_t *pkt;
3408
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003409 rsp->ring_ptr = rsp->ring;
3410 rsp->ring_index = 0;
3411 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003412 pkt = rsp->ring_ptr;
3413 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 pkt->signature = RESPONSE_PROCESSED;
3415 pkt++;
3416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417}
3418
3419/**
3420 * qla2x00_update_fw_options() - Read and process firmware options.
3421 * @ha: HA context
3422 *
3423 * Returns 0 on success.
3424 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003425void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003426qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
3428 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003429 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003432 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
3434 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3435 return;
3436
3437 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003438 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3439 "Serial link options.\n");
3440 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3441 (uint8_t *)&ha->fw_seriallink_options,
3442 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
3444 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3445 if (ha->fw_seriallink_options[3] & BIT_2) {
3446 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3447
3448 /* 1G settings */
3449 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3450 emphasis = (ha->fw_seriallink_options[2] &
3451 (BIT_4 | BIT_3)) >> 3;
3452 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003453 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 rx_sens = (ha->fw_seriallink_options[0] &
3455 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3456 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3457 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3458 if (rx_sens == 0x0)
3459 rx_sens = 0x3;
3460 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3461 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3462 ha->fw_options[10] |= BIT_5 |
3463 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3464 (tx_sens & (BIT_1 | BIT_0));
3465
3466 /* 2G settings */
3467 swing = (ha->fw_seriallink_options[2] &
3468 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3469 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3470 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003471 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 rx_sens = (ha->fw_seriallink_options[1] &
3473 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3474 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3475 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3476 if (rx_sens == 0x0)
3477 rx_sens = 0x3;
3478 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3479 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3480 ha->fw_options[11] |= BIT_5 |
3481 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3482 (tx_sens & (BIT_1 | BIT_0));
3483 }
3484
3485 /* FCP2 options. */
3486 /* Return command IOCBs without waiting for an ABTS to complete. */
3487 ha->fw_options[3] |= BIT_13;
3488
3489 /* LED scheme. */
3490 if (ha->flags.enable_led_scheme)
3491 ha->fw_options[2] |= BIT_12;
3492
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003493 /* Detect ISP6312. */
3494 if (IS_QLA6312(ha))
3495 ha->fw_options[2] |= BIT_13;
3496
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003497 /* Set Retry FLOGI in case of P2P connection */
3498 if (ha->operating_mode == P2P) {
3499 ha->fw_options[2] |= BIT_3;
3500 ql_dbg(ql_dbg_disc, vha, 0x2100,
3501 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3502 __func__, ha->fw_options[2]);
3503 }
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003506 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003509void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003510qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003511{
3512 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003513 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003514
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003515 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003516 return;
3517
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003518 /* Hold status IOCBs until ABTS response received. */
3519 if (ql2xfwholdabts)
3520 ha->fw_options[3] |= BIT_12;
3521
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003522 /* Set Retry FLOGI in case of P2P connection */
3523 if (ha->operating_mode == P2P) {
3524 ha->fw_options[2] |= BIT_3;
3525 ql_dbg(ql_dbg_disc, vha, 0x2101,
3526 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3527 __func__, ha->fw_options[2]);
3528 }
3529
Quinn Tran41dc5292017-01-19 22:28:03 -08003530 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003531 if (ql2xmvasynctoatio &&
3532 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003533 if (qla_tgt_mode_enabled(vha) ||
3534 qla_dual_mode_enabled(vha))
3535 ha->fw_options[2] |= BIT_11;
3536 else
3537 ha->fw_options[2] &= ~BIT_11;
3538 }
3539
Quinn Tranf7e761f2017-06-02 09:12:02 -07003540 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3541 /*
3542 * Tell FW to track each exchange to prevent
3543 * driver from using stale exchange.
3544 */
3545 if (qla_tgt_mode_enabled(vha) ||
3546 qla_dual_mode_enabled(vha))
3547 ha->fw_options[2] |= BIT_4;
3548 else
3549 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003550
3551 /* Reserve 1/2 of emergency exchanges for ELS.*/
3552 if (qla2xuseresexchforels)
3553 ha->fw_options[2] |= BIT_8;
3554 else
3555 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003556 }
3557
Quinn Tran83548fe2017-06-02 09:12:01 -07003558 ql_dbg(ql_dbg_init, vha, 0x00e8,
3559 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3560 __func__, ha->fw_options[1], ha->fw_options[2],
3561 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003562
3563 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3564 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003565
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003566 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003567 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003568 return;
3569
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003570 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003571 le16_to_cpu(ha->fw_seriallink_options24[1]),
3572 le16_to_cpu(ha->fw_seriallink_options24[2]),
3573 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003574 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003575 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003576 "Unable to update Serial Link options (%x).\n", rval);
3577 }
3578}
3579
3580void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003581qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003582{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003583 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003584 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003585 struct req_que *req = ha->req_q_map[0];
3586 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003587
3588 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003589 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3590 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003591 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3592 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3593 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3594 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3595 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3596 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003597
3598 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3599 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3600 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3601 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3602 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3603}
3604
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003605void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003606qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003607{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003608 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003609 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003610 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3611 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003612 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003613 uint16_t rid = 0;
3614 struct req_que *req = ha->req_q_map[0];
3615 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003616
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003617 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003618 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003619 icb->request_q_outpointer = cpu_to_le16(0);
3620 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003621 icb->request_q_length = cpu_to_le16(req->length);
3622 icb->response_q_length = cpu_to_le16(rsp->length);
3623 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3624 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3625 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3626 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003627
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003628 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003629 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003630 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3631 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3632 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3633
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003634 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003635 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003636
Chad Dupuisf73cb692014-02-26 04:15:06 -05003637 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003638 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3639 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003640 if (ha->flags.msix_enabled) {
3641 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003642 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003643 "Registering vector 0x%x for base que.\n",
3644 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003645 icb->msix = cpu_to_le16(msix->entry);
3646 }
3647 /* Use alternate PCI bus number */
3648 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003649 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003650 /* Use alternate PCI devfn */
3651 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003652 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003653
Anirban Chakraborty31557542009-12-02 10:36:55 -08003654 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003655 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3656 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003657 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003658 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003659 ql_dbg(ql_dbg_init, vha, 0x00fe,
3660 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003661 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003662 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003663 }
Bart Van Asschead950362015-07-09 07:24:08 -07003664 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003665
3666 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3667 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3668 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3669 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3670 } else {
3671 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3672 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3673 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3674 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3675 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003676 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003677
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003678 /* PCI posting */
3679 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003680}
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682/**
3683 * qla2x00_init_rings() - Initializes firmware.
3684 * @ha: HA context
3685 *
3686 * Beginning of request ring has initialization control block already built
3687 * by nvram config routine.
3688 *
3689 * Returns 0 on success.
3690 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003691int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003692qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
3694 int rval;
3695 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003696 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003697 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003698 struct req_que *req;
3699 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003700 struct mid_init_cb_24xx *mid_init_cb =
3701 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 spin_lock_irqsave(&ha->hardware_lock, flags);
3704
3705 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003706 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003707 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003708 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003709 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003710 req->out_ptr = (void *)(req->ring + req->length);
3711 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003712 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003713 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003715 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003716
3717 /* Initialize firmware. */
3718 req->ring_ptr = req->ring;
3719 req->ring_index = 0;
3720 req->cnt = req->length;
3721 }
3722
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003723 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003724 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003725 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003726 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003727 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3728 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003729 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003730 if (IS_QLAFX00(ha))
3731 qlafx00_init_response_q_entries(rsp);
3732 else
3733 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003736 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3737 ha->tgt.atio_ring_index = 0;
3738 /* Initialize ATIO queue entries */
3739 qlt_init_atio_q_entries(vha);
3740
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003741 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3744
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003745 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3746
3747 if (IS_QLAFX00(ha)) {
3748 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3749 goto next_check;
3750 }
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003753 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003755 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003756 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003757 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003758 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003759 }
3760
Andrew Vasquez24a08132009-03-24 09:08:16 -07003761 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003762 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003763 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003764 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003765 ha->flags.dport_enabled =
3766 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3767 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3768 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3769 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003770 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003771 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003772 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003773 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003774 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003775
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003776 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003777next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003779 ql_log(ql_log_fatal, vha, 0x00d2,
3780 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003782 ql_dbg(ql_dbg_init, vha, 0x00d3,
3783 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003784 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786
3787 return (rval);
3788}
3789
3790/**
3791 * qla2x00_fw_ready() - Waits for firmware ready.
3792 * @ha: HA context
3793 *
3794 * Returns 0 on success.
3795 */
3796static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003797qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
3799 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003800 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 uint16_t min_wait; /* Minimum wait time if loop is down */
3802 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003803 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003804 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003806 if (IS_QLAFX00(vha->hw))
3807 return qlafx00_fw_ready(vha);
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 rval = QLA_SUCCESS;
3810
Chad Dupuis334614912015-04-09 14:59:57 -04003811 /* Time to wait for loop down */
3812 if (IS_P3P_TYPE(ha))
3813 min_wait = 30;
3814 else
3815 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817 /*
3818 * Firmware should take at most one RATOV to login, plus 5 seconds for
3819 * our own processing.
3820 */
3821 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3822 wait_time = min_wait;
3823 }
3824
3825 /* Min wait time if loop down */
3826 mtime = jiffies + (min_wait * HZ);
3827
3828 /* wait time before firmware ready */
3829 wtime = jiffies + (wait_time * HZ);
3830
3831 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003832 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003833 ql_log(ql_log_info, vha, 0x801e,
3834 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003837 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003838 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003840 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003841 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003843 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003844 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3845 "fw_state=%x 84xx=%x.\n", state[0],
3846 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003847 if ((state[2] & FSTATE_LOGGED_IN) &&
3848 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003849 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3850 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003851
3852 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003853 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003854 if (rval != QLA_SUCCESS) {
3855 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003856 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003857 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003858 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003859 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003860
3861 /* Add time taken to initialize. */
3862 cs84xx_time = jiffies - cs84xx_time;
3863 wtime += cs84xx_time;
3864 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003865 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003866 "Increasing wait time by %ld. "
3867 "New time %ld.\n", cs84xx_time,
3868 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003869 }
3870 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003871 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3872 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003874 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 &ha->login_timeout, &ha->r_a_tov);
3876
3877 rval = QLA_SUCCESS;
3878 break;
3879 }
3880
3881 rval = QLA_FUNCTION_FAILED;
3882
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003883 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003884 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003886 * other than Wait for Login.
3887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003889 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 "Cable is unplugged...\n");
3891
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003892 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 break;
3894 }
3895 }
3896 } else {
3897 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003898 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003899 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 break;
3901 }
3902
3903 if (time_after_eq(jiffies, wtime))
3904 break;
3905
3906 /* Delay for a while */
3907 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 } while (1);
3909
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003910 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003911 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3912 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Chad Dupuiscfb09192011-11-18 09:03:07 -08003914 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003915 ql_log(ql_log_warn, vha, 0x803b,
3916 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 }
3918
3919 return (rval);
3920}
3921
3922/*
3923* qla2x00_configure_hba
3924* Setup adapter context.
3925*
3926* Input:
3927* ha = adapter state pointer.
3928*
3929* Returns:
3930* 0 = success
3931*
3932* Context:
3933* Kernel context.
3934*/
3935static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003936qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937{
3938 int rval;
3939 uint16_t loop_id;
3940 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003941 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 uint8_t al_pa;
3943 uint8_t area;
3944 uint8_t domain;
3945 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003946 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003947 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003948 port_id_t id;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08003949 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003952 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003953 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003955 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003956 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003957 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003958 ql_dbg(ql_dbg_disc, vha, 0x2008,
3959 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003960 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003961 ql_log(ql_log_warn, vha, 0x2009,
3962 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003963 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3964 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3965 ql_log(ql_log_warn, vha, 0x1151,
3966 "Doing link init.\n");
3967 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3968 return rval;
3969 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003970 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return (rval);
3973 }
3974
3975 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003976 ql_log(ql_log_info, vha, 0x200a,
3977 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return (QLA_FUNCTION_FAILED);
3979 }
3980
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003981 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 /* initialize */
3984 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3985 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003986 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
3988 switch (topo) {
3989 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003990 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 ha->current_topology = ISP_CFG_NL;
3992 strcpy(connect_type, "(Loop)");
3993 break;
3994
3995 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003996 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003997 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 ha->current_topology = ISP_CFG_FL;
3999 strcpy(connect_type, "(FL_Port)");
4000 break;
4001
4002 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004003 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 ha->operating_mode = P2P;
4005 ha->current_topology = ISP_CFG_N;
4006 strcpy(connect_type, "(N_Port-to-N_Port)");
4007 break;
4008
4009 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004010 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004011 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 ha->operating_mode = P2P;
4013 ha->current_topology = ISP_CFG_F;
4014 strcpy(connect_type, "(F_Port)");
4015 break;
4016
4017 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004018 ql_dbg(ql_dbg_disc, vha, 0x200f,
4019 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 ha->current_topology = ISP_CFG_NL;
4021 strcpy(connect_type, "(Loop)");
4022 break;
4023 }
4024
4025 /* Save Host port and loop ID. */
4026 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004027 id.b.domain = domain;
4028 id.b.area = area;
4029 id.b.al_pa = al_pa;
4030 id.b.rsvd_1 = 0;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004031 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran482c9dc2017-03-15 09:48:54 -07004032 qlt_update_host_map(vha, id);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004033 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004034
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004035 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004036 ql_log(ql_log_info, vha, 0x2010,
4037 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004038 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return(rval);
4041}
4042
Giridhar Malavalia9083012010-04-12 17:59:55 -07004043inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004044qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4045 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004046{
4047 char *st, *en;
4048 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004049 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004050 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004051 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004052
4053 if (memcmp(model, BINZERO, len) != 0) {
4054 strncpy(ha->model_number, model, len);
4055 st = en = ha->model_number;
4056 en += len - 1;
4057 while (en > st) {
4058 if (*en != 0x20 && *en != 0x00)
4059 break;
4060 *en-- = '\0';
4061 }
4062
4063 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004064 if (use_tbl &&
4065 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004066 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004067 strncpy(ha->model_desc,
4068 qla2x00_model_name[index * 2 + 1],
4069 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004070 } else {
4071 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004072 if (use_tbl &&
4073 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004074 index < QLA_MODEL_NAMES) {
4075 strcpy(ha->model_number,
4076 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004077 strncpy(ha->model_desc,
4078 qla2x00_model_name[index * 2 + 1],
4079 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004080 } else {
4081 strcpy(ha->model_number, def);
4082 }
4083 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004084 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004085 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004086 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004087}
4088
David Miller4e08df32007-04-16 12:37:43 -07004089/* On sparc systems, obtain port and node WWN from firmware
4090 * properties.
4091 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004092static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004093{
4094#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004095 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004096 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004097 struct device_node *dp = pci_device_to_OF_node(pdev);
4098 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004099 int len;
4100
4101 val = of_get_property(dp, "port-wwn", &len);
4102 if (val && len >= WWN_SIZE)
4103 memcpy(nv->port_name, val, WWN_SIZE);
4104
4105 val = of_get_property(dp, "node-wwn", &len);
4106 if (val && len >= WWN_SIZE)
4107 memcpy(nv->node_name, val, WWN_SIZE);
4108#endif
4109}
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111/*
4112* NVRAM configuration for ISP 2xxx
4113*
4114* Input:
4115* ha = adapter block pointer.
4116*
4117* Output:
4118* initialization control block in response_ring
4119* host adapters parameters in host adapter block
4120*
4121* Returns:
4122* 0 = success.
4123*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004124int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004125qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
David Miller4e08df32007-04-16 12:37:43 -07004127 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004128 uint8_t chksum = 0;
4129 uint16_t cnt;
4130 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004131 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004132 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004133 nvram_t *nv = ha->nvram;
4134 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004135 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
David Miller4e08df32007-04-16 12:37:43 -07004137 rval = QLA_SUCCESS;
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004140 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 ha->nvram_base = 0;
4142 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4143 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4144 ha->nvram_base = 0x80;
4145
4146 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004147 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004148 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4149 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004151 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4152 "Contents of NVRAM.\n");
4153 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4154 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
4156 /* Bad NVRAM data, set defaults parameters. */
4157 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4158 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4159 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004160 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004161 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004162 "detected: checksum=0x%x id=%c version=0x%x.\n",
4163 chksum, nv->id[0], nv->nvram_version);
4164 ql_log(ql_log_warn, vha, 0x0065,
4165 "Falling back to "
4166 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004167
4168 /*
4169 * Set default initialization control block.
4170 */
4171 memset(nv, 0, ha->nvram_size);
4172 nv->parameter_block_version = ICB_VERSION;
4173
4174 if (IS_QLA23XX(ha)) {
4175 nv->firmware_options[0] = BIT_2 | BIT_1;
4176 nv->firmware_options[1] = BIT_7 | BIT_5;
4177 nv->add_firmware_options[0] = BIT_5;
4178 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004179 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004180 nv->special_options[1] = BIT_7;
4181 } else if (IS_QLA2200(ha)) {
4182 nv->firmware_options[0] = BIT_2 | BIT_1;
4183 nv->firmware_options[1] = BIT_7 | BIT_5;
4184 nv->add_firmware_options[0] = BIT_5;
4185 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004186 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004187 } else if (IS_QLA2100(ha)) {
4188 nv->firmware_options[0] = BIT_3 | BIT_1;
4189 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004190 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004191 }
4192
Bart Van Asschead950362015-07-09 07:24:08 -07004193 nv->max_iocb_allocation = cpu_to_le16(256);
4194 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004195 nv->retry_count = 8;
4196 nv->retry_delay = 1;
4197
4198 nv->port_name[0] = 33;
4199 nv->port_name[3] = 224;
4200 nv->port_name[4] = 139;
4201
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004202 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004203
4204 nv->login_timeout = 4;
4205
4206 /*
4207 * Set default host adapter parameters
4208 */
4209 nv->host_p[1] = BIT_2;
4210 nv->reset_delay = 5;
4211 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004212 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004213 nv->link_down_timeout = 60;
4214
4215 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217
4218#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4219 /*
4220 * The SN2 does not provide BIOS emulation which means you can't change
4221 * potentially bogus BIOS settings. Force the use of default settings
4222 * for link rate and frame size. Hope that the rest of the settings
4223 * are valid.
4224 */
4225 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004226 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 if (IS_QLA23XX(ha))
4228 nv->special_options[1] = BIT_7;
4229 }
4230#endif
4231
4232 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004233 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235 /*
4236 * Setup driver NVRAM options.
4237 */
4238 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4239 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4240 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4241 nv->firmware_options[1] &= ~BIT_4;
4242
4243 if (IS_QLA23XX(ha)) {
4244 nv->firmware_options[0] |= BIT_2;
4245 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004246 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004247 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
4249 if (IS_QLA2300(ha)) {
4250 if (ha->fb_rev == FPM_2310) {
4251 strcpy(ha->model_number, "QLA2310");
4252 } else {
4253 strcpy(ha->model_number, "QLA2300");
4254 }
4255 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004256 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004257 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 }
4259 } else if (IS_QLA2200(ha)) {
4260 nv->firmware_options[0] |= BIT_2;
4261 /*
4262 * 'Point-to-point preferred, else loop' is not a safe
4263 * connection mode setting.
4264 */
4265 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4266 (BIT_5 | BIT_4)) {
4267 /* Force 'loop preferred, else point-to-point'. */
4268 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4269 nv->add_firmware_options[0] |= BIT_5;
4270 }
4271 strcpy(ha->model_number, "QLA22xx");
4272 } else /*if (IS_QLA2100(ha))*/ {
4273 strcpy(ha->model_number, "QLA2100");
4274 }
4275
4276 /*
4277 * Copy over NVRAM RISC parameter block to initialization control block.
4278 */
4279 dptr1 = (uint8_t *)icb;
4280 dptr2 = (uint8_t *)&nv->parameter_block_version;
4281 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4282 while (cnt--)
4283 *dptr1++ = *dptr2++;
4284
4285 /* Copy 2nd half. */
4286 dptr1 = (uint8_t *)icb->add_firmware_options;
4287 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4288 while (cnt--)
4289 *dptr1++ = *dptr2++;
4290
Andrew Vasquez5341e862006-05-17 15:09:16 -07004291 /* Use alternate WWN? */
4292 if (nv->host_p[1] & BIT_7) {
4293 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4294 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4295 }
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 /* Prepare nodename */
4298 if ((icb->firmware_options[1] & BIT_6) == 0) {
4299 /*
4300 * Firmware will apply the following mask if the nodename was
4301 * not provided.
4302 */
4303 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4304 icb->node_name[0] &= 0xF0;
4305 }
4306
4307 /*
4308 * Set host adapter parameters.
4309 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004310
4311 /*
4312 * BIT_7 in the host-parameters section allows for modification to
4313 * internal driver logging.
4314 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004315 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004316 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4318 /* Always load RISC code on non ISP2[12]00 chips. */
4319 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4320 ha->flags.disable_risc_code_load = 0;
4321 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4322 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4323 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004324 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004325 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327 ha->operating_mode =
4328 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4329
4330 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4331 sizeof(ha->fw_seriallink_options));
4332
4333 /* save HBA serial number */
4334 ha->serial0 = icb->port_name[5];
4335 ha->serial1 = icb->port_name[6];
4336 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004337 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4338 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Bart Van Asschead950362015-07-09 07:24:08 -07004340 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
4342 ha->retry_count = nv->retry_count;
4343
4344 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004345 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 nv->login_timeout = ql2xlogintimeout;
4347 if (nv->login_timeout < 4)
4348 nv->login_timeout = 4;
4349 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004351 /* Set minimum RATOV to 100 tenths of a second. */
4352 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 ha->loop_reset_delay = nv->reset_delay;
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 /* Link Down Timeout = 0:
4357 *
4358 * When Port Down timer expires we will start returning
4359 * I/O's to OS with "DID_NO_CONNECT".
4360 *
4361 * Link Down Timeout != 0:
4362 *
4363 * The driver waits for the link to come up after link down
4364 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 if (nv->link_down_timeout == 0) {
4367 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004368 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 } else {
4370 ha->link_down_timeout = nv->link_down_timeout;
4371 ha->loop_down_abort_time =
4372 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 /*
4376 * Need enough time to try and get the port back.
4377 */
4378 ha->port_down_retry_count = nv->port_down_retry_count;
4379 if (qlport_down_retry)
4380 ha->port_down_retry_count = qlport_down_retry;
4381 /* Set login_retry_count */
4382 ha->login_retry_count = nv->retry_count;
4383 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4384 ha->port_down_retry_count > 3)
4385 ha->login_retry_count = ha->port_down_retry_count;
4386 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4387 ha->login_retry_count = ha->port_down_retry_count;
4388 if (ql2xloginretrycount)
4389 ha->login_retry_count = ql2xloginretrycount;
4390
Bart Van Asschead950362015-07-09 07:24:08 -07004391 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 icb->command_resource_count = 0;
4393 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004394 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4397 /* Enable RIO */
4398 icb->firmware_options[0] &= ~BIT_3;
4399 icb->add_firmware_options[0] &=
4400 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4401 icb->add_firmware_options[0] |= BIT_2;
4402 icb->response_accumulation_timer = 3;
4403 icb->interrupt_delay_timer = 5;
4404
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004405 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004407 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004408 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004409 ha->zio_mode = icb->add_firmware_options[0] &
4410 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4411 ha->zio_timer = icb->interrupt_delay_timer ?
4412 icb->interrupt_delay_timer: 2;
4413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 icb->add_firmware_options[0] &=
4415 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004416 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004417 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004418 ha->zio_mode = QLA_ZIO_MODE_6;
4419
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004420 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004421 "ZIO mode %d enabled; timer delay (%d us).\n",
4422 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004424 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4425 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004426 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428 }
4429
David Miller4e08df32007-04-16 12:37:43 -07004430 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004431 ql_log(ql_log_warn, vha, 0x0069,
4432 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004433 }
4434 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004437static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004438qla2x00_rport_del(void *data)
4439{
4440 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004441 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004442 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004443
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004444 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004445 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004446 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004447 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004448 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004449 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4450 "%s %8phN. rport %p roles %x\n",
4451 __func__, fcport->port_name, rport,
4452 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004453
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004454 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004455 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004456}
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458/**
4459 * qla2x00_alloc_fcport() - Allocate a generic fcport.
4460 * @ha: HA context
4461 * @flags: allocation flags
4462 *
4463 * Returns a pointer to the allocated fcport, or NULL, if none available.
4464 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004465fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004466qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
4468 fc_port_t *fcport;
4469
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004470 fcport = kzalloc(sizeof(fc_port_t), flags);
4471 if (!fcport)
4472 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004475 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 fcport->port_type = FCT_UNKNOWN;
4477 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004478 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004479 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Quinn Tran726b8542017-01-19 22:28:00 -08004481 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4482 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004483 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004484 fcport->disc_state = DSC_DELETED;
4485 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4486 fcport->deleted = QLA_SESS_DELETED;
4487 fcport->login_retry = vha->hw->login_retry_count;
4488 fcport->login_retry = 5;
4489 fcport->logout_on_delete = 1;
4490
4491 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004492 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004493 "Failed to allocate ct_sns request.\n");
4494 kfree(fcport);
4495 fcport = NULL;
4496 }
4497 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4498 INIT_LIST_HEAD(&fcport->gnl_entry);
4499 INIT_LIST_HEAD(&fcport->list);
4500
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004501 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
Quinn Tran726b8542017-01-19 22:28:00 -08004504void
4505qla2x00_free_fcport(fc_port_t *fcport)
4506{
4507 if (fcport->ct_desc.ct_sns) {
4508 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4509 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4510 fcport->ct_desc.ct_sns_dma);
4511
4512 fcport->ct_desc.ct_sns = NULL;
4513 }
4514 kfree(fcport);
4515}
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517/*
4518 * qla2x00_configure_loop
4519 * Updates Fibre Channel Device Database with what is actually on loop.
4520 *
4521 * Input:
4522 * ha = adapter block pointer.
4523 *
4524 * Returns:
4525 * 0 = success.
4526 * 1 = error.
4527 * 2 = database was full and device was not configured.
4528 */
4529static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004530qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
4532 int rval;
4533 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004534 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 rval = QLA_SUCCESS;
4536
4537 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004538 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4539 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004541 ql_dbg(ql_dbg_disc, vha, 0x2013,
4542 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 return (rval);
4544 }
4545 }
4546
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004547 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004548 ql_dbg(ql_dbg_disc, vha, 0x2014,
4549 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551 /*
4552 * If we have both an RSCN and PORT UPDATE pending then handle them
4553 * both at the same time.
4554 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004555 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4556 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Michael Hernandez3064ff32009-12-15 21:29:44 -08004558 qla2x00_get_data_rate(vha);
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 /* Determine what we need to do */
4561 if (ha->current_topology == ISP_CFG_FL &&
4562 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 set_bit(RSCN_UPDATE, &flags);
4565
4566 } else if (ha->current_topology == ISP_CFG_F &&
4567 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 set_bit(RSCN_UPDATE, &flags);
4570 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4571
Andrew Vasquez21333b42006-05-17 15:09:56 -07004572 } else if (ha->current_topology == ISP_CFG_N) {
4573 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004574 if (ha->flags.rida_fmt2) {
4575 /* With Rida Format 2, the login is already triggered.
4576 * We know who is on the other side of the wire.
4577 * No need to login to do login to find out or drop into
4578 * qla2x00_configure_local_loop().
4579 */
4580 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4581 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4582 } else {
4583 if (qla_tgt_mode_enabled(vha)) {
4584 /* allow the other side to start the login */
4585 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4586 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4587 }
4588 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004589 } else if (ha->current_topology == ISP_CFG_NL) {
4590 clear_bit(RSCN_UPDATE, &flags);
4591 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004592 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 set_bit(RSCN_UPDATE, &flags);
4595 set_bit(LOCAL_LOOP_UPDATE, &flags);
4596 }
4597
4598 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004599 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4600 ql_dbg(ql_dbg_disc, vha, 0x2015,
4601 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004603 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004604 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
4606
4607 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004608 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004609 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004610 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004612 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004613 else
4614 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616
4617 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004618 if (atomic_read(&vha->loop_down_timer) ||
4619 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 rval = QLA_FUNCTION_FAILED;
4621 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004622 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004623 ql_dbg(ql_dbg_disc, vha, 0x2069,
4624 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004625 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004626
4627 /*
4628 * Process any ATIO queue entries that came in
4629 * while we weren't online.
4630 */
Quinn Tranead03852017-01-19 22:28:01 -08004631 if (qla_tgt_mode_enabled(vha) ||
4632 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004633 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4634 spin_lock_irqsave(&ha->tgt.atio_lock,
4635 flags);
4636 qlt_24xx_process_atio_queue(vha, 0);
4637 spin_unlock_irqrestore(
4638 &ha->tgt.atio_lock, flags);
4639 } else {
4640 spin_lock_irqsave(&ha->hardware_lock,
4641 flags);
4642 qlt_24xx_process_atio_queue(vha, 1);
4643 spin_unlock_irqrestore(
4644 &ha->hardware_lock, flags);
4645 }
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 }
4648 }
4649
4650 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004651 ql_dbg(ql_dbg_disc, vha, 0x206a,
4652 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004654 ql_dbg(ql_dbg_disc, vha, 0x206b,
4655 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004658 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004659 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004661 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004662 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004663 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666
4667 return (rval);
4668}
4669
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004670/*
4671 * N2N Login
4672 * Updates Fibre Channel Device Database with local loop devices.
4673 *
4674 * Input:
4675 * ha = adapter block pointer.
4676 *
4677 * Returns:
4678 */
4679static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4680 fc_port_t *fcport)
4681{
4682 struct qla_hw_data *ha = vha->hw;
4683 int res = QLA_SUCCESS, rval;
4684 int greater_wwpn = 0;
4685 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004687 if (ha->current_topology != ISP_CFG_N)
4688 return res;
4689
4690 if (wwn_to_u64(vha->port_name) >
4691 wwn_to_u64(vha->n2n_port_name)) {
4692 ql_dbg(ql_dbg_disc, vha, 0x2002,
4693 "HBA WWPN is greater %llx > target %llx\n",
4694 wwn_to_u64(vha->port_name),
4695 wwn_to_u64(vha->n2n_port_name));
4696 greater_wwpn = 1;
4697 fcport->d_id.b24 = vha->n2n_id;
4698 }
4699
4700 fcport->loop_id = vha->loop_id;
4701 fcport->fc4f_nvme = 0;
4702 fcport->query = 1;
4703
4704 ql_dbg(ql_dbg_disc, vha, 0x4001,
4705 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4706 fcport->d_id.b24, vha->loop_id);
4707
4708 /* Fill in member data. */
4709 if (!greater_wwpn) {
4710 rval = qla2x00_get_port_database(vha, fcport, 0);
4711 ql_dbg(ql_dbg_disc, vha, 0x1051,
4712 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4713 fcport->current_login_state, fcport->last_login_state,
4714 fcport->d_id.b24, fcport->loop_id, rval);
4715
4716 if (((fcport->current_login_state & 0xf) == 0x4) ||
4717 ((fcport->current_login_state & 0xf) == 0x6))
4718 logged_in = 1;
4719 }
4720
4721 if (logged_in || greater_wwpn) {
4722 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4723 qla_nvme_register_hba(vha);
4724
4725 /* Set connected N_Port d_id */
4726 if (vha->flags.nvme_enabled)
4727 fcport->fc4f_nvme = 1;
4728
4729 fcport->scan_state = QLA_FCPORT_FOUND;
4730 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4731 fcport->disc_state = DSC_GNL;
4732 fcport->n2n_flag = 1;
4733 fcport->flags = 3;
4734 vha->hw->flags.gpsc_supported = 0;
4735
4736 if (greater_wwpn) {
4737 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4738 "%s %d PLOGI ELS %8phC\n",
4739 __func__, __LINE__, fcport->port_name);
4740
4741 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4742 fcport, fcport->d_id);
4743 }
4744
4745 if (res != QLA_SUCCESS) {
4746 ql_log(ql_log_info, vha, 0xd04d,
4747 "PLOGI Failed: portid=%06x - retrying\n",
4748 fcport->d_id.b24);
4749 res = QLA_SUCCESS;
4750 } else {
4751 /* State 0x6 means FCP PRLI complete */
4752 if ((fcport->current_login_state & 0xf) == 0x6) {
4753 ql_dbg(ql_dbg_disc, vha, 0x2118,
4754 "%s %d %8phC post GPDB work\n",
4755 __func__, __LINE__, fcport->port_name);
4756 fcport->chip_reset =
4757 vha->hw->base_qpair->chip_reset;
4758 qla24xx_post_gpdb_work(vha, fcport, 0);
4759 } else {
4760 ql_dbg(ql_dbg_disc, vha, 0x2118,
4761 "%s %d %8phC post NVMe PRLI\n",
4762 __func__, __LINE__, fcport->port_name);
4763 qla24xx_post_prli_work(vha, fcport);
4764 }
4765 }
4766 } else {
4767 /* Wait for next database change */
4768 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4769 }
4770
4771 return res;
4772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774/*
4775 * qla2x00_configure_local_loop
4776 * Updates Fibre Channel Device Database with local loop devices.
4777 *
4778 * Input:
4779 * ha = adapter block pointer.
4780 *
4781 * Returns:
4782 * 0 = success.
4783 */
4784static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004785qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
4787 int rval, rval2;
4788 int found_devs;
4789 int found;
4790 fc_port_t *fcport, *new_fcport;
4791
4792 uint16_t index;
4793 uint16_t entries;
4794 char *id_iter;
4795 uint16_t loop_id;
4796 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004797 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004798 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800 found_devs = 0;
4801 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004802 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004805 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004806 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 &entries);
4808 if (rval != QLA_SUCCESS)
4809 goto cleanup_allocation;
4810
Quinn Tran83548fe2017-06-02 09:12:01 -07004811 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004812 "Entries in ID list (%d).\n", entries);
4813 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4814 (uint8_t *)ha->gid_list,
4815 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Quinn Tran9cd883f2017-12-28 12:33:24 -08004817 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4818 fcport->scan_state = QLA_FCPORT_SCAN;
4819 }
4820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004822 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004824 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004825 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 rval = QLA_MEMORY_ALLOC_FAILED;
4827 goto cleanup_allocation;
4828 }
4829 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4830
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004831 /* Inititae N2N login. */
4832 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4833 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4834 if (rval != QLA_SUCCESS)
4835 goto cleanup_allocation;
4836 return QLA_SUCCESS;
4837 }
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 /* Add devices to port list. */
4840 id_iter = (char *)ha->gid_list;
4841 for (index = 0; index < entries; index++) {
4842 domain = ((struct gid_list_info *)id_iter)->domain;
4843 area = ((struct gid_list_info *)id_iter)->area;
4844 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004845 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 loop_id = (uint16_t)
4847 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004848 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 loop_id = le16_to_cpu(
4850 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004851 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 /* Bypass reserved domain fields. */
4854 if ((domain & 0xf0) == 0xf0)
4855 continue;
4856
4857 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004858 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004859 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 continue;
4861
4862 /* Bypass invalid local loop ID. */
4863 if (loop_id > LAST_LOCAL_LOOP_ID)
4864 continue;
4865
Quinn Tran41dc5292017-01-19 22:28:03 -08004866 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 /* Fill in member data. */
4869 new_fcport->d_id.b.domain = domain;
4870 new_fcport->d_id.b.area = area;
4871 new_fcport->d_id.b.al_pa = al_pa;
4872 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004873 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004874
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004875 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004877 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004878 "Failed to retrieve fcport information "
4879 "-- get_port_database=%x, loop_id=0x%04x.\n",
4880 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004881 /* Skip retry if N2N */
4882 if (ha->current_topology != ISP_CFG_N) {
4883 ql_dbg(ql_dbg_disc, vha, 0x2105,
4884 "Scheduling resync.\n");
4885 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4886 continue;
4887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 }
4889
Quinn Tran41dc5292017-01-19 22:28:03 -08004890 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 /* Check for matching device in port list. */
4892 found = 0;
4893 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004894 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (memcmp(new_fcport->port_name, fcport->port_name,
4896 WWN_SIZE))
4897 continue;
4898
Shyam Sundarddb9b122009-03-24 09:08:10 -07004899 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 fcport->loop_id = new_fcport->loop_id;
4901 fcport->port_type = new_fcport->port_type;
4902 fcport->d_id.b24 = new_fcport->d_id.b24;
4903 memcpy(fcport->node_name, new_fcport->node_name,
4904 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004905 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 found++;
4907 break;
4908 }
4909
4910 if (!found) {
4911 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004912 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
4914 /* Allocate a new replacement fcport. */
4915 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004916
4917 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4918
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004919 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004922 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004923 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 rval = QLA_MEMORY_ALLOC_FAILED;
4925 goto cleanup_allocation;
4926 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004927 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4929 }
4930
Quinn Tran41dc5292017-01-19 22:28:03 -08004931 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4932
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004933 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004934 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 found_devs++;
4937 }
4938
Quinn Tran9cd883f2017-12-28 12:33:24 -08004939 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4940 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4941 break;
4942
4943 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4944 if ((qla_dual_mode_enabled(vha) ||
4945 qla_ini_mode_enabled(vha)) &&
4946 atomic_read(&fcport->state) == FCS_ONLINE) {
4947 qla2x00_mark_device_lost(vha, fcport,
4948 ql2xplogiabsentdevice, 0);
4949 if (fcport->loop_id != FC_NO_LOOP_ID &&
4950 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4951 fcport->port_type != FCT_INITIATOR &&
4952 fcport->port_type != FCT_BROADCAST) {
4953 ql_dbg(ql_dbg_disc, vha, 0x20f0,
4954 "%s %d %8phC post del sess\n",
4955 __func__, __LINE__,
4956 fcport->port_name);
4957
4958 qlt_schedule_sess_for_deletion_lock
4959 (fcport);
4960 continue;
4961 }
4962 }
4963 }
4964
4965 if (fcport->scan_state == QLA_FCPORT_FOUND)
4966 qla24xx_fcport_handle_login(vha, fcport);
4967 }
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004970 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004973 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004974 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 }
4976
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 return (rval);
4978}
4979
4980static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004981qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004982{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004983 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004984 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004985 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004986
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004987 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004988 return;
4989
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07004990 if (atomic_read(&fcport->state) != FCS_ONLINE)
4991 return;
4992
Andrew Vasquez39bd9622007-09-20 14:07:34 -07004993 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
4994 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004995 return;
4996
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004997 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004998 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004999 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005000 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005001 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5002 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005003 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005004 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005005 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05005006 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005007 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005008 }
5009}
5010
Quinn Tran726b8542017-01-19 22:28:00 -08005011/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005012static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005013qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005014{
5015 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005016 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005017 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005018
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005019 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5020 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005021 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5022 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5023 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005024 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005025 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005026 ql_log(ql_log_warn, vha, 0x2006,
5027 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005028 return;
5029 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005030
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005031 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005032 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005033 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005034
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005035 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005036
5037 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005038 if (fcport->port_type == FCT_INITIATOR)
5039 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5040 if (fcport->port_type == FCT_TARGET)
5041 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005042
Quinn Tran83548fe2017-06-02 09:12:01 -07005043 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5044 "%s %8phN. rport %p is %s mode\n",
5045 __func__, fcport->port_name, rport,
5046 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005047
Andrew Vasquez77d74142005-07-08 18:00:36 -07005048 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
5051/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005052 * qla2x00_update_fcport
5053 * Updates device on list.
5054 *
5055 * Input:
5056 * ha = adapter block pointer.
5057 * fcport = port structure pointer.
5058 *
5059 * Return:
5060 * 0 - Success
5061 * BIT_0 - error
5062 *
5063 * Context:
5064 * Kernel context.
5065 */
5066void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005067qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005068{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005069 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005070
Quinn Tran726b8542017-01-19 22:28:00 -08005071 if (IS_SW_RESV_ADDR(fcport->d_id))
5072 return;
5073
Quinn Tran83548fe2017-06-02 09:12:01 -07005074 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005075 __func__, fcport->port_name);
5076
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005077 if (IS_QLAFX00(vha->hw)) {
5078 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005079 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005080 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005081 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005082 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005083 fcport->disc_state = DSC_LOGIN_COMPLETE;
5084 fcport->deleted = 0;
5085 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005086
Duane Grigsbye84067d2017-06-21 13:48:43 -07005087 if (fcport->fc4f_nvme) {
5088 qla_nvme_register_remote(vha, fcport);
5089 return;
5090 }
5091
Joe Carnuccio1f93da522012-11-21 02:40:38 -05005092 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005093 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005094 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005095
5096reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005097 switch (vha->host->active_mode) {
5098 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005099 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005100 break;
5101 case MODE_TARGET:
5102 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5103 !vha->vha_tgt.qla_tgt->tgt_stopped)
5104 qlt_fc_port_added(vha, fcport);
5105 break;
5106 case MODE_DUAL:
5107 qla2x00_reg_remote_port(vha, fcport);
5108 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5109 !vha->vha_tgt.qla_tgt->tgt_stopped)
5110 qlt_fc_port_added(vha, fcport);
5111 break;
5112 default:
5113 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005114 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005115}
5116
5117/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 * qla2x00_configure_fabric
5119 * Setup SNS devices with loop ID's.
5120 *
5121 * Input:
5122 * ha = adapter block pointer.
5123 *
5124 * Returns:
5125 * 0 = success.
5126 * BIT_0 = error
5127 */
5128static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005129qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130{
Arun Easib3b02e62012-02-09 11:15:39 -08005131 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005132 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005134 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005136 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005137 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
5139 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005140 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005141 loop_id = NPH_F_PORT;
5142 else
5143 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005144 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005146 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005147 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005149 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 return (QLA_SUCCESS);
5151 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005152 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
Quinn Tran41dc5292017-01-19 22:28:03 -08005154
5155 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5156 rval = qla2x00_send_change_request(vha, 0x3, 0);
5157 if (rval != QLA_SUCCESS)
5158 ql_log(ql_log_warn, vha, 0x121,
5159 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5160 rval);
5161 }
5162
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005165 qla2x00_mgmt_svr_login(vha);
5166
Andrew Vasquezcca53352005-08-26 19:08:30 -07005167 /* FDMI support. */
5168 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005169 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5170 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005173 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005174 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5175 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005176 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5177 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5178 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5179 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005180 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005181 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005182 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005183 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5184 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005186 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005187 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005188 if (test_bit(LOOP_RESYNC_NEEDED,
5189 &vha->dpc_flags))
5190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005192 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005194 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005195 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005196 if (test_bit(LOOP_RESYNC_NEEDED,
5197 &vha->dpc_flags))
5198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005200 if (vha->flags.nvme_enabled) {
5201 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5202 ql_dbg(ql_dbg_disc, vha, 0x2049,
5203 "Register NVME FC Type Features failed.\n");
5204 }
5205 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005206 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005208 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005209 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005210 if (test_bit(LOOP_RESYNC_NEEDED,
5211 &vha->dpc_flags))
5212 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005213 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005215 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005216 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005217 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 }
5220 }
5221
Joe Carnuccio827210b2013-02-08 01:57:57 -05005222
Alexei Potashnikdf673272015-07-14 16:00:46 -04005223 /* Mark the time right before querying FW for connected ports.
5224 * This process is long, asynchronous and by the time it's done,
5225 * collected information might not be accurate anymore. E.g.
5226 * disconnected port might have re-connected and a brand new
5227 * session has been created. In this case session's generation
5228 * will be newer than discovery_gen. */
5229 qlt_do_generation_tick(vha, &discovery_gen);
5230
Quinn Trana4239942017-12-28 12:33:26 -08005231 if (USE_ASYNC_SCAN(ha)) {
5232 rval = QLA_SUCCESS;
5233 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5234 if (rval)
5235 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5236 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005237 list_for_each_entry(fcport, &vha->vp_fcports, list)
5238 fcport->scan_state = QLA_FCPORT_SCAN;
5239
Quinn Trana4239942017-12-28 12:33:26 -08005240 rval = qla2x00_find_all_fabric_devs(vha);
5241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if (rval != QLA_SUCCESS)
5243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 } while (0);
5245
Duane Grigsbye84067d2017-06-21 13:48:43 -07005246 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5247 qla_nvme_register_hba(vha);
5248
Quinn Tran726b8542017-01-19 22:28:00 -08005249 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005250 ql_dbg(ql_dbg_disc, vha, 0x2068,
5251 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
5253 return (rval);
5254}
5255
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256/*
5257 * qla2x00_find_all_fabric_devs
5258 *
5259 * Input:
5260 * ha = adapter block pointer.
5261 * dev = database device entry pointer.
5262 *
5263 * Returns:
5264 * 0 = success.
5265 *
5266 * Context:
5267 * Kernel context.
5268 */
5269static int
Quinn Tran726b8542017-01-19 22:28:00 -08005270qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271{
5272 int rval;
5273 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005274 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 int found;
5276
5277 sw_info_t *swl;
5278 int swl_idx;
5279 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005280 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005281 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005282 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005283 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
5285 rval = QLA_SUCCESS;
5286
5287 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005288 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005289 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005290 GFP_KERNEL);
5291 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005292 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005294 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005295 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005297 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005298 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005300 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5301 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005302 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005304 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5305 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005306 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005308 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5309 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005310 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5311 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005312 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5313 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005315
5316 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005317 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005318 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005319 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5320 return rval;
5321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 }
5323 swl_idx = 0;
5324
5325 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005326 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005328 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005329 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 return (QLA_MEMORY_ALLOC_FAILED);
5331 }
5332 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 /* Set start port ID scan at adapter ID. */
5334 first_dev = 1;
5335 last_dev = 0;
5336
5337 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005338 loop_id = ha->min_external_loopid;
5339 for (; loop_id <= ha->max_loop_id; loop_id++) {
5340 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 continue;
5342
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005343 if (ha->current_topology == ISP_CFG_FL &&
5344 (atomic_read(&vha->loop_down_timer) ||
5345 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005346 atomic_set(&vha->loop_down_timer, 0);
5347 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5348 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
5352 if (swl != NULL) {
5353 if (last_dev) {
5354 wrap.b24 = new_fcport->d_id.b24;
5355 } else {
5356 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5357 memcpy(new_fcport->node_name,
5358 swl[swl_idx].node_name, WWN_SIZE);
5359 memcpy(new_fcport->port_name,
5360 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005361 memcpy(new_fcport->fabric_port_name,
5362 swl[swl_idx].fabric_port_name, WWN_SIZE);
5363 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005364 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005366 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005367 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005368 if (vha->flags.nvme_enabled &&
5369 swl[swl_idx].fc4f_nvme) {
5370 new_fcport->fc4f_nvme =
5371 swl[swl_idx].fc4f_nvme;
5372 ql_log(ql_log_info, vha, 0x2131,
5373 "FOUND: NVME port %8phC as FC Type 28h\n",
5374 new_fcport->port_name);
5375 }
5376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5378 last_dev = 1;
5379 }
5380 swl_idx++;
5381 }
5382 } else {
5383 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005384 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005386 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005387 "SNS scan failed -- assuming "
5388 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 rval = QLA_SUCCESS;
5390 break;
5391 }
5392 }
5393
5394 /* If wrap on switch device list, exit. */
5395 if (first_dev) {
5396 wrap.b24 = new_fcport->d_id.b24;
5397 first_dev = 0;
5398 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005399 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005400 "Device wrap (%02x%02x%02x).\n",
5401 new_fcport->d_id.b.domain,
5402 new_fcport->d_id.b.area,
5403 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 break;
5405 }
5406
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005407 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005408 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 continue;
5410
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005411 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005412 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5413 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005414
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005415 /* Bypass if same domain and area of adapter. */
5416 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005417 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005418 ISP_CFG_FL)
5419 continue;
5420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 /* Bypass reserved domain fields. */
5422 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5423 continue;
5424
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005425 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005426 if (ql2xgffidenable &&
5427 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5428 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005429 continue;
5430
Quinn Tran726b8542017-01-19 22:28:00 -08005431 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /* Locate matching device in database. */
5434 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005435 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (memcmp(new_fcport->port_name, fcport->port_name,
5437 WWN_SIZE))
5438 continue;
5439
Joe Carnuccio827210b2013-02-08 01:57:57 -05005440 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005441
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 found++;
5443
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005444 /* Update port state. */
5445 memcpy(fcport->fabric_port_name,
5446 new_fcport->fabric_port_name, WWN_SIZE);
5447 fcport->fp_speed = new_fcport->fp_speed;
5448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005450 * If address the same and state FCS_ONLINE
5451 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 */
5453 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005454 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005455 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 break;
5457 }
5458
5459 /*
5460 * If device was not a fabric device before.
5461 */
5462 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5463 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005464 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 fcport->flags |= (FCF_FABRIC_DEVICE |
5466 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 break;
5468 }
5469
5470 /*
5471 * Port ID changed or device was marked to be updated;
5472 * Log it out if still logged in and mark it for
5473 * relogin later.
5474 */
Quinn Tran726b8542017-01-19 22:28:00 -08005475 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005476 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5477 "port changed FC ID, %8phC"
5478 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5479 fcport->port_name,
5480 fcport->d_id.b.domain,
5481 fcport->d_id.b.area,
5482 fcport->d_id.b.al_pa,
5483 fcport->loop_id,
5484 new_fcport->d_id.b.domain,
5485 new_fcport->d_id.b.area,
5486 new_fcport->d_id.b.al_pa);
5487 fcport->d_id.b24 = new_fcport->d_id.b24;
5488 break;
5489 }
5490
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 fcport->d_id.b24 = new_fcport->d_id.b24;
5492 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 break;
5494 }
5495
Quinn Tran726b8542017-01-19 22:28:00 -08005496 if (found) {
5497 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005501 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005502 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5503
5504 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
5507 /* Allocate a new replacement fcport. */
5508 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005509 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005511 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005512 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 return (QLA_MEMORY_ALLOC_FAILED);
5514 }
5515 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5516 new_fcport->d_id.b24 = nxt_d_id.b24;
5517 }
5518
Quinn Tran726b8542017-01-19 22:28:00 -08005519 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
Quinn Tran726b8542017-01-19 22:28:00 -08005521 /*
5522 * Logout all previous fabric dev marked lost, except FCP2 devices.
5523 */
5524 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5525 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5526 break;
5527
5528 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5529 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5530 continue;
5531
5532 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5533 if ((qla_dual_mode_enabled(vha) ||
5534 qla_ini_mode_enabled(vha)) &&
5535 atomic_read(&fcport->state) == FCS_ONLINE) {
5536 qla2x00_mark_device_lost(vha, fcport,
5537 ql2xplogiabsentdevice, 0);
5538 if (fcport->loop_id != FC_NO_LOOP_ID &&
5539 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5540 fcport->port_type != FCT_INITIATOR &&
5541 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005542 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005543 "%s %d %8phC post del sess\n",
5544 __func__, __LINE__,
5545 fcport->port_name);
5546
5547 qlt_schedule_sess_for_deletion_lock
5548 (fcport);
5549 continue;
5550 }
5551 }
5552 }
5553
5554 if (fcport->scan_state == QLA_FCPORT_FOUND)
5555 qla24xx_fcport_handle_login(vha, fcport);
5556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 return (rval);
5558}
5559
5560/*
5561 * qla2x00_find_new_loop_id
5562 * Scan through our port list and find a new usable loop ID.
5563 *
5564 * Input:
5565 * ha: adapter state pointer.
5566 * dev: port structure pointer.
5567 *
5568 * Returns:
5569 * qla2x00 local function return status code.
5570 *
5571 * Context:
5572 * Kernel context.
5573 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005574int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005575qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576{
5577 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005578 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005579 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580
5581 rval = QLA_SUCCESS;
5582
Chad Dupuis5f16b332012-08-22 14:21:00 -04005583 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Chad Dupuis5f16b332012-08-22 14:21:00 -04005585 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5586 LOOPID_MAP_SIZE);
5587 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5588 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5589 dev->loop_id = FC_NO_LOOP_ID;
5590 rval = QLA_FUNCTION_FAILED;
5591 } else
5592 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Chad Dupuis5f16b332012-08-22 14:21:00 -04005594 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Chad Dupuis5f16b332012-08-22 14:21:00 -04005596 if (rval == QLA_SUCCESS)
5597 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5598 "Assigning new loopid=%x, portid=%x.\n",
5599 dev->loop_id, dev->d_id.b24);
5600 else
5601 ql_log(ql_log_warn, dev->vha, 0x2087,
5602 "No loop_id's available, portid=%x.\n",
5603 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 return (rval);
5606}
5607
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609/*
5610 * qla2x00_fabric_login
5611 * Issue fabric login command.
5612 *
5613 * Input:
5614 * ha = adapter block pointer.
5615 * device = pointer to FC device type structure.
5616 *
5617 * Returns:
5618 * 0 - Login successfully
5619 * 1 - Login failed
5620 * 2 - Initiator device
5621 * 3 - Fatal error
5622 */
5623int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005624qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 uint16_t *next_loopid)
5626{
5627 int rval;
5628 int retry;
5629 uint16_t tmp_loopid;
5630 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005631 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
5633 retry = 0;
5634 tmp_loopid = 0;
5635
5636 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005637 ql_dbg(ql_dbg_disc, vha, 0x2000,
5638 "Trying Fabric Login w/loop id 0x%04x for port "
5639 "%02x%02x%02x.\n",
5640 fcport->loop_id, fcport->d_id.b.domain,
5641 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005644 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 fcport->d_id.b.domain, fcport->d_id.b.area,
5646 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005647 if (rval != QLA_SUCCESS) {
5648 return rval;
5649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 if (mb[0] == MBS_PORT_ID_USED) {
5651 /*
5652 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005653 * recommends the driver perform an implicit login with
5654 * the specified ID again. The ID we just used is save
5655 * here so we return with an ID that can be tried by
5656 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 */
5658 retry++;
5659 tmp_loopid = fcport->loop_id;
5660 fcport->loop_id = mb[1];
5661
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005662 ql_dbg(ql_dbg_disc, vha, 0x2001,
5663 "Fabric Login: port in use - next loop "
5664 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005666 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5669 /*
5670 * Login succeeded.
5671 */
5672 if (retry) {
5673 /* A retry occurred before. */
5674 *next_loopid = tmp_loopid;
5675 } else {
5676 /*
5677 * No retry occurred before. Just increment the
5678 * ID value for next login.
5679 */
5680 *next_loopid = (fcport->loop_id + 1);
5681 }
5682
5683 if (mb[1] & BIT_0) {
5684 fcport->port_type = FCT_INITIATOR;
5685 } else {
5686 fcport->port_type = FCT_TARGET;
5687 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005688 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 }
5690 }
5691
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005692 if (mb[10] & BIT_0)
5693 fcport->supported_classes |= FC_COS_CLASS2;
5694 if (mb[10] & BIT_1)
5695 fcport->supported_classes |= FC_COS_CLASS3;
5696
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005697 if (IS_FWI2_CAPABLE(ha)) {
5698 if (mb[10] & BIT_7)
5699 fcport->flags |=
5700 FCF_CONF_COMP_SUPPORTED;
5701 }
5702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 rval = QLA_SUCCESS;
5704 break;
5705 } else if (mb[0] == MBS_LOOP_ID_USED) {
5706 /*
5707 * Loop ID already used, try next loop ID.
5708 */
5709 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005710 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 if (rval != QLA_SUCCESS) {
5712 /* Ran out of loop IDs to use */
5713 break;
5714 }
5715 } else if (mb[0] == MBS_COMMAND_ERROR) {
5716 /*
5717 * Firmware possibly timed out during login. If NO
5718 * retries are left to do then the device is declared
5719 * dead.
5720 */
5721 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005722 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005723 fcport->d_id.b.domain, fcport->d_id.b.area,
5724 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005725 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
5727 rval = 1;
5728 break;
5729 } else {
5730 /*
5731 * unrecoverable / not handled error
5732 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005733 ql_dbg(ql_dbg_disc, vha, 0x2002,
5734 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5735 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5736 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5737 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005740 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005741 fcport->d_id.b.domain, fcport->d_id.b.area,
5742 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005743 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005744 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746 rval = 3;
5747 break;
5748 }
5749 }
5750
5751 return (rval);
5752}
5753
5754/*
5755 * qla2x00_local_device_login
5756 * Issue local device login command.
5757 *
5758 * Input:
5759 * ha = adapter block pointer.
5760 * loop_id = loop id of device to login to.
5761 *
5762 * Returns (Where's the #define!!!!):
5763 * 0 - Login successfully
5764 * 1 - Login failed
5765 * 3 - Fatal error
5766 */
5767int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005768qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769{
5770 int rval;
5771 uint16_t mb[MAILBOX_REGISTER_COUNT];
5772
5773 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005774 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 if (rval == QLA_SUCCESS) {
5776 /* Interrogate mailbox registers for any errors */
5777 if (mb[0] == MBS_COMMAND_ERROR)
5778 rval = 1;
5779 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5780 /* device not in PCB table */
5781 rval = 3;
5782 }
5783
5784 return (rval);
5785}
5786
5787/*
5788 * qla2x00_loop_resync
5789 * Resync with fibre channel devices.
5790 *
5791 * Input:
5792 * ha = adapter block pointer.
5793 *
5794 * Returns:
5795 * 0 = success
5796 */
5797int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005798qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005800 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005802 struct req_que *req;
5803 struct rsp_que *rsp;
5804
Michael Hernandezd7459522016-12-12 14:40:07 -08005805 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005806 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005808 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5809 if (vha->flags.online) {
5810 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5812 wait_time = 256;
5813 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005814 if (!IS_QLAFX00(vha->hw)) {
5815 /*
5816 * Issue a marker after FW becomes
5817 * ready.
5818 */
5819 qla2x00_marker(vha, req, rsp, 0, 0,
5820 MK_SYNC_ALL);
5821 vha->marker_needed = 0;
5822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
5824 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005825 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005827 if (IS_QLAFX00(vha->hw))
5828 qlafx00_configure_devices(vha);
5829 else
5830 qla2x00_configure_loop(vha);
5831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005833 } while (!atomic_read(&vha->loop_down_timer) &&
5834 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5835 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5836 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
5839
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005840 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005843 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005844 ql_dbg(ql_dbg_disc, vha, 0x206c,
5845 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
5847 return (rval);
5848}
5849
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005850/*
5851* qla2x00_perform_loop_resync
5852* Description: This function will set the appropriate flags and call
5853* qla2x00_loop_resync. If successful loop will be resynced
5854* Arguments : scsi_qla_host_t pointer
5855* returm : Success or Failure
5856*/
5857
5858int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5859{
5860 int32_t rval = 0;
5861
5862 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5863 /*Configure the flags so that resync happens properly*/
5864 atomic_set(&ha->loop_down_timer, 0);
5865 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5866 atomic_set(&ha->loop_state, LOOP_UP);
5867 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5868 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5869 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5870
5871 rval = qla2x00_loop_resync(ha);
5872 } else
5873 atomic_set(&ha->loop_state, LOOP_DEAD);
5874
5875 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5876 }
5877
5878 return rval;
5879}
5880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005882qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005883{
5884 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005885 struct scsi_qla_host *vha;
5886 struct qla_hw_data *ha = base_vha->hw;
5887 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005888
Arun Easifeafb7b2010-09-03 14:57:00 -07005889 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005890 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005891 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5892 atomic_inc(&vha->vref_count);
5893 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005894 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005895 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5896 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005897 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005898
Arun Easifeafb7b2010-09-03 14:57:00 -07005899 spin_lock_irqsave(&ha->vport_slock, flags);
5900 }
5901 }
5902 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005903 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005904 }
5905 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005906}
5907
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005908/* Assumes idc_lock always held on entry */
5909void
5910qla83xx_reset_ownership(scsi_qla_host_t *vha)
5911{
5912 struct qla_hw_data *ha = vha->hw;
5913 uint32_t drv_presence, drv_presence_mask;
5914 uint32_t dev_part_info1, dev_part_info2, class_type;
5915 uint32_t class_type_mask = 0x3;
5916 uint16_t fcoe_other_function = 0xffff, i;
5917
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005918 if (IS_QLA8044(ha)) {
5919 drv_presence = qla8044_rd_direct(vha,
5920 QLA8044_CRB_DRV_ACTIVE_INDEX);
5921 dev_part_info1 = qla8044_rd_direct(vha,
5922 QLA8044_CRB_DEV_PART_INFO_INDEX);
5923 dev_part_info2 = qla8044_rd_direct(vha,
5924 QLA8044_CRB_DEV_PART_INFO2);
5925 } else {
5926 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5927 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5928 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5929 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005930 for (i = 0; i < 8; i++) {
5931 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5932 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5933 (i != ha->portnum)) {
5934 fcoe_other_function = i;
5935 break;
5936 }
5937 }
5938 if (fcoe_other_function == 0xffff) {
5939 for (i = 0; i < 8; i++) {
5940 class_type = ((dev_part_info2 >> (i * 4)) &
5941 class_type_mask);
5942 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5943 ((i + 8) != ha->portnum)) {
5944 fcoe_other_function = i + 8;
5945 break;
5946 }
5947 }
5948 }
5949 /*
5950 * Prepare drv-presence mask based on fcoe functions present.
5951 * However consider only valid physical fcoe function numbers (0-15).
5952 */
5953 drv_presence_mask = ~((1 << (ha->portnum)) |
5954 ((fcoe_other_function == 0xffff) ?
5955 0 : (1 << (fcoe_other_function))));
5956
5957 /* We are the reset owner iff:
5958 * - No other protocol drivers present.
5959 * - This is the lowest among fcoe functions. */
5960 if (!(drv_presence & drv_presence_mask) &&
5961 (ha->portnum < fcoe_other_function)) {
5962 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5963 "This host is Reset owner.\n");
5964 ha->flags.nic_core_reset_owner = 1;
5965 }
5966}
5967
Saurav Kashyapfa492632012-11-21 02:40:29 -05005968static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005969__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5970{
5971 int rval = QLA_SUCCESS;
5972 struct qla_hw_data *ha = vha->hw;
5973 uint32_t drv_ack;
5974
5975 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5976 if (rval == QLA_SUCCESS) {
5977 drv_ack |= (1 << ha->portnum);
5978 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5979 }
5980
5981 return rval;
5982}
5983
Saurav Kashyapfa492632012-11-21 02:40:29 -05005984static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005985__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5986{
5987 int rval = QLA_SUCCESS;
5988 struct qla_hw_data *ha = vha->hw;
5989 uint32_t drv_ack;
5990
5991 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5992 if (rval == QLA_SUCCESS) {
5993 drv_ack &= ~(1 << ha->portnum);
5994 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5995 }
5996
5997 return rval;
5998}
5999
Saurav Kashyapfa492632012-11-21 02:40:29 -05006000static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006001qla83xx_dev_state_to_string(uint32_t dev_state)
6002{
6003 switch (dev_state) {
6004 case QLA8XXX_DEV_COLD:
6005 return "COLD/RE-INIT";
6006 case QLA8XXX_DEV_INITIALIZING:
6007 return "INITIALIZING";
6008 case QLA8XXX_DEV_READY:
6009 return "READY";
6010 case QLA8XXX_DEV_NEED_RESET:
6011 return "NEED RESET";
6012 case QLA8XXX_DEV_NEED_QUIESCENT:
6013 return "NEED QUIESCENT";
6014 case QLA8XXX_DEV_FAILED:
6015 return "FAILED";
6016 case QLA8XXX_DEV_QUIESCENT:
6017 return "QUIESCENT";
6018 default:
6019 return "Unknown";
6020 }
6021}
6022
6023/* Assumes idc-lock always held on entry */
6024void
6025qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6026{
6027 struct qla_hw_data *ha = vha->hw;
6028 uint32_t idc_audit_reg = 0, duration_secs = 0;
6029
6030 switch (audit_type) {
6031 case IDC_AUDIT_TIMESTAMP:
6032 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6033 idc_audit_reg = (ha->portnum) |
6034 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6035 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6036 break;
6037
6038 case IDC_AUDIT_COMPLETION:
6039 duration_secs = ((jiffies_to_msecs(jiffies) -
6040 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6041 idc_audit_reg = (ha->portnum) |
6042 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6043 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6044 break;
6045
6046 default:
6047 ql_log(ql_log_warn, vha, 0xb078,
6048 "Invalid audit type specified.\n");
6049 break;
6050 }
6051}
6052
6053/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006054static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006055qla83xx_initiating_reset(scsi_qla_host_t *vha)
6056{
6057 struct qla_hw_data *ha = vha->hw;
6058 uint32_t idc_control, dev_state;
6059
6060 __qla83xx_get_idc_control(vha, &idc_control);
6061 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6062 ql_log(ql_log_info, vha, 0xb080,
6063 "NIC Core reset has been disabled. idc-control=0x%x\n",
6064 idc_control);
6065 return QLA_FUNCTION_FAILED;
6066 }
6067
6068 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6069 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6070 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6071 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6072 QLA8XXX_DEV_NEED_RESET);
6073 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6074 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6075 } else {
6076 const char *state = qla83xx_dev_state_to_string(dev_state);
6077 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6078
6079 /* SV: XXX: Is timeout required here? */
6080 /* Wait for IDC state change READY -> NEED_RESET */
6081 while (dev_state == QLA8XXX_DEV_READY) {
6082 qla83xx_idc_unlock(vha, 0);
6083 msleep(200);
6084 qla83xx_idc_lock(vha, 0);
6085 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6086 }
6087 }
6088
6089 /* Send IDC ack by writing to drv-ack register */
6090 __qla83xx_set_drv_ack(vha);
6091
6092 return QLA_SUCCESS;
6093}
6094
6095int
6096__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6097{
6098 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6099}
6100
6101int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006102__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6103{
6104 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6105}
6106
Saurav Kashyapfa492632012-11-21 02:40:29 -05006107static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006108qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6109{
6110 uint32_t drv_presence = 0;
6111 struct qla_hw_data *ha = vha->hw;
6112
6113 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6114 if (drv_presence & (1 << ha->portnum))
6115 return QLA_SUCCESS;
6116 else
6117 return QLA_TEST_FAILED;
6118}
6119
6120int
6121qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6122{
6123 int rval = QLA_SUCCESS;
6124 struct qla_hw_data *ha = vha->hw;
6125
6126 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6127 "Entered %s().\n", __func__);
6128
6129 if (vha->device_flags & DFLG_DEV_FAILED) {
6130 ql_log(ql_log_warn, vha, 0xb059,
6131 "Device in unrecoverable FAILED state.\n");
6132 return QLA_FUNCTION_FAILED;
6133 }
6134
6135 qla83xx_idc_lock(vha, 0);
6136
6137 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6138 ql_log(ql_log_warn, vha, 0xb05a,
6139 "Function=0x%x has been removed from IDC participation.\n",
6140 ha->portnum);
6141 rval = QLA_FUNCTION_FAILED;
6142 goto exit;
6143 }
6144
6145 qla83xx_reset_ownership(vha);
6146
6147 rval = qla83xx_initiating_reset(vha);
6148
6149 /*
6150 * Perform reset if we are the reset-owner,
6151 * else wait till IDC state changes to READY/FAILED.
6152 */
6153 if (rval == QLA_SUCCESS) {
6154 rval = qla83xx_idc_state_handler(vha);
6155
6156 if (rval == QLA_SUCCESS)
6157 ha->flags.nic_core_hung = 0;
6158 __qla83xx_clear_drv_ack(vha);
6159 }
6160
6161exit:
6162 qla83xx_idc_unlock(vha, 0);
6163
6164 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6165
6166 return rval;
6167}
6168
Saurav Kashyap81178772012-08-22 14:21:04 -04006169int
6170qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6171{
6172 struct qla_hw_data *ha = vha->hw;
6173 int rval = QLA_FUNCTION_FAILED;
6174
6175 if (!IS_MCTP_CAPABLE(ha)) {
6176 /* This message can be removed from the final version */
6177 ql_log(ql_log_info, vha, 0x506d,
6178 "This board is not MCTP capable\n");
6179 return rval;
6180 }
6181
6182 if (!ha->mctp_dump) {
6183 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6184 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6185
6186 if (!ha->mctp_dump) {
6187 ql_log(ql_log_warn, vha, 0x506e,
6188 "Failed to allocate memory for mctp dump\n");
6189 return rval;
6190 }
6191 }
6192
6193#define MCTP_DUMP_STR_ADDR 0x00000000
6194 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6195 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6196 if (rval != QLA_SUCCESS) {
6197 ql_log(ql_log_warn, vha, 0x506f,
6198 "Failed to capture mctp dump\n");
6199 } else {
6200 ql_log(ql_log_info, vha, 0x5070,
6201 "Mctp dump capture for host (%ld/%p).\n",
6202 vha->host_no, ha->mctp_dump);
6203 ha->mctp_dumped = 1;
6204 }
6205
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006206 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006207 ha->flags.nic_core_reset_hdlr_active = 1;
6208 rval = qla83xx_restart_nic_firmware(vha);
6209 if (rval)
6210 /* NIC Core reset failed. */
6211 ql_log(ql_log_warn, vha, 0x5071,
6212 "Failed to restart nic firmware\n");
6213 else
6214 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6215 "Restarted NIC firmware successfully.\n");
6216 ha->flags.nic_core_reset_hdlr_active = 0;
6217 }
6218
6219 return rval;
6220
6221}
6222
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006223/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006224* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006225* Description: This function will block the new I/Os
6226* Its not aborting any I/Os as context
6227* is not destroyed during quiescence
6228* Arguments: scsi_qla_host_t
6229* return : void
6230*/
6231void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006232qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006233{
6234 struct qla_hw_data *ha = vha->hw;
6235 struct scsi_qla_host *vp;
6236
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006237 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6238 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006239
6240 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6241 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6242 atomic_set(&vha->loop_state, LOOP_DOWN);
6243 qla2x00_mark_all_devices_lost(vha, 0);
6244 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006245 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006246 } else {
6247 if (!atomic_read(&vha->loop_down_timer))
6248 atomic_set(&vha->loop_down_timer,
6249 LOOP_DOWN_TIME);
6250 }
6251 /* Wait for pending cmds to complete */
6252 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6253}
6254
Giridhar Malavalia9083012010-04-12 17:59:55 -07006255void
6256qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6257{
6258 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006259 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006260 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006261 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006262 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006263
Saurav Kashyape46ef002011-02-23 15:27:16 -08006264 /* For ISP82XX, driver waits for completion of the commands.
6265 * online flag should be set.
6266 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006267 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006268 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006269 ha->flags.chip_reset_done = 0;
6270 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006271 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006272
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006273 ql_log(ql_log_info, vha, 0x00af,
6274 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006275
Saurav Kashyape46ef002011-02-23 15:27:16 -08006276 /* For ISP82XX, reset_chip is just disabling interrupts.
6277 * Driver waits for the completion of the commands.
6278 * the interrupts need to be enabled.
6279 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006280 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006281 ha->isp_ops->reset_chip(vha);
6282
Quinn Tran9cd883f2017-12-28 12:33:24 -08006283 SAVE_TOPO(ha);
6284 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006285 ha->flags.n2n_ae = 0;
6286 ha->flags.lip_ae = 0;
6287 ha->current_topology = 0;
6288 ha->flags.fw_started = 0;
6289 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006290 ha->base_qpair->chip_reset++;
6291 for (i = 0; i < ha->max_qpairs; i++) {
6292 if (ha->queue_pair_map[i])
6293 ha->queue_pair_map[i]->chip_reset =
6294 ha->base_qpair->chip_reset;
6295 }
Quinn Tran726b8542017-01-19 22:28:00 -08006296
Giridhar Malavalia9083012010-04-12 17:59:55 -07006297 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6298 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6299 atomic_set(&vha->loop_state, LOOP_DOWN);
6300 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006301
6302 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006303 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006304 atomic_inc(&vp->vref_count);
6305 spin_unlock_irqrestore(&ha->vport_slock, flags);
6306
Giridhar Malavalia9083012010-04-12 17:59:55 -07006307 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006308
6309 spin_lock_irqsave(&ha->vport_slock, flags);
6310 atomic_dec(&vp->vref_count);
6311 }
6312 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006313 } else {
6314 if (!atomic_read(&vha->loop_down_timer))
6315 atomic_set(&vha->loop_down_timer,
6316 LOOP_DOWN_TIME);
6317 }
6318
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006319 /* Clear all async request states across all VPs. */
6320 list_for_each_entry(fcport, &vha->vp_fcports, list)
6321 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6322 spin_lock_irqsave(&ha->vport_slock, flags);
6323 list_for_each_entry(vp, &ha->vp_list, list) {
6324 atomic_inc(&vp->vref_count);
6325 spin_unlock_irqrestore(&ha->vport_slock, flags);
6326
6327 list_for_each_entry(fcport, &vp->vp_fcports, list)
6328 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6329
6330 spin_lock_irqsave(&ha->vport_slock, flags);
6331 atomic_dec(&vp->vref_count);
6332 }
6333 spin_unlock_irqrestore(&ha->vport_slock, flags);
6334
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006335 if (!ha->flags.eeh_busy) {
6336 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006337 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006338 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006339 ql_log(ql_log_info, vha, 0x00b4,
6340 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006341
Saurav Kashyape46ef002011-02-23 15:27:16 -08006342 /* Done waiting for pending commands.
6343 * Reset the online flag.
6344 */
6345 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006348 /* Requeue all commands in outstanding command list. */
6349 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6350 }
Arun Easib6a029e2014-09-25 06:14:52 -04006351 /* memory barrier */
6352 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353}
6354
6355/*
6356* qla2x00_abort_isp
6357* Resets ISP and aborts all outstanding commands.
6358*
6359* Input:
6360* ha = adapter block pointer.
6361*
6362* Returns:
6363* 0 = success
6364*/
6365int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006366qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006368 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006370 struct qla_hw_data *ha = vha->hw;
6371 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006372 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006373 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006375 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006376 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Santosh Vernekara61712972012-08-22 14:21:13 -04006378 if (IS_QLA8031(ha)) {
6379 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6380 "Clearing fcoe driver presence.\n");
6381 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6382 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6383 "Error while clearing DRV-Presence.\n");
6384 }
6385
Andrew Vasquez85880802009-12-15 21:29:46 -08006386 if (unlikely(pci_channel_offline(ha->pdev) &&
6387 ha->flags.pci_channel_io_perm_failure)) {
6388 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6389 status = 0;
6390 return status;
6391 }
6392
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006393 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006394
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006395 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006397 if (!qla2x00_restart_isp(vha)) {
6398 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006400 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 /*
6402 * Issue marker command only when we are going
6403 * to start the I/O .
6404 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006405 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 }
6407
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006408 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006410 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006412 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006413 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006414
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006415 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6416 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006417 if (ha->fce) {
6418 ha->flags.fce_enabled = 1;
6419 memset(ha->fce, 0,
6420 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006421 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006422 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6423 &ha->fce_bufs);
6424 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006425 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006426 "Unable to reinitialize FCE "
6427 "(%d).\n", rval);
6428 ha->flags.fce_enabled = 0;
6429 }
6430 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006431
6432 if (ha->eft) {
6433 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006434 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006435 ha->eft_dma, EFT_NUM_BUFFERS);
6436 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006437 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006438 "Unable to reinitialize EFT "
6439 "(%d).\n", rval);
6440 }
6441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006443 vha->flags.online = 1;
6444 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006446 ql_log(ql_log_fatal, vha, 0x8035,
6447 "ISP error recover failed - "
6448 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006449 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 * The next call disables the board
6451 * completely.
6452 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006453 ha->isp_ops->reset_adapter(vha);
6454 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006456 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 status = 0;
6458 } else { /* schedule another ISP abort */
6459 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006460 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6461 "ISP abort - retry remaining %d.\n",
6462 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 status = 1;
6464 }
6465 } else {
6466 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006467 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6468 "ISP error recovery - retrying (%d) "
6469 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006470 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 status = 1;
6472 }
6473 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006474
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 }
6476
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006477 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006478 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006479 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006480 spin_lock_irqsave(&ha->vport_slock, flags);
6481 list_for_each_entry(vp, &ha->vp_list, list) {
6482 if (vp->vp_idx) {
6483 atomic_inc(&vp->vref_count);
6484 spin_unlock_irqrestore(&ha->vport_slock, flags);
6485
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006486 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006487
6488 spin_lock_irqsave(&ha->vport_slock, flags);
6489 atomic_dec(&vp->vref_count);
6490 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006491 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006492 spin_unlock_irqrestore(&ha->vport_slock, flags);
6493
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006494 if (IS_QLA8031(ha)) {
6495 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6496 "Setting back fcoe driver presence.\n");
6497 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6498 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6499 "Error while setting DRV-Presence.\n");
6500 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006501 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006502 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6503 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 }
6505
6506 return(status);
6507}
6508
6509/*
6510* qla2x00_restart_isp
6511* restarts the ISP after a reset
6512*
6513* Input:
6514* ha = adapter block pointer.
6515*
6516* Returns:
6517* 0 = success
6518*/
6519static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006520qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006522 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006523 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006524 struct req_que *req = ha->req_q_map[0];
6525 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
6527 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006528 if (qla2x00_isp_firmware(vha)) {
6529 vha->flags.online = 0;
6530 status = ha->isp_ops->chip_diag(vha);
6531 if (!status)
6532 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 }
6534
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006535 if (!status && !(status = qla2x00_init_rings(vha))) {
6536 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006537 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006538
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006539 /* Initialize the queues in use */
6540 qla25xx_init_queues(ha);
6541
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006542 status = qla2x00_fw_ready(vha);
6543 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006544 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006545 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006546 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 }
6548
6549 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006550 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 }
6553 return (status);
6554}
6555
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006556static int
6557qla25xx_init_queues(struct qla_hw_data *ha)
6558{
6559 struct rsp_que *rsp = NULL;
6560 struct req_que *req = NULL;
6561 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6562 int ret = -1;
6563 int i;
6564
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006565 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006566 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006567 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006568 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006569 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006570 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006571 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6572 "%s Rsp que: %d init failed.\n",
6573 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006574 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006575 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6576 "%s Rsp que: %d inited.\n",
6577 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006578 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006579 }
6580 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006581 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006582 if (req && test_bit(i, ha->req_qid_map)) {
6583 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006584 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006585 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006586 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006587 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6588 "%s Req que: %d init failed.\n",
6589 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006590 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006591 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6592 "%s Req que: %d inited.\n",
6593 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006594 }
6595 }
6596 return ret;
6597}
6598
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599/*
6600* qla2x00_reset_adapter
6601* Reset adapter.
6602*
6603* Input:
6604* ha = adapter block pointer.
6605*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006606void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006607qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608{
6609 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006610 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006611 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006613 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006614 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 spin_lock_irqsave(&ha->hardware_lock, flags);
6617 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6618 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6619 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6620 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6621 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6622}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006623
6624void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006625qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006626{
6627 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006628 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006629 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6630
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006631 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006632 return;
6633
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006634 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006635 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006636
6637 spin_lock_irqsave(&ha->hardware_lock, flags);
6638 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6639 RD_REG_DWORD(&reg->hccr);
6640 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6641 RD_REG_DWORD(&reg->hccr);
6642 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006643
6644 if (IS_NOPOLLING_TYPE(ha))
6645 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006646}
6647
David Miller4e08df32007-04-16 12:37:43 -07006648/* On sparc systems, obtain port and node WWN from firmware
6649 * properties.
6650 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006651static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6652 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006653{
6654#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006655 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006656 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006657 struct device_node *dp = pci_device_to_OF_node(pdev);
6658 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006659 int len;
6660
6661 val = of_get_property(dp, "port-wwn", &len);
6662 if (val && len >= WWN_SIZE)
6663 memcpy(nv->port_name, val, WWN_SIZE);
6664
6665 val = of_get_property(dp, "node-wwn", &len);
6666 if (val && len >= WWN_SIZE)
6667 memcpy(nv->node_name, val, WWN_SIZE);
6668#endif
6669}
6670
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006671int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006672qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006673{
David Miller4e08df32007-04-16 12:37:43 -07006674 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006675 struct init_cb_24xx *icb;
6676 struct nvram_24xx *nv;
6677 uint32_t *dptr;
6678 uint8_t *dptr1, *dptr2;
6679 uint32_t chksum;
6680 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006681 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006682
David Miller4e08df32007-04-16 12:37:43 -07006683 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006684 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006685 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006686
6687 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006688 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006689 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6690 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6691 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006692 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006693 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6694 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006695
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006696 ha->nvram_size = sizeof(struct nvram_24xx);
6697 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006698
Seokmann Ju281afe12007-07-26 13:43:34 -07006699 /* Get VPD data into cache */
6700 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006701 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006702 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6703
6704 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006705 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006706 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006707 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006708 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6709 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006710
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006711 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6712 "Contents of NVRAM\n");
6713 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6714 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006715
6716 /* Bad NVRAM data, set defaults parameters. */
6717 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6718 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006719 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006720 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006721 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006722 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006723 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6724 ql_log(ql_log_warn, vha, 0x006c,
6725 "Falling back to functioning (yet invalid -- WWPN) "
6726 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006727
6728 /*
6729 * Set default initialization control block.
6730 */
6731 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006732 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6733 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006734 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006735 nv->execution_throttle = cpu_to_le16(0xFFFF);
6736 nv->exchange_count = cpu_to_le16(0);
6737 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006738 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006739 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006740 nv->port_name[2] = 0x00;
6741 nv->port_name[3] = 0xe0;
6742 nv->port_name[4] = 0x8b;
6743 nv->port_name[5] = 0x1c;
6744 nv->port_name[6] = 0x55;
6745 nv->port_name[7] = 0x86;
6746 nv->node_name[0] = 0x20;
6747 nv->node_name[1] = 0x00;
6748 nv->node_name[2] = 0x00;
6749 nv->node_name[3] = 0xe0;
6750 nv->node_name[4] = 0x8b;
6751 nv->node_name[5] = 0x1c;
6752 nv->node_name[6] = 0x55;
6753 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006754 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006755 nv->login_retry_count = cpu_to_le16(8);
6756 nv->interrupt_delay_timer = cpu_to_le16(0);
6757 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006758 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006759 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6760 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6761 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6762 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6763 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6764 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006765 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006766 nv->max_luns_per_target = cpu_to_le16(128);
6767 nv->port_down_retry_count = cpu_to_le16(30);
6768 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006769
6770 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006771 }
6772
Quinn Tran726b8542017-01-19 22:28:00 -08006773 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006774 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006775 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006776 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006777 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006778 }
6779
6780 qlt_24xx_config_nvram_stage1(vha, nv);
6781
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006782 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006783 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006784
6785 /* Copy 1st segment. */
6786 dptr1 = (uint8_t *)icb;
6787 dptr2 = (uint8_t *)&nv->version;
6788 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6789 while (cnt--)
6790 *dptr1++ = *dptr2++;
6791
6792 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006793 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006794
6795 /* Copy 2nd segment. */
6796 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6797 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6798 cnt = (uint8_t *)&icb->reserved_3 -
6799 (uint8_t *)&icb->interrupt_delay_timer;
6800 while (cnt--)
6801 *dptr1++ = *dptr2++;
6802
6803 /*
6804 * Setup driver NVRAM options.
6805 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006806 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006807 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006808
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006809 qlt_24xx_config_nvram_stage2(vha, icb);
6810
Bart Van Asschead950362015-07-09 07:24:08 -07006811 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006812 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006813 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6814 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6815 }
6816
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006817 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006818 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006819 /*
6820 * Firmware will apply the following mask if the nodename was
6821 * not provided.
6822 */
6823 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6824 icb->node_name[0] &= 0xF0;
6825 }
6826
6827 /* Set host adapter parameters. */
6828 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006829 ha->flags.enable_lip_reset = 0;
6830 ha->flags.enable_lip_full_login =
6831 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6832 ha->flags.enable_target_reset =
6833 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006834 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006835 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006836
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006837 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6838 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006839
6840 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6841 sizeof(ha->fw_seriallink_options24));
6842
6843 /* save HBA serial number */
6844 ha->serial0 = icb->port_name[5];
6845 ha->serial1 = icb->port_name[6];
6846 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006847 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6848 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006849
Bart Van Asschead950362015-07-09 07:24:08 -07006850 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006851
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006852 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6853
6854 /* Set minimum login_timeout to 4 seconds. */
6855 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6856 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6857 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006858 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006859 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006860
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006861 /* Set minimum RATOV to 100 tenths of a second. */
6862 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006863
6864 ha->loop_reset_delay = nv->reset_delay;
6865
6866 /* Link Down Timeout = 0:
6867 *
6868 * When Port Down timer expires we will start returning
6869 * I/O's to OS with "DID_NO_CONNECT".
6870 *
6871 * Link Down Timeout != 0:
6872 *
6873 * The driver waits for the link to come up after link down
6874 * before returning I/Os to OS with "DID_NO_CONNECT".
6875 */
6876 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6877 ha->loop_down_abort_time =
6878 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6879 } else {
6880 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6881 ha->loop_down_abort_time =
6882 (LOOP_DOWN_TIME - ha->link_down_timeout);
6883 }
6884
6885 /* Need enough time to try and get the port back. */
6886 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6887 if (qlport_down_retry)
6888 ha->port_down_retry_count = qlport_down_retry;
6889
6890 /* Set login_retry_count */
6891 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6892 if (ha->port_down_retry_count ==
6893 le16_to_cpu(nv->port_down_retry_count) &&
6894 ha->port_down_retry_count > 3)
6895 ha->login_retry_count = ha->port_down_retry_count;
6896 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6897 ha->login_retry_count = ha->port_down_retry_count;
6898 if (ql2xloginretrycount)
6899 ha->login_retry_count = ql2xloginretrycount;
6900
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006901 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006902 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006903 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6904 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6905 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6906 le16_to_cpu(icb->interrupt_delay_timer): 2;
6907 }
Bart Van Asschead950362015-07-09 07:24:08 -07006908 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006909 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006910 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006911 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006912 ha->zio_mode = QLA_ZIO_MODE_6;
6913
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006914 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006915 "ZIO mode %d enabled; timer delay (%d us).\n",
6916 ha->zio_mode, ha->zio_timer * 100);
6917
6918 icb->firmware_options_2 |= cpu_to_le32(
6919 (uint32_t)ha->zio_mode);
6920 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006921 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006922 }
6923
David Miller4e08df32007-04-16 12:37:43 -07006924 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006925 ql_log(ql_log_warn, vha, 0x0070,
6926 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006927 }
6928 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006929}
6930
Sawan Chandak4243c112016-01-27 12:03:31 -05006931uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6932{
6933 struct qla27xx_image_status pri_image_status, sec_image_status;
6934 uint8_t valid_pri_image, valid_sec_image;
6935 uint32_t *wptr;
6936 uint32_t cnt, chksum, size;
6937 struct qla_hw_data *ha = vha->hw;
6938
6939 valid_pri_image = valid_sec_image = 1;
6940 ha->active_image = 0;
6941 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6942
6943 if (!ha->flt_region_img_status_pri) {
6944 valid_pri_image = 0;
6945 goto check_sec_image;
6946 }
6947
6948 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6949 ha->flt_region_img_status_pri, size);
6950
6951 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6952 ql_dbg(ql_dbg_init, vha, 0x018b,
6953 "Primary image signature (0x%x) not valid\n",
6954 pri_image_status.signature);
6955 valid_pri_image = 0;
6956 goto check_sec_image;
6957 }
6958
6959 wptr = (uint32_t *)(&pri_image_status);
6960 cnt = size;
6961
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006962 for (chksum = 0; cnt--; wptr++)
6963 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006964
Sawan Chandak4243c112016-01-27 12:03:31 -05006965 if (chksum) {
6966 ql_dbg(ql_dbg_init, vha, 0x018c,
6967 "Checksum validation failed for primary image (0x%x)\n",
6968 chksum);
6969 valid_pri_image = 0;
6970 }
6971
6972check_sec_image:
6973 if (!ha->flt_region_img_status_sec) {
6974 valid_sec_image = 0;
6975 goto check_valid_image;
6976 }
6977
6978 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6979 ha->flt_region_img_status_sec, size);
6980
6981 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6982 ql_dbg(ql_dbg_init, vha, 0x018d,
6983 "Secondary image signature(0x%x) not valid\n",
6984 sec_image_status.signature);
6985 valid_sec_image = 0;
6986 goto check_valid_image;
6987 }
6988
6989 wptr = (uint32_t *)(&sec_image_status);
6990 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006991 for (chksum = 0; cnt--; wptr++)
6992 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05006993 if (chksum) {
6994 ql_dbg(ql_dbg_init, vha, 0x018e,
6995 "Checksum validation failed for secondary image (0x%x)\n",
6996 chksum);
6997 valid_sec_image = 0;
6998 }
6999
7000check_valid_image:
7001 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7002 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7003 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7004 if (!ha->active_image ||
7005 pri_image_status.generation_number <
7006 sec_image_status.generation_number)
7007 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7008 }
7009
7010 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7011 ha->active_image == 0 ? "default bootld and fw" :
7012 ha->active_image == 1 ? "primary" :
7013 ha->active_image == 2 ? "secondary" :
7014 "Invalid");
7015
7016 return ha->active_image;
7017}
7018
Adrian Bunk413975a2006-06-30 02:33:06 -07007019static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007020qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7021 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007022{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007023 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007024 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007025 uint32_t *dcode, dlen;
7026 uint32_t risc_addr;
7027 uint32_t risc_size;
7028 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007029 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007030 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007031
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007032 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007033 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007034
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007035 rval = QLA_SUCCESS;
7036
7037 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007038 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007039 *srisc_addr = 0;
7040
Sawan Chandak4243c112016-01-27 12:03:31 -05007041 if (IS_QLA27XX(ha) &&
7042 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7043 faddr = ha->flt_region_fw_sec;
7044
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007045 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007046 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007047 for (i = 0; i < 4; i++)
7048 dcode[i] = be32_to_cpu(dcode[i]);
7049 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7050 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7051 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7052 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007053 ql_log(ql_log_fatal, vha, 0x008c,
7054 "Unable to verify the integrity of flash firmware "
7055 "image.\n");
7056 ql_log(ql_log_fatal, vha, 0x008d,
7057 "Firmware data: %08x %08x %08x %08x.\n",
7058 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007059
7060 return QLA_FUNCTION_FAILED;
7061 }
7062
7063 while (segments && rval == QLA_SUCCESS) {
7064 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007065 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007066
7067 risc_addr = be32_to_cpu(dcode[2]);
7068 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7069 risc_size = be32_to_cpu(dcode[3]);
7070
7071 fragment = 0;
7072 while (risc_size > 0 && rval == QLA_SUCCESS) {
7073 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7074 if (dlen > risc_size)
7075 dlen = risc_size;
7076
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007077 ql_dbg(ql_dbg_init, vha, 0x008e,
7078 "Loading risc segment@ risc addr %x "
7079 "number of dwords 0x%x offset 0x%x.\n",
7080 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007081
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007082 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007083 for (i = 0; i < dlen; i++)
7084 dcode[i] = swab32(dcode[i]);
7085
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007086 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007087 dlen);
7088 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007089 ql_log(ql_log_fatal, vha, 0x008f,
7090 "Failed to load segment %d of firmware.\n",
7091 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007092 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007093 }
7094
7095 faddr += dlen;
7096 risc_addr += dlen;
7097 risc_size -= dlen;
7098 fragment++;
7099 }
7100
7101 /* Next segment. */
7102 segments--;
7103 }
7104
Chad Dupuisf73cb692014-02-26 04:15:06 -05007105 if (!IS_QLA27XX(ha))
7106 return rval;
7107
7108 if (ha->fw_dump_template)
7109 vfree(ha->fw_dump_template);
7110 ha->fw_dump_template = NULL;
7111 ha->fw_dump_template_len = 0;
7112
7113 ql_dbg(ql_dbg_init, vha, 0x0161,
7114 "Loading fwdump template from %x\n", faddr);
7115 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7116 risc_size = be32_to_cpu(dcode[2]);
7117 ql_dbg(ql_dbg_init, vha, 0x0162,
7118 "-> array size %x dwords\n", risc_size);
7119 if (risc_size == 0 || risc_size == ~0)
7120 goto default_template;
7121
7122 dlen = (risc_size - 8) * sizeof(*dcode);
7123 ql_dbg(ql_dbg_init, vha, 0x0163,
7124 "-> template allocating %x bytes...\n", dlen);
7125 ha->fw_dump_template = vmalloc(dlen);
7126 if (!ha->fw_dump_template) {
7127 ql_log(ql_log_warn, vha, 0x0164,
7128 "Failed fwdump template allocate %x bytes.\n", risc_size);
7129 goto default_template;
7130 }
7131
7132 faddr += 7;
7133 risc_size -= 8;
7134 dcode = ha->fw_dump_template;
7135 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7136 for (i = 0; i < risc_size; i++)
7137 dcode[i] = le32_to_cpu(dcode[i]);
7138
7139 if (!qla27xx_fwdt_template_valid(dcode)) {
7140 ql_log(ql_log_warn, vha, 0x0165,
7141 "Failed fwdump template validate\n");
7142 goto default_template;
7143 }
7144
7145 dlen = qla27xx_fwdt_template_size(dcode);
7146 ql_dbg(ql_dbg_init, vha, 0x0166,
7147 "-> template size %x bytes\n", dlen);
7148 if (dlen > risc_size * sizeof(*dcode)) {
7149 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007150 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007151 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007152 goto default_template;
7153 }
7154 ha->fw_dump_template_len = dlen;
7155 return rval;
7156
7157default_template:
7158 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7159 if (ha->fw_dump_template)
7160 vfree(ha->fw_dump_template);
7161 ha->fw_dump_template = NULL;
7162 ha->fw_dump_template_len = 0;
7163
7164 dlen = qla27xx_fwdt_template_default_size();
7165 ql_dbg(ql_dbg_init, vha, 0x0169,
7166 "-> template allocating %x bytes...\n", dlen);
7167 ha->fw_dump_template = vmalloc(dlen);
7168 if (!ha->fw_dump_template) {
7169 ql_log(ql_log_warn, vha, 0x016a,
7170 "Failed fwdump template allocate %x bytes.\n", risc_size);
7171 goto failed_template;
7172 }
7173
7174 dcode = ha->fw_dump_template;
7175 risc_size = dlen / sizeof(*dcode);
7176 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7177 for (i = 0; i < risc_size; i++)
7178 dcode[i] = be32_to_cpu(dcode[i]);
7179
7180 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7181 ql_log(ql_log_warn, vha, 0x016b,
7182 "Failed fwdump template validate\n");
7183 goto failed_template;
7184 }
7185
7186 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7187 ql_dbg(ql_dbg_init, vha, 0x016c,
7188 "-> template size %x bytes\n", dlen);
7189 ha->fw_dump_template_len = dlen;
7190 return rval;
7191
7192failed_template:
7193 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7194 if (ha->fw_dump_template)
7195 vfree(ha->fw_dump_template);
7196 ha->fw_dump_template = NULL;
7197 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007198 return rval;
7199}
7200
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007201#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007202
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007203int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007204qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007205{
7206 int rval;
7207 int i, fragment;
7208 uint16_t *wcode, *fwcode;
7209 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7210 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007211 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007212 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007213
7214 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007215 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007216 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007217 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007218 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007219 ql_log(ql_log_info, vha, 0x0084,
7220 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007221 return QLA_FUNCTION_FAILED;
7222 }
7223
7224 rval = QLA_SUCCESS;
7225
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007226 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007227 *srisc_addr = 0;
7228 fwcode = (uint16_t *)blob->fw->data;
7229 fwclen = 0;
7230
7231 /* Validate firmware image by checking version. */
7232 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007233 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007234 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007235 blob->fw->size);
7236 goto fail_fw_integrity;
7237 }
7238 for (i = 0; i < 4; i++)
7239 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7240 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7241 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7242 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007243 ql_log(ql_log_fatal, vha, 0x0086,
7244 "Unable to verify integrity of firmware image.\n");
7245 ql_log(ql_log_fatal, vha, 0x0087,
7246 "Firmware data: %04x %04x %04x %04x.\n",
7247 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007248 goto fail_fw_integrity;
7249 }
7250
7251 seg = blob->segs;
7252 while (*seg && rval == QLA_SUCCESS) {
7253 risc_addr = *seg;
7254 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7255 risc_size = be16_to_cpu(fwcode[3]);
7256
7257 /* Validate firmware image size. */
7258 fwclen += risc_size * sizeof(uint16_t);
7259 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007260 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007261 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007262 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007263 goto fail_fw_integrity;
7264 }
7265
7266 fragment = 0;
7267 while (risc_size > 0 && rval == QLA_SUCCESS) {
7268 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7269 if (wlen > risc_size)
7270 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007271 ql_dbg(ql_dbg_init, vha, 0x0089,
7272 "Loading risc segment@ risc addr %x number of "
7273 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007274
7275 for (i = 0; i < wlen; i++)
7276 wcode[i] = swab16(fwcode[i]);
7277
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007278 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007279 wlen);
7280 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007281 ql_log(ql_log_fatal, vha, 0x008a,
7282 "Failed to load segment %d of firmware.\n",
7283 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007284 break;
7285 }
7286
7287 fwcode += wlen;
7288 risc_addr += wlen;
7289 risc_size -= wlen;
7290 fragment++;
7291 }
7292
7293 /* Next segment. */
7294 seg++;
7295 }
7296 return rval;
7297
7298fail_fw_integrity:
7299 return QLA_FUNCTION_FAILED;
7300}
7301
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007302static int
7303qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007304{
7305 int rval;
7306 int segments, fragment;
7307 uint32_t *dcode, dlen;
7308 uint32_t risc_addr;
7309 uint32_t risc_size;
7310 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007311 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007312 const uint32_t *fwcode;
7313 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007314 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007315 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007316
Andrew Vasquez54333832005-11-09 15:49:04 -08007317 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007318 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007319 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007320 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007321 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007322 ql_log(ql_log_warn, vha, 0x0091,
7323 "Firmware images can be retrieved from: "
7324 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007325
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007326 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007327 }
7328
Chad Dupuiscfb09192011-11-18 09:03:07 -08007329 ql_dbg(ql_dbg_init, vha, 0x0092,
7330 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007331
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007332 rval = QLA_SUCCESS;
7333
7334 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007335 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007336 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007337 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007338 fwclen = 0;
7339
7340 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007341 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007342 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007343 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007344 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007345 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007346 }
7347 for (i = 0; i < 4; i++)
7348 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7349 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7350 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7351 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7352 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007353 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007354 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007355 blob->fw->size);
7356 ql_log(ql_log_fatal, vha, 0x0095,
7357 "Firmware data: %08x %08x %08x %08x.\n",
7358 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007359 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007360 }
7361
7362 while (segments && rval == QLA_SUCCESS) {
7363 risc_addr = be32_to_cpu(fwcode[2]);
7364 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7365 risc_size = be32_to_cpu(fwcode[3]);
7366
7367 /* Validate firmware image size. */
7368 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007369 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007370 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007371 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007372 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007373 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007374 }
7375
7376 fragment = 0;
7377 while (risc_size > 0 && rval == QLA_SUCCESS) {
7378 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7379 if (dlen > risc_size)
7380 dlen = risc_size;
7381
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007382 ql_dbg(ql_dbg_init, vha, 0x0097,
7383 "Loading risc segment@ risc addr %x "
7384 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007385
7386 for (i = 0; i < dlen; i++)
7387 dcode[i] = swab32(fwcode[i]);
7388
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007389 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007390 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007391 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007392 ql_log(ql_log_fatal, vha, 0x0098,
7393 "Failed to load segment %d of firmware.\n",
7394 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007395 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007396 }
7397
7398 fwcode += dlen;
7399 risc_addr += dlen;
7400 risc_size -= dlen;
7401 fragment++;
7402 }
7403
7404 /* Next segment. */
7405 segments--;
7406 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007407
7408 if (!IS_QLA27XX(ha))
7409 return rval;
7410
7411 if (ha->fw_dump_template)
7412 vfree(ha->fw_dump_template);
7413 ha->fw_dump_template = NULL;
7414 ha->fw_dump_template_len = 0;
7415
7416 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007417 "Loading fwdump template from %x\n",
7418 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007419 risc_size = be32_to_cpu(fwcode[2]);
7420 ql_dbg(ql_dbg_init, vha, 0x172,
7421 "-> array size %x dwords\n", risc_size);
7422 if (risc_size == 0 || risc_size == ~0)
7423 goto default_template;
7424
7425 dlen = (risc_size - 8) * sizeof(*fwcode);
7426 ql_dbg(ql_dbg_init, vha, 0x0173,
7427 "-> template allocating %x bytes...\n", dlen);
7428 ha->fw_dump_template = vmalloc(dlen);
7429 if (!ha->fw_dump_template) {
7430 ql_log(ql_log_warn, vha, 0x0174,
7431 "Failed fwdump template allocate %x bytes.\n", risc_size);
7432 goto default_template;
7433 }
7434
7435 fwcode += 7;
7436 risc_size -= 8;
7437 dcode = ha->fw_dump_template;
7438 for (i = 0; i < risc_size; i++)
7439 dcode[i] = le32_to_cpu(fwcode[i]);
7440
7441 if (!qla27xx_fwdt_template_valid(dcode)) {
7442 ql_log(ql_log_warn, vha, 0x0175,
7443 "Failed fwdump template validate\n");
7444 goto default_template;
7445 }
7446
7447 dlen = qla27xx_fwdt_template_size(dcode);
7448 ql_dbg(ql_dbg_init, vha, 0x0176,
7449 "-> template size %x bytes\n", dlen);
7450 if (dlen > risc_size * sizeof(*fwcode)) {
7451 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007452 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007453 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007454 goto default_template;
7455 }
7456 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007457 return rval;
7458
Chad Dupuisf73cb692014-02-26 04:15:06 -05007459default_template:
7460 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7461 if (ha->fw_dump_template)
7462 vfree(ha->fw_dump_template);
7463 ha->fw_dump_template = NULL;
7464 ha->fw_dump_template_len = 0;
7465
7466 dlen = qla27xx_fwdt_template_default_size();
7467 ql_dbg(ql_dbg_init, vha, 0x0179,
7468 "-> template allocating %x bytes...\n", dlen);
7469 ha->fw_dump_template = vmalloc(dlen);
7470 if (!ha->fw_dump_template) {
7471 ql_log(ql_log_warn, vha, 0x017a,
7472 "Failed fwdump template allocate %x bytes.\n", risc_size);
7473 goto failed_template;
7474 }
7475
7476 dcode = ha->fw_dump_template;
7477 risc_size = dlen / sizeof(*fwcode);
7478 fwcode = qla27xx_fwdt_template_default();
7479 for (i = 0; i < risc_size; i++)
7480 dcode[i] = be32_to_cpu(fwcode[i]);
7481
7482 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7483 ql_log(ql_log_warn, vha, 0x017b,
7484 "Failed fwdump template validate\n");
7485 goto failed_template;
7486 }
7487
7488 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7489 ql_dbg(ql_dbg_init, vha, 0x017c,
7490 "-> template size %x bytes\n", dlen);
7491 ha->fw_dump_template_len = dlen;
7492 return rval;
7493
7494failed_template:
7495 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7496 if (ha->fw_dump_template)
7497 vfree(ha->fw_dump_template);
7498 ha->fw_dump_template = NULL;
7499 ha->fw_dump_template_len = 0;
7500 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007501}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007502
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007503int
7504qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7505{
7506 int rval;
7507
Andrew Vasqueze337d902009-04-06 22:33:49 -07007508 if (ql2xfwloadbin == 1)
7509 return qla81xx_load_risc(vha, srisc_addr);
7510
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007511 /*
7512 * FW Load priority:
7513 * 1) Firmware via request-firmware interface (.bin file).
7514 * 2) Firmware residing in flash.
7515 */
7516 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7517 if (rval == QLA_SUCCESS)
7518 return rval;
7519
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007520 return qla24xx_load_risc_flash(vha, srisc_addr,
7521 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007522}
7523
7524int
7525qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7526{
7527 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007528 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007529
Andrew Vasqueze337d902009-04-06 22:33:49 -07007530 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007531 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007532
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007533 /*
7534 * FW Load priority:
7535 * 1) Firmware residing in flash.
7536 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007537 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007538 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007539 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007540 if (rval == QLA_SUCCESS)
7541 return rval;
7542
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007543try_blob_fw:
7544 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7545 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7546 return rval;
7547
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007548 ql_log(ql_log_info, vha, 0x0099,
7549 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007550 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7551 if (rval != QLA_SUCCESS)
7552 return rval;
7553
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007554 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007555 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007556 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007557}
7558
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007559void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007560qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007561{
7562 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007563 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007564
Andrew Vasquez85880802009-12-15 21:29:46 -08007565 if (ha->flags.pci_channel_io_perm_failure)
7566 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007567 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007568 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007569 if (!ha->fw_major_version)
7570 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007571 if (!ha->flags.fw_started)
7572 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007573
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007574 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007575 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007576 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007577 ha->isp_ops->reset_chip(vha);
7578 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007579 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007580 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007581 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007582 ql_log(ql_log_info, vha, 0x8015,
7583 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007584 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007585 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007586
Quinn Tran4b60c822017-06-13 20:47:21 -07007587 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007588 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007589}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007590
7591int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007592qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007593{
7594 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007595 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007596 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007597 struct qla_hw_data *ha = vha->hw;
7598 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007599 struct req_que *req;
7600 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007601
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007602 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007603 return -EINVAL;
7604
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007605 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007606 if (vha->qpair)
7607 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007608 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007609 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007610 rsp = req->rsp;
7611
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007612 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007613 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007614 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007615 }
7616
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007617 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007618
7619 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007620 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7621 BIT_1);
7622 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7623 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7624 ql_dbg(ql_dbg_init, vha, 0x0120,
7625 "Failed SNS login: loop_id=%x, rval2=%d\n",
7626 NPH_SNS, rval2);
7627 else
7628 ql_dbg(ql_dbg_init, vha, 0x0103,
7629 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7630 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7631 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007632 return (QLA_FUNCTION_FAILED);
7633 }
7634
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007635 atomic_set(&vha->loop_down_timer, 0);
7636 atomic_set(&vha->loop_state, LOOP_UP);
7637 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7638 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7639 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007640
7641 return rval;
7642}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007643
7644/* 84XX Support **************************************************************/
7645
7646static LIST_HEAD(qla_cs84xx_list);
7647static DEFINE_MUTEX(qla_cs84xx_mutex);
7648
7649static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007650qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007651{
7652 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007653 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007654
7655 mutex_lock(&qla_cs84xx_mutex);
7656
7657 /* Find any shared 84xx chip. */
7658 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7659 if (cs84xx->bus == ha->pdev->bus) {
7660 kref_get(&cs84xx->kref);
7661 goto done;
7662 }
7663 }
7664
7665 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7666 if (!cs84xx)
7667 goto done;
7668
7669 kref_init(&cs84xx->kref);
7670 spin_lock_init(&cs84xx->access_lock);
7671 mutex_init(&cs84xx->fw_update_mutex);
7672 cs84xx->bus = ha->pdev->bus;
7673
7674 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7675done:
7676 mutex_unlock(&qla_cs84xx_mutex);
7677 return cs84xx;
7678}
7679
7680static void
7681__qla84xx_chip_release(struct kref *kref)
7682{
7683 struct qla_chip_state_84xx *cs84xx =
7684 container_of(kref, struct qla_chip_state_84xx, kref);
7685
7686 mutex_lock(&qla_cs84xx_mutex);
7687 list_del(&cs84xx->list);
7688 mutex_unlock(&qla_cs84xx_mutex);
7689 kfree(cs84xx);
7690}
7691
7692void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007693qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007694{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007695 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007696 if (ha->cs84xx)
7697 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7698}
7699
7700static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007701qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007702{
7703 int rval;
7704 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007705 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007706
7707 mutex_lock(&ha->cs84xx->fw_update_mutex);
7708
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007709 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007710
7711 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7712
7713 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7714 QLA_SUCCESS;
7715}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007716
7717/* 81XX Support **************************************************************/
7718
7719int
7720qla81xx_nvram_config(scsi_qla_host_t *vha)
7721{
7722 int rval;
7723 struct init_cb_81xx *icb;
7724 struct nvram_81xx *nv;
7725 uint32_t *dptr;
7726 uint8_t *dptr1, *dptr2;
7727 uint32_t chksum;
7728 uint16_t cnt;
7729 struct qla_hw_data *ha = vha->hw;
7730
7731 rval = QLA_SUCCESS;
7732 icb = (struct init_cb_81xx *)ha->init_cb;
7733 nv = ha->nvram;
7734
7735 /* Determine NVRAM starting address. */
7736 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007737 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007738 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7739 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007740
7741 /* Get VPD data into cache */
7742 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007743 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7744 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007745
7746 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007747 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007748 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007749 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007750 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7751 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007752
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007753 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7754 "Contents of NVRAM:\n");
7755 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7756 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007757
7758 /* Bad NVRAM data, set defaults parameters. */
7759 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7760 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007761 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007762 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007763 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007764 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007765 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007766 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007767 ql_log(ql_log_info, vha, 0x0074,
7768 "Falling back to functioning (yet invalid -- WWPN) "
7769 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007770
7771 /*
7772 * Set default initialization control block.
7773 */
7774 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007775 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7776 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007777 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007778 nv->execution_throttle = cpu_to_le16(0xFFFF);
7779 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007780 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007781 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007782 nv->port_name[2] = 0x00;
7783 nv->port_name[3] = 0xe0;
7784 nv->port_name[4] = 0x8b;
7785 nv->port_name[5] = 0x1c;
7786 nv->port_name[6] = 0x55;
7787 nv->port_name[7] = 0x86;
7788 nv->node_name[0] = 0x20;
7789 nv->node_name[1] = 0x00;
7790 nv->node_name[2] = 0x00;
7791 nv->node_name[3] = 0xe0;
7792 nv->node_name[4] = 0x8b;
7793 nv->node_name[5] = 0x1c;
7794 nv->node_name[6] = 0x55;
7795 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007796 nv->login_retry_count = cpu_to_le16(8);
7797 nv->interrupt_delay_timer = cpu_to_le16(0);
7798 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007799 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007800 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7801 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7802 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7803 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7804 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7805 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007806 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007807 nv->max_luns_per_target = cpu_to_le16(128);
7808 nv->port_down_retry_count = cpu_to_le16(30);
7809 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007810 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007811 nv->enode_mac[1] = 0xC0;
7812 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007813 nv->enode_mac[3] = 0x04;
7814 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007815 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007816
7817 rval = 1;
7818 }
7819
Arun Easi9e522cd2012-08-22 14:21:31 -04007820 if (IS_T10_PI_CAPABLE(ha))
7821 nv->frame_payload_size &= ~7;
7822
Arun Easiaa230bc2013-01-30 03:34:39 -05007823 qlt_81xx_config_nvram_stage1(vha, nv);
7824
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007825 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007826 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007827
7828 /* Copy 1st segment. */
7829 dptr1 = (uint8_t *)icb;
7830 dptr2 = (uint8_t *)&nv->version;
7831 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7832 while (cnt--)
7833 *dptr1++ = *dptr2++;
7834
7835 icb->login_retry_count = nv->login_retry_count;
7836
7837 /* Copy 2nd segment. */
7838 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7839 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7840 cnt = (uint8_t *)&icb->reserved_5 -
7841 (uint8_t *)&icb->interrupt_delay_timer;
7842 while (cnt--)
7843 *dptr1++ = *dptr2++;
7844
7845 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7846 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7847 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007848 icb->enode_mac[0] = 0x00;
7849 icb->enode_mac[1] = 0xC0;
7850 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007851 icb->enode_mac[3] = 0x04;
7852 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007853 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007854 }
7855
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007856 /* Use extended-initialization control block. */
7857 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7858
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007859 /*
7860 * Setup driver NVRAM options.
7861 */
7862 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007863 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007864
Arun Easiaa230bc2013-01-30 03:34:39 -05007865 qlt_81xx_config_nvram_stage2(vha, icb);
7866
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007867 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007868 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007869 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7870 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7871 }
7872
7873 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007874 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007875 /*
7876 * Firmware will apply the following mask if the nodename was
7877 * not provided.
7878 */
7879 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7880 icb->node_name[0] &= 0xF0;
7881 }
7882
7883 /* Set host adapter parameters. */
7884 ha->flags.disable_risc_code_load = 0;
7885 ha->flags.enable_lip_reset = 0;
7886 ha->flags.enable_lip_full_login =
7887 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7888 ha->flags.enable_target_reset =
7889 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7890 ha->flags.enable_led_scheme = 0;
7891 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7892
7893 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7894 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7895
7896 /* save HBA serial number */
7897 ha->serial0 = icb->port_name[5];
7898 ha->serial1 = icb->port_name[6];
7899 ha->serial2 = icb->port_name[7];
7900 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7901 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7902
Bart Van Asschead950362015-07-09 07:24:08 -07007903 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007904
7905 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7906
7907 /* Set minimum login_timeout to 4 seconds. */
7908 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7909 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7910 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007911 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007912 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007913
7914 /* Set minimum RATOV to 100 tenths of a second. */
7915 ha->r_a_tov = 100;
7916
7917 ha->loop_reset_delay = nv->reset_delay;
7918
7919 /* Link Down Timeout = 0:
7920 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007921 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007922 * I/O's to OS with "DID_NO_CONNECT".
7923 *
7924 * Link Down Timeout != 0:
7925 *
7926 * The driver waits for the link to come up after link down
7927 * before returning I/Os to OS with "DID_NO_CONNECT".
7928 */
7929 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7930 ha->loop_down_abort_time =
7931 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7932 } else {
7933 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7934 ha->loop_down_abort_time =
7935 (LOOP_DOWN_TIME - ha->link_down_timeout);
7936 }
7937
7938 /* Need enough time to try and get the port back. */
7939 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7940 if (qlport_down_retry)
7941 ha->port_down_retry_count = qlport_down_retry;
7942
7943 /* Set login_retry_count */
7944 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7945 if (ha->port_down_retry_count ==
7946 le16_to_cpu(nv->port_down_retry_count) &&
7947 ha->port_down_retry_count > 3)
7948 ha->login_retry_count = ha->port_down_retry_count;
7949 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7950 ha->login_retry_count = ha->port_down_retry_count;
7951 if (ql2xloginretrycount)
7952 ha->login_retry_count = ql2xloginretrycount;
7953
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007954 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007955 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007956 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007957
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007958 /* Enable ZIO. */
7959 if (!vha->flags.init_done) {
7960 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7961 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7962 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7963 le16_to_cpu(icb->interrupt_delay_timer): 2;
7964 }
Bart Van Asschead950362015-07-09 07:24:08 -07007965 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007966 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7967 vha->flags.process_response_queue = 0;
7968 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7969 ha->zio_mode = QLA_ZIO_MODE_6;
7970
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007971 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007972 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007973 ha->zio_mode,
7974 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007975
7976 icb->firmware_options_2 |= cpu_to_le32(
7977 (uint32_t)ha->zio_mode);
7978 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7979 vha->flags.process_response_queue = 1;
7980 }
7981
Quinn Tran41dc5292017-01-19 22:28:03 -08007982 /* enable RIDA Format2 */
7983 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7984 icb->firmware_options_3 |= BIT_0;
7985
Duane Grigsbyedd05de2017-10-13 09:34:06 -07007986 if (IS_QLA27XX(ha)) {
7987 icb->firmware_options_3 |= BIT_8;
7988 ql_dbg(ql_log_info, vha, 0x0075,
7989 "Enabling direct connection.\n");
7990 }
7991
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007992 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007993 ql_log(ql_log_warn, vha, 0x0076,
7994 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007995 }
7996 return (rval);
7997}
7998
Giridhar Malavalia9083012010-04-12 17:59:55 -07007999int
8000qla82xx_restart_isp(scsi_qla_host_t *vha)
8001{
8002 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008003 struct qla_hw_data *ha = vha->hw;
8004 struct req_que *req = ha->req_q_map[0];
8005 struct rsp_que *rsp = ha->rsp_q_map[0];
8006 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008007 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008008
8009 status = qla2x00_init_rings(vha);
8010 if (!status) {
8011 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8012 ha->flags.chip_reset_done = 1;
8013
8014 status = qla2x00_fw_ready(vha);
8015 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008016 /* Issue a marker after FW becomes ready. */
8017 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008018 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008019 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008020 }
8021
8022 /* if no cable then assume it's good */
8023 if ((vha->device_flags & DFLG_NO_CABLE))
8024 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008025 }
8026
8027 if (!status) {
8028 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8029
8030 if (!atomic_read(&vha->loop_down_timer)) {
8031 /*
8032 * Issue marker command only when we are going
8033 * to start the I/O .
8034 */
8035 vha->marker_needed = 1;
8036 }
8037
Giridhar Malavalia9083012010-04-12 17:59:55 -07008038 ha->isp_ops->enable_intrs(ha);
8039
8040 ha->isp_abort_cnt = 0;
8041 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8042
Saurav Kashyap53296782011-05-10 11:30:06 -07008043 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008044 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008045
Giridhar Malavalia9083012010-04-12 17:59:55 -07008046 if (ha->fce) {
8047 ha->flags.fce_enabled = 1;
8048 memset(ha->fce, 0,
8049 fce_calc_size(ha->fce_bufs));
8050 rval = qla2x00_enable_fce_trace(vha,
8051 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8052 &ha->fce_bufs);
8053 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008054 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008055 "Unable to reinitialize FCE (%d).\n",
8056 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008057 ha->flags.fce_enabled = 0;
8058 }
8059 }
8060
8061 if (ha->eft) {
8062 memset(ha->eft, 0, EFT_SIZE);
8063 rval = qla2x00_enable_eft_trace(vha,
8064 ha->eft_dma, EFT_NUM_BUFFERS);
8065 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008066 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008067 "Unable to reinitialize EFT (%d).\n",
8068 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008069 }
8070 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008071 }
8072
8073 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008074 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008075 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008076
8077 spin_lock_irqsave(&ha->vport_slock, flags);
8078 list_for_each_entry(vp, &ha->vp_list, list) {
8079 if (vp->vp_idx) {
8080 atomic_inc(&vp->vref_count);
8081 spin_unlock_irqrestore(&ha->vport_slock, flags);
8082
Giridhar Malavalia9083012010-04-12 17:59:55 -07008083 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008084
8085 spin_lock_irqsave(&ha->vport_slock, flags);
8086 atomic_dec(&vp->vref_count);
8087 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008088 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008089 spin_unlock_irqrestore(&ha->vport_slock, flags);
8090
Giridhar Malavalia9083012010-04-12 17:59:55 -07008091 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008092 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008093 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008094 }
8095
8096 return status;
8097}
8098
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008099void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008100qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008101{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008102 struct qla_hw_data *ha = vha->hw;
8103
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008104 /* Hold status IOCBs until ABTS response received. */
8105 if (ql2xfwholdabts)
8106 ha->fw_options[3] |= BIT_12;
8107
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008108 /* Set Retry FLOGI in case of P2P connection */
8109 if (ha->operating_mode == P2P) {
8110 ha->fw_options[2] |= BIT_3;
8111 ql_dbg(ql_dbg_disc, vha, 0x2103,
8112 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8113 __func__, ha->fw_options[2]);
8114 }
8115
Quinn Tran41dc5292017-01-19 22:28:03 -08008116 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8117 if (ql2xmvasynctoatio) {
8118 if (qla_tgt_mode_enabled(vha) ||
8119 qla_dual_mode_enabled(vha))
8120 ha->fw_options[2] |= BIT_11;
8121 else
8122 ha->fw_options[2] &= ~BIT_11;
8123 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008124
Quinn Tranf7e761f2017-06-02 09:12:02 -07008125 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008126 qla_dual_mode_enabled(vha)) {
8127 /* FW auto send SCSI status during */
8128 ha->fw_options[1] |= BIT_8;
8129 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8130
8131 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008132 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008133 } else {
8134 ha->fw_options[1] &= ~BIT_8;
8135 ha->fw_options[10] &= 0x00ff;
8136
Quinn Tranf7e761f2017-06-02 09:12:02 -07008137 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008138 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008139
Quinn Tran41dc5292017-01-19 22:28:03 -08008140 if (ql2xetsenable) {
8141 /* Enable ETS Burst. */
8142 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8143 ha->fw_options[2] |= BIT_9;
8144 }
8145
Quinn Tran83548fe2017-06-02 09:12:01 -07008146 ql_dbg(ql_dbg_init, vha, 0x00e9,
8147 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8148 __func__, ha->fw_options[1], ha->fw_options[2],
8149 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008150
Andrew Vasquezae97c912010-02-18 10:07:28 -08008151 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008152}
Sarang Radke09ff7012010-03-19 17:03:59 -07008153
8154/*
8155 * qla24xx_get_fcp_prio
8156 * Gets the fcp cmd priority value for the logged in port.
8157 * Looks for a match of the port descriptors within
8158 * each of the fcp prio config entries. If a match is found,
8159 * the tag (priority) value is returned.
8160 *
8161 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008162 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008163 * fcport = port structure pointer.
8164 *
8165 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008166 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008167 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008168 *
8169 * Context:
8170 * Kernel context
8171 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008172static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008173qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8174{
8175 int i, entries;
8176 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008177 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008178 uint32_t pid1, pid2;
8179 uint64_t wwn1, wwn2;
8180 struct qla_fcp_prio_entry *pri_entry;
8181 struct qla_hw_data *ha = vha->hw;
8182
8183 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008184 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008185
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008186 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008187 entries = ha->fcp_prio_cfg->num_entries;
8188 pri_entry = &ha->fcp_prio_cfg->entry[0];
8189
8190 for (i = 0; i < entries; i++) {
8191 pid_match = wwn_match = 0;
8192
8193 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8194 pri_entry++;
8195 continue;
8196 }
8197
8198 /* check source pid for a match */
8199 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8200 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8201 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8202 if (pid1 == INVALID_PORT_ID)
8203 pid_match++;
8204 else if (pid1 == pid2)
8205 pid_match++;
8206 }
8207
8208 /* check destination pid for a match */
8209 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8210 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8211 pid2 = fcport->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 source WWN for a match */
8219 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8220 wwn1 = wwn_to_u64(vha->port_name);
8221 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8222 if (wwn2 == (uint64_t)-1)
8223 wwn_match++;
8224 else if (wwn1 == wwn2)
8225 wwn_match++;
8226 }
8227
8228 /* check destination WWN for a match */
8229 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8230 wwn1 = wwn_to_u64(fcport->port_name);
8231 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8232 if (wwn2 == (uint64_t)-1)
8233 wwn_match++;
8234 else if (wwn1 == wwn2)
8235 wwn_match++;
8236 }
8237
8238 if (pid_match == 2 || wwn_match == 2) {
8239 /* Found a matching entry */
8240 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8241 priority = pri_entry->tag;
8242 break;
8243 }
8244
8245 pri_entry++;
8246 }
8247
8248 return priority;
8249}
8250
8251/*
8252 * qla24xx_update_fcport_fcp_prio
8253 * Activates fcp priority for the logged in fc port
8254 *
8255 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008256 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008257 * fcp = port structure pointer.
8258 *
8259 * Return:
8260 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8261 *
8262 * Context:
8263 * Kernel context.
8264 */
8265int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008266qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008267{
8268 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008269 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008270 uint16_t mb[5];
8271
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008272 if (fcport->port_type != FCT_TARGET ||
8273 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008274 return QLA_FUNCTION_FAILED;
8275
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008276 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008277 if (priority < 0)
8278 return QLA_FUNCTION_FAILED;
8279
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008280 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008281 fcport->fcp_prio = priority & 0xf;
8282 return QLA_SUCCESS;
8283 }
8284
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008285 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008286 if (ret == QLA_SUCCESS) {
8287 if (fcport->fcp_prio != priority)
8288 ql_dbg(ql_dbg_user, vha, 0x709e,
8289 "Updated FCP_CMND priority - value=%d loop_id=%d "
8290 "port_id=%02x%02x%02x.\n", priority,
8291 fcport->loop_id, fcport->d_id.b.domain,
8292 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008293 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008294 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008295 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008296 "Unable to update FCP_CMND priority - ret=0x%x for "
8297 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8298 fcport->d_id.b.domain, fcport->d_id.b.area,
8299 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008300 return ret;
8301}
8302
8303/*
8304 * qla24xx_update_all_fcp_prio
8305 * Activates fcp priority for all the logged in ports
8306 *
8307 * Input:
8308 * ha = adapter block pointer.
8309 *
8310 * Return:
8311 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8312 *
8313 * Context:
8314 * Kernel context.
8315 */
8316int
8317qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8318{
8319 int ret;
8320 fc_port_t *fcport;
8321
8322 ret = QLA_FUNCTION_FAILED;
8323 /* We need to set priority for all logged in ports */
8324 list_for_each_entry(fcport, &vha->vp_fcports, list)
8325 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8326
8327 return ret;
8328}
Michael Hernandezd7459522016-12-12 14:40:07 -08008329
Quinn Tran82de8022017-06-13 20:47:17 -07008330struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8331 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008332{
8333 int rsp_id = 0;
8334 int req_id = 0;
8335 int i;
8336 struct qla_hw_data *ha = vha->hw;
8337 uint16_t qpair_id = 0;
8338 struct qla_qpair *qpair = NULL;
8339 struct qla_msix_entry *msix;
8340
8341 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8342 ql_log(ql_log_warn, vha, 0x00181,
8343 "FW/Driver is not multi-queue capable.\n");
8344 return NULL;
8345 }
8346
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008347 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008348 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8349 if (qpair == NULL) {
8350 ql_log(ql_log_warn, vha, 0x0182,
8351 "Failed to allocate memory for queue pair.\n");
8352 return NULL;
8353 }
8354 memset(qpair, 0, sizeof(struct qla_qpair));
8355
8356 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008357 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008358 qpair->qp_lock_ptr = &qpair->qp_lock;
8359 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008360 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008361
8362 /* Assign available que pair id */
8363 mutex_lock(&ha->mq_lock);
8364 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008365 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008366 mutex_unlock(&ha->mq_lock);
8367 ql_log(ql_log_warn, vha, 0x0183,
8368 "No resources to create additional q pair.\n");
8369 goto fail_qid_map;
8370 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008371 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008372 set_bit(qpair_id, ha->qpair_qid_map);
8373 ha->queue_pair_map[qpair_id] = qpair;
8374 qpair->id = qpair_id;
8375 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008376 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008377 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008378 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008379 qpair->chip_reset = ha->base_qpair->chip_reset;
8380 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8381 qpair->enable_explicit_conf =
8382 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008383
8384 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008385 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008386 if (msix->in_use)
8387 continue;
8388 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008389 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008390 "Vector %x selected for qpair\n", msix->vector);
8391 break;
8392 }
8393 if (!qpair->msix) {
8394 ql_log(ql_log_warn, vha, 0x0184,
8395 "Out of MSI-X vectors!.\n");
8396 goto fail_msix;
8397 }
8398
8399 qpair->msix->in_use = 1;
8400 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008401 qpair->pdev = ha->pdev;
8402 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8403 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008404
8405 mutex_unlock(&ha->mq_lock);
8406
8407 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008408 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008409 if (!rsp_id) {
8410 ql_log(ql_log_warn, vha, 0x0185,
8411 "Failed to create response queue.\n");
8412 goto fail_rsp;
8413 }
8414
8415 qpair->rsp = ha->rsp_q_map[rsp_id];
8416
8417 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008418 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8419 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008420 if (!req_id) {
8421 ql_log(ql_log_warn, vha, 0x0186,
8422 "Failed to create request queue.\n");
8423 goto fail_req;
8424 }
8425
8426 qpair->req = ha->req_q_map[req_id];
8427 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008428 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008429 /* init qpair to this cpu. Will adjust at run time. */
8430 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008431
8432 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8433 if (ha->fw_attributes & BIT_4)
8434 qpair->difdix_supported = 1;
8435 }
8436
8437 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8438 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008439 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008440 "Failed to create srb mempool for qpair %d\n",
8441 qpair->id);
8442 goto fail_mempool;
8443 }
8444
8445 /* Mark as online */
8446 qpair->online = 1;
8447
8448 if (!vha->flags.qpairs_available)
8449 vha->flags.qpairs_available = 1;
8450
8451 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8452 "Request/Response queue pair created, id %d\n",
8453 qpair->id);
8454 ql_dbg(ql_dbg_init, vha, 0x0187,
8455 "Request/Response queue pair created, id %d\n",
8456 qpair->id);
8457 }
8458 return qpair;
8459
8460fail_mempool:
8461fail_req:
8462 qla25xx_delete_rsp_que(vha, qpair->rsp);
8463fail_rsp:
8464 mutex_lock(&ha->mq_lock);
8465 qpair->msix->in_use = 0;
8466 list_del(&qpair->qp_list_elem);
8467 if (list_empty(&vha->qp_list))
8468 vha->flags.qpairs_available = 0;
8469fail_msix:
8470 ha->queue_pair_map[qpair_id] = NULL;
8471 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008472 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008473 mutex_unlock(&ha->mq_lock);
8474fail_qid_map:
8475 kfree(qpair);
8476 return NULL;
8477}
8478
8479int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8480{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008481 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008482 struct qla_hw_data *ha = qpair->hw;
8483
8484 qpair->delete_in_progress = 1;
8485 while (atomic_read(&qpair->ref_count))
8486 msleep(500);
8487
8488 ret = qla25xx_delete_req_que(vha, qpair->req);
8489 if (ret != QLA_SUCCESS)
8490 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008491
Michael Hernandezd7459522016-12-12 14:40:07 -08008492 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8493 if (ret != QLA_SUCCESS)
8494 goto fail;
8495
8496 mutex_lock(&ha->mq_lock);
8497 ha->queue_pair_map[qpair->id] = NULL;
8498 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008499 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008500 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008501 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008502 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008503 vha->flags.qpairs_req_created = 0;
8504 vha->flags.qpairs_rsp_created = 0;
8505 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008506 mempool_destroy(qpair->srb_mempool);
8507 kfree(qpair);
8508 mutex_unlock(&ha->mq_lock);
8509
8510 return QLA_SUCCESS;
8511fail:
8512 return ret;
8513}