blob: f26acb7ce3153519950b822111f3f960e0facd37 [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 Trana4239942017-12-28 12:33:26 -0800342 if (ea->rc) {
343 ql_dbg(ql_dbg_disc, vha, 0x2066,
344 "%s %8phC: adisc fail: post delete\n",
345 __func__, ea->fcport->port_name);
346 qlt_schedule_sess_for_deletion(ea->fcport, 1);
347 return;
348 }
349 ql_dbg(ql_dbg_disc, vha, 0x20d2,
350 "%s %8phC DS %d LS %d\n", __func__, ea->fcport->port_name,
351 ea->fcport->disc_state, ea->fcport->fw_login_state);
352
353 if (ea->fcport->disc_state == DSC_DELETE_PEND)
354 return;
355
356 if (ea->sp->gen2 != ea->fcport->login_gen) {
357 /* target side must have changed it. */
358 ql_dbg(ql_dbg_disc, vha, 0x20d3,
359 "%s %8phC generation changed rscn %d|%d login %d|%d\n",
360 __func__, ea->fcport->port_name, ea->fcport->last_rscn_gen,
361 ea->fcport->rscn_gen, ea->fcport->last_login_gen,
362 ea->fcport->login_gen);
363 return;
364 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
365 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
366 __func__, __LINE__, ea->fcport->port_name);
367 qla24xx_post_gidpn_work(vha, ea->fcport);
368 return;
369 }
370
371 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800372}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700373
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700374static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800375qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700376{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800377 srb_t *sp = ptr;
378 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800379 struct event_arg ea;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700380
Quinn Tranf13515a2017-12-28 12:33:15 -0800381 ql_dbg(ql_dbg_disc, vha, 0x2066,
382 "Async done-%s res %x %8phC\n",
383 sp->name, res, sp->fcport->port_name);
384
385 memset(&ea, 0, sizeof(ea));
386 ea.event = FCME_ADISC_DONE;
387 ea.rc = res;
388 ea.fcport = sp->fcport;
389 ea.sp = sp;
390
391 qla2x00_fcport_event_handler(vha, &ea);
392
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800393 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700394}
395
396int
397qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
398 uint16_t *data)
399{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700400 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700401 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700402 int rval;
403
404 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800405 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800406 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700407 if (!sp)
408 goto done;
409
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800410 sp->type = SRB_ADISC_CMD;
411 sp->name = "adisc";
412 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
413
414 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700415 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800416 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700417 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700418 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700419 rval = qla2x00_start_sp(sp);
420 if (rval != QLA_SUCCESS)
421 goto done_free_sp;
422
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700423 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800424 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
425 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700426 return rval;
427
428done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800429 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700430done:
Quinn Tran726b8542017-01-19 22:28:00 -0800431 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tranf13515a2017-12-28 12:33:15 -0800432 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700433 return rval;
434}
435
Quinn Tran726b8542017-01-19 22:28:00 -0800436static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
437 struct event_arg *ea)
438{
439 fc_port_t *fcport, *conflict_fcport;
440 struct get_name_list_extended *e;
441 u16 i, n, found = 0, loop_id;
442 port_id_t id;
443 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800444 u16 data[2];
445 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800446
447 fcport = ea->fcport;
448
Quinn Trana4239942017-12-28 12:33:26 -0800449 if (fcport->disc_state == DSC_DELETE_PEND)
450 return;
451
Quinn Tran726b8542017-01-19 22:28:00 -0800452 if (ea->rc) { /* rval */
453 if (fcport->login_retry == 0) {
454 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700455 ql_dbg(ql_dbg_disc, vha, 0x20de,
456 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
457 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800458 }
459 return;
460 }
461
462 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700463 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800464 "%s %8phC rscn gen changed rscn %d|%d \n",
465 __func__, fcport->port_name,
466 fcport->last_rscn_gen, fcport->rscn_gen);
467 qla24xx_post_gidpn_work(vha, fcport);
468 return;
469 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700470 ql_dbg(ql_dbg_disc, vha, 0x20e0,
471 "%s %8phC login gen changed login %d|%d\n",
472 __func__, fcport->port_name,
473 fcport->last_login_gen, fcport->login_gen);
Quinn Tran726b8542017-01-19 22:28:00 -0800474 return;
475 }
476
477 n = ea->data[0] / sizeof(struct get_name_list_extended);
478
Quinn Tran83548fe2017-06-02 09:12:01 -0700479 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800480 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
481 __func__, __LINE__, fcport->port_name, n,
482 fcport->d_id.b.domain, fcport->d_id.b.area,
483 fcport->d_id.b.al_pa, fcport->loop_id);
484
485 for (i = 0; i < n; i++) {
486 e = &vha->gnl.l[i];
487 wwn = wwn_to_u64(e->port_name);
488
489 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
490 continue;
491
492 found = 1;
493 id.b.domain = e->port_id[2];
494 id.b.area = e->port_id[1];
495 id.b.al_pa = e->port_id[0];
496 id.b.rsvd_1 = 0;
497
498 loop_id = le16_to_cpu(e->nport_handle);
499 loop_id = (loop_id & 0x7fff);
500
Quinn Tran83548fe2017-06-02 09:12:01 -0700501 ql_dbg(ql_dbg_disc, vha, 0x20e2,
502 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
503 __func__, fcport->port_name,
504 e->current_login_state, fcport->fw_login_state,
505 id.b.domain, id.b.area, id.b.al_pa,
506 fcport->d_id.b.domain, fcport->d_id.b.area,
507 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800508
509 if ((id.b24 != fcport->d_id.b24) ||
510 ((fcport->loop_id != FC_NO_LOOP_ID) &&
511 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700512 ql_dbg(ql_dbg_disc, vha, 0x20e3,
513 "%s %d %8phC post del sess\n",
514 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800515 qlt_schedule_sess_for_deletion(fcport, 1);
516 return;
517 }
518
519 fcport->loop_id = loop_id;
520
521 wwn = wwn_to_u64(fcport->port_name);
522 qlt_find_sess_invalidate_other(vha, wwn,
523 id, loop_id, &conflict_fcport);
524
525 if (conflict_fcport) {
526 /*
527 * Another share fcport share the same loop_id &
528 * nport id. Conflict fcport needs to finish
529 * cleanup before this fcport can proceed to login.
530 */
531 conflict_fcport->conflict = fcport;
532 fcport->login_pause = 1;
533 }
534
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700535 if (fcport->fc4f_nvme)
536 current_login_state = e->current_login_state >> 4;
537 else
538 current_login_state = e->current_login_state & 0xf;
539
540 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800541 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700542 ql_dbg(ql_dbg_disc, vha, 0x20e4,
543 "%s %d %8phC post gpdb\n",
544 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800545
546 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
547 fcport->port_type = FCT_INITIATOR;
548 else
549 fcport->port_type = FCT_TARGET;
550
551 data[0] = data[1] = 0;
552 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800553 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800554 case DSC_LS_PORT_UNAVAIL:
555 default:
556 if (fcport->loop_id == FC_NO_LOOP_ID) {
557 qla2x00_find_new_loop_id(vha, fcport);
558 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
559 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700560 ql_dbg(ql_dbg_disc, vha, 0x20e5,
561 "%s %d %8phC\n",
562 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800563 qla24xx_fcport_handle_login(vha, fcport);
564 break;
565 }
566 }
567
568 if (!found) {
569 /* fw has no record of this port */
570 if (fcport->loop_id == FC_NO_LOOP_ID) {
571 qla2x00_find_new_loop_id(vha, fcport);
572 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
573 } else {
574 for (i = 0; i < n; i++) {
575 e = &vha->gnl.l[i];
576 id.b.domain = e->port_id[0];
577 id.b.area = e->port_id[1];
578 id.b.al_pa = e->port_id[2];
579 id.b.rsvd_1 = 0;
580 loop_id = le16_to_cpu(e->nport_handle);
581
582 if (fcport->d_id.b24 == id.b24) {
583 conflict_fcport =
584 qla2x00_find_fcport_by_wwpn(vha,
585 e->port_name, 0);
586
Quinn Tran83548fe2017-06-02 09:12:01 -0700587 ql_dbg(ql_dbg_disc, vha, 0x20e6,
Quinn Tran726b8542017-01-19 22:28:00 -0800588 "%s %d %8phC post del sess\n",
589 __func__, __LINE__,
590 conflict_fcport->port_name);
591 qlt_schedule_sess_for_deletion
592 (conflict_fcport, 1);
593 }
594
595 if (fcport->loop_id == loop_id) {
596 /* FW already picked this loop id for another fcport */
597 qla2x00_find_new_loop_id(vha, fcport);
598 }
599 }
600 }
601 qla24xx_fcport_handle_login(vha, fcport);
602 }
603} /* gnl_event */
604
605static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800606qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800607{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800608 struct srb *sp = s;
609 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800610 unsigned long flags;
611 struct fc_port *fcport = NULL, *tf;
612 u16 i, n = 0, loop_id;
613 struct event_arg ea;
614 struct get_name_list_extended *e;
615 u64 wwn;
616 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800617 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800618
Quinn Tran83548fe2017-06-02 09:12:01 -0700619 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800620 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
621 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
622 sp->u.iocb_cmd.u.mbx.in_mb[2]);
623
624 memset(&ea, 0, sizeof(ea));
625 ea.sp = sp;
626 ea.rc = res;
627 ea.event = FCME_GNL_DONE;
628
629 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
630 sizeof(struct get_name_list_extended)) {
631 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
632 sizeof(struct get_name_list_extended);
633 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
634 }
635
636 for (i = 0; i < n; i++) {
637 e = &vha->gnl.l[i];
638 loop_id = le16_to_cpu(e->nport_handle);
639 /* mask out reserve bit */
640 loop_id = (loop_id & 0x7fff);
641 set_bit(loop_id, vha->hw->loop_id_map);
642 wwn = wwn_to_u64(e->port_name);
643
Quinn Tran83548fe2017-06-02 09:12:01 -0700644 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800645 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
646 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
647 e->port_id[0], e->current_login_state, e->last_login_state,
648 (loop_id & 0x7fff));
649 }
650
651 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
652 vha->gnl.sent = 0;
653
654 INIT_LIST_HEAD(&h);
655 fcport = tf = NULL;
656 if (!list_empty(&vha->gnl.fcports))
657 list_splice_init(&vha->gnl.fcports, &h);
658
659 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
660 list_del_init(&fcport->gnl_entry);
661 fcport->flags &= ~FCF_ASYNC_SENT;
662 ea.fcport = fcport;
663
664 qla2x00_fcport_event_handler(vha, &ea);
665 }
666
Quinn Trana4239942017-12-28 12:33:26 -0800667 /* create new fcport if fw has knowledge of new sessions */
668 for (i = 0; i < n; i++) {
669 port_id_t id;
670 u64 wwnn;
671
672 e = &vha->gnl.l[i];
673 wwn = wwn_to_u64(e->port_name);
674
675 found = false;
676 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
677 if (!memcmp((u8 *)&wwn, fcport->port_name,
678 WWN_SIZE)) {
679 found = true;
680 break;
681 }
682 }
683
684 id.b.domain = e->port_id[0];
685 id.b.area = e->port_id[1];
686 id.b.al_pa = e->port_id[2];
687 id.b.rsvd_1 = 0;
688
689 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
690 ql_dbg(ql_dbg_disc, vha, 0x2065,
691 "%s %d %8phC post new sess\n",
692 __func__, __LINE__, (u8 *)&wwn);
693 wwnn = wwn_to_u64(e->node_name);
694 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
695 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
696 }
697 }
698
Quinn Tran726b8542017-01-19 22:28:00 -0800699 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
700
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800701 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800702}
703
704int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
705{
706 srb_t *sp;
707 struct srb_iocb *mbx;
708 int rval = QLA_FUNCTION_FAILED;
709 unsigned long flags;
710 u16 *mb;
711
712 if (!vha->flags.online)
713 goto done;
714
Quinn Tran83548fe2017-06-02 09:12:01 -0700715 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800716 "Async-gnlist WWPN %8phC \n", fcport->port_name);
717
718 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
719 fcport->flags |= FCF_ASYNC_SENT;
720 fcport->disc_state = DSC_GNL;
721 fcport->last_rscn_gen = fcport->rscn_gen;
722 fcport->last_login_gen = fcport->login_gen;
723
724 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
725 if (vha->gnl.sent) {
726 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
727 rval = QLA_SUCCESS;
728 goto done;
729 }
730 vha->gnl.sent = 1;
731 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
732
733 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
734 if (!sp)
735 goto done;
736 sp->type = SRB_MB_IOCB;
737 sp->name = "gnlist";
738 sp->gen1 = fcport->rscn_gen;
739 sp->gen2 = fcport->login_gen;
740
741 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
742
743 mb = sp->u.iocb_cmd.u.mbx.out_mb;
744 mb[0] = MBC_PORT_NODE_NAME_LIST;
745 mb[1] = BIT_2 | BIT_3;
746 mb[2] = MSW(vha->gnl.ldma);
747 mb[3] = LSW(vha->gnl.ldma);
748 mb[6] = MSW(MSD(vha->gnl.ldma));
749 mb[7] = LSW(MSD(vha->gnl.ldma));
750 mb[8] = vha->gnl.size;
751 mb[9] = vha->vp_idx;
752
753 mbx = &sp->u.iocb_cmd;
754 mbx->timeout = qla2x00_async_iocb_timeout;
755
756 sp->done = qla24xx_async_gnl_sp_done;
757
758 rval = qla2x00_start_sp(sp);
759 if (rval != QLA_SUCCESS)
760 goto done_free_sp;
761
Quinn Tran83548fe2017-06-02 09:12:01 -0700762 ql_dbg(ql_dbg_disc, vha, 0x20da,
763 "Async-%s - OUT WWPN %8phC hndl %x\n",
764 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800765
766 return rval;
767
768done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800769 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800770done:
771 fcport->flags &= ~FCF_ASYNC_SENT;
772 return rval;
773}
774
775int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
776{
777 struct qla_work_evt *e;
778
779 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
780 if (!e)
781 return QLA_FUNCTION_FAILED;
782
783 e->u.fcport.fcport = fcport;
784 return qla2x00_post_work(vha, e);
785}
786
787static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800788void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800789{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800790 struct srb *sp = s;
791 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800792 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800793 fc_port_t *fcport = sp->fcport;
794 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800795 struct event_arg ea;
796
Quinn Tran83548fe2017-06-02 09:12:01 -0700797 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800798 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
799 sp->name, res, fcport->port_name, mb[1], mb[2]);
800
801 fcport->flags &= ~FCF_ASYNC_SENT;
802
Quinn Tran726b8542017-01-19 22:28:00 -0800803 memset(&ea, 0, sizeof(ea));
804 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800805 ea.fcport = fcport;
806 ea.sp = sp;
807
808 qla2x00_fcport_event_handler(vha, &ea);
809
810 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
811 sp->u.iocb_cmd.u.mbx.in_dma);
812
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800813 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800814}
815
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700816static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
817{
818 struct qla_work_evt *e;
819
820 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
821 if (!e)
822 return QLA_FUNCTION_FAILED;
823
824 e->u.fcport.fcport = fcport;
825
826 return qla2x00_post_work(vha, e);
827}
828
829static void
830qla2x00_async_prli_sp_done(void *ptr, int res)
831{
832 srb_t *sp = ptr;
833 struct scsi_qla_host *vha = sp->vha;
834 struct srb_iocb *lio = &sp->u.iocb_cmd;
835 struct event_arg ea;
836
837 ql_dbg(ql_dbg_disc, vha, 0x2129,
838 "%s %8phC res %d \n", __func__,
839 sp->fcport->port_name, res);
840
841 sp->fcport->flags &= ~FCF_ASYNC_SENT;
842
843 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
844 memset(&ea, 0, sizeof(ea));
845 ea.event = FCME_PRLI_DONE;
846 ea.fcport = sp->fcport;
847 ea.data[0] = lio->u.logio.data[0];
848 ea.data[1] = lio->u.logio.data[1];
849 ea.iop[0] = lio->u.logio.iop[0];
850 ea.iop[1] = lio->u.logio.iop[1];
851 ea.sp = sp;
852
853 qla2x00_fcport_event_handler(vha, &ea);
854 }
855
856 sp->free(sp);
857}
858
859int
860qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
861{
862 srb_t *sp;
863 struct srb_iocb *lio;
864 int rval = QLA_FUNCTION_FAILED;
865
866 if (!vha->flags.online)
867 return rval;
868
869 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
870 fcport->fw_login_state == DSC_LS_PLOGI_COMP ||
871 fcport->fw_login_state == DSC_LS_PRLI_PEND)
872 return rval;
873
874 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
875 if (!sp)
876 return rval;
877
878 fcport->flags |= FCF_ASYNC_SENT;
879 fcport->logout_completed = 0;
880
881 sp->type = SRB_PRLI_CMD;
882 sp->name = "prli";
883 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
884
885 lio = &sp->u.iocb_cmd;
886 lio->timeout = qla2x00_async_iocb_timeout;
887 sp->done = qla2x00_async_prli_sp_done;
888 lio->u.logio.flags = 0;
889
890 if (fcport->fc4f_nvme)
891 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
892
893 rval = qla2x00_start_sp(sp);
894 if (rval != QLA_SUCCESS) {
895 fcport->flags &= ~FCF_ASYNC_SENT;
896 fcport->flags |= FCF_LOGIN_NEEDED;
897 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
898 goto done_free_sp;
899 }
900
901 ql_dbg(ql_dbg_disc, vha, 0x211b,
902 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
903 fcport->port_name, sp->handle, fcport->loop_id,
904 fcport->d_id.b24, fcport->login_retry);
905
906 return rval;
907
908done_free_sp:
909 sp->free(sp);
910 fcport->flags &= ~FCF_ASYNC_SENT;
911 return rval;
912}
913
Quinn Trana07fc0a2017-08-23 15:05:21 -0700914int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800915{
916 struct qla_work_evt *e;
917
918 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
919 if (!e)
920 return QLA_FUNCTION_FAILED;
921
922 e->u.fcport.fcport = fcport;
923 e->u.fcport.opt = opt;
924 return qla2x00_post_work(vha, e);
925}
926
927int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
928{
929 srb_t *sp;
930 struct srb_iocb *mbx;
931 int rval = QLA_FUNCTION_FAILED;
932 u16 *mb;
933 dma_addr_t pd_dma;
934 struct port_database_24xx *pd;
935 struct qla_hw_data *ha = vha->hw;
936
937 if (!vha->flags.online)
938 goto done;
939
940 fcport->flags |= FCF_ASYNC_SENT;
941 fcport->disc_state = DSC_GPDB;
942
943 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
944 if (!sp)
945 goto done;
946
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700947 sp->type = SRB_MB_IOCB;
948 sp->name = "gpdb";
949 sp->gen1 = fcport->rscn_gen;
950 sp->gen2 = fcport->login_gen;
951 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
952
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200953 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800954 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700955 ql_log(ql_log_warn, vha, 0xd043,
956 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800957 goto done_free_sp;
958 }
Quinn Tran726b8542017-01-19 22:28:00 -0800959
Quinn Tran726b8542017-01-19 22:28:00 -0800960 mb = sp->u.iocb_cmd.u.mbx.out_mb;
961 mb[0] = MBC_GET_PORT_DATABASE;
962 mb[1] = fcport->loop_id;
963 mb[2] = MSW(pd_dma);
964 mb[3] = LSW(pd_dma);
965 mb[6] = MSW(MSD(pd_dma));
966 mb[7] = LSW(MSD(pd_dma));
967 mb[9] = vha->vp_idx;
968 mb[10] = opt;
969
970 mbx = &sp->u.iocb_cmd;
971 mbx->timeout = qla2x00_async_iocb_timeout;
972 mbx->u.mbx.in = (void *)pd;
973 mbx->u.mbx.in_dma = pd_dma;
974
975 sp->done = qla24xx_async_gpdb_sp_done;
976
977 rval = qla2x00_start_sp(sp);
978 if (rval != QLA_SUCCESS)
979 goto done_free_sp;
980
Quinn Tran83548fe2017-06-02 09:12:01 -0700981 ql_dbg(ql_dbg_disc, vha, 0x20dc,
982 "Async-%s %8phC hndl %x opt %x\n",
983 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800984
985 return rval;
986
987done_free_sp:
988 if (pd)
989 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
990
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800991 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800992done:
993 fcport->flags &= ~FCF_ASYNC_SENT;
994 qla24xx_post_gpdb_work(vha, fcport, opt);
995 return rval;
996}
997
998static
Quinn Trana4239942017-12-28 12:33:26 -0800999void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001000{
Quinn Tran726b8542017-01-19 22:28:00 -08001001 unsigned long flags;
1002
Quinn Tran726b8542017-01-19 22:28:00 -08001003 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001004 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001005 ea->fcport->deleted = 0;
1006 ea->fcport->logout_on_delete = 1;
1007
1008 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1009 vha->fcport_count++;
1010 ea->fcport->login_succ = 1;
1011
1012 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1013 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001014 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001015 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001016 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001017 vha->fcport_count);
1018
Quinn Trana4239942017-12-28 12:33:26 -08001019 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001020 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001021 if (ea->fcport->id_changed) {
1022 ea->fcport->id_changed = 0;
1023 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1024 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1025 __func__, __LINE__, ea->fcport->port_name,
1026 vha->fcport_count);
1027 qla24xx_post_gfpnid_work(vha, ea->fcport);
1028 } else {
1029 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1030 "%s %d %8phC post gpsc fcp_cnt %d\n",
1031 __func__, __LINE__, ea->fcport->port_name,
1032 vha->fcport_count);
1033 qla24xx_post_gpsc_work(vha, ea->fcport);
1034 }
Quinn Tran726b8542017-01-19 22:28:00 -08001035 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001036 } else if (ea->fcport->login_succ) {
1037 /*
1038 * We have an existing session. A late RSCN delivery
1039 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001040 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001041 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001042 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1043 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001044 __func__, __LINE__, ea->fcport->port_name);
1045 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001046 }
1047 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001048}
Quinn Tran726b8542017-01-19 22:28:00 -08001049
Quinn Trana4239942017-12-28 12:33:26 -08001050static
1051void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1052{
1053 int rval = ea->rc;
1054 fc_port_t *fcport = ea->fcport;
1055 struct port_database_24xx *pd;
1056 struct srb *sp = ea->sp;
1057
1058 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1059
1060 fcport->flags &= ~FCF_ASYNC_SENT;
1061
1062 ql_dbg(ql_dbg_disc, vha, 0x20d2,
1063 "%s %8phC DS %d LS %d rval %d\n", __func__, fcport->port_name,
1064 fcport->disc_state, pd->current_login_state, rval);
1065
1066 if (fcport->disc_state == DSC_DELETE_PEND)
1067 return;
1068
1069 switch (pd->current_login_state) {
1070 case PDS_PRLI_COMPLETE:
1071 __qla24xx_parse_gpdb(vha, fcport, pd);
1072 break;
1073 case PDS_PLOGI_PENDING:
1074 case PDS_PLOGI_COMPLETE:
1075 case PDS_PRLI_PENDING:
1076 case PDS_PRLI2_PENDING:
1077 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1078 __func__, __LINE__, fcport->port_name);
1079 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1080 return;
1081 case PDS_LOGO_PENDING:
1082 case PDS_PORT_UNAVAILABLE:
1083 default:
1084 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1085 __func__, __LINE__, fcport->port_name);
1086 qlt_schedule_sess_for_deletion_lock(fcport);
1087 return;
1088 }
1089 __qla24xx_handle_gpdb_event(vha, ea);
1090} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001091
1092static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1093{
1094 u8 login = 0;
1095
1096 if (qla_tgt_mode_enabled(vha))
1097 return;
1098
1099 if (qla_dual_mode_enabled(vha)) {
1100 if (N2N_TOPO(vha->hw)) {
1101 u64 mywwn, wwn;
1102
1103 mywwn = wwn_to_u64(vha->port_name);
1104 wwn = wwn_to_u64(fcport->port_name);
1105 if (mywwn > wwn)
1106 login = 1;
1107 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1108 && time_after_eq(jiffies,
1109 fcport->plogi_nack_done_deadline))
1110 login = 1;
1111 } else {
1112 login = 1;
1113 }
1114 } else {
1115 /* initiator mode */
1116 login = 1;
1117 }
1118
1119 if (login) {
1120 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1121 "%s %d %8phC post login\n",
1122 __func__, __LINE__, fcport->port_name);
1123 fcport->disc_state = DSC_LOGIN_PEND;
1124 qla2x00_post_async_login_work(vha, fcport, NULL);
1125 }
1126}
1127
Quinn Tran726b8542017-01-19 22:28:00 -08001128int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1129{
Quinn Tranf13515a2017-12-28 12:33:15 -08001130 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001131 u64 wwn;
1132
1133 ql_dbg(ql_dbg_disc, vha, 0x20d8,
1134 "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d|%d retry %d lid %d scan %d\n",
1135 __func__, fcport->port_name, fcport->disc_state,
1136 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1137 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
1138 fcport->last_login_gen, fcport->login_gen, fcport->login_retry,
1139 fcport->loop_id, fcport->scan_state);
1140
Quinn Tran726b8542017-01-19 22:28:00 -08001141 if (fcport->login_retry == 0)
1142 return 0;
1143
1144 if (fcport->scan_state != QLA_FCPORT_FOUND)
1145 return 0;
1146
Quinn Tran726b8542017-01-19 22:28:00 -08001147 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001148 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1149 return 0;
1150
Quinn Tran5b334692017-03-15 09:48:48 -07001151 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001152 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1153 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001154 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001155 }
Quinn Tran5b334692017-03-15 09:48:48 -07001156 }
1157
Quinn Tran726b8542017-01-19 22:28:00 -08001158 /* for pure Target Mode. Login will not be initiated */
1159 if (vha->host->active_mode == MODE_TARGET)
1160 return 0;
1161
1162 if (fcport->flags & FCF_ASYNC_SENT) {
1163 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1164 return 0;
1165 }
1166
Quinn Trana4239942017-12-28 12:33:26 -08001167 fcport->login_retry--;
1168
Quinn Tran726b8542017-01-19 22:28:00 -08001169 switch (fcport->disc_state) {
1170 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001171 wwn = wwn_to_u64(fcport->node_name);
1172 if (wwn == 0) {
1173 ql_dbg(ql_dbg_disc, vha, 0xffff,
1174 "%s %d %8phC post GNNID\n",
1175 __func__, __LINE__, fcport->port_name);
1176 qla24xx_post_gnnid_work(vha, fcport);
1177 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001178 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1179 "%s %d %8phC post gnl\n",
1180 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001181 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001182 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001183 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001184 }
1185 break;
1186
1187 case DSC_GNL:
1188 if (fcport->login_pause) {
1189 fcport->last_rscn_gen = fcport->rscn_gen;
1190 fcport->last_login_gen = fcport->login_gen;
1191 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1192 break;
1193 }
1194
Quinn Tran9cd883f2017-12-28 12:33:24 -08001195 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001196 break;
1197
1198 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001199 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1200 "%s %d %8phC post gidpn\n",
1201 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001202 if (N2N_TOPO(vha->hw))
1203 qla_chk_n2n_b4_login(vha, fcport);
1204 else
1205 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001206 break;
1207
1208 case DSC_LOGIN_COMPLETE:
1209 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001210 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001211 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001212 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001213 data[0] = data[1] = 0;
1214 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001215 break;
1216
1217 default:
1218 break;
1219 }
1220
1221 return 0;
1222}
1223
1224static
1225void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1226{
1227 fcport->rscn_gen++;
1228
Quinn Tran83548fe2017-06-02 09:12:01 -07001229 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1230 "%s %8phC DS %d LS %d\n",
1231 __func__, fcport->port_name, fcport->disc_state,
1232 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001233
1234 if (fcport->flags & FCF_ASYNC_SENT)
1235 return;
1236
1237 switch (fcport->disc_state) {
1238 case DSC_DELETED:
1239 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001240 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001241 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001242 default:
1243 break;
1244 }
1245}
1246
1247int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001248 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001249{
1250 struct qla_work_evt *e;
1251 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1252 if (!e)
1253 return QLA_FUNCTION_FAILED;
1254
1255 e->u.new_sess.id = *id;
1256 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001257 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001258 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001259 if (node_name)
1260 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001261
1262 return qla2x00_post_work(vha, e);
1263}
1264
1265static
Quinn Tran726b8542017-01-19 22:28:00 -08001266void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1267 struct event_arg *ea)
1268{
1269 fc_port_t *fcport = ea->fcport;
1270
1271 if (fcport->scan_state != QLA_FCPORT_FOUND) {
1272 fcport->login_retry++;
1273 return;
1274 }
1275
Quinn Tran83548fe2017-06-02 09:12:01 -07001276 ql_dbg(ql_dbg_disc, vha, 0x2102,
1277 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1278 __func__, fcport->port_name, fcport->disc_state,
1279 fcport->fw_login_state, fcport->login_pause,
1280 fcport->deleted, fcport->conflict,
1281 fcport->last_rscn_gen, fcport->rscn_gen,
1282 fcport->last_login_gen, fcport->login_gen,
1283 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001284
1285 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001286 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1287 return;
1288
Quinn Tran5b334692017-03-15 09:48:48 -07001289 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001290 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1291 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001292 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001293 }
Quinn Tran5b334692017-03-15 09:48:48 -07001294 }
1295
Quinn Tran726b8542017-01-19 22:28:00 -08001296 if (fcport->flags & FCF_ASYNC_SENT) {
1297 fcport->login_retry++;
1298 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1299 return;
1300 }
1301
1302 if (fcport->disc_state == DSC_DELETE_PEND) {
1303 fcport->login_retry++;
1304 return;
1305 }
1306
1307 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001308 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001309 __func__, __LINE__, fcport->port_name);
1310
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001311 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001312 return;
1313 }
1314
1315 qla24xx_fcport_handle_login(vha, fcport);
1316}
1317
Quinn Tran41dc5292017-01-19 22:28:03 -08001318void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001319{
Quinn Tran41dc5292017-01-19 22:28:03 -08001320 fc_port_t *fcport, *f, *tf;
1321 uint32_t id = 0, mask, rid;
Quinn Tran726b8542017-01-19 22:28:00 -08001322 int rc;
1323
1324 switch (ea->event) {
1325 case FCME_RELOGIN:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001326 case FCME_RSCN:
1327 case FCME_GIDPN_DONE:
1328 case FCME_GPSC_DONE:
1329 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001330 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001331 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1332 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1333 return;
1334 break;
1335 default:
1336 break;
1337 }
1338
1339 switch (ea->event) {
1340 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001341 if (test_bit(UNLOADING, &vha->dpc_flags))
1342 return;
1343
1344 qla24xx_handle_relogin_event(vha, ea);
1345 break;
1346 case FCME_RSCN:
1347 if (test_bit(UNLOADING, &vha->dpc_flags))
1348 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001349 switch (ea->id.b.rsvd_1) {
1350 case RSCN_PORT_ADDR:
1351 fcport = qla2x00_find_fcport_by_nportid(vha, &ea->id, 1);
1352 if (!fcport) {
1353 /* cable moved */
1354 rc = qla24xx_post_gpnid_work(vha, &ea->id);
1355 if (rc) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001356 ql_log(ql_log_warn, vha, 0xd044,
1357 "RSCN GPNID work failed %02x%02x%02x\n",
1358 ea->id.b.domain, ea->id.b.area,
1359 ea->id.b.al_pa);
Quinn Tran41dc5292017-01-19 22:28:03 -08001360 }
1361 } else {
1362 ea->fcport = fcport;
1363 qla24xx_handle_rscn_event(fcport, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001364 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001365 break;
1366 case RSCN_AREA_ADDR:
1367 case RSCN_DOM_ADDR:
1368 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1369 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001370 ql_dbg(ql_dbg_async, vha, 0x5044,
1371 "RSCN: Area 0x%06x was affected\n",
1372 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001373 } else {
1374 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001375 ql_dbg(ql_dbg_async, vha, 0x507a,
1376 "RSCN: Domain 0x%06x was affected\n",
1377 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001378 }
1379
1380 rid = ea->id.b24 & mask;
1381 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1382 list) {
1383 id = f->d_id.b24 & mask;
1384 if (rid == id) {
1385 ea->fcport = f;
1386 qla24xx_handle_rscn_event(f, ea);
1387 }
1388 }
1389 break;
1390 case RSCN_FAB_ADDR:
1391 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001392 ql_log(ql_log_warn, vha, 0xd045,
1393 "RSCN: Fabric was affected. Addr format %d\n",
1394 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001395 qla2x00_mark_all_devices_lost(vha, 1);
1396 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1397 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001398 }
1399 break;
1400 case FCME_GIDPN_DONE:
1401 qla24xx_handle_gidpn_event(vha, ea);
1402 break;
1403 case FCME_GNL_DONE:
1404 qla24xx_handle_gnl_done_event(vha, ea);
1405 break;
1406 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001407 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001408 break;
1409 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1410 qla24xx_handle_plogi_done_event(vha, ea);
1411 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001412 case FCME_PRLI_DONE:
1413 qla24xx_handle_prli_done_event(vha, ea);
1414 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001415 case FCME_GPDB_DONE:
1416 qla24xx_handle_gpdb_event(vha, ea);
1417 break;
1418 case FCME_GPNID_DONE:
1419 qla24xx_handle_gpnid_event(vha, ea);
1420 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001421 case FCME_GFFID_DONE:
1422 qla24xx_handle_gffid_event(vha, ea);
1423 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001424 case FCME_ADISC_DONE:
1425 qla24xx_handle_adisc_event(vha, ea);
1426 break;
Quinn Trana4239942017-12-28 12:33:26 -08001427 case FCME_GNNID_DONE:
1428 qla24xx_handle_gnnid_event(vha, ea);
1429 break;
1430 case FCME_GFPNID_DONE:
1431 qla24xx_handle_gfpnid_event(vha, ea);
1432 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001433 default:
1434 BUG_ON(1);
1435 break;
1436 }
1437}
1438
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001439static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001440qla2x00_tmf_iocb_timeout(void *data)
1441{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001442 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001443 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1444
1445 tmf->u.tmf.comp_status = CS_TIMEOUT;
1446 complete(&tmf->u.tmf.comp);
1447}
1448
1449static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001450qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001451{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001452 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001453 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001454
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001455 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001456}
1457
1458int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001459qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001460 uint32_t tag)
1461{
1462 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001463 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001464 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001465 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001466
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001467 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001468 if (!sp)
1469 goto done;
1470
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001471 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001472 sp->type = SRB_TM_CMD;
1473 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001474 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1475 tm_iocb->u.tmf.flags = flags;
1476 tm_iocb->u.tmf.lun = lun;
1477 tm_iocb->u.tmf.data = tag;
1478 sp->done = qla2x00_tmf_sp_done;
1479 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1480 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001481
1482 rval = qla2x00_start_sp(sp);
1483 if (rval != QLA_SUCCESS)
1484 goto done_free_sp;
1485
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001486 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001487 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1488 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1489 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001490
1491 wait_for_completion(&tm_iocb->u.tmf.comp);
1492
1493 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1494 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1495
1496 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1497 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1498 "TM IOCB failed (%x).\n", rval);
1499 }
1500
1501 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1502 flags = tm_iocb->u.tmf.flags;
1503 lun = (uint16_t)tm_iocb->u.tmf.lun;
1504
1505 /* Issue Marker IOCB */
1506 qla2x00_marker(vha, vha->hw->req_q_map[0],
1507 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1508 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1509 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001510
1511done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001512 sp->free(sp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001513done:
1514 return rval;
1515}
1516
Armen Baloyan4440e462014-02-26 04:15:18 -05001517static void
1518qla24xx_abort_iocb_timeout(void *data)
1519{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001520 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001521 struct srb_iocb *abt = &sp->u.iocb_cmd;
1522
1523 abt->u.abt.comp_status = CS_TIMEOUT;
1524 complete(&abt->u.abt.comp);
1525}
1526
1527static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001528qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001529{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001530 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001531 struct srb_iocb *abt = &sp->u.iocb_cmd;
1532
1533 complete(&abt->u.abt.comp);
1534}
1535
Quinn Tran15f30a52017-03-15 09:48:52 -07001536int
Armen Baloyan4440e462014-02-26 04:15:18 -05001537qla24xx_async_abort_cmd(srb_t *cmd_sp)
1538{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001539 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001540 fc_port_t *fcport = cmd_sp->fcport;
1541 struct srb_iocb *abt_iocb;
1542 srb_t *sp;
1543 int rval = QLA_FUNCTION_FAILED;
1544
1545 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1546 if (!sp)
1547 goto done;
1548
1549 abt_iocb = &sp->u.iocb_cmd;
1550 sp->type = SRB_ABT_CMD;
1551 sp->name = "abort";
1552 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1553 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1554 sp->done = qla24xx_abort_sp_done;
1555 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1556 init_completion(&abt_iocb->u.abt.comp);
1557
1558 rval = qla2x00_start_sp(sp);
1559 if (rval != QLA_SUCCESS)
1560 goto done_free_sp;
1561
1562 ql_dbg(ql_dbg_async, vha, 0x507c,
1563 "Abort command issued - hdl=%x, target_id=%x\n",
1564 cmd_sp->handle, fcport->tgt_id);
1565
1566 wait_for_completion(&abt_iocb->u.abt.comp);
1567
1568 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1569 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1570
1571done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001572 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001573done:
1574 return rval;
1575}
1576
1577int
1578qla24xx_async_abort_command(srb_t *sp)
1579{
1580 unsigned long flags = 0;
1581
1582 uint32_t handle;
1583 fc_port_t *fcport = sp->fcport;
1584 struct scsi_qla_host *vha = fcport->vha;
1585 struct qla_hw_data *ha = vha->hw;
1586 struct req_que *req = vha->req;
1587
1588 spin_lock_irqsave(&ha->hardware_lock, flags);
1589 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1590 if (req->outstanding_cmds[handle] == sp)
1591 break;
1592 }
1593 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1594 if (handle == req->num_outstanding_cmds) {
1595 /* Command not found. */
1596 return QLA_FUNCTION_FAILED;
1597 }
1598 if (sp->type == SRB_FXIOCB_DCMD)
1599 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1600 FXDISC_ABORT_IOCTL);
1601
1602 return qla24xx_async_abort_cmd(sp);
1603}
1604
Quinn Tran726b8542017-01-19 22:28:00 -08001605static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001606qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1607{
1608 switch (ea->data[0]) {
1609 case MBS_COMMAND_COMPLETE:
1610 ql_dbg(ql_dbg_disc, vha, 0x2118,
1611 "%s %d %8phC post gpdb\n",
1612 __func__, __LINE__, ea->fcport->port_name);
1613
1614 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1615 ea->fcport->logout_on_delete = 1;
1616 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1617 break;
1618 default:
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001619 if (ea->fcport->n2n_flag) {
1620 ql_dbg(ql_dbg_disc, vha, 0x2118,
1621 "%s %d %8phC post fc4 prli\n",
1622 __func__, __LINE__, ea->fcport->port_name);
1623 ea->fcport->fc4f_nvme = 0;
1624 ea->fcport->n2n_flag = 0;
1625 qla24xx_post_prli_work(vha, ea->fcport);
1626 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001627 ql_dbg(ql_dbg_disc, vha, 0x2119,
1628 "%s %d %8phC unhandle event of %x\n",
1629 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1630 break;
1631 }
1632}
1633
1634static void
Quinn Tran726b8542017-01-19 22:28:00 -08001635qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001636{
Quinn Tran726b8542017-01-19 22:28:00 -08001637 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001638 u16 lid;
1639 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001640 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001641 struct fc_port *fcport = ea->fcport;
1642
1643 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1644 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1645 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1646 "%s %d %8phC Remote is trying to login\n",
1647 __func__, __LINE__, fcport->port_name);
1648 return;
1649 }
1650
1651 if (fcport->disc_state == DSC_DELETE_PEND)
1652 return;
1653
1654 if (ea->sp->gen2 != fcport->login_gen) {
1655 /* target side must have changed it. */
1656 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1657 "%s %8phC generation changed rscn %d|%d login %d|%d\n",
1658 __func__, fcport->port_name, fcport->last_rscn_gen,
1659 fcport->rscn_gen, fcport->last_login_gen,
1660 fcport->login_gen);
1661 return;
1662 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1663 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1664 __func__, __LINE__, fcport->port_name);
1665 qla24xx_post_gidpn_work(vha, fcport);
1666 return;
1667 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001668
Quinn Tran726b8542017-01-19 22:28:00 -08001669 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001670 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001671 /*
1672 * Driver must validate login state - If PRLI not complete,
1673 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1674 * requests.
1675 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001676 if (ea->fcport->fc4f_nvme) {
1677 ql_dbg(ql_dbg_disc, vha, 0x2117,
1678 "%s %d %8phC post prli\n",
1679 __func__, __LINE__, ea->fcport->port_name);
1680 qla24xx_post_prli_work(vha, ea->fcport);
1681 } else {
1682 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001683 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1684 __func__, __LINE__, ea->fcport->port_name,
1685 ea->fcport->loop_id, ea->fcport->d_id.b24);
1686
1687 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001688 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana084fd62017-12-04 14:45:00 -08001689 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001690 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1691 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001692 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001693 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1694 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1695
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001696 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1697 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001698 break;
1699 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001700 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001701 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1702
1703 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1704 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1705 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001706 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1707 else
Quinn Tran726b8542017-01-19 22:28:00 -08001708 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001709 break;
1710 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001711 /* data[1] = IO PARAM 1 = nport ID */
1712 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1713 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1714 cid.b.al_pa = ea->iop[1] & 0xff;
1715 cid.b.rsvd_1 = 0;
1716
Quinn Tran83548fe2017-06-02 09:12:01 -07001717 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1718 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1719 __func__, __LINE__, ea->fcport->port_name,
1720 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001721
1722 if (IS_SW_RESV_ADDR(cid)) {
1723 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1724 ea->fcport->loop_id = FC_NO_LOOP_ID;
1725 } else {
1726 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001727 }
Quinn Tran726b8542017-01-19 22:28:00 -08001728 qla24xx_post_gnl_work(vha, ea->fcport);
1729 break;
1730 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001731 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1732 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1733 __func__, __LINE__, ea->fcport->port_name,
1734 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1735 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001736
Quinn Trana084fd62017-12-04 14:45:00 -08001737 lid = ea->iop[1] & 0xffff;
1738 qlt_find_sess_invalidate_other(vha,
1739 wwn_to_u64(ea->fcport->port_name),
1740 ea->fcport->d_id, lid, &conflict_fcport);
1741
1742 if (conflict_fcport) {
1743 /*
1744 * Another fcport share the same loop_id/nport id.
1745 * Conflict fcport needs to finish cleanup before this
1746 * fcport can proceed to login.
1747 */
1748 conflict_fcport->conflict = ea->fcport;
1749 ea->fcport->login_pause = 1;
1750
1751 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1752 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1753 __func__, __LINE__, ea->fcport->port_name,
1754 ea->fcport->d_id.b24, lid);
1755 qla2x00_clear_loop_id(ea->fcport);
1756 qla24xx_post_gidpn_work(vha, ea->fcport);
1757 } else {
1758 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1759 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1760 __func__, __LINE__, ea->fcport->port_name,
1761 ea->fcport->d_id.b24, lid);
1762
1763 qla2x00_clear_loop_id(ea->fcport);
1764 set_bit(lid, vha->hw->loop_id_map);
1765 ea->fcport->loop_id = lid;
1766 ea->fcport->keep_nport_handle = 0;
1767 qlt_schedule_sess_for_deletion(ea->fcport, false);
1768 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001769 break;
1770 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001771 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001772}
1773
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001774void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001775qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1776 uint16_t *data)
1777{
Quinn Tran726b8542017-01-19 22:28:00 -08001778 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001779 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001780 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001781 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001782}
1783
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001784void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001785qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1786 uint16_t *data)
1787{
1788 if (data[0] == MBS_COMMAND_COMPLETE) {
1789 qla2x00_update_fcport(vha, fcport);
1790
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001791 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001792 }
1793
1794 /* Retry login. */
1795 fcport->flags &= ~FCF_ASYNC_SENT;
1796 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1797 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1798 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001799 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001800
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001801 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/****************************************************************************/
1805/* QLogic ISP2x00 Hardware Support Functions. */
1806/****************************************************************************/
1807
Saurav Kashyapfa492632012-11-21 02:40:29 -05001808static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001809qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1810{
1811 int rval = QLA_SUCCESS;
1812 struct qla_hw_data *ha = vha->hw;
1813 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001814 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001815
1816 qla83xx_idc_lock(vha, 0);
1817
1818 /* SV: TODO: Assign initialization timeout from
1819 * flash-info / other param
1820 */
1821 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1822 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1823
1824 /* Set our fcoe function presence */
1825 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1826 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1827 "Error while setting DRV-Presence.\n");
1828 rval = QLA_FUNCTION_FAILED;
1829 goto exit;
1830 }
1831
1832 /* Decide the reset ownership */
1833 qla83xx_reset_ownership(vha);
1834
1835 /*
1836 * On first protocol driver load:
1837 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1838 * register.
1839 * Others: Check compatibility with current IDC Major version.
1840 */
1841 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1842 if (ha->flags.nic_core_reset_owner) {
1843 /* Set IDC Major version */
1844 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1845 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1846
1847 /* Clearing IDC-Lock-Recovery register */
1848 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1849 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1850 /*
1851 * Clear further IDC participation if we are not compatible with
1852 * the current IDC Major Version.
1853 */
1854 ql_log(ql_log_warn, vha, 0xb07d,
1855 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1856 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1857 __qla83xx_clear_drv_presence(vha);
1858 rval = QLA_FUNCTION_FAILED;
1859 goto exit;
1860 }
1861 /* Each function sets its supported Minor version. */
1862 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1863 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1864 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1865
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001866 if (ha->flags.nic_core_reset_owner) {
1867 memset(config, 0, sizeof(config));
1868 if (!qla81xx_get_port_config(vha, config))
1869 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1870 QLA8XXX_DEV_READY);
1871 }
1872
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001873 rval = qla83xx_idc_state_handler(vha);
1874
1875exit:
1876 qla83xx_idc_unlock(vha, 0);
1877
1878 return rval;
1879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881/*
1882* qla2x00_initialize_adapter
1883* Initialize board.
1884*
1885* Input:
1886* ha = adapter block pointer.
1887*
1888* Returns:
1889* 0 = success
1890*/
1891int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001892qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001895 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001896 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001897
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001898 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1899 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001902 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001903 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001904 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001905 ha->flags.pci_channel_io_perm_failure = 0;
1906 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001907 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001908 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1909 atomic_set(&vha->loop_state, LOOP_DOWN);
1910 vha->device_flags = DFLG_NO_CABLE;
1911 vha->dpc_flags = 0;
1912 vha->flags.management_server_logged_in = 0;
1913 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ha->isp_abort_cnt = 0;
1915 ha->beacon_blink_led = 0;
1916
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001917 set_bit(0, ha->req_qid_map);
1918 set_bit(0, ha->rsp_qid_map);
1919
Chad Dupuiscfb09192011-11-18 09:03:07 -08001920 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001921 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001922 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001924 ql_log(ql_log_warn, vha, 0x0044,
1925 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return (rval);
1927 }
1928
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001929 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001931 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001932 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001933 ql_log(ql_log_fatal, vha, 0x004f,
1934 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001935 return rval;
1936 }
1937
1938 if (IS_QLA8044(ha)) {
1939 qla8044_read_reset_template(vha);
1940
1941 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1942 * If DONRESET_BIT0 is set, drivers should not set dev_state
1943 * to NEED_RESET. But if NEED_RESET is set, drivers should
1944 * should honor the reset. */
1945 if (ql2xdontresethba == 1)
1946 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001947 }
1948
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001949 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001950 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001951 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001952
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001953 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001955 if (ha->flags.disable_serdes) {
1956 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001957 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001958 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001959 return QLA_FUNCTION_FAILED;
1960 }
1961
Chad Dupuiscfb09192011-11-18 09:03:07 -08001962 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001963 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001965 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1966 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001967 if (rval)
1968 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001969 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001970 if (rval)
1971 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001973
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001974 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001975 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001976 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001977 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001978 "Unable to configure ISP84XX.\n");
1979 return QLA_FUNCTION_FAILED;
1980 }
1981 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001982
Quinn Tranead03852017-01-19 22:28:01 -08001983 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001984 rval = qla2x00_init_rings(vha);
1985
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001986 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001988 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001989 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001990 rval = qla84xx_init_chip(vha);
1991 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001992 ql_log(ql_log_warn, vha, 0x00d4,
1993 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07001994 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001995 }
1996 }
1997
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001998 /* Load the NIC Core f/w if we are the first protocol driver. */
1999 if (IS_QLA8031(ha)) {
2000 rval = qla83xx_nic_core_fw_load(vha);
2001 if (rval)
2002 ql_log(ql_log_warn, vha, 0x0124,
2003 "Error in initializing NIC Core f/w.\n");
2004 }
2005
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002006 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2007 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002008
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002009 if (IS_P3P_TYPE(ha))
2010 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2011 else
2012 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return (rval);
2015}
2016
2017/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002018 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * @ha: HA context
2020 *
2021 * Returns 0 on success.
2022 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002023int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002024qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002026 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002027 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002028 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002029 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002032 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002035 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2037
Andrew Vasquez737faec2008-10-24 15:13:45 -07002038 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002040 /* Get PCI bus information. */
2041 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002042 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002043 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2044
2045 return QLA_SUCCESS;
2046}
2047
2048/**
2049 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2050 * @ha: HA context
2051 *
2052 * Returns 0 on success.
2053 */
2054int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002055qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002056{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002057 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002058 unsigned long flags = 0;
2059 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002060 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002061 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002062
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002063 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002064 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002065
2066 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002067 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002068
2069 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2070 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002071 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002072
2073 /*
2074 * If this is a 2300 card and not 2312, reset the
2075 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2076 * the 2310 also reports itself as a 2300 so we need to get the
2077 * fb revision level -- a 6 indicates it really is a 2300 and
2078 * not a 2310.
2079 */
2080 if (IS_QLA2300(ha)) {
2081 spin_lock_irqsave(&ha->hardware_lock, flags);
2082
2083 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002084 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002085 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002086 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002087 break;
2088
2089 udelay(10);
2090 }
2091
2092 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002093 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2094 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002095
2096 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002097 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002098
2099 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002100 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002101
2102 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002103 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2104 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002105
2106 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002107 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002108 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002109 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002110 break;
2111
2112 udelay(10);
2113 }
2114
2115 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2116 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002117
2118 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2119
Andrew Vasquez737faec2008-10-24 15:13:45 -07002120 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002121
2122 /* Get PCI bus information. */
2123 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002124 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002125 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2126
2127 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002131 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2132 * @ha: HA context
2133 *
2134 * Returns 0 on success.
2135 */
2136int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002137qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002138{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002139 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002140 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002141 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002142 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002143
2144 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002145 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002146
2147 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002148 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002149 w &= ~PCI_COMMAND_INTX_DISABLE;
2150 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2151
2152 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2153
2154 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002155 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2156 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002157
2158 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002159 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002160 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002161
Andrew Vasquez737faec2008-10-24 15:13:45 -07002162 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002163
Auke Kok44c10132007-06-08 15:46:36 -07002164 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002165
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002166 /* Get PCI bus information. */
2167 spin_lock_irqsave(&ha->hardware_lock, flags);
2168 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2169 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2170
2171 return QLA_SUCCESS;
2172}
2173
2174/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002175 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2176 * @ha: HA context
2177 *
2178 * Returns 0 on success.
2179 */
2180int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002181qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002182{
2183 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002184 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002185
2186 pci_set_master(ha->pdev);
2187 pci_try_set_mwi(ha->pdev);
2188
2189 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2190 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2191 w &= ~PCI_COMMAND_INTX_DISABLE;
2192 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2193
2194 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002195 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002196 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002197
Andrew Vasquez737faec2008-10-24 15:13:45 -07002198 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002199
2200 ha->chip_revision = ha->pdev->revision;
2201
2202 return QLA_SUCCESS;
2203}
2204
2205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 * qla2x00_isp_firmware() - Choose firmware image.
2207 * @ha: HA context
2208 *
2209 * Returns 0 on success.
2210 */
2211static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002212qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002215 uint16_t loop_id, topo, sw_cap;
2216 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002217 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002220 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002223 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002226 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002227 if (rval == QLA_SUCCESS) {
2228 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002229 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002230 &area, &domain, &topo, &sw_cap);
2231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002234 if (rval)
2235 ql_dbg(ql_dbg_init, vha, 0x007a,
2236 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 return (rval);
2239}
2240
2241/**
2242 * qla2x00_reset_chip() - Reset ISP chip.
2243 * @ha: HA context
2244 *
2245 * Returns 0 on success.
2246 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002247void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002248qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002251 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002252 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 uint16_t cmd;
2255
Andrew Vasquez85880802009-12-15 21:29:46 -08002256 if (unlikely(pci_channel_offline(ha->pdev)))
2257 return;
2258
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002259 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 spin_lock_irqsave(&ha->hardware_lock, flags);
2262
2263 /* Turn off master enable */
2264 cmd = 0;
2265 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2266 cmd &= ~PCI_COMMAND_MASTER;
2267 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2268
2269 if (!IS_QLA2100(ha)) {
2270 /* Pause RISC. */
2271 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2272 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2273 for (cnt = 0; cnt < 30000; cnt++) {
2274 if ((RD_REG_WORD(&reg->hccr) &
2275 HCCR_RISC_PAUSE) != 0)
2276 break;
2277 udelay(100);
2278 }
2279 } else {
2280 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2281 udelay(10);
2282 }
2283
2284 /* Select FPM registers. */
2285 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2286 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2287
2288 /* FPM Soft Reset. */
2289 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2290 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2291
2292 /* Toggle Fpm Reset. */
2293 if (!IS_QLA2200(ha)) {
2294 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2295 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2296 }
2297
2298 /* Select frame buffer registers. */
2299 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2300 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2301
2302 /* Reset frame buffer FIFOs. */
2303 if (IS_QLA2200(ha)) {
2304 WRT_FB_CMD_REG(ha, reg, 0xa000);
2305 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2306 } else {
2307 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2308
2309 /* Read back fb_cmd until zero or 3 seconds max */
2310 for (cnt = 0; cnt < 3000; cnt++) {
2311 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2312 break;
2313 udelay(100);
2314 }
2315 }
2316
2317 /* Select RISC module registers. */
2318 WRT_REG_WORD(&reg->ctrl_status, 0);
2319 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2320
2321 /* Reset RISC processor. */
2322 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2323 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2324
2325 /* Release RISC processor. */
2326 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2327 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2328 }
2329
2330 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2331 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2332
2333 /* Reset ISP chip. */
2334 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2335
2336 /* Wait for RISC to recover from reset. */
2337 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2338 /*
2339 * It is necessary to for a delay here since the card doesn't
2340 * respond to PCI reads during a reset. On some architectures
2341 * this will result in an MCA.
2342 */
2343 udelay(20);
2344 for (cnt = 30000; cnt; cnt--) {
2345 if ((RD_REG_WORD(&reg->ctrl_status) &
2346 CSR_ISP_SOFT_RESET) == 0)
2347 break;
2348 udelay(100);
2349 }
2350 } else
2351 udelay(10);
2352
2353 /* Reset RISC processor. */
2354 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2355
2356 WRT_REG_WORD(&reg->semaphore, 0);
2357
2358 /* Release RISC processor. */
2359 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2360 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2361
2362 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2363 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002364 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 udelay(100);
2368 }
2369 } else
2370 udelay(100);
2371
2372 /* Turn on master enable */
2373 cmd |= PCI_COMMAND_MASTER;
2374 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2375
2376 /* Disable RISC pause on FPM parity error. */
2377 if (!IS_QLA2100(ha)) {
2378 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2379 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2380 }
2381
2382 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2383}
2384
2385/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002386 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2387 *
2388 * Returns 0 on success.
2389 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002390static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002391qla81xx_reset_mpi(scsi_qla_host_t *vha)
2392{
2393 uint16_t mb[4] = {0x1010, 0, 1, 0};
2394
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002395 if (!IS_QLA81XX(vha->hw))
2396 return QLA_SUCCESS;
2397
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002398 return qla81xx_write_mpi_register(vha, mb);
2399}
2400
2401/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002402 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002403 * @ha: HA context
2404 *
2405 * Returns 0 on success.
2406 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002407static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002408qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002409{
2410 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002411 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002412 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002413 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002414 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002415 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002416 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002417
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002418 spin_lock_irqsave(&ha->hardware_lock, flags);
2419
2420 /* Reset RISC. */
2421 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2422 for (cnt = 0; cnt < 30000; cnt++) {
2423 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2424 break;
2425
2426 udelay(10);
2427 }
2428
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002429 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2430 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2431
2432 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2433 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2434 RD_REG_DWORD(&reg->hccr),
2435 RD_REG_DWORD(&reg->ctrl_status),
2436 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2437
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002438 WRT_REG_DWORD(&reg->ctrl_status,
2439 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002440 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002441
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002442 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002443
Andrew Vasquez88c26662005-07-08 17:59:26 -07002444 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002445 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002446 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2447 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002448 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002449 if (cnt)
2450 udelay(5);
2451 else
2452 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002453 }
2454
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002455 if (rval == QLA_SUCCESS)
2456 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2457
2458 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2459 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2460 RD_REG_DWORD(&reg->hccr),
2461 RD_REG_DWORD(&reg->mailbox0));
2462
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002463 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002464 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002465 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002466 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002467 if ((RD_REG_DWORD(&reg->ctrl_status) &
2468 CSRX_ISP_SOFT_RESET) == 0)
2469 break;
2470
2471 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002472 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002473 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2474 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2475
2476 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2477 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2478 RD_REG_DWORD(&reg->hccr),
2479 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002480
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002481 /* If required, do an MPI FW reset now */
2482 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2483 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2484 if (++abts_cnt < 5) {
2485 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2486 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2487 } else {
2488 /*
2489 * We exhausted the ISP abort retries. We have to
2490 * set the board offline.
2491 */
2492 abts_cnt = 0;
2493 vha->flags.online = 0;
2494 }
2495 }
2496 }
2497
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002498 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2499 RD_REG_DWORD(&reg->hccr);
2500
2501 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2502 RD_REG_DWORD(&reg->hccr);
2503
2504 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2505 RD_REG_DWORD(&reg->hccr);
2506
Bart Van Assche52c82822015-07-09 07:23:26 -07002507 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002508 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002509 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002510 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002511 if (cnt)
2512 udelay(5);
2513 else
2514 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002515 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002516 if (rval == QLA_SUCCESS)
2517 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2518
2519 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2520 "Host Risc 0x%x, mailbox0 0x%x\n",
2521 RD_REG_DWORD(&reg->hccr),
2522 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002523
2524 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002525
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002526 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2527 "Driver in %s mode\n",
2528 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2529
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002530 if (IS_NOPOLLING_TYPE(ha))
2531 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002532
2533 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002534}
2535
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002536static void
2537qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2538{
2539 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2540
2541 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2542 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2543
2544}
2545
2546static void
2547qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2548{
2549 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2550
2551 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2552 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2553}
2554
2555static void
2556qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2557{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002558 uint32_t wd32 = 0;
2559 uint delta_msec = 100;
2560 uint elapsed_msec = 0;
2561 uint timeout_msec;
2562 ulong n;
2563
Joe Carnucciocc790762015-08-04 13:37:53 -04002564 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2565 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002566 return;
2567
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002568 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2569 udelay(100);
2570
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002571attempt:
2572 timeout_msec = TIMEOUT_SEMAPHORE;
2573 n = timeout_msec / delta_msec;
2574 while (n--) {
2575 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2576 qla25xx_read_risc_sema_reg(vha, &wd32);
2577 if (wd32 & RISC_SEMAPHORE)
2578 break;
2579 msleep(delta_msec);
2580 elapsed_msec += delta_msec;
2581 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2582 goto force;
2583 }
2584
2585 if (!(wd32 & RISC_SEMAPHORE))
2586 goto force;
2587
2588 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2589 goto acquired;
2590
2591 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2592 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2593 n = timeout_msec / delta_msec;
2594 while (n--) {
2595 qla25xx_read_risc_sema_reg(vha, &wd32);
2596 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2597 break;
2598 msleep(delta_msec);
2599 elapsed_msec += delta_msec;
2600 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2601 goto force;
2602 }
2603
2604 if (wd32 & RISC_SEMAPHORE_FORCE)
2605 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2606
2607 goto attempt;
2608
2609force:
2610 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2611
2612acquired:
2613 return;
2614}
2615
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002616/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002617 * qla24xx_reset_chip() - Reset ISP24xx chip.
2618 * @ha: HA context
2619 *
2620 * Returns 0 on success.
2621 */
2622void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002623qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002624{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002625 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002626
2627 if (pci_channel_offline(ha->pdev) &&
2628 ha->flags.pci_channel_io_perm_failure) {
2629 return;
2630 }
2631
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002632 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002633
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002634 qla25xx_manipulate_risc_semaphore(vha);
2635
Andrew Vasquez88c26662005-07-08 17:59:26 -07002636 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002637 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002638}
2639
2640/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 * qla2x00_chip_diag() - Test chip for proper operation.
2642 * @ha: HA context
2643 *
2644 * Returns 0 on success.
2645 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002646int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002647qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
2649 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002650 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002651 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 unsigned long flags = 0;
2653 uint16_t data;
2654 uint32_t cnt;
2655 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002656 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 /* Assume a failed state */
2659 rval = QLA_FUNCTION_FAILED;
2660
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002661 ql_dbg(ql_dbg_init, vha, 0x007b,
2662 "Testing device at %lx.\n", (u_long)&reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 spin_lock_irqsave(&ha->hardware_lock, flags);
2665
2666 /* Reset ISP chip. */
2667 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2668
2669 /*
2670 * We need to have a delay here since the card will not respond while
2671 * in reset causing an MCA on some architectures.
2672 */
2673 udelay(20);
2674 data = qla2x00_debounce_register(&reg->ctrl_status);
2675 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2676 udelay(5);
2677 data = RD_REG_WORD(&reg->ctrl_status);
2678 barrier();
2679 }
2680
2681 if (!cnt)
2682 goto chip_diag_failed;
2683
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002684 ql_dbg(ql_dbg_init, vha, 0x007c,
2685 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 /* Reset RISC processor. */
2688 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2689 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2690
2691 /* Workaround for QLA2312 PCI parity error */
2692 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2693 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2694 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2695 udelay(5);
2696 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002697 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699 } else
2700 udelay(10);
2701
2702 if (!cnt)
2703 goto chip_diag_failed;
2704
2705 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002706 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2709 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2710 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2711 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2712 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2713 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002714 ql_log(ql_log_warn, vha, 0x0062,
2715 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2716 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 goto chip_diag_failed;
2719 }
2720 ha->product_id[0] = mb[1];
2721 ha->product_id[1] = mb[2];
2722 ha->product_id[2] = mb[3];
2723 ha->product_id[3] = mb[4];
2724
2725 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002726 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2728 else
2729 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002730 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 if (IS_QLA2200(ha) &&
2733 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2734 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002735 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002737 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 ha->fw_transfer_size = 128;
2739 }
2740
2741 /* Wrap Incoming Mailboxes Test. */
2742 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2743
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002744 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002745 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002746 if (rval)
2747 ql_log(ql_log_warn, vha, 0x0080,
2748 "Failed mailbox send register test.\n");
2749 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /* Flag a successful rval */
2751 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 spin_lock_irqsave(&ha->hardware_lock, flags);
2753
2754chip_diag_failed:
2755 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002756 ql_log(ql_log_info, vha, 0x0081,
2757 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2760
2761 return (rval);
2762}
2763
2764/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002765 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2766 * @ha: HA context
2767 *
2768 * Returns 0 on success.
2769 */
2770int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002771qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002772{
2773 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002774 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002775 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002776
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002777 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002778 return QLA_SUCCESS;
2779
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002780 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002781
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002782 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002783 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002784 ql_log(ql_log_warn, vha, 0x0082,
2785 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002786 } else {
2787 /* Flag a successful rval */
2788 rval = QLA_SUCCESS;
2789 }
2790
2791 return rval;
2792}
2793
Quinn Tranad0a0b02017-12-28 12:33:14 -08002794static void
2795qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002796{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002797 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002798 dma_addr_t tc_dma;
2799 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002800 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002801
Quinn Tranad0a0b02017-12-28 12:33:14 -08002802 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002803 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002804 "%s: Offload Mem is already allocated.\n",
2805 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002806 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002807 }
2808
Quinn Tranad0a0b02017-12-28 12:33:14 -08002809 if (IS_FWI2_CAPABLE(ha)) {
2810 /* Allocate memory for Fibre Channel Event Buffer. */
2811 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2812 !IS_QLA27XX(ha))
2813 goto try_eft;
2814
2815 if (ha->fce)
2816 dma_free_coherent(&ha->pdev->dev,
2817 FCE_SIZE, ha->fce, ha->fce_dma);
2818
2819 /* Allocate memory for Fibre Channel Event Buffer. */
2820 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2821 GFP_KERNEL);
2822 if (!tc) {
2823 ql_log(ql_log_warn, vha, 0x00be,
2824 "Unable to allocate (%d KB) for FCE.\n",
2825 FCE_SIZE / 1024);
2826 goto try_eft;
2827 }
2828
2829 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2830 ha->fce_mb, &ha->fce_bufs);
2831 if (rval) {
2832 ql_log(ql_log_warn, vha, 0x00bf,
2833 "Unable to initialize FCE (%d).\n", rval);
2834 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2835 tc_dma);
2836 ha->flags.fce_enabled = 0;
2837 goto try_eft;
2838 }
2839 ql_dbg(ql_dbg_init, vha, 0x00c0,
2840 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2841
2842 ha->flags.fce_enabled = 1;
2843 ha->fce_dma = tc_dma;
2844 ha->fce = tc;
2845
2846try_eft:
2847 if (ha->eft)
2848 dma_free_coherent(&ha->pdev->dev,
2849 EFT_SIZE, ha->eft, ha->eft_dma);
2850
2851 /* Allocate memory for Extended Trace Buffer. */
2852 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2853 GFP_KERNEL);
2854 if (!tc) {
2855 ql_log(ql_log_warn, vha, 0x00c1,
2856 "Unable to allocate (%d KB) for EFT.\n",
2857 EFT_SIZE / 1024);
2858 goto eft_err;
2859 }
2860
2861 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2862 if (rval) {
2863 ql_log(ql_log_warn, vha, 0x00c2,
2864 "Unable to initialize EFT (%d).\n", rval);
2865 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2866 tc_dma);
2867 goto eft_err;
2868 }
2869 ql_dbg(ql_dbg_init, vha, 0x00c3,
2870 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2871
2872 ha->eft_dma = tc_dma;
2873 ha->eft = tc;
2874 }
2875
2876eft_err:
2877 return;
2878}
2879
2880void
2881qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2882{
2883 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2884 eft_size, fce_size, mq_size;
2885 struct qla_hw_data *ha = vha->hw;
2886 struct req_que *req = ha->req_q_map[0];
2887 struct rsp_que *rsp = ha->rsp_q_map[0];
2888 struct qla2xxx_fw_dump *fw_dump;
2889
Chad Dupuisf73cb692014-02-26 04:15:06 -05002890 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2891 req_q_size = rsp_q_size = 0;
2892
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002893 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2894 fixed_size = sizeof(struct qla2100_fw_dump);
2895 } else if (IS_QLA23XX(ha)) {
2896 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2897 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2898 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002899 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002900 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002901 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2902 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002903 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2904 else if (IS_QLA25XX(ha))
2905 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2906 else
2907 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002908
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002909 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2910 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002911 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002912 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002913 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002914 /*
2915 * Allocate maximum buffer size for all queues.
2916 * Resizing must be done at end-of-dump processing.
2917 */
2918 mq_size += ha->max_req_queues *
2919 (req->length * sizeof(request_t));
2920 mq_size += ha->max_rsp_queues *
2921 (rsp->length * sizeof(response_t));
2922 }
Arun Easi00876ae2013-03-25 02:21:37 -04002923 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002924 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002925 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002926 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2927 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002928 goto try_eft;
2929
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002930 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002931try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002932 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002933 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002934 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002935 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002936
Chad Dupuisf73cb692014-02-26 04:15:06 -05002937 if (IS_QLA27XX(ha)) {
2938 if (!ha->fw_dump_template) {
2939 ql_log(ql_log_warn, vha, 0x00ba,
2940 "Failed missing fwdump template\n");
2941 return;
2942 }
2943 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2944 ql_dbg(ql_dbg_init, vha, 0x00fa,
2945 "-> allocating fwdump (%x bytes)...\n", dump_size);
2946 goto allocate;
2947 }
2948
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002949 req_q_size = req->length * sizeof(request_t);
2950 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002951 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002952 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002953 ha->chain_offset = dump_size;
2954 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002955
Quinn Tranb945e772017-06-13 20:47:29 -07002956 if (ha->exchoffld_buf)
2957 dump_size += sizeof(struct qla2xxx_offld_chain) +
2958 ha->exchoffld_size;
2959 if (ha->exlogin_buf)
2960 dump_size += sizeof(struct qla2xxx_offld_chain) +
2961 ha->exlogin_size;
2962
Chad Dupuisf73cb692014-02-26 04:15:06 -05002963allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08002964 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
2965 fw_dump = vmalloc(dump_size);
2966 if (!fw_dump) {
2967 ql_log(ql_log_warn, vha, 0x00c4,
2968 "Unable to allocate (%d KB) for firmware dump.\n",
2969 dump_size / 1024);
2970 } else {
2971 if (ha->fw_dump)
2972 vfree(ha->fw_dump);
2973 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002974
Quinn Tranad0a0b02017-12-28 12:33:14 -08002975 ha->fw_dump_len = dump_size;
2976 ql_dbg(ql_dbg_init, vha, 0x00c5,
2977 "Allocated (%d KB) for firmware dump.\n",
2978 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002979
Quinn Tranad0a0b02017-12-28 12:33:14 -08002980 if (IS_QLA27XX(ha))
2981 return;
2982
2983 ha->fw_dump->signature[0] = 'Q';
2984 ha->fw_dump->signature[1] = 'L';
2985 ha->fw_dump->signature[2] = 'G';
2986 ha->fw_dump->signature[3] = 'C';
2987 ha->fw_dump->version = htonl(1);
2988
2989 ha->fw_dump->fixed_size = htonl(fixed_size);
2990 ha->fw_dump->mem_size = htonl(mem_size);
2991 ha->fw_dump->req_q_size = htonl(req_q_size);
2992 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
2993
2994 ha->fw_dump->eft_size = htonl(eft_size);
2995 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
2996 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
2997
2998 ha->fw_dump->header_size =
2999 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003000 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003001 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003002}
3003
Andrew Vasquez18e75552009-06-03 09:55:30 -07003004static int
3005qla81xx_mpi_sync(scsi_qla_host_t *vha)
3006{
3007#define MPS_MASK 0xe0
3008 int rval;
3009 uint16_t dc;
3010 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003011
3012 if (!IS_QLA81XX(vha->hw))
3013 return QLA_SUCCESS;
3014
3015 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3016 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003017 ql_log(ql_log_warn, vha, 0x0105,
3018 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003019 goto done;
3020 }
3021
3022 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3023 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3024 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003025 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003026 goto done_release;
3027 }
3028
3029 dc &= MPS_MASK;
3030 if (dc == (dw & MPS_MASK))
3031 goto done_release;
3032
3033 dw &= ~MPS_MASK;
3034 dw |= dc;
3035 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3036 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003037 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003038 }
3039
3040done_release:
3041 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3042 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003043 ql_log(ql_log_warn, vha, 0x006d,
3044 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003045 }
3046
3047done:
3048 return rval;
3049}
3050
Chad Dupuis8d93f552013-01-30 03:34:37 -05003051int
3052qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3053{
3054 /* Don't try to reallocate the array */
3055 if (req->outstanding_cmds)
3056 return QLA_SUCCESS;
3057
Michael Hernandezd7459522016-12-12 14:40:07 -08003058 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003059 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3060 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003061 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3062 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003063 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003064 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003065 }
3066
3067 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3068 req->num_outstanding_cmds, GFP_KERNEL);
3069
3070 if (!req->outstanding_cmds) {
3071 /*
3072 * Try to allocate a minimal size just so we can get through
3073 * initialization.
3074 */
3075 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3076 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3077 req->num_outstanding_cmds, GFP_KERNEL);
3078
3079 if (!req->outstanding_cmds) {
3080 ql_log(ql_log_fatal, NULL, 0x0126,
3081 "Failed to allocate memory for "
3082 "outstanding_cmds for req_que %p.\n", req);
3083 req->num_outstanding_cmds = 0;
3084 return QLA_FUNCTION_FAILED;
3085 }
3086 }
3087
3088 return QLA_SUCCESS;
3089}
3090
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003091#define PRINT_FIELD(_field, _flag, _str) { \
3092 if (a0->_field & _flag) {\
3093 if (p) {\
3094 strcat(ptr, "|");\
3095 ptr++;\
3096 leftover--;\
3097 } \
3098 len = snprintf(ptr, leftover, "%s", _str); \
3099 p = 1;\
3100 leftover -= len;\
3101 ptr += len; \
3102 } \
3103}
3104
3105static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3106{
3107#define STR_LEN 64
3108 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3109 u8 str[STR_LEN], *ptr, p;
3110 int leftover, len;
3111
3112 memset(str, 0, STR_LEN);
3113 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3114 ql_dbg(ql_dbg_init, vha, 0x015a,
3115 "SFP MFG Name: %s\n", str);
3116
3117 memset(str, 0, STR_LEN);
3118 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3119 ql_dbg(ql_dbg_init, vha, 0x015c,
3120 "SFP Part Name: %s\n", str);
3121
3122 /* media */
3123 memset(str, 0, STR_LEN);
3124 ptr = str;
3125 leftover = STR_LEN;
3126 p = len = 0;
3127 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3128 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3129 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3130 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3131 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3132 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3133 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3134 ql_dbg(ql_dbg_init, vha, 0x0160,
3135 "SFP Media: %s\n", str);
3136
3137 /* link length */
3138 memset(str, 0, STR_LEN);
3139 ptr = str;
3140 leftover = STR_LEN;
3141 p = len = 0;
3142 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3143 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3144 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3145 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3146 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3147 ql_dbg(ql_dbg_init, vha, 0x0196,
3148 "SFP Link Length: %s\n", str);
3149
3150 memset(str, 0, STR_LEN);
3151 ptr = str;
3152 leftover = STR_LEN;
3153 p = len = 0;
3154 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3155 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3156 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3157 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3158 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3159 ql_dbg(ql_dbg_init, vha, 0x016e,
3160 "SFP FC Link Tech: %s\n", str);
3161
3162 if (a0->length_km)
3163 ql_dbg(ql_dbg_init, vha, 0x016f,
3164 "SFP Distant: %d km\n", a0->length_km);
3165 if (a0->length_100m)
3166 ql_dbg(ql_dbg_init, vha, 0x0170,
3167 "SFP Distant: %d m\n", a0->length_100m*100);
3168 if (a0->length_50um_10m)
3169 ql_dbg(ql_dbg_init, vha, 0x0189,
3170 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3171 if (a0->length_62um_10m)
3172 ql_dbg(ql_dbg_init, vha, 0x018a,
3173 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3174 if (a0->length_om4_10m)
3175 ql_dbg(ql_dbg_init, vha, 0x0194,
3176 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3177 if (a0->length_om3_10m)
3178 ql_dbg(ql_dbg_init, vha, 0x0195,
3179 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3180}
3181
3182
3183/*
3184 * Return Code:
3185 * QLA_SUCCESS: no action
3186 * QLA_INTERFACE_ERROR: SFP is not there.
3187 * QLA_FUNCTION_FAILED: detected New SFP
3188 */
3189int
3190qla24xx_detect_sfp(scsi_qla_host_t *vha)
3191{
3192 int rc = QLA_SUCCESS;
3193 struct sff_8247_a0 *a;
3194 struct qla_hw_data *ha = vha->hw;
3195
3196 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3197 goto out;
3198
3199 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3200 if (rc)
3201 goto out;
3202
3203 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3204 qla2xxx_print_sfp_info(vha);
3205
3206 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3207 /* long range */
3208 ha->flags.detected_lr_sfp = 1;
3209
3210 if (a->length_km > 5 || a->length_100m > 50)
3211 ha->long_range_distance = LR_DISTANCE_10K;
3212 else
3213 ha->long_range_distance = LR_DISTANCE_5K;
3214
3215 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3216 ql_dbg(ql_dbg_async, vha, 0x507b,
3217 "Detected Long Range SFP.\n");
3218 } else {
3219 /* short range */
3220 ha->flags.detected_lr_sfp = 0;
3221 if (ha->flags.using_lr_setting)
3222 ql_dbg(ql_dbg_async, vha, 0x5084,
3223 "Detected Short Range SFP.\n");
3224 }
3225
3226 if (!vha->flags.init_done)
3227 rc = QLA_SUCCESS;
3228out:
3229 return rc;
3230}
3231
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003232/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 * qla2x00_setup_chip() - Load and start RISC firmware.
3234 * @ha: HA context
3235 *
3236 * Returns 0 on success.
3237 */
3238static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003239qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003241 int rval;
3242 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003243 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003244 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3245 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003246 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003247
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003248 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003249 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003250 if (rval == QLA_SUCCESS) {
3251 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003252 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003253 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003254 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003255 }
3256
Andrew Vasquez3db06522008-01-31 12:33:49 -08003257 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3258 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3259 spin_lock_irqsave(&ha->hardware_lock, flags);
3260 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3261 RD_REG_WORD(&reg->hccr);
3262 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Andrew Vasquez18e75552009-06-03 09:55:30 -07003265 qla81xx_mpi_sync(vha);
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003268 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003269 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003270 ql_dbg(ql_dbg_init, vha, 0x00c9,
3271 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003273 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (rval == QLA_SUCCESS) {
3275 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003276 ql_dbg(ql_dbg_init, vha, 0x00ca,
3277 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003279 if (ql2xexlogins)
3280 ha->flags.exlogins_enabled = 1;
3281
Quinn Tran99e1b682017-06-02 09:12:03 -07003282 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003283 ha->flags.exchoffld_enabled = 1;
3284
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003285 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003287 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003288 qla24xx_detect_sfp(vha);
3289
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003290 rval = qla2x00_set_exlogins_buffer(vha);
3291 if (rval != QLA_SUCCESS)
3292 goto failed;
3293
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003294 rval = qla2x00_set_exchoffld_buffer(vha);
3295 if (rval != QLA_SUCCESS)
3296 goto failed;
3297
Giridhar Malavalia9083012010-04-12 17:59:55 -07003298enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003299 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003300 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003301 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003302 else
3303 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003304 if (rval != QLA_SUCCESS)
3305 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003306 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003307 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003308 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003309 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003310 if ((!ha->max_npiv_vports) ||
3311 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003312 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003313 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003314 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003315 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003316 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003317
Chad Dupuis8d93f552013-01-30 03:34:37 -05003318 /*
3319 * Allocate the array of outstanding commands
3320 * now that we know the firmware resources.
3321 */
3322 rval = qla2x00_alloc_outstanding_cmds(ha,
3323 vha->req);
3324 if (rval != QLA_SUCCESS)
3325 goto failed;
3326
Quinn Tranad0a0b02017-12-28 12:33:14 -08003327 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3328 qla2x00_alloc_offload_mem(vha);
3329
3330 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003331 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003332
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003333 } else {
3334 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003337 ql_log(ql_log_fatal, vha, 0x00cd,
3338 "ISP Firmware failed checksum.\n");
3339 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003341 } else
3342 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Andrew Vasquez3db06522008-01-31 12:33:49 -08003344 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3345 /* Enable proper parity. */
3346 spin_lock_irqsave(&ha->hardware_lock, flags);
3347 if (IS_QLA2300(ha))
3348 /* SRAM parity */
3349 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3350 else
3351 /* SRAM, Instruction RAM and GP RAM parity */
3352 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3353 RD_REG_WORD(&reg->hccr);
3354 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3355 }
3356
Chad Dupuisf3982d82014-09-25 05:16:57 -04003357 if (IS_QLA27XX(ha))
3358 ha->flags.fac_supported = 1;
3359 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003360 uint32_t size;
3361
3362 rval = qla81xx_fac_get_sector_size(vha, &size);
3363 if (rval == QLA_SUCCESS) {
3364 ha->flags.fac_supported = 1;
3365 ha->fdt_block_size = size << 2;
3366 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003367 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003368 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3369 ha->fw_major_version, ha->fw_minor_version,
3370 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003371
Chad Dupuisf73cb692014-02-26 04:15:06 -05003372 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003373 ha->flags.fac_supported = 0;
3374 rval = QLA_SUCCESS;
3375 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003376 }
3377 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003378failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003380 ql_log(ql_log_fatal, vha, 0x00cf,
3381 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
3383
3384 return (rval);
3385}
3386
3387/**
3388 * qla2x00_init_response_q_entries() - Initializes response queue entries.
3389 * @ha: HA context
3390 *
3391 * Beginning of request ring has initialization control block already built
3392 * by nvram config routine.
3393 *
3394 * Returns 0 on success.
3395 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003396void
3397qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398{
3399 uint16_t cnt;
3400 response_t *pkt;
3401
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003402 rsp->ring_ptr = rsp->ring;
3403 rsp->ring_index = 0;
3404 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003405 pkt = rsp->ring_ptr;
3406 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 pkt->signature = RESPONSE_PROCESSED;
3408 pkt++;
3409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
3412/**
3413 * qla2x00_update_fw_options() - Read and process firmware options.
3414 * @ha: HA context
3415 *
3416 * Returns 0 on success.
3417 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003418void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003419qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
3421 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003422 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003425 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3428 return;
3429
3430 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003431 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3432 "Serial link options.\n");
3433 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3434 (uint8_t *)&ha->fw_seriallink_options,
3435 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3438 if (ha->fw_seriallink_options[3] & BIT_2) {
3439 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3440
3441 /* 1G settings */
3442 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3443 emphasis = (ha->fw_seriallink_options[2] &
3444 (BIT_4 | BIT_3)) >> 3;
3445 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003446 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 rx_sens = (ha->fw_seriallink_options[0] &
3448 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3449 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3450 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3451 if (rx_sens == 0x0)
3452 rx_sens = 0x3;
3453 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3454 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3455 ha->fw_options[10] |= BIT_5 |
3456 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3457 (tx_sens & (BIT_1 | BIT_0));
3458
3459 /* 2G settings */
3460 swing = (ha->fw_seriallink_options[2] &
3461 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3462 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3463 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003464 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 rx_sens = (ha->fw_seriallink_options[1] &
3466 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3467 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3468 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3469 if (rx_sens == 0x0)
3470 rx_sens = 0x3;
3471 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3472 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3473 ha->fw_options[11] |= BIT_5 |
3474 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3475 (tx_sens & (BIT_1 | BIT_0));
3476 }
3477
3478 /* FCP2 options. */
3479 /* Return command IOCBs without waiting for an ABTS to complete. */
3480 ha->fw_options[3] |= BIT_13;
3481
3482 /* LED scheme. */
3483 if (ha->flags.enable_led_scheme)
3484 ha->fw_options[2] |= BIT_12;
3485
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003486 /* Detect ISP6312. */
3487 if (IS_QLA6312(ha))
3488 ha->fw_options[2] |= BIT_13;
3489
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003490 /* Set Retry FLOGI in case of P2P connection */
3491 if (ha->operating_mode == P2P) {
3492 ha->fw_options[2] |= BIT_3;
3493 ql_dbg(ql_dbg_disc, vha, 0x2100,
3494 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3495 __func__, ha->fw_options[2]);
3496 }
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003499 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003502void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003503qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003504{
3505 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003506 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003507
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003508 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003509 return;
3510
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003511 /* Hold status IOCBs until ABTS response received. */
3512 if (ql2xfwholdabts)
3513 ha->fw_options[3] |= BIT_12;
3514
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003515 /* Set Retry FLOGI in case of P2P connection */
3516 if (ha->operating_mode == P2P) {
3517 ha->fw_options[2] |= BIT_3;
3518 ql_dbg(ql_dbg_disc, vha, 0x2101,
3519 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3520 __func__, ha->fw_options[2]);
3521 }
3522
Quinn Tran41dc5292017-01-19 22:28:03 -08003523 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003524 if (ql2xmvasynctoatio &&
3525 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003526 if (qla_tgt_mode_enabled(vha) ||
3527 qla_dual_mode_enabled(vha))
3528 ha->fw_options[2] |= BIT_11;
3529 else
3530 ha->fw_options[2] &= ~BIT_11;
3531 }
3532
Quinn Tranf7e761f2017-06-02 09:12:02 -07003533 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3534 /*
3535 * Tell FW to track each exchange to prevent
3536 * driver from using stale exchange.
3537 */
3538 if (qla_tgt_mode_enabled(vha) ||
3539 qla_dual_mode_enabled(vha))
3540 ha->fw_options[2] |= BIT_4;
3541 else
3542 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003543
3544 /* Reserve 1/2 of emergency exchanges for ELS.*/
3545 if (qla2xuseresexchforels)
3546 ha->fw_options[2] |= BIT_8;
3547 else
3548 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003549 }
3550
Quinn Tran83548fe2017-06-02 09:12:01 -07003551 ql_dbg(ql_dbg_init, vha, 0x00e8,
3552 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3553 __func__, ha->fw_options[1], ha->fw_options[2],
3554 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003555
3556 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3557 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003558
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003559 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003560 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003561 return;
3562
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003563 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003564 le16_to_cpu(ha->fw_seriallink_options24[1]),
3565 le16_to_cpu(ha->fw_seriallink_options24[2]),
3566 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003567 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003568 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003569 "Unable to update Serial Link options (%x).\n", rval);
3570 }
3571}
3572
3573void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003574qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003575{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003576 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003577 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003578 struct req_que *req = ha->req_q_map[0];
3579 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003580
3581 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003582 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3583 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003584 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3585 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3586 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3587 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3588 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3589 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003590
3591 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3592 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3593 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3594 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3595 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3596}
3597
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003598void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003599qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003600{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003601 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003602 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003603 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3604 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003605 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003606 uint16_t rid = 0;
3607 struct req_que *req = ha->req_q_map[0];
3608 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003609
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003610 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003611 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003612 icb->request_q_outpointer = cpu_to_le16(0);
3613 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003614 icb->request_q_length = cpu_to_le16(req->length);
3615 icb->response_q_length = cpu_to_le16(rsp->length);
3616 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3617 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3618 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3619 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003620
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003621 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003622 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003623 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3624 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3625 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3626
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003627 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003628 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003629
Chad Dupuisf73cb692014-02-26 04:15:06 -05003630 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003631 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3632 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003633 if (ha->flags.msix_enabled) {
3634 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003635 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003636 "Registering vector 0x%x for base que.\n",
3637 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003638 icb->msix = cpu_to_le16(msix->entry);
3639 }
3640 /* Use alternate PCI bus number */
3641 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003642 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003643 /* Use alternate PCI devfn */
3644 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003645 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003646
Anirban Chakraborty31557542009-12-02 10:36:55 -08003647 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003648 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3649 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003650 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003651 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003652 ql_dbg(ql_dbg_init, vha, 0x00fe,
3653 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003654 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003655 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003656 }
Bart Van Asschead950362015-07-09 07:24:08 -07003657 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003658
3659 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3660 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3661 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3662 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3663 } else {
3664 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3665 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3666 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3667 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3668 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003669 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003670
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003671 /* PCI posting */
3672 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003673}
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675/**
3676 * qla2x00_init_rings() - Initializes firmware.
3677 * @ha: HA context
3678 *
3679 * Beginning of request ring has initialization control block already built
3680 * by nvram config routine.
3681 *
3682 * Returns 0 on success.
3683 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003684int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003685qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 int rval;
3688 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003689 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003690 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003691 struct req_que *req;
3692 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003693 struct mid_init_cb_24xx *mid_init_cb =
3694 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
3696 spin_lock_irqsave(&ha->hardware_lock, flags);
3697
3698 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003699 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003700 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003701 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003702 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003703 req->out_ptr = (void *)(req->ring + req->length);
3704 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003705 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003706 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003708 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003709
3710 /* Initialize firmware. */
3711 req->ring_ptr = req->ring;
3712 req->ring_index = 0;
3713 req->cnt = req->length;
3714 }
3715
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003716 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003717 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003718 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003719 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003720 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3721 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003722 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003723 if (IS_QLAFX00(ha))
3724 qlafx00_init_response_q_entries(rsp);
3725 else
3726 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003729 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3730 ha->tgt.atio_ring_index = 0;
3731 /* Initialize ATIO queue entries */
3732 qlt_init_atio_q_entries(vha);
3733
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003734 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3737
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003738 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3739
3740 if (IS_QLAFX00(ha)) {
3741 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3742 goto next_check;
3743 }
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003746 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003748 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003749 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003750 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003751 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003752 }
3753
Andrew Vasquez24a08132009-03-24 09:08:16 -07003754 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003755 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003756 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003757 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003758 ha->flags.dport_enabled =
3759 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3760 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3761 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3762 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003763 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003764 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003765 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003766 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003767 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003768
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003769 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003770next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003772 ql_log(ql_log_fatal, vha, 0x00d2,
3773 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003775 ql_dbg(ql_dbg_init, vha, 0x00d3,
3776 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003777 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
3780 return (rval);
3781}
3782
3783/**
3784 * qla2x00_fw_ready() - Waits for firmware ready.
3785 * @ha: HA context
3786 *
3787 * Returns 0 on success.
3788 */
3789static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003790qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003793 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 uint16_t min_wait; /* Minimum wait time if loop is down */
3795 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003796 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003797 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003799 if (IS_QLAFX00(vha->hw))
3800 return qlafx00_fw_ready(vha);
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 rval = QLA_SUCCESS;
3803
Chad Dupuis334614912015-04-09 14:59:57 -04003804 /* Time to wait for loop down */
3805 if (IS_P3P_TYPE(ha))
3806 min_wait = 30;
3807 else
3808 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
3810 /*
3811 * Firmware should take at most one RATOV to login, plus 5 seconds for
3812 * our own processing.
3813 */
3814 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3815 wait_time = min_wait;
3816 }
3817
3818 /* Min wait time if loop down */
3819 mtime = jiffies + (min_wait * HZ);
3820
3821 /* wait time before firmware ready */
3822 wtime = jiffies + (wait_time * HZ);
3823
3824 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003825 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003826 ql_log(ql_log_info, vha, 0x801e,
3827 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003830 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003831 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003833 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003834 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003836 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003837 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3838 "fw_state=%x 84xx=%x.\n", state[0],
3839 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003840 if ((state[2] & FSTATE_LOGGED_IN) &&
3841 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003842 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3843 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003844
3845 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003846 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003847 if (rval != QLA_SUCCESS) {
3848 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003849 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003850 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003851 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003852 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003853
3854 /* Add time taken to initialize. */
3855 cs84xx_time = jiffies - cs84xx_time;
3856 wtime += cs84xx_time;
3857 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003858 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003859 "Increasing wait time by %ld. "
3860 "New time %ld.\n", cs84xx_time,
3861 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003862 }
3863 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003864 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3865 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003867 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 &ha->login_timeout, &ha->r_a_tov);
3869
3870 rval = QLA_SUCCESS;
3871 break;
3872 }
3873
3874 rval = QLA_FUNCTION_FAILED;
3875
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003876 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003877 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003879 * other than Wait for Login.
3880 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003882 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 "Cable is unplugged...\n");
3884
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003885 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 break;
3887 }
3888 }
3889 } else {
3890 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003891 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003892 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 break;
3894 }
3895
3896 if (time_after_eq(jiffies, wtime))
3897 break;
3898
3899 /* Delay for a while */
3900 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 } while (1);
3902
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003903 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003904 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3905 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Chad Dupuiscfb09192011-11-18 09:03:07 -08003907 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003908 ql_log(ql_log_warn, vha, 0x803b,
3909 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
3911
3912 return (rval);
3913}
3914
3915/*
3916* qla2x00_configure_hba
3917* Setup adapter context.
3918*
3919* Input:
3920* ha = adapter state pointer.
3921*
3922* Returns:
3923* 0 = success
3924*
3925* Context:
3926* Kernel context.
3927*/
3928static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003929qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930{
3931 int rval;
3932 uint16_t loop_id;
3933 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003934 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 uint8_t al_pa;
3936 uint8_t area;
3937 uint8_t domain;
3938 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003939 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003940 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003941 port_id_t id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003944 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003945 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003947 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003948 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003949 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003950 ql_dbg(ql_dbg_disc, vha, 0x2008,
3951 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003952 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003953 ql_log(ql_log_warn, vha, 0x2009,
3954 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003955 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3956 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3957 ql_log(ql_log_warn, vha, 0x1151,
3958 "Doing link init.\n");
3959 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3960 return rval;
3961 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003962 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return (rval);
3965 }
3966
3967 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003968 ql_log(ql_log_info, vha, 0x200a,
3969 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 return (QLA_FUNCTION_FAILED);
3971 }
3972
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003973 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
3975 /* initialize */
3976 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3977 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003978 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 switch (topo) {
3981 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003982 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 ha->current_topology = ISP_CFG_NL;
3984 strcpy(connect_type, "(Loop)");
3985 break;
3986
3987 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003988 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003989 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 ha->current_topology = ISP_CFG_FL;
3991 strcpy(connect_type, "(FL_Port)");
3992 break;
3993
3994 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003995 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 ha->operating_mode = P2P;
3997 ha->current_topology = ISP_CFG_N;
3998 strcpy(connect_type, "(N_Port-to-N_Port)");
3999 break;
4000
4001 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004002 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004003 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 ha->operating_mode = P2P;
4005 ha->current_topology = ISP_CFG_F;
4006 strcpy(connect_type, "(F_Port)");
4007 break;
4008
4009 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004010 ql_dbg(ql_dbg_disc, vha, 0x200f,
4011 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 ha->current_topology = ISP_CFG_NL;
4013 strcpy(connect_type, "(Loop)");
4014 break;
4015 }
4016
4017 /* Save Host port and loop ID. */
4018 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004019 id.b.domain = domain;
4020 id.b.area = area;
4021 id.b.al_pa = al_pa;
4022 id.b.rsvd_1 = 0;
4023 qlt_update_host_map(vha, id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004024
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004025 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004026 ql_log(ql_log_info, vha, 0x2010,
4027 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004028 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return(rval);
4031}
4032
Giridhar Malavalia9083012010-04-12 17:59:55 -07004033inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004034qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4035 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004036{
4037 char *st, *en;
4038 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004039 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004040 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004041 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004042
4043 if (memcmp(model, BINZERO, len) != 0) {
4044 strncpy(ha->model_number, model, len);
4045 st = en = ha->model_number;
4046 en += len - 1;
4047 while (en > st) {
4048 if (*en != 0x20 && *en != 0x00)
4049 break;
4050 *en-- = '\0';
4051 }
4052
4053 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004054 if (use_tbl &&
4055 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004056 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004057 strncpy(ha->model_desc,
4058 qla2x00_model_name[index * 2 + 1],
4059 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004060 } else {
4061 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004062 if (use_tbl &&
4063 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004064 index < QLA_MODEL_NAMES) {
4065 strcpy(ha->model_number,
4066 qla2x00_model_name[index * 2]);
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 strcpy(ha->model_number, def);
4072 }
4073 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004074 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004075 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004076 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004077}
4078
David Miller4e08df32007-04-16 12:37:43 -07004079/* On sparc systems, obtain port and node WWN from firmware
4080 * properties.
4081 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004082static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004083{
4084#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004085 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004086 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004087 struct device_node *dp = pci_device_to_OF_node(pdev);
4088 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004089 int len;
4090
4091 val = of_get_property(dp, "port-wwn", &len);
4092 if (val && len >= WWN_SIZE)
4093 memcpy(nv->port_name, val, WWN_SIZE);
4094
4095 val = of_get_property(dp, "node-wwn", &len);
4096 if (val && len >= WWN_SIZE)
4097 memcpy(nv->node_name, val, WWN_SIZE);
4098#endif
4099}
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101/*
4102* NVRAM configuration for ISP 2xxx
4103*
4104* Input:
4105* ha = adapter block pointer.
4106*
4107* Output:
4108* initialization control block in response_ring
4109* host adapters parameters in host adapter block
4110*
4111* Returns:
4112* 0 = success.
4113*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004114int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004115qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
David Miller4e08df32007-04-16 12:37:43 -07004117 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004118 uint8_t chksum = 0;
4119 uint16_t cnt;
4120 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004121 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004122 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004123 nvram_t *nv = ha->nvram;
4124 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004125 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
David Miller4e08df32007-04-16 12:37:43 -07004127 rval = QLA_SUCCESS;
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004130 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 ha->nvram_base = 0;
4132 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4133 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4134 ha->nvram_base = 0x80;
4135
4136 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004137 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004138 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4139 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004141 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4142 "Contents of NVRAM.\n");
4143 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4144 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 /* Bad NVRAM data, set defaults parameters. */
4147 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4148 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4149 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004150 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004151 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004152 "detected: checksum=0x%x id=%c version=0x%x.\n",
4153 chksum, nv->id[0], nv->nvram_version);
4154 ql_log(ql_log_warn, vha, 0x0065,
4155 "Falling back to "
4156 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004157
4158 /*
4159 * Set default initialization control block.
4160 */
4161 memset(nv, 0, ha->nvram_size);
4162 nv->parameter_block_version = ICB_VERSION;
4163
4164 if (IS_QLA23XX(ha)) {
4165 nv->firmware_options[0] = BIT_2 | BIT_1;
4166 nv->firmware_options[1] = BIT_7 | BIT_5;
4167 nv->add_firmware_options[0] = BIT_5;
4168 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004169 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004170 nv->special_options[1] = BIT_7;
4171 } else if (IS_QLA2200(ha)) {
4172 nv->firmware_options[0] = BIT_2 | BIT_1;
4173 nv->firmware_options[1] = BIT_7 | BIT_5;
4174 nv->add_firmware_options[0] = BIT_5;
4175 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004176 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004177 } else if (IS_QLA2100(ha)) {
4178 nv->firmware_options[0] = BIT_3 | BIT_1;
4179 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004180 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004181 }
4182
Bart Van Asschead950362015-07-09 07:24:08 -07004183 nv->max_iocb_allocation = cpu_to_le16(256);
4184 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004185 nv->retry_count = 8;
4186 nv->retry_delay = 1;
4187
4188 nv->port_name[0] = 33;
4189 nv->port_name[3] = 224;
4190 nv->port_name[4] = 139;
4191
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004192 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004193
4194 nv->login_timeout = 4;
4195
4196 /*
4197 * Set default host adapter parameters
4198 */
4199 nv->host_p[1] = BIT_2;
4200 nv->reset_delay = 5;
4201 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004202 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004203 nv->link_down_timeout = 60;
4204
4205 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
4207
4208#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4209 /*
4210 * The SN2 does not provide BIOS emulation which means you can't change
4211 * potentially bogus BIOS settings. Force the use of default settings
4212 * for link rate and frame size. Hope that the rest of the settings
4213 * are valid.
4214 */
4215 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004216 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (IS_QLA23XX(ha))
4218 nv->special_options[1] = BIT_7;
4219 }
4220#endif
4221
4222 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004223 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 /*
4226 * Setup driver NVRAM options.
4227 */
4228 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4229 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4230 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4231 nv->firmware_options[1] &= ~BIT_4;
4232
4233 if (IS_QLA23XX(ha)) {
4234 nv->firmware_options[0] |= BIT_2;
4235 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004236 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004237 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 if (IS_QLA2300(ha)) {
4240 if (ha->fb_rev == FPM_2310) {
4241 strcpy(ha->model_number, "QLA2310");
4242 } else {
4243 strcpy(ha->model_number, "QLA2300");
4244 }
4245 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004246 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004247 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
4249 } else if (IS_QLA2200(ha)) {
4250 nv->firmware_options[0] |= BIT_2;
4251 /*
4252 * 'Point-to-point preferred, else loop' is not a safe
4253 * connection mode setting.
4254 */
4255 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4256 (BIT_5 | BIT_4)) {
4257 /* Force 'loop preferred, else point-to-point'. */
4258 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4259 nv->add_firmware_options[0] |= BIT_5;
4260 }
4261 strcpy(ha->model_number, "QLA22xx");
4262 } else /*if (IS_QLA2100(ha))*/ {
4263 strcpy(ha->model_number, "QLA2100");
4264 }
4265
4266 /*
4267 * Copy over NVRAM RISC parameter block to initialization control block.
4268 */
4269 dptr1 = (uint8_t *)icb;
4270 dptr2 = (uint8_t *)&nv->parameter_block_version;
4271 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4272 while (cnt--)
4273 *dptr1++ = *dptr2++;
4274
4275 /* Copy 2nd half. */
4276 dptr1 = (uint8_t *)icb->add_firmware_options;
4277 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4278 while (cnt--)
4279 *dptr1++ = *dptr2++;
4280
Andrew Vasquez5341e862006-05-17 15:09:16 -07004281 /* Use alternate WWN? */
4282 if (nv->host_p[1] & BIT_7) {
4283 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4284 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4285 }
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 /* Prepare nodename */
4288 if ((icb->firmware_options[1] & BIT_6) == 0) {
4289 /*
4290 * Firmware will apply the following mask if the nodename was
4291 * not provided.
4292 */
4293 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4294 icb->node_name[0] &= 0xF0;
4295 }
4296
4297 /*
4298 * Set host adapter parameters.
4299 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004300
4301 /*
4302 * BIT_7 in the host-parameters section allows for modification to
4303 * internal driver logging.
4304 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004305 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004306 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4308 /* Always load RISC code on non ISP2[12]00 chips. */
4309 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4310 ha->flags.disable_risc_code_load = 0;
4311 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4312 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4313 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004314 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004315 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 ha->operating_mode =
4318 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4319
4320 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4321 sizeof(ha->fw_seriallink_options));
4322
4323 /* save HBA serial number */
4324 ha->serial0 = icb->port_name[5];
4325 ha->serial1 = icb->port_name[6];
4326 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004327 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4328 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Bart Van Asschead950362015-07-09 07:24:08 -07004330 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 ha->retry_count = nv->retry_count;
4333
4334 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004335 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 nv->login_timeout = ql2xlogintimeout;
4337 if (nv->login_timeout < 4)
4338 nv->login_timeout = 4;
4339 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004341 /* Set minimum RATOV to 100 tenths of a second. */
4342 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 ha->loop_reset_delay = nv->reset_delay;
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 /* Link Down Timeout = 0:
4347 *
4348 * When Port Down timer expires we will start returning
4349 * I/O's to OS with "DID_NO_CONNECT".
4350 *
4351 * Link Down Timeout != 0:
4352 *
4353 * The driver waits for the link to come up after link down
4354 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 if (nv->link_down_timeout == 0) {
4357 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004358 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 } else {
4360 ha->link_down_timeout = nv->link_down_timeout;
4361 ha->loop_down_abort_time =
4362 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 /*
4366 * Need enough time to try and get the port back.
4367 */
4368 ha->port_down_retry_count = nv->port_down_retry_count;
4369 if (qlport_down_retry)
4370 ha->port_down_retry_count = qlport_down_retry;
4371 /* Set login_retry_count */
4372 ha->login_retry_count = nv->retry_count;
4373 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4374 ha->port_down_retry_count > 3)
4375 ha->login_retry_count = ha->port_down_retry_count;
4376 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4377 ha->login_retry_count = ha->port_down_retry_count;
4378 if (ql2xloginretrycount)
4379 ha->login_retry_count = ql2xloginretrycount;
4380
Bart Van Asschead950362015-07-09 07:24:08 -07004381 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 icb->command_resource_count = 0;
4383 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004384 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
4386 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4387 /* Enable RIO */
4388 icb->firmware_options[0] &= ~BIT_3;
4389 icb->add_firmware_options[0] &=
4390 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4391 icb->add_firmware_options[0] |= BIT_2;
4392 icb->response_accumulation_timer = 3;
4393 icb->interrupt_delay_timer = 5;
4394
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004395 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004397 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004398 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004399 ha->zio_mode = icb->add_firmware_options[0] &
4400 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4401 ha->zio_timer = icb->interrupt_delay_timer ?
4402 icb->interrupt_delay_timer: 2;
4403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 icb->add_firmware_options[0] &=
4405 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004406 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004407 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004408 ha->zio_mode = QLA_ZIO_MODE_6;
4409
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004410 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004411 "ZIO mode %d enabled; timer delay (%d us).\n",
4412 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004414 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4415 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004416 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418 }
4419
David Miller4e08df32007-04-16 12:37:43 -07004420 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004421 ql_log(ql_log_warn, vha, 0x0069,
4422 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004423 }
4424 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004427static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004428qla2x00_rport_del(void *data)
4429{
4430 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004431 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004432 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004433
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004434 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004435 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004436 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004437 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004438 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004439 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4440 "%s %8phN. rport %p roles %x\n",
4441 __func__, fcport->port_name, rport,
4442 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004443
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004444 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004445 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004446}
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448/**
4449 * qla2x00_alloc_fcport() - Allocate a generic fcport.
4450 * @ha: HA context
4451 * @flags: allocation flags
4452 *
4453 * Returns a pointer to the allocated fcport, or NULL, if none available.
4454 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004455fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004456qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
4458 fc_port_t *fcport;
4459
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004460 fcport = kzalloc(sizeof(fc_port_t), flags);
4461 if (!fcport)
4462 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004465 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 fcport->port_type = FCT_UNKNOWN;
4467 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004468 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004469 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Quinn Tran726b8542017-01-19 22:28:00 -08004471 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4472 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004473 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004474 fcport->disc_state = DSC_DELETED;
4475 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4476 fcport->deleted = QLA_SESS_DELETED;
4477 fcport->login_retry = vha->hw->login_retry_count;
4478 fcport->login_retry = 5;
4479 fcport->logout_on_delete = 1;
4480
4481 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004482 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004483 "Failed to allocate ct_sns request.\n");
4484 kfree(fcport);
4485 fcport = NULL;
4486 }
4487 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4488 INIT_LIST_HEAD(&fcport->gnl_entry);
4489 INIT_LIST_HEAD(&fcport->list);
4490
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004491 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
4493
Quinn Tran726b8542017-01-19 22:28:00 -08004494void
4495qla2x00_free_fcport(fc_port_t *fcport)
4496{
4497 if (fcport->ct_desc.ct_sns) {
4498 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4499 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4500 fcport->ct_desc.ct_sns_dma);
4501
4502 fcport->ct_desc.ct_sns = NULL;
4503 }
4504 kfree(fcport);
4505}
4506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507/*
4508 * qla2x00_configure_loop
4509 * Updates Fibre Channel Device Database with what is actually on loop.
4510 *
4511 * Input:
4512 * ha = adapter block pointer.
4513 *
4514 * Returns:
4515 * 0 = success.
4516 * 1 = error.
4517 * 2 = database was full and device was not configured.
4518 */
4519static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004520qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
4522 int rval;
4523 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004524 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 rval = QLA_SUCCESS;
4526
4527 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004528 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4529 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004531 ql_dbg(ql_dbg_disc, vha, 0x2013,
4532 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 return (rval);
4534 }
4535 }
4536
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004537 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004538 ql_dbg(ql_dbg_disc, vha, 0x2014,
4539 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 /*
4542 * If we have both an RSCN and PORT UPDATE pending then handle them
4543 * both at the same time.
4544 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004545 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4546 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Michael Hernandez3064ff32009-12-15 21:29:44 -08004548 qla2x00_get_data_rate(vha);
4549
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 /* Determine what we need to do */
4551 if (ha->current_topology == ISP_CFG_FL &&
4552 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 set_bit(RSCN_UPDATE, &flags);
4555
4556 } else if (ha->current_topology == ISP_CFG_F &&
4557 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 set_bit(RSCN_UPDATE, &flags);
4560 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4561
Andrew Vasquez21333b42006-05-17 15:09:56 -07004562 } else if (ha->current_topology == ISP_CFG_N) {
4563 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004564 if (ha->flags.rida_fmt2) {
4565 /* With Rida Format 2, the login is already triggered.
4566 * We know who is on the other side of the wire.
4567 * No need to login to do login to find out or drop into
4568 * qla2x00_configure_local_loop().
4569 */
4570 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4571 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4572 } else {
4573 if (qla_tgt_mode_enabled(vha)) {
4574 /* allow the other side to start the login */
4575 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4576 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4577 }
4578 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004579 } else if (ha->current_topology == ISP_CFG_NL) {
4580 clear_bit(RSCN_UPDATE, &flags);
4581 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004582 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 set_bit(RSCN_UPDATE, &flags);
4585 set_bit(LOCAL_LOOP_UPDATE, &flags);
4586 }
4587
4588 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004589 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4590 ql_dbg(ql_dbg_disc, vha, 0x2015,
4591 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004593 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004594 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 }
4596
4597 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004598 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004599 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004600 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004602 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004603 else
4604 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
4606
4607 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004608 if (atomic_read(&vha->loop_down_timer) ||
4609 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 rval = QLA_FUNCTION_FAILED;
4611 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004612 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004613 ql_dbg(ql_dbg_disc, vha, 0x2069,
4614 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004615 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004616
4617 /*
4618 * Process any ATIO queue entries that came in
4619 * while we weren't online.
4620 */
Quinn Tranead03852017-01-19 22:28:01 -08004621 if (qla_tgt_mode_enabled(vha) ||
4622 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004623 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4624 spin_lock_irqsave(&ha->tgt.atio_lock,
4625 flags);
4626 qlt_24xx_process_atio_queue(vha, 0);
4627 spin_unlock_irqrestore(
4628 &ha->tgt.atio_lock, flags);
4629 } else {
4630 spin_lock_irqsave(&ha->hardware_lock,
4631 flags);
4632 qlt_24xx_process_atio_queue(vha, 1);
4633 spin_unlock_irqrestore(
4634 &ha->hardware_lock, flags);
4635 }
4636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
4638 }
4639
4640 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004641 ql_dbg(ql_dbg_disc, vha, 0x206a,
4642 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004644 ql_dbg(ql_dbg_disc, vha, 0x206b,
4645 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 }
4647
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004648 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004649 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004651 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004652 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004653 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
4656
4657 return (rval);
4658}
4659
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004660/*
4661 * N2N Login
4662 * Updates Fibre Channel Device Database with local loop devices.
4663 *
4664 * Input:
4665 * ha = adapter block pointer.
4666 *
4667 * Returns:
4668 */
4669static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4670 fc_port_t *fcport)
4671{
4672 struct qla_hw_data *ha = vha->hw;
4673 int res = QLA_SUCCESS, rval;
4674 int greater_wwpn = 0;
4675 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004677 if (ha->current_topology != ISP_CFG_N)
4678 return res;
4679
4680 if (wwn_to_u64(vha->port_name) >
4681 wwn_to_u64(vha->n2n_port_name)) {
4682 ql_dbg(ql_dbg_disc, vha, 0x2002,
4683 "HBA WWPN is greater %llx > target %llx\n",
4684 wwn_to_u64(vha->port_name),
4685 wwn_to_u64(vha->n2n_port_name));
4686 greater_wwpn = 1;
4687 fcport->d_id.b24 = vha->n2n_id;
4688 }
4689
4690 fcport->loop_id = vha->loop_id;
4691 fcport->fc4f_nvme = 0;
4692 fcport->query = 1;
4693
4694 ql_dbg(ql_dbg_disc, vha, 0x4001,
4695 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4696 fcport->d_id.b24, vha->loop_id);
4697
4698 /* Fill in member data. */
4699 if (!greater_wwpn) {
4700 rval = qla2x00_get_port_database(vha, fcport, 0);
4701 ql_dbg(ql_dbg_disc, vha, 0x1051,
4702 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4703 fcport->current_login_state, fcport->last_login_state,
4704 fcport->d_id.b24, fcport->loop_id, rval);
4705
4706 if (((fcport->current_login_state & 0xf) == 0x4) ||
4707 ((fcport->current_login_state & 0xf) == 0x6))
4708 logged_in = 1;
4709 }
4710
4711 if (logged_in || greater_wwpn) {
4712 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4713 qla_nvme_register_hba(vha);
4714
4715 /* Set connected N_Port d_id */
4716 if (vha->flags.nvme_enabled)
4717 fcport->fc4f_nvme = 1;
4718
4719 fcport->scan_state = QLA_FCPORT_FOUND;
4720 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4721 fcport->disc_state = DSC_GNL;
4722 fcport->n2n_flag = 1;
4723 fcport->flags = 3;
4724 vha->hw->flags.gpsc_supported = 0;
4725
4726 if (greater_wwpn) {
4727 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4728 "%s %d PLOGI ELS %8phC\n",
4729 __func__, __LINE__, fcport->port_name);
4730
4731 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4732 fcport, fcport->d_id);
4733 }
4734
4735 if (res != QLA_SUCCESS) {
4736 ql_log(ql_log_info, vha, 0xd04d,
4737 "PLOGI Failed: portid=%06x - retrying\n",
4738 fcport->d_id.b24);
4739 res = QLA_SUCCESS;
4740 } else {
4741 /* State 0x6 means FCP PRLI complete */
4742 if ((fcport->current_login_state & 0xf) == 0x6) {
4743 ql_dbg(ql_dbg_disc, vha, 0x2118,
4744 "%s %d %8phC post GPDB work\n",
4745 __func__, __LINE__, fcport->port_name);
4746 fcport->chip_reset =
4747 vha->hw->base_qpair->chip_reset;
4748 qla24xx_post_gpdb_work(vha, fcport, 0);
4749 } else {
4750 ql_dbg(ql_dbg_disc, vha, 0x2118,
4751 "%s %d %8phC post NVMe PRLI\n",
4752 __func__, __LINE__, fcport->port_name);
4753 qla24xx_post_prli_work(vha, fcport);
4754 }
4755 }
4756 } else {
4757 /* Wait for next database change */
4758 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4759 }
4760
4761 return res;
4762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
4764/*
4765 * qla2x00_configure_local_loop
4766 * Updates Fibre Channel Device Database with local loop devices.
4767 *
4768 * Input:
4769 * ha = adapter block pointer.
4770 *
4771 * Returns:
4772 * 0 = success.
4773 */
4774static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004775qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
4777 int rval, rval2;
4778 int found_devs;
4779 int found;
4780 fc_port_t *fcport, *new_fcport;
4781
4782 uint16_t index;
4783 uint16_t entries;
4784 char *id_iter;
4785 uint16_t loop_id;
4786 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004787 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004788 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 found_devs = 0;
4791 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004792 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004795 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004796 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 &entries);
4798 if (rval != QLA_SUCCESS)
4799 goto cleanup_allocation;
4800
Quinn Tran83548fe2017-06-02 09:12:01 -07004801 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004802 "Entries in ID list (%d).\n", entries);
4803 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4804 (uint8_t *)ha->gid_list,
4805 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
Quinn Tran9cd883f2017-12-28 12:33:24 -08004807 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4808 fcport->scan_state = QLA_FCPORT_SCAN;
4809 }
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004812 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004814 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004815 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 rval = QLA_MEMORY_ALLOC_FAILED;
4817 goto cleanup_allocation;
4818 }
4819 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4820
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004821 /* Inititae N2N login. */
4822 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4823 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4824 if (rval != QLA_SUCCESS)
4825 goto cleanup_allocation;
4826 return QLA_SUCCESS;
4827 }
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 /* Add devices to port list. */
4830 id_iter = (char *)ha->gid_list;
4831 for (index = 0; index < entries; index++) {
4832 domain = ((struct gid_list_info *)id_iter)->domain;
4833 area = ((struct gid_list_info *)id_iter)->area;
4834 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004835 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 loop_id = (uint16_t)
4837 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004838 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 loop_id = le16_to_cpu(
4840 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004841 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
4843 /* Bypass reserved domain fields. */
4844 if ((domain & 0xf0) == 0xf0)
4845 continue;
4846
4847 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004848 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004849 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 continue;
4851
4852 /* Bypass invalid local loop ID. */
4853 if (loop_id > LAST_LOCAL_LOOP_ID)
4854 continue;
4855
Quinn Tran41dc5292017-01-19 22:28:03 -08004856 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 /* Fill in member data. */
4859 new_fcport->d_id.b.domain = domain;
4860 new_fcport->d_id.b.area = area;
4861 new_fcport->d_id.b.al_pa = al_pa;
4862 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004863 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004864
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004865 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004867 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004868 "Failed to retrieve fcport information "
4869 "-- get_port_database=%x, loop_id=0x%04x.\n",
4870 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004871 /* Skip retry if N2N */
4872 if (ha->current_topology != ISP_CFG_N) {
4873 ql_dbg(ql_dbg_disc, vha, 0x2105,
4874 "Scheduling resync.\n");
4875 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4876 continue;
4877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
4879
Quinn Tran41dc5292017-01-19 22:28:03 -08004880 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 /* Check for matching device in port list. */
4882 found = 0;
4883 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004884 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 if (memcmp(new_fcport->port_name, fcport->port_name,
4886 WWN_SIZE))
4887 continue;
4888
Shyam Sundarddb9b122009-03-24 09:08:10 -07004889 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 fcport->loop_id = new_fcport->loop_id;
4891 fcport->port_type = new_fcport->port_type;
4892 fcport->d_id.b24 = new_fcport->d_id.b24;
4893 memcpy(fcport->node_name, new_fcport->node_name,
4894 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004895 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 found++;
4897 break;
4898 }
4899
4900 if (!found) {
4901 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004902 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
4904 /* Allocate a new replacement fcport. */
4905 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004906
4907 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4908
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004909 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004912 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004913 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 rval = QLA_MEMORY_ALLOC_FAILED;
4915 goto cleanup_allocation;
4916 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004917 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4919 }
4920
Quinn Tran41dc5292017-01-19 22:28:03 -08004921 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4922
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004923 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004924 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 found_devs++;
4927 }
4928
Quinn Tran9cd883f2017-12-28 12:33:24 -08004929 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4930 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4931 break;
4932
4933 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4934 if ((qla_dual_mode_enabled(vha) ||
4935 qla_ini_mode_enabled(vha)) &&
4936 atomic_read(&fcport->state) == FCS_ONLINE) {
4937 qla2x00_mark_device_lost(vha, fcport,
4938 ql2xplogiabsentdevice, 0);
4939 if (fcport->loop_id != FC_NO_LOOP_ID &&
4940 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4941 fcport->port_type != FCT_INITIATOR &&
4942 fcport->port_type != FCT_BROADCAST) {
4943 ql_dbg(ql_dbg_disc, vha, 0x20f0,
4944 "%s %d %8phC post del sess\n",
4945 __func__, __LINE__,
4946 fcport->port_name);
4947
4948 qlt_schedule_sess_for_deletion_lock
4949 (fcport);
4950 continue;
4951 }
4952 }
4953 }
4954
4955 if (fcport->scan_state == QLA_FCPORT_FOUND)
4956 qla24xx_fcport_handle_login(vha, fcport);
4957 }
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004960 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004963 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004964 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 }
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return (rval);
4968}
4969
4970static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004971qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004972{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004973 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004974 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004975 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004976
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004977 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004978 return;
4979
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07004980 if (atomic_read(&fcport->state) != FCS_ONLINE)
4981 return;
4982
Andrew Vasquez39bd9622007-09-20 14:07:34 -07004983 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
4984 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004985 return;
4986
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004987 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004988 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004989 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004990 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004991 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
4992 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004993 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004994 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004995 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05004996 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004997 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004998 }
4999}
5000
Quinn Tran726b8542017-01-19 22:28:00 -08005001/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005002static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005003qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005004{
5005 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005006 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005007 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005008
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005009 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5010 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005011 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5012 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5013 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005014 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005015 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005016 ql_log(ql_log_warn, vha, 0x2006,
5017 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005018 return;
5019 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005020
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005021 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005022 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005023 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005024
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005025 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005026
5027 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005028 if (fcport->port_type == FCT_INITIATOR)
5029 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5030 if (fcport->port_type == FCT_TARGET)
5031 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005032
Quinn Tran83548fe2017-06-02 09:12:01 -07005033 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5034 "%s %8phN. rport %p is %s mode\n",
5035 __func__, fcport->port_name, rport,
5036 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005037
Andrew Vasquez77d74142005-07-08 18:00:36 -07005038 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040
5041/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005042 * qla2x00_update_fcport
5043 * Updates device on list.
5044 *
5045 * Input:
5046 * ha = adapter block pointer.
5047 * fcport = port structure pointer.
5048 *
5049 * Return:
5050 * 0 - Success
5051 * BIT_0 - error
5052 *
5053 * Context:
5054 * Kernel context.
5055 */
5056void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005057qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005058{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005059 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005060
Quinn Tran726b8542017-01-19 22:28:00 -08005061 if (IS_SW_RESV_ADDR(fcport->d_id))
5062 return;
5063
Quinn Tran83548fe2017-06-02 09:12:01 -07005064 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005065 __func__, fcport->port_name);
5066
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005067 if (IS_QLAFX00(vha->hw)) {
5068 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005069 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005070 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005071 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005072 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005073 fcport->disc_state = DSC_LOGIN_COMPLETE;
5074 fcport->deleted = 0;
5075 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005076
Duane Grigsbye84067d2017-06-21 13:48:43 -07005077 if (fcport->fc4f_nvme) {
5078 qla_nvme_register_remote(vha, fcport);
5079 return;
5080 }
5081
Joe Carnuccio1f93da522012-11-21 02:40:38 -05005082 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005083 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005084 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005085
5086reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005087 switch (vha->host->active_mode) {
5088 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005089 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005090 break;
5091 case MODE_TARGET:
5092 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5093 !vha->vha_tgt.qla_tgt->tgt_stopped)
5094 qlt_fc_port_added(vha, fcport);
5095 break;
5096 case MODE_DUAL:
5097 qla2x00_reg_remote_port(vha, fcport);
5098 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5099 !vha->vha_tgt.qla_tgt->tgt_stopped)
5100 qlt_fc_port_added(vha, fcport);
5101 break;
5102 default:
5103 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005104 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005105}
5106
5107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 * qla2x00_configure_fabric
5109 * Setup SNS devices with loop ID's.
5110 *
5111 * Input:
5112 * ha = adapter block pointer.
5113 *
5114 * Returns:
5115 * 0 = success.
5116 * BIT_0 = error
5117 */
5118static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005119qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120{
Arun Easib3b02e62012-02-09 11:15:39 -08005121 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005122 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005124 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005126 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005127 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
5129 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005130 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005131 loop_id = NPH_F_PORT;
5132 else
5133 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005134 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005136 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005137 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005139 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 return (QLA_SUCCESS);
5141 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005142 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Quinn Tran41dc5292017-01-19 22:28:03 -08005144
5145 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5146 rval = qla2x00_send_change_request(vha, 0x3, 0);
5147 if (rval != QLA_SUCCESS)
5148 ql_log(ql_log_warn, vha, 0x121,
5149 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5150 rval);
5151 }
5152
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005155 qla2x00_mgmt_svr_login(vha);
5156
Andrew Vasquezcca53352005-08-26 19:08:30 -07005157 /* FDMI support. */
5158 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005159 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5160 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005163 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005164 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5165 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005166 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5167 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5168 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5169 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005170 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005171 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005172 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005173 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5174 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005176 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005177 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005178 if (test_bit(LOOP_RESYNC_NEEDED,
5179 &vha->dpc_flags))
5180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005182 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005184 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005185 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005186 if (test_bit(LOOP_RESYNC_NEEDED,
5187 &vha->dpc_flags))
5188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005190 if (vha->flags.nvme_enabled) {
5191 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5192 ql_dbg(ql_dbg_disc, vha, 0x2049,
5193 "Register NVME FC Type Features failed.\n");
5194 }
5195 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005196 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005198 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005199 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005200 if (test_bit(LOOP_RESYNC_NEEDED,
5201 &vha->dpc_flags))
5202 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005203 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005205 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005206 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005207 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
5210 }
5211
Joe Carnuccio827210b2013-02-08 01:57:57 -05005212 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5213 fcport->scan_state = QLA_FCPORT_SCAN;
5214 }
5215
Alexei Potashnikdf673272015-07-14 16:00:46 -04005216 /* Mark the time right before querying FW for connected ports.
5217 * This process is long, asynchronous and by the time it's done,
5218 * collected information might not be accurate anymore. E.g.
5219 * disconnected port might have re-connected and a brand new
5220 * session has been created. In this case session's generation
5221 * will be newer than discovery_gen. */
5222 qlt_do_generation_tick(vha, &discovery_gen);
5223
Quinn Trana4239942017-12-28 12:33:26 -08005224 if (USE_ASYNC_SCAN(ha)) {
5225 rval = QLA_SUCCESS;
5226 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5227 if (rval)
5228 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5229 } else {
5230 rval = qla2x00_find_all_fabric_devs(vha);
5231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (rval != QLA_SUCCESS)
5233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 } while (0);
5235
Duane Grigsbye84067d2017-06-21 13:48:43 -07005236 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5237 qla_nvme_register_hba(vha);
5238
Quinn Tran726b8542017-01-19 22:28:00 -08005239 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005240 ql_dbg(ql_dbg_disc, vha, 0x2068,
5241 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
5243 return (rval);
5244}
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246/*
5247 * qla2x00_find_all_fabric_devs
5248 *
5249 * Input:
5250 * ha = adapter block pointer.
5251 * dev = database device entry pointer.
5252 *
5253 * Returns:
5254 * 0 = success.
5255 *
5256 * Context:
5257 * Kernel context.
5258 */
5259static int
Quinn Tran726b8542017-01-19 22:28:00 -08005260qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261{
5262 int rval;
5263 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005264 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 int found;
5266
5267 sw_info_t *swl;
5268 int swl_idx;
5269 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005270 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005271 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005272 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005273 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
5275 rval = QLA_SUCCESS;
5276
5277 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005278 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005279 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005280 GFP_KERNEL);
5281 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005282 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005284 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005285 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005287 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005288 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005290 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5291 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005292 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005294 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5295 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005296 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005298 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5299 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005300 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5301 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005302 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5303 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005305
5306 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005307 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005308 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005309 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5310 return rval;
5311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 }
5313 swl_idx = 0;
5314
5315 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005316 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005318 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005319 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 return (QLA_MEMORY_ALLOC_FAILED);
5321 }
5322 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 /* Set start port ID scan at adapter ID. */
5324 first_dev = 1;
5325 last_dev = 0;
5326
5327 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005328 loop_id = ha->min_external_loopid;
5329 for (; loop_id <= ha->max_loop_id; loop_id++) {
5330 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 continue;
5332
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005333 if (ha->current_topology == ISP_CFG_FL &&
5334 (atomic_read(&vha->loop_down_timer) ||
5335 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005336 atomic_set(&vha->loop_down_timer, 0);
5337 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5338 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
5342 if (swl != NULL) {
5343 if (last_dev) {
5344 wrap.b24 = new_fcport->d_id.b24;
5345 } else {
5346 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5347 memcpy(new_fcport->node_name,
5348 swl[swl_idx].node_name, WWN_SIZE);
5349 memcpy(new_fcport->port_name,
5350 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005351 memcpy(new_fcport->fabric_port_name,
5352 swl[swl_idx].fabric_port_name, WWN_SIZE);
5353 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005354 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005356 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005357 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005358 if (vha->flags.nvme_enabled &&
5359 swl[swl_idx].fc4f_nvme) {
5360 new_fcport->fc4f_nvme =
5361 swl[swl_idx].fc4f_nvme;
5362 ql_log(ql_log_info, vha, 0x2131,
5363 "FOUND: NVME port %8phC as FC Type 28h\n",
5364 new_fcport->port_name);
5365 }
5366
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5368 last_dev = 1;
5369 }
5370 swl_idx++;
5371 }
5372 } else {
5373 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005374 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005376 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005377 "SNS scan failed -- assuming "
5378 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 rval = QLA_SUCCESS;
5380 break;
5381 }
5382 }
5383
5384 /* If wrap on switch device list, exit. */
5385 if (first_dev) {
5386 wrap.b24 = new_fcport->d_id.b24;
5387 first_dev = 0;
5388 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005389 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005390 "Device wrap (%02x%02x%02x).\n",
5391 new_fcport->d_id.b.domain,
5392 new_fcport->d_id.b.area,
5393 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 break;
5395 }
5396
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005397 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005398 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 continue;
5400
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005401 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005402 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5403 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005404
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005405 /* Bypass if same domain and area of adapter. */
5406 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005407 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005408 ISP_CFG_FL)
5409 continue;
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 /* Bypass reserved domain fields. */
5412 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5413 continue;
5414
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005415 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005416 if (ql2xgffidenable &&
5417 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5418 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005419 continue;
5420
Quinn Tran726b8542017-01-19 22:28:00 -08005421 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 /* Locate matching device in database. */
5424 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005425 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 if (memcmp(new_fcport->port_name, fcport->port_name,
5427 WWN_SIZE))
5428 continue;
5429
Joe Carnuccio827210b2013-02-08 01:57:57 -05005430 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005431
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 found++;
5433
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005434 /* Update port state. */
5435 memcpy(fcport->fabric_port_name,
5436 new_fcport->fabric_port_name, WWN_SIZE);
5437 fcport->fp_speed = new_fcport->fp_speed;
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005440 * If address the same and state FCS_ONLINE
5441 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 */
5443 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005444 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005445 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 break;
5447 }
5448
5449 /*
5450 * If device was not a fabric device before.
5451 */
5452 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5453 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005454 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 fcport->flags |= (FCF_FABRIC_DEVICE |
5456 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 break;
5458 }
5459
5460 /*
5461 * Port ID changed or device was marked to be updated;
5462 * Log it out if still logged in and mark it for
5463 * relogin later.
5464 */
Quinn Tran726b8542017-01-19 22:28:00 -08005465 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005466 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5467 "port changed FC ID, %8phC"
5468 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5469 fcport->port_name,
5470 fcport->d_id.b.domain,
5471 fcport->d_id.b.area,
5472 fcport->d_id.b.al_pa,
5473 fcport->loop_id,
5474 new_fcport->d_id.b.domain,
5475 new_fcport->d_id.b.area,
5476 new_fcport->d_id.b.al_pa);
5477 fcport->d_id.b24 = new_fcport->d_id.b24;
5478 break;
5479 }
5480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 fcport->d_id.b24 = new_fcport->d_id.b24;
5482 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 break;
5484 }
5485
Quinn Tran726b8542017-01-19 22:28:00 -08005486 if (found) {
5487 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005491 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005492 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5493
5494 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5495
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
5497 /* Allocate a new replacement fcport. */
5498 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005499 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005501 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005502 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 return (QLA_MEMORY_ALLOC_FAILED);
5504 }
5505 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5506 new_fcport->d_id.b24 = nxt_d_id.b24;
5507 }
5508
Quinn Tran726b8542017-01-19 22:28:00 -08005509 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Quinn Tran726b8542017-01-19 22:28:00 -08005511 /*
5512 * Logout all previous fabric dev marked lost, except FCP2 devices.
5513 */
5514 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5515 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5516 break;
5517
5518 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5519 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5520 continue;
5521
5522 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5523 if ((qla_dual_mode_enabled(vha) ||
5524 qla_ini_mode_enabled(vha)) &&
5525 atomic_read(&fcport->state) == FCS_ONLINE) {
5526 qla2x00_mark_device_lost(vha, fcport,
5527 ql2xplogiabsentdevice, 0);
5528 if (fcport->loop_id != FC_NO_LOOP_ID &&
5529 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5530 fcport->port_type != FCT_INITIATOR &&
5531 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005532 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005533 "%s %d %8phC post del sess\n",
5534 __func__, __LINE__,
5535 fcport->port_name);
5536
5537 qlt_schedule_sess_for_deletion_lock
5538 (fcport);
5539 continue;
5540 }
5541 }
5542 }
5543
5544 if (fcport->scan_state == QLA_FCPORT_FOUND)
5545 qla24xx_fcport_handle_login(vha, fcport);
5546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 return (rval);
5548}
5549
5550/*
5551 * qla2x00_find_new_loop_id
5552 * Scan through our port list and find a new usable loop ID.
5553 *
5554 * Input:
5555 * ha: adapter state pointer.
5556 * dev: port structure pointer.
5557 *
5558 * Returns:
5559 * qla2x00 local function return status code.
5560 *
5561 * Context:
5562 * Kernel context.
5563 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005564int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005565qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
5567 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005568 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005569 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
5571 rval = QLA_SUCCESS;
5572
Chad Dupuis5f16b332012-08-22 14:21:00 -04005573 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
Chad Dupuis5f16b332012-08-22 14:21:00 -04005575 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5576 LOOPID_MAP_SIZE);
5577 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5578 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5579 dev->loop_id = FC_NO_LOOP_ID;
5580 rval = QLA_FUNCTION_FAILED;
5581 } else
5582 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
Chad Dupuis5f16b332012-08-22 14:21:00 -04005584 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Chad Dupuis5f16b332012-08-22 14:21:00 -04005586 if (rval == QLA_SUCCESS)
5587 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5588 "Assigning new loopid=%x, portid=%x.\n",
5589 dev->loop_id, dev->d_id.b24);
5590 else
5591 ql_log(ql_log_warn, dev->vha, 0x2087,
5592 "No loop_id's available, portid=%x.\n",
5593 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
5595 return (rval);
5596}
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599/*
5600 * qla2x00_fabric_login
5601 * Issue fabric login command.
5602 *
5603 * Input:
5604 * ha = adapter block pointer.
5605 * device = pointer to FC device type structure.
5606 *
5607 * Returns:
5608 * 0 - Login successfully
5609 * 1 - Login failed
5610 * 2 - Initiator device
5611 * 3 - Fatal error
5612 */
5613int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005614qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 uint16_t *next_loopid)
5616{
5617 int rval;
5618 int retry;
5619 uint16_t tmp_loopid;
5620 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005621 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
5623 retry = 0;
5624 tmp_loopid = 0;
5625
5626 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005627 ql_dbg(ql_dbg_disc, vha, 0x2000,
5628 "Trying Fabric Login w/loop id 0x%04x for port "
5629 "%02x%02x%02x.\n",
5630 fcport->loop_id, fcport->d_id.b.domain,
5631 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
5633 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005634 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 fcport->d_id.b.domain, fcport->d_id.b.area,
5636 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005637 if (rval != QLA_SUCCESS) {
5638 return rval;
5639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 if (mb[0] == MBS_PORT_ID_USED) {
5641 /*
5642 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005643 * recommends the driver perform an implicit login with
5644 * the specified ID again. The ID we just used is save
5645 * here so we return with an ID that can be tried by
5646 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 */
5648 retry++;
5649 tmp_loopid = fcport->loop_id;
5650 fcport->loop_id = mb[1];
5651
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005652 ql_dbg(ql_dbg_disc, vha, 0x2001,
5653 "Fabric Login: port in use - next loop "
5654 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005656 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5659 /*
5660 * Login succeeded.
5661 */
5662 if (retry) {
5663 /* A retry occurred before. */
5664 *next_loopid = tmp_loopid;
5665 } else {
5666 /*
5667 * No retry occurred before. Just increment the
5668 * ID value for next login.
5669 */
5670 *next_loopid = (fcport->loop_id + 1);
5671 }
5672
5673 if (mb[1] & BIT_0) {
5674 fcport->port_type = FCT_INITIATOR;
5675 } else {
5676 fcport->port_type = FCT_TARGET;
5677 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005678 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680 }
5681
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005682 if (mb[10] & BIT_0)
5683 fcport->supported_classes |= FC_COS_CLASS2;
5684 if (mb[10] & BIT_1)
5685 fcport->supported_classes |= FC_COS_CLASS3;
5686
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005687 if (IS_FWI2_CAPABLE(ha)) {
5688 if (mb[10] & BIT_7)
5689 fcport->flags |=
5690 FCF_CONF_COMP_SUPPORTED;
5691 }
5692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 rval = QLA_SUCCESS;
5694 break;
5695 } else if (mb[0] == MBS_LOOP_ID_USED) {
5696 /*
5697 * Loop ID already used, try next loop ID.
5698 */
5699 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005700 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 if (rval != QLA_SUCCESS) {
5702 /* Ran out of loop IDs to use */
5703 break;
5704 }
5705 } else if (mb[0] == MBS_COMMAND_ERROR) {
5706 /*
5707 * Firmware possibly timed out during login. If NO
5708 * retries are left to do then the device is declared
5709 * dead.
5710 */
5711 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005712 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005713 fcport->d_id.b.domain, fcport->d_id.b.area,
5714 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005715 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 rval = 1;
5718 break;
5719 } else {
5720 /*
5721 * unrecoverable / not handled error
5722 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005723 ql_dbg(ql_dbg_disc, vha, 0x2002,
5724 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5725 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5726 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5727 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005730 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005731 fcport->d_id.b.domain, fcport->d_id.b.area,
5732 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005733 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005734 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 rval = 3;
5737 break;
5738 }
5739 }
5740
5741 return (rval);
5742}
5743
5744/*
5745 * qla2x00_local_device_login
5746 * Issue local device login command.
5747 *
5748 * Input:
5749 * ha = adapter block pointer.
5750 * loop_id = loop id of device to login to.
5751 *
5752 * Returns (Where's the #define!!!!):
5753 * 0 - Login successfully
5754 * 1 - Login failed
5755 * 3 - Fatal error
5756 */
5757int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005758qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759{
5760 int rval;
5761 uint16_t mb[MAILBOX_REGISTER_COUNT];
5762
5763 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005764 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 if (rval == QLA_SUCCESS) {
5766 /* Interrogate mailbox registers for any errors */
5767 if (mb[0] == MBS_COMMAND_ERROR)
5768 rval = 1;
5769 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5770 /* device not in PCB table */
5771 rval = 3;
5772 }
5773
5774 return (rval);
5775}
5776
5777/*
5778 * qla2x00_loop_resync
5779 * Resync with fibre channel devices.
5780 *
5781 * Input:
5782 * ha = adapter block pointer.
5783 *
5784 * Returns:
5785 * 0 = success
5786 */
5787int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005788qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005790 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005792 struct req_que *req;
5793 struct rsp_que *rsp;
5794
Michael Hernandezd7459522016-12-12 14:40:07 -08005795 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005796 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005798 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5799 if (vha->flags.online) {
5800 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5802 wait_time = 256;
5803 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005804 if (!IS_QLAFX00(vha->hw)) {
5805 /*
5806 * Issue a marker after FW becomes
5807 * ready.
5808 */
5809 qla2x00_marker(vha, req, rsp, 0, 0,
5810 MK_SYNC_ALL);
5811 vha->marker_needed = 0;
5812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005815 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005817 if (IS_QLAFX00(vha->hw))
5818 qlafx00_configure_devices(vha);
5819 else
5820 qla2x00_configure_loop(vha);
5821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005823 } while (!atomic_read(&vha->loop_down_timer) &&
5824 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5825 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5826 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
5829
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005830 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005833 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005834 ql_dbg(ql_dbg_disc, vha, 0x206c,
5835 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
5837 return (rval);
5838}
5839
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005840/*
5841* qla2x00_perform_loop_resync
5842* Description: This function will set the appropriate flags and call
5843* qla2x00_loop_resync. If successful loop will be resynced
5844* Arguments : scsi_qla_host_t pointer
5845* returm : Success or Failure
5846*/
5847
5848int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5849{
5850 int32_t rval = 0;
5851
5852 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5853 /*Configure the flags so that resync happens properly*/
5854 atomic_set(&ha->loop_down_timer, 0);
5855 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5856 atomic_set(&ha->loop_state, LOOP_UP);
5857 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5858 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5859 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5860
5861 rval = qla2x00_loop_resync(ha);
5862 } else
5863 atomic_set(&ha->loop_state, LOOP_DEAD);
5864
5865 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5866 }
5867
5868 return rval;
5869}
5870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005872qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005873{
5874 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005875 struct scsi_qla_host *vha;
5876 struct qla_hw_data *ha = base_vha->hw;
5877 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005878
Arun Easifeafb7b2010-09-03 14:57:00 -07005879 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005880 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005881 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5882 atomic_inc(&vha->vref_count);
5883 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005884 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005885 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5886 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005887 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005888
Arun Easifeafb7b2010-09-03 14:57:00 -07005889 spin_lock_irqsave(&ha->vport_slock, flags);
5890 }
5891 }
5892 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005893 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005894 }
5895 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005896}
5897
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005898/* Assumes idc_lock always held on entry */
5899void
5900qla83xx_reset_ownership(scsi_qla_host_t *vha)
5901{
5902 struct qla_hw_data *ha = vha->hw;
5903 uint32_t drv_presence, drv_presence_mask;
5904 uint32_t dev_part_info1, dev_part_info2, class_type;
5905 uint32_t class_type_mask = 0x3;
5906 uint16_t fcoe_other_function = 0xffff, i;
5907
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005908 if (IS_QLA8044(ha)) {
5909 drv_presence = qla8044_rd_direct(vha,
5910 QLA8044_CRB_DRV_ACTIVE_INDEX);
5911 dev_part_info1 = qla8044_rd_direct(vha,
5912 QLA8044_CRB_DEV_PART_INFO_INDEX);
5913 dev_part_info2 = qla8044_rd_direct(vha,
5914 QLA8044_CRB_DEV_PART_INFO2);
5915 } else {
5916 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5917 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5918 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5919 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005920 for (i = 0; i < 8; i++) {
5921 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5922 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5923 (i != ha->portnum)) {
5924 fcoe_other_function = i;
5925 break;
5926 }
5927 }
5928 if (fcoe_other_function == 0xffff) {
5929 for (i = 0; i < 8; i++) {
5930 class_type = ((dev_part_info2 >> (i * 4)) &
5931 class_type_mask);
5932 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5933 ((i + 8) != ha->portnum)) {
5934 fcoe_other_function = i + 8;
5935 break;
5936 }
5937 }
5938 }
5939 /*
5940 * Prepare drv-presence mask based on fcoe functions present.
5941 * However consider only valid physical fcoe function numbers (0-15).
5942 */
5943 drv_presence_mask = ~((1 << (ha->portnum)) |
5944 ((fcoe_other_function == 0xffff) ?
5945 0 : (1 << (fcoe_other_function))));
5946
5947 /* We are the reset owner iff:
5948 * - No other protocol drivers present.
5949 * - This is the lowest among fcoe functions. */
5950 if (!(drv_presence & drv_presence_mask) &&
5951 (ha->portnum < fcoe_other_function)) {
5952 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5953 "This host is Reset owner.\n");
5954 ha->flags.nic_core_reset_owner = 1;
5955 }
5956}
5957
Saurav Kashyapfa492632012-11-21 02:40:29 -05005958static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005959__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5960{
5961 int rval = QLA_SUCCESS;
5962 struct qla_hw_data *ha = vha->hw;
5963 uint32_t drv_ack;
5964
5965 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5966 if (rval == QLA_SUCCESS) {
5967 drv_ack |= (1 << ha->portnum);
5968 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5969 }
5970
5971 return rval;
5972}
5973
Saurav Kashyapfa492632012-11-21 02:40:29 -05005974static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005975__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5976{
5977 int rval = QLA_SUCCESS;
5978 struct qla_hw_data *ha = vha->hw;
5979 uint32_t drv_ack;
5980
5981 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5982 if (rval == QLA_SUCCESS) {
5983 drv_ack &= ~(1 << ha->portnum);
5984 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5985 }
5986
5987 return rval;
5988}
5989
Saurav Kashyapfa492632012-11-21 02:40:29 -05005990static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005991qla83xx_dev_state_to_string(uint32_t dev_state)
5992{
5993 switch (dev_state) {
5994 case QLA8XXX_DEV_COLD:
5995 return "COLD/RE-INIT";
5996 case QLA8XXX_DEV_INITIALIZING:
5997 return "INITIALIZING";
5998 case QLA8XXX_DEV_READY:
5999 return "READY";
6000 case QLA8XXX_DEV_NEED_RESET:
6001 return "NEED RESET";
6002 case QLA8XXX_DEV_NEED_QUIESCENT:
6003 return "NEED QUIESCENT";
6004 case QLA8XXX_DEV_FAILED:
6005 return "FAILED";
6006 case QLA8XXX_DEV_QUIESCENT:
6007 return "QUIESCENT";
6008 default:
6009 return "Unknown";
6010 }
6011}
6012
6013/* Assumes idc-lock always held on entry */
6014void
6015qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6016{
6017 struct qla_hw_data *ha = vha->hw;
6018 uint32_t idc_audit_reg = 0, duration_secs = 0;
6019
6020 switch (audit_type) {
6021 case IDC_AUDIT_TIMESTAMP:
6022 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6023 idc_audit_reg = (ha->portnum) |
6024 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6025 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6026 break;
6027
6028 case IDC_AUDIT_COMPLETION:
6029 duration_secs = ((jiffies_to_msecs(jiffies) -
6030 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6031 idc_audit_reg = (ha->portnum) |
6032 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6033 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6034 break;
6035
6036 default:
6037 ql_log(ql_log_warn, vha, 0xb078,
6038 "Invalid audit type specified.\n");
6039 break;
6040 }
6041}
6042
6043/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006044static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006045qla83xx_initiating_reset(scsi_qla_host_t *vha)
6046{
6047 struct qla_hw_data *ha = vha->hw;
6048 uint32_t idc_control, dev_state;
6049
6050 __qla83xx_get_idc_control(vha, &idc_control);
6051 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6052 ql_log(ql_log_info, vha, 0xb080,
6053 "NIC Core reset has been disabled. idc-control=0x%x\n",
6054 idc_control);
6055 return QLA_FUNCTION_FAILED;
6056 }
6057
6058 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6059 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6060 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6061 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6062 QLA8XXX_DEV_NEED_RESET);
6063 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6064 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6065 } else {
6066 const char *state = qla83xx_dev_state_to_string(dev_state);
6067 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6068
6069 /* SV: XXX: Is timeout required here? */
6070 /* Wait for IDC state change READY -> NEED_RESET */
6071 while (dev_state == QLA8XXX_DEV_READY) {
6072 qla83xx_idc_unlock(vha, 0);
6073 msleep(200);
6074 qla83xx_idc_lock(vha, 0);
6075 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6076 }
6077 }
6078
6079 /* Send IDC ack by writing to drv-ack register */
6080 __qla83xx_set_drv_ack(vha);
6081
6082 return QLA_SUCCESS;
6083}
6084
6085int
6086__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6087{
6088 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6089}
6090
6091int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006092__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6093{
6094 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6095}
6096
Saurav Kashyapfa492632012-11-21 02:40:29 -05006097static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006098qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6099{
6100 uint32_t drv_presence = 0;
6101 struct qla_hw_data *ha = vha->hw;
6102
6103 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6104 if (drv_presence & (1 << ha->portnum))
6105 return QLA_SUCCESS;
6106 else
6107 return QLA_TEST_FAILED;
6108}
6109
6110int
6111qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6112{
6113 int rval = QLA_SUCCESS;
6114 struct qla_hw_data *ha = vha->hw;
6115
6116 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6117 "Entered %s().\n", __func__);
6118
6119 if (vha->device_flags & DFLG_DEV_FAILED) {
6120 ql_log(ql_log_warn, vha, 0xb059,
6121 "Device in unrecoverable FAILED state.\n");
6122 return QLA_FUNCTION_FAILED;
6123 }
6124
6125 qla83xx_idc_lock(vha, 0);
6126
6127 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6128 ql_log(ql_log_warn, vha, 0xb05a,
6129 "Function=0x%x has been removed from IDC participation.\n",
6130 ha->portnum);
6131 rval = QLA_FUNCTION_FAILED;
6132 goto exit;
6133 }
6134
6135 qla83xx_reset_ownership(vha);
6136
6137 rval = qla83xx_initiating_reset(vha);
6138
6139 /*
6140 * Perform reset if we are the reset-owner,
6141 * else wait till IDC state changes to READY/FAILED.
6142 */
6143 if (rval == QLA_SUCCESS) {
6144 rval = qla83xx_idc_state_handler(vha);
6145
6146 if (rval == QLA_SUCCESS)
6147 ha->flags.nic_core_hung = 0;
6148 __qla83xx_clear_drv_ack(vha);
6149 }
6150
6151exit:
6152 qla83xx_idc_unlock(vha, 0);
6153
6154 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6155
6156 return rval;
6157}
6158
Saurav Kashyap81178772012-08-22 14:21:04 -04006159int
6160qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6161{
6162 struct qla_hw_data *ha = vha->hw;
6163 int rval = QLA_FUNCTION_FAILED;
6164
6165 if (!IS_MCTP_CAPABLE(ha)) {
6166 /* This message can be removed from the final version */
6167 ql_log(ql_log_info, vha, 0x506d,
6168 "This board is not MCTP capable\n");
6169 return rval;
6170 }
6171
6172 if (!ha->mctp_dump) {
6173 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6174 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6175
6176 if (!ha->mctp_dump) {
6177 ql_log(ql_log_warn, vha, 0x506e,
6178 "Failed to allocate memory for mctp dump\n");
6179 return rval;
6180 }
6181 }
6182
6183#define MCTP_DUMP_STR_ADDR 0x00000000
6184 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6185 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6186 if (rval != QLA_SUCCESS) {
6187 ql_log(ql_log_warn, vha, 0x506f,
6188 "Failed to capture mctp dump\n");
6189 } else {
6190 ql_log(ql_log_info, vha, 0x5070,
6191 "Mctp dump capture for host (%ld/%p).\n",
6192 vha->host_no, ha->mctp_dump);
6193 ha->mctp_dumped = 1;
6194 }
6195
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006196 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006197 ha->flags.nic_core_reset_hdlr_active = 1;
6198 rval = qla83xx_restart_nic_firmware(vha);
6199 if (rval)
6200 /* NIC Core reset failed. */
6201 ql_log(ql_log_warn, vha, 0x5071,
6202 "Failed to restart nic firmware\n");
6203 else
6204 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6205 "Restarted NIC firmware successfully.\n");
6206 ha->flags.nic_core_reset_hdlr_active = 0;
6207 }
6208
6209 return rval;
6210
6211}
6212
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006213/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006214* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006215* Description: This function will block the new I/Os
6216* Its not aborting any I/Os as context
6217* is not destroyed during quiescence
6218* Arguments: scsi_qla_host_t
6219* return : void
6220*/
6221void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006222qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006223{
6224 struct qla_hw_data *ha = vha->hw;
6225 struct scsi_qla_host *vp;
6226
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006227 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6228 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006229
6230 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6231 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6232 atomic_set(&vha->loop_state, LOOP_DOWN);
6233 qla2x00_mark_all_devices_lost(vha, 0);
6234 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006235 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006236 } else {
6237 if (!atomic_read(&vha->loop_down_timer))
6238 atomic_set(&vha->loop_down_timer,
6239 LOOP_DOWN_TIME);
6240 }
6241 /* Wait for pending cmds to complete */
6242 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6243}
6244
Giridhar Malavalia9083012010-04-12 17:59:55 -07006245void
6246qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6247{
6248 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006249 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006250 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006251 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006252 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006253
Saurav Kashyape46ef002011-02-23 15:27:16 -08006254 /* For ISP82XX, driver waits for completion of the commands.
6255 * online flag should be set.
6256 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006257 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006258 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006259 ha->flags.chip_reset_done = 0;
6260 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006261 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006262
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006263 ql_log(ql_log_info, vha, 0x00af,
6264 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006265
Saurav Kashyape46ef002011-02-23 15:27:16 -08006266 /* For ISP82XX, reset_chip is just disabling interrupts.
6267 * Driver waits for the completion of the commands.
6268 * the interrupts need to be enabled.
6269 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006270 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006271 ha->isp_ops->reset_chip(vha);
6272
Quinn Tran9cd883f2017-12-28 12:33:24 -08006273 SAVE_TOPO(ha);
6274 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006275 ha->flags.n2n_ae = 0;
6276 ha->flags.lip_ae = 0;
6277 ha->current_topology = 0;
6278 ha->flags.fw_started = 0;
6279 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006280 ha->base_qpair->chip_reset++;
6281 for (i = 0; i < ha->max_qpairs; i++) {
6282 if (ha->queue_pair_map[i])
6283 ha->queue_pair_map[i]->chip_reset =
6284 ha->base_qpair->chip_reset;
6285 }
Quinn Tran726b8542017-01-19 22:28:00 -08006286
Giridhar Malavalia9083012010-04-12 17:59:55 -07006287 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6288 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6289 atomic_set(&vha->loop_state, LOOP_DOWN);
6290 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006291
6292 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006293 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006294 atomic_inc(&vp->vref_count);
6295 spin_unlock_irqrestore(&ha->vport_slock, flags);
6296
Giridhar Malavalia9083012010-04-12 17:59:55 -07006297 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006298
6299 spin_lock_irqsave(&ha->vport_slock, flags);
6300 atomic_dec(&vp->vref_count);
6301 }
6302 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006303 } else {
6304 if (!atomic_read(&vha->loop_down_timer))
6305 atomic_set(&vha->loop_down_timer,
6306 LOOP_DOWN_TIME);
6307 }
6308
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006309 /* Clear all async request states across all VPs. */
6310 list_for_each_entry(fcport, &vha->vp_fcports, list)
6311 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6312 spin_lock_irqsave(&ha->vport_slock, flags);
6313 list_for_each_entry(vp, &ha->vp_list, list) {
6314 atomic_inc(&vp->vref_count);
6315 spin_unlock_irqrestore(&ha->vport_slock, flags);
6316
6317 list_for_each_entry(fcport, &vp->vp_fcports, list)
6318 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6319
6320 spin_lock_irqsave(&ha->vport_slock, flags);
6321 atomic_dec(&vp->vref_count);
6322 }
6323 spin_unlock_irqrestore(&ha->vport_slock, flags);
6324
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006325 if (!ha->flags.eeh_busy) {
6326 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006327 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006328 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006329 ql_log(ql_log_info, vha, 0x00b4,
6330 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006331
Saurav Kashyape46ef002011-02-23 15:27:16 -08006332 /* Done waiting for pending commands.
6333 * Reset the online flag.
6334 */
6335 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006338 /* Requeue all commands in outstanding command list. */
6339 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6340 }
Arun Easib6a029e2014-09-25 06:14:52 -04006341 /* memory barrier */
6342 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343}
6344
6345/*
6346* qla2x00_abort_isp
6347* Resets ISP and aborts all outstanding commands.
6348*
6349* Input:
6350* ha = adapter block pointer.
6351*
6352* Returns:
6353* 0 = success
6354*/
6355int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006356qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006358 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006360 struct qla_hw_data *ha = vha->hw;
6361 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006362 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006363 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006365 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006366 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Santosh Vernekara61712972012-08-22 14:21:13 -04006368 if (IS_QLA8031(ha)) {
6369 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6370 "Clearing fcoe driver presence.\n");
6371 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6372 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6373 "Error while clearing DRV-Presence.\n");
6374 }
6375
Andrew Vasquez85880802009-12-15 21:29:46 -08006376 if (unlikely(pci_channel_offline(ha->pdev) &&
6377 ha->flags.pci_channel_io_perm_failure)) {
6378 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6379 status = 0;
6380 return status;
6381 }
6382
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006383 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006384
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006385 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006387 if (!qla2x00_restart_isp(vha)) {
6388 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006390 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 /*
6392 * Issue marker command only when we are going
6393 * to start the I/O .
6394 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006395 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 }
6397
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006398 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006400 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006402 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006403 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006404
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006405 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6406 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006407 if (ha->fce) {
6408 ha->flags.fce_enabled = 1;
6409 memset(ha->fce, 0,
6410 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006411 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006412 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6413 &ha->fce_bufs);
6414 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006415 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006416 "Unable to reinitialize FCE "
6417 "(%d).\n", rval);
6418 ha->flags.fce_enabled = 0;
6419 }
6420 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006421
6422 if (ha->eft) {
6423 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006424 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006425 ha->eft_dma, EFT_NUM_BUFFERS);
6426 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006427 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006428 "Unable to reinitialize EFT "
6429 "(%d).\n", rval);
6430 }
6431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006433 vha->flags.online = 1;
6434 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006436 ql_log(ql_log_fatal, vha, 0x8035,
6437 "ISP error recover failed - "
6438 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006439 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 * The next call disables the board
6441 * completely.
6442 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006443 ha->isp_ops->reset_adapter(vha);
6444 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006446 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 status = 0;
6448 } else { /* schedule another ISP abort */
6449 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006450 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6451 "ISP abort - retry remaining %d.\n",
6452 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 status = 1;
6454 }
6455 } else {
6456 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006457 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6458 "ISP error recovery - retrying (%d) "
6459 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006460 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 status = 1;
6462 }
6463 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006464
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 }
6466
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006467 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006468 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006469 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006470 spin_lock_irqsave(&ha->vport_slock, flags);
6471 list_for_each_entry(vp, &ha->vp_list, list) {
6472 if (vp->vp_idx) {
6473 atomic_inc(&vp->vref_count);
6474 spin_unlock_irqrestore(&ha->vport_slock, flags);
6475
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006476 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006477
6478 spin_lock_irqsave(&ha->vport_slock, flags);
6479 atomic_dec(&vp->vref_count);
6480 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006481 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006482 spin_unlock_irqrestore(&ha->vport_slock, flags);
6483
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006484 if (IS_QLA8031(ha)) {
6485 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6486 "Setting back fcoe driver presence.\n");
6487 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6488 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6489 "Error while setting DRV-Presence.\n");
6490 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006491 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006492 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6493 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 }
6495
6496 return(status);
6497}
6498
6499/*
6500* qla2x00_restart_isp
6501* restarts the ISP after a reset
6502*
6503* Input:
6504* ha = adapter block pointer.
6505*
6506* Returns:
6507* 0 = success
6508*/
6509static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006510qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006512 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006513 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006514 struct req_que *req = ha->req_q_map[0];
6515 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
6517 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006518 if (qla2x00_isp_firmware(vha)) {
6519 vha->flags.online = 0;
6520 status = ha->isp_ops->chip_diag(vha);
6521 if (!status)
6522 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 }
6524
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006525 if (!status && !(status = qla2x00_init_rings(vha))) {
6526 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006527 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006528
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006529 /* Initialize the queues in use */
6530 qla25xx_init_queues(ha);
6531
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006532 status = qla2x00_fw_ready(vha);
6533 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006534 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006535 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006536 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 }
6538
6539 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006540 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 }
6543 return (status);
6544}
6545
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006546static int
6547qla25xx_init_queues(struct qla_hw_data *ha)
6548{
6549 struct rsp_que *rsp = NULL;
6550 struct req_que *req = NULL;
6551 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6552 int ret = -1;
6553 int i;
6554
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006555 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006556 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006557 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006558 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006559 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006560 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006561 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6562 "%s Rsp que: %d init failed.\n",
6563 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006564 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006565 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6566 "%s Rsp que: %d inited.\n",
6567 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006568 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006569 }
6570 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006571 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006572 if (req && test_bit(i, ha->req_qid_map)) {
6573 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006574 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006575 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006576 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006577 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6578 "%s Req que: %d init failed.\n",
6579 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006580 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006581 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6582 "%s Req que: %d inited.\n",
6583 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006584 }
6585 }
6586 return ret;
6587}
6588
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589/*
6590* qla2x00_reset_adapter
6591* Reset adapter.
6592*
6593* Input:
6594* ha = adapter block pointer.
6595*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006596void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006597qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598{
6599 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006600 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006601 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006603 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006604 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 spin_lock_irqsave(&ha->hardware_lock, flags);
6607 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6608 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6609 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6610 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6611 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6612}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006613
6614void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006615qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006616{
6617 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006618 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006619 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6620
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006621 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006622 return;
6623
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006624 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006625 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006626
6627 spin_lock_irqsave(&ha->hardware_lock, flags);
6628 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6629 RD_REG_DWORD(&reg->hccr);
6630 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6631 RD_REG_DWORD(&reg->hccr);
6632 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006633
6634 if (IS_NOPOLLING_TYPE(ha))
6635 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006636}
6637
David Miller4e08df32007-04-16 12:37:43 -07006638/* On sparc systems, obtain port and node WWN from firmware
6639 * properties.
6640 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006641static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6642 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006643{
6644#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006645 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006646 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006647 struct device_node *dp = pci_device_to_OF_node(pdev);
6648 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006649 int len;
6650
6651 val = of_get_property(dp, "port-wwn", &len);
6652 if (val && len >= WWN_SIZE)
6653 memcpy(nv->port_name, val, WWN_SIZE);
6654
6655 val = of_get_property(dp, "node-wwn", &len);
6656 if (val && len >= WWN_SIZE)
6657 memcpy(nv->node_name, val, WWN_SIZE);
6658#endif
6659}
6660
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006661int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006662qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006663{
David Miller4e08df32007-04-16 12:37:43 -07006664 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006665 struct init_cb_24xx *icb;
6666 struct nvram_24xx *nv;
6667 uint32_t *dptr;
6668 uint8_t *dptr1, *dptr2;
6669 uint32_t chksum;
6670 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006671 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006672
David Miller4e08df32007-04-16 12:37:43 -07006673 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006674 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006675 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006676
6677 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006678 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006679 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6680 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6681 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006682 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006683 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6684 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006685
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006686 ha->nvram_size = sizeof(struct nvram_24xx);
6687 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006688
Seokmann Ju281afe12007-07-26 13:43:34 -07006689 /* Get VPD data into cache */
6690 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006691 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006692 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6693
6694 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006695 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006696 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006697 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006698 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6699 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006700
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006701 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6702 "Contents of NVRAM\n");
6703 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6704 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006705
6706 /* Bad NVRAM data, set defaults parameters. */
6707 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6708 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006709 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006710 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006711 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006712 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006713 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6714 ql_log(ql_log_warn, vha, 0x006c,
6715 "Falling back to functioning (yet invalid -- WWPN) "
6716 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006717
6718 /*
6719 * Set default initialization control block.
6720 */
6721 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006722 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6723 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006724 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006725 nv->execution_throttle = cpu_to_le16(0xFFFF);
6726 nv->exchange_count = cpu_to_le16(0);
6727 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006728 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006729 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006730 nv->port_name[2] = 0x00;
6731 nv->port_name[3] = 0xe0;
6732 nv->port_name[4] = 0x8b;
6733 nv->port_name[5] = 0x1c;
6734 nv->port_name[6] = 0x55;
6735 nv->port_name[7] = 0x86;
6736 nv->node_name[0] = 0x20;
6737 nv->node_name[1] = 0x00;
6738 nv->node_name[2] = 0x00;
6739 nv->node_name[3] = 0xe0;
6740 nv->node_name[4] = 0x8b;
6741 nv->node_name[5] = 0x1c;
6742 nv->node_name[6] = 0x55;
6743 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006744 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006745 nv->login_retry_count = cpu_to_le16(8);
6746 nv->interrupt_delay_timer = cpu_to_le16(0);
6747 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006748 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006749 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6750 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6751 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6752 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6753 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6754 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006755 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006756 nv->max_luns_per_target = cpu_to_le16(128);
6757 nv->port_down_retry_count = cpu_to_le16(30);
6758 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006759
6760 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006761 }
6762
Quinn Tran726b8542017-01-19 22:28:00 -08006763 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006764 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006765 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006766 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006767 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006768 }
6769
6770 qlt_24xx_config_nvram_stage1(vha, nv);
6771
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006772 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006773 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006774
6775 /* Copy 1st segment. */
6776 dptr1 = (uint8_t *)icb;
6777 dptr2 = (uint8_t *)&nv->version;
6778 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6779 while (cnt--)
6780 *dptr1++ = *dptr2++;
6781
6782 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006783 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006784
6785 /* Copy 2nd segment. */
6786 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6787 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6788 cnt = (uint8_t *)&icb->reserved_3 -
6789 (uint8_t *)&icb->interrupt_delay_timer;
6790 while (cnt--)
6791 *dptr1++ = *dptr2++;
6792
6793 /*
6794 * Setup driver NVRAM options.
6795 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006796 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006797 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006798
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006799 qlt_24xx_config_nvram_stage2(vha, icb);
6800
Bart Van Asschead950362015-07-09 07:24:08 -07006801 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006802 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006803 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6804 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6805 }
6806
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006807 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006808 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006809 /*
6810 * Firmware will apply the following mask if the nodename was
6811 * not provided.
6812 */
6813 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6814 icb->node_name[0] &= 0xF0;
6815 }
6816
6817 /* Set host adapter parameters. */
6818 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006819 ha->flags.enable_lip_reset = 0;
6820 ha->flags.enable_lip_full_login =
6821 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6822 ha->flags.enable_target_reset =
6823 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006824 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006825 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006826
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006827 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6828 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006829
6830 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6831 sizeof(ha->fw_seriallink_options24));
6832
6833 /* save HBA serial number */
6834 ha->serial0 = icb->port_name[5];
6835 ha->serial1 = icb->port_name[6];
6836 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006837 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6838 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006839
Bart Van Asschead950362015-07-09 07:24:08 -07006840 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006841
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006842 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6843
6844 /* Set minimum login_timeout to 4 seconds. */
6845 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6846 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6847 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006848 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006849 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006850
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006851 /* Set minimum RATOV to 100 tenths of a second. */
6852 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006853
6854 ha->loop_reset_delay = nv->reset_delay;
6855
6856 /* Link Down Timeout = 0:
6857 *
6858 * When Port Down timer expires we will start returning
6859 * I/O's to OS with "DID_NO_CONNECT".
6860 *
6861 * Link Down Timeout != 0:
6862 *
6863 * The driver waits for the link to come up after link down
6864 * before returning I/Os to OS with "DID_NO_CONNECT".
6865 */
6866 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6867 ha->loop_down_abort_time =
6868 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6869 } else {
6870 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6871 ha->loop_down_abort_time =
6872 (LOOP_DOWN_TIME - ha->link_down_timeout);
6873 }
6874
6875 /* Need enough time to try and get the port back. */
6876 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6877 if (qlport_down_retry)
6878 ha->port_down_retry_count = qlport_down_retry;
6879
6880 /* Set login_retry_count */
6881 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6882 if (ha->port_down_retry_count ==
6883 le16_to_cpu(nv->port_down_retry_count) &&
6884 ha->port_down_retry_count > 3)
6885 ha->login_retry_count = ha->port_down_retry_count;
6886 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6887 ha->login_retry_count = ha->port_down_retry_count;
6888 if (ql2xloginretrycount)
6889 ha->login_retry_count = ql2xloginretrycount;
6890
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006891 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006892 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006893 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6894 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6895 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6896 le16_to_cpu(icb->interrupt_delay_timer): 2;
6897 }
Bart Van Asschead950362015-07-09 07:24:08 -07006898 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006899 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006900 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006901 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006902 ha->zio_mode = QLA_ZIO_MODE_6;
6903
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006904 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006905 "ZIO mode %d enabled; timer delay (%d us).\n",
6906 ha->zio_mode, ha->zio_timer * 100);
6907
6908 icb->firmware_options_2 |= cpu_to_le32(
6909 (uint32_t)ha->zio_mode);
6910 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006911 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006912 }
6913
David Miller4e08df32007-04-16 12:37:43 -07006914 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006915 ql_log(ql_log_warn, vha, 0x0070,
6916 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006917 }
6918 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006919}
6920
Sawan Chandak4243c112016-01-27 12:03:31 -05006921uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6922{
6923 struct qla27xx_image_status pri_image_status, sec_image_status;
6924 uint8_t valid_pri_image, valid_sec_image;
6925 uint32_t *wptr;
6926 uint32_t cnt, chksum, size;
6927 struct qla_hw_data *ha = vha->hw;
6928
6929 valid_pri_image = valid_sec_image = 1;
6930 ha->active_image = 0;
6931 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6932
6933 if (!ha->flt_region_img_status_pri) {
6934 valid_pri_image = 0;
6935 goto check_sec_image;
6936 }
6937
6938 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6939 ha->flt_region_img_status_pri, size);
6940
6941 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6942 ql_dbg(ql_dbg_init, vha, 0x018b,
6943 "Primary image signature (0x%x) not valid\n",
6944 pri_image_status.signature);
6945 valid_pri_image = 0;
6946 goto check_sec_image;
6947 }
6948
6949 wptr = (uint32_t *)(&pri_image_status);
6950 cnt = size;
6951
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006952 for (chksum = 0; cnt--; wptr++)
6953 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006954
Sawan Chandak4243c112016-01-27 12:03:31 -05006955 if (chksum) {
6956 ql_dbg(ql_dbg_init, vha, 0x018c,
6957 "Checksum validation failed for primary image (0x%x)\n",
6958 chksum);
6959 valid_pri_image = 0;
6960 }
6961
6962check_sec_image:
6963 if (!ha->flt_region_img_status_sec) {
6964 valid_sec_image = 0;
6965 goto check_valid_image;
6966 }
6967
6968 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6969 ha->flt_region_img_status_sec, size);
6970
6971 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6972 ql_dbg(ql_dbg_init, vha, 0x018d,
6973 "Secondary image signature(0x%x) not valid\n",
6974 sec_image_status.signature);
6975 valid_sec_image = 0;
6976 goto check_valid_image;
6977 }
6978
6979 wptr = (uint32_t *)(&sec_image_status);
6980 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006981 for (chksum = 0; cnt--; wptr++)
6982 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05006983 if (chksum) {
6984 ql_dbg(ql_dbg_init, vha, 0x018e,
6985 "Checksum validation failed for secondary image (0x%x)\n",
6986 chksum);
6987 valid_sec_image = 0;
6988 }
6989
6990check_valid_image:
6991 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
6992 ha->active_image = QLA27XX_PRIMARY_IMAGE;
6993 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
6994 if (!ha->active_image ||
6995 pri_image_status.generation_number <
6996 sec_image_status.generation_number)
6997 ha->active_image = QLA27XX_SECONDARY_IMAGE;
6998 }
6999
7000 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7001 ha->active_image == 0 ? "default bootld and fw" :
7002 ha->active_image == 1 ? "primary" :
7003 ha->active_image == 2 ? "secondary" :
7004 "Invalid");
7005
7006 return ha->active_image;
7007}
7008
Adrian Bunk413975a2006-06-30 02:33:06 -07007009static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007010qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7011 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007012{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007013 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007014 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007015 uint32_t *dcode, dlen;
7016 uint32_t risc_addr;
7017 uint32_t risc_size;
7018 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007019 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007020 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007021
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007022 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007023 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007024
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007025 rval = QLA_SUCCESS;
7026
7027 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007028 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007029 *srisc_addr = 0;
7030
Sawan Chandak4243c112016-01-27 12:03:31 -05007031 if (IS_QLA27XX(ha) &&
7032 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7033 faddr = ha->flt_region_fw_sec;
7034
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007035 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007036 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007037 for (i = 0; i < 4; i++)
7038 dcode[i] = be32_to_cpu(dcode[i]);
7039 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7040 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7041 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7042 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007043 ql_log(ql_log_fatal, vha, 0x008c,
7044 "Unable to verify the integrity of flash firmware "
7045 "image.\n");
7046 ql_log(ql_log_fatal, vha, 0x008d,
7047 "Firmware data: %08x %08x %08x %08x.\n",
7048 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007049
7050 return QLA_FUNCTION_FAILED;
7051 }
7052
7053 while (segments && rval == QLA_SUCCESS) {
7054 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007055 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007056
7057 risc_addr = be32_to_cpu(dcode[2]);
7058 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7059 risc_size = be32_to_cpu(dcode[3]);
7060
7061 fragment = 0;
7062 while (risc_size > 0 && rval == QLA_SUCCESS) {
7063 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7064 if (dlen > risc_size)
7065 dlen = risc_size;
7066
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007067 ql_dbg(ql_dbg_init, vha, 0x008e,
7068 "Loading risc segment@ risc addr %x "
7069 "number of dwords 0x%x offset 0x%x.\n",
7070 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007071
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007072 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007073 for (i = 0; i < dlen; i++)
7074 dcode[i] = swab32(dcode[i]);
7075
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007076 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007077 dlen);
7078 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007079 ql_log(ql_log_fatal, vha, 0x008f,
7080 "Failed to load segment %d of firmware.\n",
7081 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007082 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007083 }
7084
7085 faddr += dlen;
7086 risc_addr += dlen;
7087 risc_size -= dlen;
7088 fragment++;
7089 }
7090
7091 /* Next segment. */
7092 segments--;
7093 }
7094
Chad Dupuisf73cb692014-02-26 04:15:06 -05007095 if (!IS_QLA27XX(ha))
7096 return rval;
7097
7098 if (ha->fw_dump_template)
7099 vfree(ha->fw_dump_template);
7100 ha->fw_dump_template = NULL;
7101 ha->fw_dump_template_len = 0;
7102
7103 ql_dbg(ql_dbg_init, vha, 0x0161,
7104 "Loading fwdump template from %x\n", faddr);
7105 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7106 risc_size = be32_to_cpu(dcode[2]);
7107 ql_dbg(ql_dbg_init, vha, 0x0162,
7108 "-> array size %x dwords\n", risc_size);
7109 if (risc_size == 0 || risc_size == ~0)
7110 goto default_template;
7111
7112 dlen = (risc_size - 8) * sizeof(*dcode);
7113 ql_dbg(ql_dbg_init, vha, 0x0163,
7114 "-> template allocating %x bytes...\n", dlen);
7115 ha->fw_dump_template = vmalloc(dlen);
7116 if (!ha->fw_dump_template) {
7117 ql_log(ql_log_warn, vha, 0x0164,
7118 "Failed fwdump template allocate %x bytes.\n", risc_size);
7119 goto default_template;
7120 }
7121
7122 faddr += 7;
7123 risc_size -= 8;
7124 dcode = ha->fw_dump_template;
7125 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7126 for (i = 0; i < risc_size; i++)
7127 dcode[i] = le32_to_cpu(dcode[i]);
7128
7129 if (!qla27xx_fwdt_template_valid(dcode)) {
7130 ql_log(ql_log_warn, vha, 0x0165,
7131 "Failed fwdump template validate\n");
7132 goto default_template;
7133 }
7134
7135 dlen = qla27xx_fwdt_template_size(dcode);
7136 ql_dbg(ql_dbg_init, vha, 0x0166,
7137 "-> template size %x bytes\n", dlen);
7138 if (dlen > risc_size * sizeof(*dcode)) {
7139 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007140 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007141 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007142 goto default_template;
7143 }
7144 ha->fw_dump_template_len = dlen;
7145 return rval;
7146
7147default_template:
7148 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7149 if (ha->fw_dump_template)
7150 vfree(ha->fw_dump_template);
7151 ha->fw_dump_template = NULL;
7152 ha->fw_dump_template_len = 0;
7153
7154 dlen = qla27xx_fwdt_template_default_size();
7155 ql_dbg(ql_dbg_init, vha, 0x0169,
7156 "-> template allocating %x bytes...\n", dlen);
7157 ha->fw_dump_template = vmalloc(dlen);
7158 if (!ha->fw_dump_template) {
7159 ql_log(ql_log_warn, vha, 0x016a,
7160 "Failed fwdump template allocate %x bytes.\n", risc_size);
7161 goto failed_template;
7162 }
7163
7164 dcode = ha->fw_dump_template;
7165 risc_size = dlen / sizeof(*dcode);
7166 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7167 for (i = 0; i < risc_size; i++)
7168 dcode[i] = be32_to_cpu(dcode[i]);
7169
7170 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7171 ql_log(ql_log_warn, vha, 0x016b,
7172 "Failed fwdump template validate\n");
7173 goto failed_template;
7174 }
7175
7176 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7177 ql_dbg(ql_dbg_init, vha, 0x016c,
7178 "-> template size %x bytes\n", dlen);
7179 ha->fw_dump_template_len = dlen;
7180 return rval;
7181
7182failed_template:
7183 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7184 if (ha->fw_dump_template)
7185 vfree(ha->fw_dump_template);
7186 ha->fw_dump_template = NULL;
7187 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007188 return rval;
7189}
7190
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007191#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007192
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007193int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007194qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007195{
7196 int rval;
7197 int i, fragment;
7198 uint16_t *wcode, *fwcode;
7199 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7200 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007201 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007202 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007203
7204 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007205 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007206 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007207 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007208 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007209 ql_log(ql_log_info, vha, 0x0084,
7210 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007211 return QLA_FUNCTION_FAILED;
7212 }
7213
7214 rval = QLA_SUCCESS;
7215
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007216 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007217 *srisc_addr = 0;
7218 fwcode = (uint16_t *)blob->fw->data;
7219 fwclen = 0;
7220
7221 /* Validate firmware image by checking version. */
7222 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007223 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007224 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007225 blob->fw->size);
7226 goto fail_fw_integrity;
7227 }
7228 for (i = 0; i < 4; i++)
7229 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7230 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7231 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7232 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007233 ql_log(ql_log_fatal, vha, 0x0086,
7234 "Unable to verify integrity of firmware image.\n");
7235 ql_log(ql_log_fatal, vha, 0x0087,
7236 "Firmware data: %04x %04x %04x %04x.\n",
7237 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007238 goto fail_fw_integrity;
7239 }
7240
7241 seg = blob->segs;
7242 while (*seg && rval == QLA_SUCCESS) {
7243 risc_addr = *seg;
7244 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7245 risc_size = be16_to_cpu(fwcode[3]);
7246
7247 /* Validate firmware image size. */
7248 fwclen += risc_size * sizeof(uint16_t);
7249 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007250 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007251 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007252 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007253 goto fail_fw_integrity;
7254 }
7255
7256 fragment = 0;
7257 while (risc_size > 0 && rval == QLA_SUCCESS) {
7258 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7259 if (wlen > risc_size)
7260 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007261 ql_dbg(ql_dbg_init, vha, 0x0089,
7262 "Loading risc segment@ risc addr %x number of "
7263 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007264
7265 for (i = 0; i < wlen; i++)
7266 wcode[i] = swab16(fwcode[i]);
7267
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007268 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007269 wlen);
7270 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007271 ql_log(ql_log_fatal, vha, 0x008a,
7272 "Failed to load segment %d of firmware.\n",
7273 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007274 break;
7275 }
7276
7277 fwcode += wlen;
7278 risc_addr += wlen;
7279 risc_size -= wlen;
7280 fragment++;
7281 }
7282
7283 /* Next segment. */
7284 seg++;
7285 }
7286 return rval;
7287
7288fail_fw_integrity:
7289 return QLA_FUNCTION_FAILED;
7290}
7291
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007292static int
7293qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007294{
7295 int rval;
7296 int segments, fragment;
7297 uint32_t *dcode, dlen;
7298 uint32_t risc_addr;
7299 uint32_t risc_size;
7300 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007301 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007302 const uint32_t *fwcode;
7303 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007304 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007305 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007306
Andrew Vasquez54333832005-11-09 15:49:04 -08007307 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007308 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007309 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007310 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007311 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007312 ql_log(ql_log_warn, vha, 0x0091,
7313 "Firmware images can be retrieved from: "
7314 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007315
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007316 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007317 }
7318
Chad Dupuiscfb09192011-11-18 09:03:07 -08007319 ql_dbg(ql_dbg_init, vha, 0x0092,
7320 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007321
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007322 rval = QLA_SUCCESS;
7323
7324 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007325 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007326 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007327 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007328 fwclen = 0;
7329
7330 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007331 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007332 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007333 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007334 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007335 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007336 }
7337 for (i = 0; i < 4; i++)
7338 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7339 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7340 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7341 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7342 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007343 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007344 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007345 blob->fw->size);
7346 ql_log(ql_log_fatal, vha, 0x0095,
7347 "Firmware data: %08x %08x %08x %08x.\n",
7348 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007349 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007350 }
7351
7352 while (segments && rval == QLA_SUCCESS) {
7353 risc_addr = be32_to_cpu(fwcode[2]);
7354 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7355 risc_size = be32_to_cpu(fwcode[3]);
7356
7357 /* Validate firmware image size. */
7358 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007359 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007360 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007361 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007362 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007363 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007364 }
7365
7366 fragment = 0;
7367 while (risc_size > 0 && rval == QLA_SUCCESS) {
7368 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7369 if (dlen > risc_size)
7370 dlen = risc_size;
7371
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007372 ql_dbg(ql_dbg_init, vha, 0x0097,
7373 "Loading risc segment@ risc addr %x "
7374 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007375
7376 for (i = 0; i < dlen; i++)
7377 dcode[i] = swab32(fwcode[i]);
7378
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007379 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007380 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007381 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007382 ql_log(ql_log_fatal, vha, 0x0098,
7383 "Failed to load segment %d of firmware.\n",
7384 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007385 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007386 }
7387
7388 fwcode += dlen;
7389 risc_addr += dlen;
7390 risc_size -= dlen;
7391 fragment++;
7392 }
7393
7394 /* Next segment. */
7395 segments--;
7396 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007397
7398 if (!IS_QLA27XX(ha))
7399 return rval;
7400
7401 if (ha->fw_dump_template)
7402 vfree(ha->fw_dump_template);
7403 ha->fw_dump_template = NULL;
7404 ha->fw_dump_template_len = 0;
7405
7406 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007407 "Loading fwdump template from %x\n",
7408 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007409 risc_size = be32_to_cpu(fwcode[2]);
7410 ql_dbg(ql_dbg_init, vha, 0x172,
7411 "-> array size %x dwords\n", risc_size);
7412 if (risc_size == 0 || risc_size == ~0)
7413 goto default_template;
7414
7415 dlen = (risc_size - 8) * sizeof(*fwcode);
7416 ql_dbg(ql_dbg_init, vha, 0x0173,
7417 "-> template allocating %x bytes...\n", dlen);
7418 ha->fw_dump_template = vmalloc(dlen);
7419 if (!ha->fw_dump_template) {
7420 ql_log(ql_log_warn, vha, 0x0174,
7421 "Failed fwdump template allocate %x bytes.\n", risc_size);
7422 goto default_template;
7423 }
7424
7425 fwcode += 7;
7426 risc_size -= 8;
7427 dcode = ha->fw_dump_template;
7428 for (i = 0; i < risc_size; i++)
7429 dcode[i] = le32_to_cpu(fwcode[i]);
7430
7431 if (!qla27xx_fwdt_template_valid(dcode)) {
7432 ql_log(ql_log_warn, vha, 0x0175,
7433 "Failed fwdump template validate\n");
7434 goto default_template;
7435 }
7436
7437 dlen = qla27xx_fwdt_template_size(dcode);
7438 ql_dbg(ql_dbg_init, vha, 0x0176,
7439 "-> template size %x bytes\n", dlen);
7440 if (dlen > risc_size * sizeof(*fwcode)) {
7441 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007442 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007443 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007444 goto default_template;
7445 }
7446 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007447 return rval;
7448
Chad Dupuisf73cb692014-02-26 04:15:06 -05007449default_template:
7450 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7451 if (ha->fw_dump_template)
7452 vfree(ha->fw_dump_template);
7453 ha->fw_dump_template = NULL;
7454 ha->fw_dump_template_len = 0;
7455
7456 dlen = qla27xx_fwdt_template_default_size();
7457 ql_dbg(ql_dbg_init, vha, 0x0179,
7458 "-> template allocating %x bytes...\n", dlen);
7459 ha->fw_dump_template = vmalloc(dlen);
7460 if (!ha->fw_dump_template) {
7461 ql_log(ql_log_warn, vha, 0x017a,
7462 "Failed fwdump template allocate %x bytes.\n", risc_size);
7463 goto failed_template;
7464 }
7465
7466 dcode = ha->fw_dump_template;
7467 risc_size = dlen / sizeof(*fwcode);
7468 fwcode = qla27xx_fwdt_template_default();
7469 for (i = 0; i < risc_size; i++)
7470 dcode[i] = be32_to_cpu(fwcode[i]);
7471
7472 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7473 ql_log(ql_log_warn, vha, 0x017b,
7474 "Failed fwdump template validate\n");
7475 goto failed_template;
7476 }
7477
7478 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7479 ql_dbg(ql_dbg_init, vha, 0x017c,
7480 "-> template size %x bytes\n", dlen);
7481 ha->fw_dump_template_len = dlen;
7482 return rval;
7483
7484failed_template:
7485 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7486 if (ha->fw_dump_template)
7487 vfree(ha->fw_dump_template);
7488 ha->fw_dump_template = NULL;
7489 ha->fw_dump_template_len = 0;
7490 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007491}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007492
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007493int
7494qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7495{
7496 int rval;
7497
Andrew Vasqueze337d902009-04-06 22:33:49 -07007498 if (ql2xfwloadbin == 1)
7499 return qla81xx_load_risc(vha, srisc_addr);
7500
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007501 /*
7502 * FW Load priority:
7503 * 1) Firmware via request-firmware interface (.bin file).
7504 * 2) Firmware residing in flash.
7505 */
7506 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7507 if (rval == QLA_SUCCESS)
7508 return rval;
7509
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007510 return qla24xx_load_risc_flash(vha, srisc_addr,
7511 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007512}
7513
7514int
7515qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7516{
7517 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007518 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007519
Andrew Vasqueze337d902009-04-06 22:33:49 -07007520 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007521 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007522
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007523 /*
7524 * FW Load priority:
7525 * 1) Firmware residing in flash.
7526 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007527 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007528 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007529 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007530 if (rval == QLA_SUCCESS)
7531 return rval;
7532
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007533try_blob_fw:
7534 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7535 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7536 return rval;
7537
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007538 ql_log(ql_log_info, vha, 0x0099,
7539 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007540 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7541 if (rval != QLA_SUCCESS)
7542 return rval;
7543
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007544 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007545 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007546 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007547}
7548
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007549void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007550qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007551{
7552 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007553 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007554
Andrew Vasquez85880802009-12-15 21:29:46 -08007555 if (ha->flags.pci_channel_io_perm_failure)
7556 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007557 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007558 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007559 if (!ha->fw_major_version)
7560 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007561 if (!ha->flags.fw_started)
7562 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007563
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007564 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007565 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007566 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007567 ha->isp_ops->reset_chip(vha);
7568 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007569 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007570 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007571 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007572 ql_log(ql_log_info, vha, 0x8015,
7573 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007574 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007575 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007576
Quinn Tran4b60c822017-06-13 20:47:21 -07007577 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007578 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007579}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007580
7581int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007582qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007583{
7584 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007585 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007586 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007587 struct qla_hw_data *ha = vha->hw;
7588 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007589 struct req_que *req;
7590 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007591
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007592 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007593 return -EINVAL;
7594
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007595 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007596 if (vha->qpair)
7597 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007598 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007599 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007600 rsp = req->rsp;
7601
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007602 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007603 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007604 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007605 }
7606
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007607 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007608
7609 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007610 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7611 BIT_1);
7612 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7613 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7614 ql_dbg(ql_dbg_init, vha, 0x0120,
7615 "Failed SNS login: loop_id=%x, rval2=%d\n",
7616 NPH_SNS, rval2);
7617 else
7618 ql_dbg(ql_dbg_init, vha, 0x0103,
7619 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7620 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7621 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007622 return (QLA_FUNCTION_FAILED);
7623 }
7624
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007625 atomic_set(&vha->loop_down_timer, 0);
7626 atomic_set(&vha->loop_state, LOOP_UP);
7627 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7628 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7629 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007630
7631 return rval;
7632}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007633
7634/* 84XX Support **************************************************************/
7635
7636static LIST_HEAD(qla_cs84xx_list);
7637static DEFINE_MUTEX(qla_cs84xx_mutex);
7638
7639static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007640qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007641{
7642 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007643 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007644
7645 mutex_lock(&qla_cs84xx_mutex);
7646
7647 /* Find any shared 84xx chip. */
7648 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7649 if (cs84xx->bus == ha->pdev->bus) {
7650 kref_get(&cs84xx->kref);
7651 goto done;
7652 }
7653 }
7654
7655 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7656 if (!cs84xx)
7657 goto done;
7658
7659 kref_init(&cs84xx->kref);
7660 spin_lock_init(&cs84xx->access_lock);
7661 mutex_init(&cs84xx->fw_update_mutex);
7662 cs84xx->bus = ha->pdev->bus;
7663
7664 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7665done:
7666 mutex_unlock(&qla_cs84xx_mutex);
7667 return cs84xx;
7668}
7669
7670static void
7671__qla84xx_chip_release(struct kref *kref)
7672{
7673 struct qla_chip_state_84xx *cs84xx =
7674 container_of(kref, struct qla_chip_state_84xx, kref);
7675
7676 mutex_lock(&qla_cs84xx_mutex);
7677 list_del(&cs84xx->list);
7678 mutex_unlock(&qla_cs84xx_mutex);
7679 kfree(cs84xx);
7680}
7681
7682void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007683qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007684{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007685 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007686 if (ha->cs84xx)
7687 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7688}
7689
7690static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007691qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007692{
7693 int rval;
7694 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007695 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007696
7697 mutex_lock(&ha->cs84xx->fw_update_mutex);
7698
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007699 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007700
7701 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7702
7703 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7704 QLA_SUCCESS;
7705}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007706
7707/* 81XX Support **************************************************************/
7708
7709int
7710qla81xx_nvram_config(scsi_qla_host_t *vha)
7711{
7712 int rval;
7713 struct init_cb_81xx *icb;
7714 struct nvram_81xx *nv;
7715 uint32_t *dptr;
7716 uint8_t *dptr1, *dptr2;
7717 uint32_t chksum;
7718 uint16_t cnt;
7719 struct qla_hw_data *ha = vha->hw;
7720
7721 rval = QLA_SUCCESS;
7722 icb = (struct init_cb_81xx *)ha->init_cb;
7723 nv = ha->nvram;
7724
7725 /* Determine NVRAM starting address. */
7726 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007727 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007728 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7729 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007730
7731 /* Get VPD data into cache */
7732 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007733 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7734 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007735
7736 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007737 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007738 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007739 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007740 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7741 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007742
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007743 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7744 "Contents of NVRAM:\n");
7745 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7746 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007747
7748 /* Bad NVRAM data, set defaults parameters. */
7749 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7750 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007751 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007752 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007753 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007754 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007755 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007756 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007757 ql_log(ql_log_info, vha, 0x0074,
7758 "Falling back to functioning (yet invalid -- WWPN) "
7759 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007760
7761 /*
7762 * Set default initialization control block.
7763 */
7764 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007765 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7766 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007767 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007768 nv->execution_throttle = cpu_to_le16(0xFFFF);
7769 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007770 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007771 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007772 nv->port_name[2] = 0x00;
7773 nv->port_name[3] = 0xe0;
7774 nv->port_name[4] = 0x8b;
7775 nv->port_name[5] = 0x1c;
7776 nv->port_name[6] = 0x55;
7777 nv->port_name[7] = 0x86;
7778 nv->node_name[0] = 0x20;
7779 nv->node_name[1] = 0x00;
7780 nv->node_name[2] = 0x00;
7781 nv->node_name[3] = 0xe0;
7782 nv->node_name[4] = 0x8b;
7783 nv->node_name[5] = 0x1c;
7784 nv->node_name[6] = 0x55;
7785 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007786 nv->login_retry_count = cpu_to_le16(8);
7787 nv->interrupt_delay_timer = cpu_to_le16(0);
7788 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007789 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007790 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7791 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7792 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7793 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7794 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7795 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007796 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007797 nv->max_luns_per_target = cpu_to_le16(128);
7798 nv->port_down_retry_count = cpu_to_le16(30);
7799 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007800 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007801 nv->enode_mac[1] = 0xC0;
7802 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007803 nv->enode_mac[3] = 0x04;
7804 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007805 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007806
7807 rval = 1;
7808 }
7809
Arun Easi9e522cd2012-08-22 14:21:31 -04007810 if (IS_T10_PI_CAPABLE(ha))
7811 nv->frame_payload_size &= ~7;
7812
Arun Easiaa230bc2013-01-30 03:34:39 -05007813 qlt_81xx_config_nvram_stage1(vha, nv);
7814
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007815 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007816 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007817
7818 /* Copy 1st segment. */
7819 dptr1 = (uint8_t *)icb;
7820 dptr2 = (uint8_t *)&nv->version;
7821 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7822 while (cnt--)
7823 *dptr1++ = *dptr2++;
7824
7825 icb->login_retry_count = nv->login_retry_count;
7826
7827 /* Copy 2nd segment. */
7828 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7829 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7830 cnt = (uint8_t *)&icb->reserved_5 -
7831 (uint8_t *)&icb->interrupt_delay_timer;
7832 while (cnt--)
7833 *dptr1++ = *dptr2++;
7834
7835 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7836 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7837 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007838 icb->enode_mac[0] = 0x00;
7839 icb->enode_mac[1] = 0xC0;
7840 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007841 icb->enode_mac[3] = 0x04;
7842 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007843 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007844 }
7845
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007846 /* Use extended-initialization control block. */
7847 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7848
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007849 /*
7850 * Setup driver NVRAM options.
7851 */
7852 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007853 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007854
Arun Easiaa230bc2013-01-30 03:34:39 -05007855 qlt_81xx_config_nvram_stage2(vha, icb);
7856
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007857 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007858 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007859 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7860 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7861 }
7862
7863 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007864 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007865 /*
7866 * Firmware will apply the following mask if the nodename was
7867 * not provided.
7868 */
7869 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7870 icb->node_name[0] &= 0xF0;
7871 }
7872
7873 /* Set host adapter parameters. */
7874 ha->flags.disable_risc_code_load = 0;
7875 ha->flags.enable_lip_reset = 0;
7876 ha->flags.enable_lip_full_login =
7877 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7878 ha->flags.enable_target_reset =
7879 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7880 ha->flags.enable_led_scheme = 0;
7881 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7882
7883 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7884 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7885
7886 /* save HBA serial number */
7887 ha->serial0 = icb->port_name[5];
7888 ha->serial1 = icb->port_name[6];
7889 ha->serial2 = icb->port_name[7];
7890 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7891 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7892
Bart Van Asschead950362015-07-09 07:24:08 -07007893 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007894
7895 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7896
7897 /* Set minimum login_timeout to 4 seconds. */
7898 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7899 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7900 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007901 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007902 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007903
7904 /* Set minimum RATOV to 100 tenths of a second. */
7905 ha->r_a_tov = 100;
7906
7907 ha->loop_reset_delay = nv->reset_delay;
7908
7909 /* Link Down Timeout = 0:
7910 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007911 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007912 * I/O's to OS with "DID_NO_CONNECT".
7913 *
7914 * Link Down Timeout != 0:
7915 *
7916 * The driver waits for the link to come up after link down
7917 * before returning I/Os to OS with "DID_NO_CONNECT".
7918 */
7919 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7920 ha->loop_down_abort_time =
7921 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7922 } else {
7923 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7924 ha->loop_down_abort_time =
7925 (LOOP_DOWN_TIME - ha->link_down_timeout);
7926 }
7927
7928 /* Need enough time to try and get the port back. */
7929 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7930 if (qlport_down_retry)
7931 ha->port_down_retry_count = qlport_down_retry;
7932
7933 /* Set login_retry_count */
7934 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7935 if (ha->port_down_retry_count ==
7936 le16_to_cpu(nv->port_down_retry_count) &&
7937 ha->port_down_retry_count > 3)
7938 ha->login_retry_count = ha->port_down_retry_count;
7939 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7940 ha->login_retry_count = ha->port_down_retry_count;
7941 if (ql2xloginretrycount)
7942 ha->login_retry_count = ql2xloginretrycount;
7943
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007944 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007945 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007946 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007947
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007948 /* Enable ZIO. */
7949 if (!vha->flags.init_done) {
7950 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7951 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7952 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7953 le16_to_cpu(icb->interrupt_delay_timer): 2;
7954 }
Bart Van Asschead950362015-07-09 07:24:08 -07007955 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007956 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7957 vha->flags.process_response_queue = 0;
7958 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7959 ha->zio_mode = QLA_ZIO_MODE_6;
7960
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007961 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007962 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007963 ha->zio_mode,
7964 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007965
7966 icb->firmware_options_2 |= cpu_to_le32(
7967 (uint32_t)ha->zio_mode);
7968 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7969 vha->flags.process_response_queue = 1;
7970 }
7971
Quinn Tran41dc5292017-01-19 22:28:03 -08007972 /* enable RIDA Format2 */
7973 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7974 icb->firmware_options_3 |= BIT_0;
7975
Duane Grigsbyedd05de2017-10-13 09:34:06 -07007976 if (IS_QLA27XX(ha)) {
7977 icb->firmware_options_3 |= BIT_8;
7978 ql_dbg(ql_log_info, vha, 0x0075,
7979 "Enabling direct connection.\n");
7980 }
7981
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007982 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007983 ql_log(ql_log_warn, vha, 0x0076,
7984 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007985 }
7986 return (rval);
7987}
7988
Giridhar Malavalia9083012010-04-12 17:59:55 -07007989int
7990qla82xx_restart_isp(scsi_qla_host_t *vha)
7991{
7992 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007993 struct qla_hw_data *ha = vha->hw;
7994 struct req_que *req = ha->req_q_map[0];
7995 struct rsp_que *rsp = ha->rsp_q_map[0];
7996 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07007997 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007998
7999 status = qla2x00_init_rings(vha);
8000 if (!status) {
8001 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8002 ha->flags.chip_reset_done = 1;
8003
8004 status = qla2x00_fw_ready(vha);
8005 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008006 /* Issue a marker after FW becomes ready. */
8007 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008008 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008009 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008010 }
8011
8012 /* if no cable then assume it's good */
8013 if ((vha->device_flags & DFLG_NO_CABLE))
8014 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008015 }
8016
8017 if (!status) {
8018 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8019
8020 if (!atomic_read(&vha->loop_down_timer)) {
8021 /*
8022 * Issue marker command only when we are going
8023 * to start the I/O .
8024 */
8025 vha->marker_needed = 1;
8026 }
8027
Giridhar Malavalia9083012010-04-12 17:59:55 -07008028 ha->isp_ops->enable_intrs(ha);
8029
8030 ha->isp_abort_cnt = 0;
8031 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8032
Saurav Kashyap53296782011-05-10 11:30:06 -07008033 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008034 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008035
Giridhar Malavalia9083012010-04-12 17:59:55 -07008036 if (ha->fce) {
8037 ha->flags.fce_enabled = 1;
8038 memset(ha->fce, 0,
8039 fce_calc_size(ha->fce_bufs));
8040 rval = qla2x00_enable_fce_trace(vha,
8041 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8042 &ha->fce_bufs);
8043 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008044 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008045 "Unable to reinitialize FCE (%d).\n",
8046 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008047 ha->flags.fce_enabled = 0;
8048 }
8049 }
8050
8051 if (ha->eft) {
8052 memset(ha->eft, 0, EFT_SIZE);
8053 rval = qla2x00_enable_eft_trace(vha,
8054 ha->eft_dma, EFT_NUM_BUFFERS);
8055 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008056 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008057 "Unable to reinitialize EFT (%d).\n",
8058 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008059 }
8060 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008061 }
8062
8063 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008064 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008065 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008066
8067 spin_lock_irqsave(&ha->vport_slock, flags);
8068 list_for_each_entry(vp, &ha->vp_list, list) {
8069 if (vp->vp_idx) {
8070 atomic_inc(&vp->vref_count);
8071 spin_unlock_irqrestore(&ha->vport_slock, flags);
8072
Giridhar Malavalia9083012010-04-12 17:59:55 -07008073 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008074
8075 spin_lock_irqsave(&ha->vport_slock, flags);
8076 atomic_dec(&vp->vref_count);
8077 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008078 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008079 spin_unlock_irqrestore(&ha->vport_slock, flags);
8080
Giridhar Malavalia9083012010-04-12 17:59:55 -07008081 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008082 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008083 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008084 }
8085
8086 return status;
8087}
8088
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008089void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008090qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008091{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008092 struct qla_hw_data *ha = vha->hw;
8093
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008094 /* Hold status IOCBs until ABTS response received. */
8095 if (ql2xfwholdabts)
8096 ha->fw_options[3] |= BIT_12;
8097
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008098 /* Set Retry FLOGI in case of P2P connection */
8099 if (ha->operating_mode == P2P) {
8100 ha->fw_options[2] |= BIT_3;
8101 ql_dbg(ql_dbg_disc, vha, 0x2103,
8102 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8103 __func__, ha->fw_options[2]);
8104 }
8105
Quinn Tran41dc5292017-01-19 22:28:03 -08008106 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8107 if (ql2xmvasynctoatio) {
8108 if (qla_tgt_mode_enabled(vha) ||
8109 qla_dual_mode_enabled(vha))
8110 ha->fw_options[2] |= BIT_11;
8111 else
8112 ha->fw_options[2] &= ~BIT_11;
8113 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008114
Quinn Tranf7e761f2017-06-02 09:12:02 -07008115 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008116 qla_dual_mode_enabled(vha)) {
8117 /* FW auto send SCSI status during */
8118 ha->fw_options[1] |= BIT_8;
8119 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8120
8121 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008122 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008123 } else {
8124 ha->fw_options[1] &= ~BIT_8;
8125 ha->fw_options[10] &= 0x00ff;
8126
Quinn Tranf7e761f2017-06-02 09:12:02 -07008127 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008128 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008129
Quinn Tran41dc5292017-01-19 22:28:03 -08008130 if (ql2xetsenable) {
8131 /* Enable ETS Burst. */
8132 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8133 ha->fw_options[2] |= BIT_9;
8134 }
8135
Quinn Tran83548fe2017-06-02 09:12:01 -07008136 ql_dbg(ql_dbg_init, vha, 0x00e9,
8137 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8138 __func__, ha->fw_options[1], ha->fw_options[2],
8139 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008140
Andrew Vasquezae97c912010-02-18 10:07:28 -08008141 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008142}
Sarang Radke09ff7012010-03-19 17:03:59 -07008143
8144/*
8145 * qla24xx_get_fcp_prio
8146 * Gets the fcp cmd priority value for the logged in port.
8147 * Looks for a match of the port descriptors within
8148 * each of the fcp prio config entries. If a match is found,
8149 * the tag (priority) value is returned.
8150 *
8151 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008152 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008153 * fcport = port structure pointer.
8154 *
8155 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008156 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008157 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008158 *
8159 * Context:
8160 * Kernel context
8161 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008162static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008163qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8164{
8165 int i, entries;
8166 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008167 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008168 uint32_t pid1, pid2;
8169 uint64_t wwn1, wwn2;
8170 struct qla_fcp_prio_entry *pri_entry;
8171 struct qla_hw_data *ha = vha->hw;
8172
8173 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008174 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008175
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008176 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008177 entries = ha->fcp_prio_cfg->num_entries;
8178 pri_entry = &ha->fcp_prio_cfg->entry[0];
8179
8180 for (i = 0; i < entries; i++) {
8181 pid_match = wwn_match = 0;
8182
8183 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8184 pri_entry++;
8185 continue;
8186 }
8187
8188 /* check source pid for a match */
8189 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8190 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8191 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8192 if (pid1 == INVALID_PORT_ID)
8193 pid_match++;
8194 else if (pid1 == pid2)
8195 pid_match++;
8196 }
8197
8198 /* check destination pid for a match */
8199 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8200 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8201 pid2 = fcport->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 source WWN for a match */
8209 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8210 wwn1 = wwn_to_u64(vha->port_name);
8211 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8212 if (wwn2 == (uint64_t)-1)
8213 wwn_match++;
8214 else if (wwn1 == wwn2)
8215 wwn_match++;
8216 }
8217
8218 /* check destination WWN for a match */
8219 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8220 wwn1 = wwn_to_u64(fcport->port_name);
8221 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8222 if (wwn2 == (uint64_t)-1)
8223 wwn_match++;
8224 else if (wwn1 == wwn2)
8225 wwn_match++;
8226 }
8227
8228 if (pid_match == 2 || wwn_match == 2) {
8229 /* Found a matching entry */
8230 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8231 priority = pri_entry->tag;
8232 break;
8233 }
8234
8235 pri_entry++;
8236 }
8237
8238 return priority;
8239}
8240
8241/*
8242 * qla24xx_update_fcport_fcp_prio
8243 * Activates fcp priority for the logged in fc port
8244 *
8245 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008246 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008247 * fcp = port structure pointer.
8248 *
8249 * Return:
8250 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8251 *
8252 * Context:
8253 * Kernel context.
8254 */
8255int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008256qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008257{
8258 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008259 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008260 uint16_t mb[5];
8261
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008262 if (fcport->port_type != FCT_TARGET ||
8263 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008264 return QLA_FUNCTION_FAILED;
8265
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008266 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008267 if (priority < 0)
8268 return QLA_FUNCTION_FAILED;
8269
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008270 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008271 fcport->fcp_prio = priority & 0xf;
8272 return QLA_SUCCESS;
8273 }
8274
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008275 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008276 if (ret == QLA_SUCCESS) {
8277 if (fcport->fcp_prio != priority)
8278 ql_dbg(ql_dbg_user, vha, 0x709e,
8279 "Updated FCP_CMND priority - value=%d loop_id=%d "
8280 "port_id=%02x%02x%02x.\n", priority,
8281 fcport->loop_id, fcport->d_id.b.domain,
8282 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008283 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008284 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008285 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008286 "Unable to update FCP_CMND priority - ret=0x%x for "
8287 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8288 fcport->d_id.b.domain, fcport->d_id.b.area,
8289 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008290 return ret;
8291}
8292
8293/*
8294 * qla24xx_update_all_fcp_prio
8295 * Activates fcp priority for all the logged in ports
8296 *
8297 * Input:
8298 * ha = adapter block pointer.
8299 *
8300 * Return:
8301 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8302 *
8303 * Context:
8304 * Kernel context.
8305 */
8306int
8307qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8308{
8309 int ret;
8310 fc_port_t *fcport;
8311
8312 ret = QLA_FUNCTION_FAILED;
8313 /* We need to set priority for all logged in ports */
8314 list_for_each_entry(fcport, &vha->vp_fcports, list)
8315 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8316
8317 return ret;
8318}
Michael Hernandezd7459522016-12-12 14:40:07 -08008319
Quinn Tran82de8022017-06-13 20:47:17 -07008320struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8321 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008322{
8323 int rsp_id = 0;
8324 int req_id = 0;
8325 int i;
8326 struct qla_hw_data *ha = vha->hw;
8327 uint16_t qpair_id = 0;
8328 struct qla_qpair *qpair = NULL;
8329 struct qla_msix_entry *msix;
8330
8331 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8332 ql_log(ql_log_warn, vha, 0x00181,
8333 "FW/Driver is not multi-queue capable.\n");
8334 return NULL;
8335 }
8336
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008337 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008338 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8339 if (qpair == NULL) {
8340 ql_log(ql_log_warn, vha, 0x0182,
8341 "Failed to allocate memory for queue pair.\n");
8342 return NULL;
8343 }
8344 memset(qpair, 0, sizeof(struct qla_qpair));
8345
8346 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008347 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008348 qpair->qp_lock_ptr = &qpair->qp_lock;
8349 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008350 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008351
8352 /* Assign available que pair id */
8353 mutex_lock(&ha->mq_lock);
8354 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008355 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008356 mutex_unlock(&ha->mq_lock);
8357 ql_log(ql_log_warn, vha, 0x0183,
8358 "No resources to create additional q pair.\n");
8359 goto fail_qid_map;
8360 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008361 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008362 set_bit(qpair_id, ha->qpair_qid_map);
8363 ha->queue_pair_map[qpair_id] = qpair;
8364 qpair->id = qpair_id;
8365 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008366 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008367 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008368 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008369 qpair->chip_reset = ha->base_qpair->chip_reset;
8370 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8371 qpair->enable_explicit_conf =
8372 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008373
8374 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008375 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008376 if (msix->in_use)
8377 continue;
8378 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008379 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008380 "Vector %x selected for qpair\n", msix->vector);
8381 break;
8382 }
8383 if (!qpair->msix) {
8384 ql_log(ql_log_warn, vha, 0x0184,
8385 "Out of MSI-X vectors!.\n");
8386 goto fail_msix;
8387 }
8388
8389 qpair->msix->in_use = 1;
8390 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008391 qpair->pdev = ha->pdev;
8392 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8393 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008394
8395 mutex_unlock(&ha->mq_lock);
8396
8397 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008398 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008399 if (!rsp_id) {
8400 ql_log(ql_log_warn, vha, 0x0185,
8401 "Failed to create response queue.\n");
8402 goto fail_rsp;
8403 }
8404
8405 qpair->rsp = ha->rsp_q_map[rsp_id];
8406
8407 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008408 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8409 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008410 if (!req_id) {
8411 ql_log(ql_log_warn, vha, 0x0186,
8412 "Failed to create request queue.\n");
8413 goto fail_req;
8414 }
8415
8416 qpair->req = ha->req_q_map[req_id];
8417 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008418 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008419 /* init qpair to this cpu. Will adjust at run time. */
8420 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008421
8422 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8423 if (ha->fw_attributes & BIT_4)
8424 qpair->difdix_supported = 1;
8425 }
8426
8427 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8428 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008429 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008430 "Failed to create srb mempool for qpair %d\n",
8431 qpair->id);
8432 goto fail_mempool;
8433 }
8434
8435 /* Mark as online */
8436 qpair->online = 1;
8437
8438 if (!vha->flags.qpairs_available)
8439 vha->flags.qpairs_available = 1;
8440
8441 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8442 "Request/Response queue pair created, id %d\n",
8443 qpair->id);
8444 ql_dbg(ql_dbg_init, vha, 0x0187,
8445 "Request/Response queue pair created, id %d\n",
8446 qpair->id);
8447 }
8448 return qpair;
8449
8450fail_mempool:
8451fail_req:
8452 qla25xx_delete_rsp_que(vha, qpair->rsp);
8453fail_rsp:
8454 mutex_lock(&ha->mq_lock);
8455 qpair->msix->in_use = 0;
8456 list_del(&qpair->qp_list_elem);
8457 if (list_empty(&vha->qp_list))
8458 vha->flags.qpairs_available = 0;
8459fail_msix:
8460 ha->queue_pair_map[qpair_id] = NULL;
8461 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008462 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008463 mutex_unlock(&ha->mq_lock);
8464fail_qid_map:
8465 kfree(qpair);
8466 return NULL;
8467}
8468
8469int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8470{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008471 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008472 struct qla_hw_data *ha = qpair->hw;
8473
8474 qpair->delete_in_progress = 1;
8475 while (atomic_read(&qpair->ref_count))
8476 msleep(500);
8477
8478 ret = qla25xx_delete_req_que(vha, qpair->req);
8479 if (ret != QLA_SUCCESS)
8480 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008481
Michael Hernandezd7459522016-12-12 14:40:07 -08008482 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8483 if (ret != QLA_SUCCESS)
8484 goto fail;
8485
8486 mutex_lock(&ha->mq_lock);
8487 ha->queue_pair_map[qpair->id] = NULL;
8488 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008489 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008490 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008491 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008492 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008493 vha->flags.qpairs_req_created = 0;
8494 vha->flags.qpairs_rsp_created = 0;
8495 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008496 mempool_destroy(qpair->srb_mempool);
8497 kfree(qpair);
8498 mutex_unlock(&ha->mq_lock);
8499
8500 return QLA_SUCCESS;
8501fail:
8502 return ret;
8503}