blob: 15a96dc205d00e2e99b9a746d38cc6e0fb2771d3 [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 Tran6d6749272017-12-28 12:33:41 -0800112 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran5c25d452017-12-28 12:33:09 -0800113 } 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:
himanshu.madhani@cavium.com7ac0c332018-01-15 20:46:48 -0800120 if (!fcport)
121 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700122 /* Retry as needed. */
123 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
124 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
125 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800126 memset(&ea, 0, sizeof(ea));
127 ea.event = FCME_PLOGI_DONE;
128 ea.fcport = sp->fcport;
129 ea.data[0] = lio->u.logio.data[0];
130 ea.data[1] = lio->u.logio.data[1];
131 ea.sp = sp;
132 qla24xx_handle_plogi_done_event(fcport->vha, &ea);
133 break;
134 case SRB_LOGOUT_CMD:
himanshu.madhani@cavium.com7ac0c332018-01-15 20:46:48 -0800135 if (!fcport)
136 break;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400137 qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800138 break;
139 case SRB_CT_PTHRU_CMD:
140 case SRB_MB_IOCB:
141 case SRB_NACK_PLOGI:
142 case SRB_NACK_PRLI:
143 case SRB_NACK_LOGO:
Quinn Tran28531922017-12-28 12:33:10 -0800144 case SRB_CTRL_VP:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800145 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800146 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700147 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700148}
149
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700150static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800151qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700152{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800153 srb_t *sp = ptr;
154 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800155 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800156 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700157
Quinn Tran83548fe2017-06-02 09:12:01 -0700158 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800159 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800160
Quinn Tran6d6749272017-12-28 12:33:41 -0800161 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
162
Quinn Tran726b8542017-01-19 22:28:00 -0800163 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
164 memset(&ea, 0, sizeof(ea));
165 ea.event = FCME_PLOGI_DONE;
166 ea.fcport = sp->fcport;
167 ea.data[0] = lio->u.logio.data[0];
168 ea.data[1] = lio->u.logio.data[1];
169 ea.iop[0] = lio->u.logio.iop[0];
170 ea.iop[1] = lio->u.logio.iop[1];
171 ea.sp = sp;
172 qla2x00_fcport_event_handler(vha, &ea);
173 }
174
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800175 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700176}
177
Andrew Vasquezac280b62009-08-20 11:06:05 -0700178int
179qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
180 uint16_t *data)
181{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700182 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700183 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800184 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700185
Quinn Tran726b8542017-01-19 22:28:00 -0800186 if (!vha->flags.online)
187 goto done;
188
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800189 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700190 if (!sp)
191 goto done;
192
Quinn Tran726b8542017-01-19 22:28:00 -0800193 fcport->flags |= FCF_ASYNC_SENT;
194 fcport->logout_completed = 0;
195
Quinn Trana4239942017-12-28 12:33:26 -0800196 fcport->disc_state = DSC_LOGIN_PEND;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800197 sp->type = SRB_LOGIN_CMD;
198 sp->name = "login";
Quinn Trana4239942017-12-28 12:33:26 -0800199 sp->gen1 = fcport->rscn_gen;
200 sp->gen2 = fcport->login_gen;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800201 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
202
203 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700204 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800205 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700206 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700207
208 if (fcport->fc4f_nvme)
209 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
210
Andrew Vasquezac280b62009-08-20 11:06:05 -0700211 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700212 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700213 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500214 if (rval != QLA_SUCCESS) {
Chad Dupuis080c9512016-01-27 12:03:37 -0500215 fcport->flags |= FCF_LOGIN_NEEDED;
216 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700217 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500218 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700219
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700220 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800221 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
222 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800223 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
224 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700225 return rval;
226
227done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800228 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800229 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800230done:
Andrew Vasquezac280b62009-08-20 11:06:05 -0700231 return rval;
232}
233
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700234static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800235qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700236{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800237 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800238 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700239
Quinn Tran6d6749272017-12-28 12:33:41 -0800240 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800241 if (!test_bit(UNLOADING, &sp->vha->dpc_flags))
242 qla2x00_post_async_logout_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800243 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800244 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700245}
246
Andrew Vasquezac280b62009-08-20 11:06:05 -0700247int
248qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
249{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700250 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700251 struct srb_iocb *lio;
Quinn Tran3dbec592017-12-28 12:33:40 -0800252 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700253
Quinn Tran3dbec592017-12-28 12:33:40 -0800254 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
255 return rval;
256
Quinn Tran726b8542017-01-19 22:28:00 -0800257 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800258 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700259 if (!sp)
260 goto done;
261
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800262 sp->type = SRB_LOGOUT_CMD;
263 sp->name = "logout";
264 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
265
266 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700267 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800268 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700269 rval = qla2x00_start_sp(sp);
270 if (rval != QLA_SUCCESS)
271 goto done_free_sp;
272
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700273 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800274 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800275 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800276 fcport->d_id.b.area, fcport->d_id.b.al_pa,
277 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700278 return rval;
279
280done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800281 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700282done:
Quinn Tran726b8542017-01-19 22:28:00 -0800283 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700284 return rval;
285}
Quinn Tran11aea162017-12-28 12:33:20 -0800286
287void
288qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
289 uint16_t *data)
290{
291 /* Don't re-login in target mode */
292 if (!fcport->tgt_session)
293 qla2x00_mark_device_lost(vha, fcport, 1, 0);
294 qlt_logo_completion_handler(fcport, data[0]);
295}
296
297static void
298qla2x00_async_prlo_sp_done(void *s, int res)
299{
300 srb_t *sp = (srb_t *)s;
301 struct srb_iocb *lio = &sp->u.iocb_cmd;
302 struct scsi_qla_host *vha = sp->vha;
303
304 if (!test_bit(UNLOADING, &vha->dpc_flags))
305 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
306 lio->u.logio.data);
307 sp->free(sp);
308}
309
310int
311qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
312{
313 srb_t *sp;
314 struct srb_iocb *lio;
315 int rval;
316
317 rval = QLA_FUNCTION_FAILED;
318 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
319 if (!sp)
320 goto done;
321
322 sp->type = SRB_PRLO_CMD;
323 sp->name = "prlo";
324 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
325
326 lio = &sp->u.iocb_cmd;
327 lio->timeout = qla2x00_async_iocb_timeout;
328 sp->done = qla2x00_async_prlo_sp_done;
329 rval = qla2x00_start_sp(sp);
330 if (rval != QLA_SUCCESS)
331 goto done_free_sp;
332
333 ql_dbg(ql_dbg_disc, vha, 0x2070,
334 "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
335 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
336 fcport->d_id.b.area, fcport->d_id.b.al_pa);
337 return rval;
338
339done_free_sp:
340 sp->free(sp);
341done:
342 return rval;
343}
344
Quinn Tranf13515a2017-12-28 12:33:15 -0800345static
346void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
347{
Quinn Tran0616e962017-12-28 12:33:34 -0800348 struct fc_port *fcport = ea->fcport;
349
350 ql_dbg(ql_dbg_disc, vha, 0x20d2,
351 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
352 __func__, fcport->port_name, fcport->disc_state,
353 fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
354 fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
355
356 if (ea->data[0] != MBS_COMMAND_COMPLETE) {
Quinn Trana4239942017-12-28 12:33:26 -0800357 ql_dbg(ql_dbg_disc, vha, 0x2066,
358 "%s %8phC: adisc fail: post delete\n",
359 __func__, ea->fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800360 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana4239942017-12-28 12:33:26 -0800361 return;
362 }
Quinn Trana4239942017-12-28 12:33:26 -0800363
364 if (ea->fcport->disc_state == DSC_DELETE_PEND)
365 return;
366
367 if (ea->sp->gen2 != ea->fcport->login_gen) {
368 /* target side must have changed it. */
369 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran0616e962017-12-28 12:33:34 -0800370 "%s %8phC generation changed\n",
371 __func__, ea->fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800372 return;
373 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
374 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
375 __func__, __LINE__, ea->fcport->port_name);
376 qla24xx_post_gidpn_work(vha, ea->fcport);
377 return;
378 }
379
380 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800381}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700382
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700383static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800384qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700385{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800386 srb_t *sp = ptr;
387 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800388 struct event_arg ea;
Quinn Tran0616e962017-12-28 12:33:34 -0800389 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700390
Quinn Tranf13515a2017-12-28 12:33:15 -0800391 ql_dbg(ql_dbg_disc, vha, 0x2066,
392 "Async done-%s res %x %8phC\n",
393 sp->name, res, sp->fcport->port_name);
394
395 memset(&ea, 0, sizeof(ea));
396 ea.event = FCME_ADISC_DONE;
397 ea.rc = res;
Quinn Tran0616e962017-12-28 12:33:34 -0800398 ea.data[0] = lio->u.logio.data[0];
399 ea.data[1] = lio->u.logio.data[1];
400 ea.iop[0] = lio->u.logio.iop[0];
401 ea.iop[1] = lio->u.logio.iop[1];
Quinn Tranf13515a2017-12-28 12:33:15 -0800402 ea.fcport = sp->fcport;
403 ea.sp = sp;
404
405 qla2x00_fcport_event_handler(vha, &ea);
406
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800407 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700408}
409
410int
411qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
412 uint16_t *data)
413{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700414 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700415 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700416 int rval;
417
418 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800419 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800420 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700421 if (!sp)
422 goto done;
423
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800424 sp->type = SRB_ADISC_CMD;
425 sp->name = "adisc";
426 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
427
428 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700429 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800430 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700431 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700432 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700433 rval = qla2x00_start_sp(sp);
434 if (rval != QLA_SUCCESS)
435 goto done_free_sp;
436
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700437 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800438 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
439 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700440 return rval;
441
442done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800443 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700444done:
Quinn Tran726b8542017-01-19 22:28:00 -0800445 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tranf13515a2017-12-28 12:33:15 -0800446 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700447 return rval;
448}
449
Quinn Tran726b8542017-01-19 22:28:00 -0800450static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
451 struct event_arg *ea)
452{
453 fc_port_t *fcport, *conflict_fcport;
454 struct get_name_list_extended *e;
455 u16 i, n, found = 0, loop_id;
456 port_id_t id;
457 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800458 u16 data[2];
459 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800460
461 fcport = ea->fcport;
Quinn Tranf352eeb2017-12-28 12:33:35 -0800462 ql_dbg(ql_dbg_disc, vha, 0xffff,
463 "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
464 __func__, fcport->port_name, fcport->disc_state,
465 fcport->fw_login_state, ea->rc,
466 fcport->login_gen, fcport->last_login_gen,
467 fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800468
Quinn Trana4239942017-12-28 12:33:26 -0800469 if (fcport->disc_state == DSC_DELETE_PEND)
470 return;
471
Quinn Tran726b8542017-01-19 22:28:00 -0800472 if (ea->rc) { /* rval */
473 if (fcport->login_retry == 0) {
474 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700475 ql_dbg(ql_dbg_disc, vha, 0x20de,
476 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
477 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800478 }
479 return;
480 }
481
482 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700483 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800484 "%s %8phC rscn gen changed rscn %d|%d \n",
485 __func__, fcport->port_name,
486 fcport->last_rscn_gen, fcport->rscn_gen);
487 qla24xx_post_gidpn_work(vha, fcport);
488 return;
489 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700490 ql_dbg(ql_dbg_disc, vha, 0x20e0,
Quinn Tranf352eeb2017-12-28 12:33:35 -0800491 "%s %8phC login gen changed\n",
492 __func__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800493 return;
494 }
495
496 n = ea->data[0] / sizeof(struct get_name_list_extended);
497
Quinn Tran83548fe2017-06-02 09:12:01 -0700498 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800499 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
500 __func__, __LINE__, fcport->port_name, n,
501 fcport->d_id.b.domain, fcport->d_id.b.area,
502 fcport->d_id.b.al_pa, fcport->loop_id);
503
504 for (i = 0; i < n; i++) {
505 e = &vha->gnl.l[i];
506 wwn = wwn_to_u64(e->port_name);
507
508 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
509 continue;
510
511 found = 1;
512 id.b.domain = e->port_id[2];
513 id.b.area = e->port_id[1];
514 id.b.al_pa = e->port_id[0];
515 id.b.rsvd_1 = 0;
516
517 loop_id = le16_to_cpu(e->nport_handle);
518 loop_id = (loop_id & 0x7fff);
519
Quinn Tran83548fe2017-06-02 09:12:01 -0700520 ql_dbg(ql_dbg_disc, vha, 0x20e2,
521 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
522 __func__, fcport->port_name,
523 e->current_login_state, fcport->fw_login_state,
524 id.b.domain, id.b.area, id.b.al_pa,
525 fcport->d_id.b.domain, fcport->d_id.b.area,
526 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800527
528 if ((id.b24 != fcport->d_id.b24) ||
529 ((fcport->loop_id != FC_NO_LOOP_ID) &&
530 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700531 ql_dbg(ql_dbg_disc, vha, 0x20e3,
532 "%s %d %8phC post del sess\n",
533 __func__, __LINE__, fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800534 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800535 return;
536 }
537
538 fcport->loop_id = loop_id;
539
540 wwn = wwn_to_u64(fcport->port_name);
541 qlt_find_sess_invalidate_other(vha, wwn,
542 id, loop_id, &conflict_fcport);
543
544 if (conflict_fcport) {
545 /*
546 * Another share fcport share the same loop_id &
547 * nport id. Conflict fcport needs to finish
548 * cleanup before this fcport can proceed to login.
549 */
550 conflict_fcport->conflict = fcport;
551 fcport->login_pause = 1;
552 }
553
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700554 if (fcport->fc4f_nvme)
555 current_login_state = e->current_login_state >> 4;
556 else
557 current_login_state = e->current_login_state & 0xf;
558
559 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800560 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700561 ql_dbg(ql_dbg_disc, vha, 0x20e4,
562 "%s %d %8phC post gpdb\n",
563 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800564
565 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
566 fcport->port_type = FCT_INITIATOR;
567 else
568 fcport->port_type = FCT_TARGET;
569
570 data[0] = data[1] = 0;
571 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800572 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800573 case DSC_LS_PORT_UNAVAIL:
574 default:
575 if (fcport->loop_id == FC_NO_LOOP_ID) {
576 qla2x00_find_new_loop_id(vha, fcport);
577 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
578 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700579 ql_dbg(ql_dbg_disc, vha, 0x20e5,
580 "%s %d %8phC\n",
581 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800582 qla24xx_fcport_handle_login(vha, fcport);
583 break;
584 }
585 }
586
587 if (!found) {
588 /* fw has no record of this port */
Quinn Tran040036b2017-12-28 12:33:38 -0800589 for (i = 0; i < n; i++) {
590 e = &vha->gnl.l[i];
591 id.b.domain = e->port_id[0];
592 id.b.area = e->port_id[1];
593 id.b.al_pa = e->port_id[2];
594 id.b.rsvd_1 = 0;
595 loop_id = le16_to_cpu(e->nport_handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800596
Quinn Tran040036b2017-12-28 12:33:38 -0800597 if (fcport->d_id.b24 == id.b24) {
598 conflict_fcport =
599 qla2x00_find_fcport_by_wwpn(vha,
600 e->port_name, 0);
601 ql_dbg(ql_dbg_disc, vha, 0x20e6,
602 "%s %d %8phC post del sess\n",
603 __func__, __LINE__,
604 conflict_fcport->port_name);
605 qlt_schedule_sess_for_deletion
Quinn Tran94cff6e2017-12-28 12:33:42 -0800606 (conflict_fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800607 }
Quinn Tran040036b2017-12-28 12:33:38 -0800608
609 /* FW already picked this loop id for another fcport */
610 if (fcport->loop_id == loop_id)
611 fcport->loop_id = FC_NO_LOOP_ID;
Quinn Tran726b8542017-01-19 22:28:00 -0800612 }
613 qla24xx_fcport_handle_login(vha, fcport);
614 }
615} /* gnl_event */
616
617static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800618qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800619{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800620 struct srb *sp = s;
621 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800622 unsigned long flags;
623 struct fc_port *fcport = NULL, *tf;
624 u16 i, n = 0, loop_id;
625 struct event_arg ea;
626 struct get_name_list_extended *e;
627 u64 wwn;
628 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800629 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800630
Quinn Tran83548fe2017-06-02 09:12:01 -0700631 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800632 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
633 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
634 sp->u.iocb_cmd.u.mbx.in_mb[2]);
635
636 memset(&ea, 0, sizeof(ea));
637 ea.sp = sp;
638 ea.rc = res;
639 ea.event = FCME_GNL_DONE;
640
641 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
642 sizeof(struct get_name_list_extended)) {
643 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
644 sizeof(struct get_name_list_extended);
645 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
646 }
647
648 for (i = 0; i < n; i++) {
649 e = &vha->gnl.l[i];
650 loop_id = le16_to_cpu(e->nport_handle);
651 /* mask out reserve bit */
652 loop_id = (loop_id & 0x7fff);
653 set_bit(loop_id, vha->hw->loop_id_map);
654 wwn = wwn_to_u64(e->port_name);
655
Quinn Tran83548fe2017-06-02 09:12:01 -0700656 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800657 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
658 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
659 e->port_id[0], e->current_login_state, e->last_login_state,
660 (loop_id & 0x7fff));
661 }
662
663 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
664 vha->gnl.sent = 0;
665
666 INIT_LIST_HEAD(&h);
667 fcport = tf = NULL;
668 if (!list_empty(&vha->gnl.fcports))
669 list_splice_init(&vha->gnl.fcports, &h);
670
671 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
672 list_del_init(&fcport->gnl_entry);
Quinn Tran6d6749272017-12-28 12:33:41 -0800673 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800674 ea.fcport = fcport;
675
676 qla2x00_fcport_event_handler(vha, &ea);
677 }
678
Quinn Trana4239942017-12-28 12:33:26 -0800679 /* create new fcport if fw has knowledge of new sessions */
680 for (i = 0; i < n; i++) {
681 port_id_t id;
682 u64 wwnn;
683
684 e = &vha->gnl.l[i];
685 wwn = wwn_to_u64(e->port_name);
686
687 found = false;
688 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
689 if (!memcmp((u8 *)&wwn, fcport->port_name,
690 WWN_SIZE)) {
691 found = true;
692 break;
693 }
694 }
695
Quinn Trancf055fb2017-12-28 12:33:33 -0800696 id.b.domain = e->port_id[2];
Quinn Trana4239942017-12-28 12:33:26 -0800697 id.b.area = e->port_id[1];
Quinn Trancf055fb2017-12-28 12:33:33 -0800698 id.b.al_pa = e->port_id[0];
Quinn Trana4239942017-12-28 12:33:26 -0800699 id.b.rsvd_1 = 0;
700
701 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
702 ql_dbg(ql_dbg_disc, vha, 0x2065,
Quinn Trancf055fb2017-12-28 12:33:33 -0800703 "%s %d %8phC %06x post new sess\n",
704 __func__, __LINE__, (u8 *)&wwn, id.b24);
Quinn Trana4239942017-12-28 12:33:26 -0800705 wwnn = wwn_to_u64(e->node_name);
706 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
707 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
708 }
709 }
710
Quinn Tran726b8542017-01-19 22:28:00 -0800711 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
712
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800713 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800714}
715
716int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
717{
718 srb_t *sp;
719 struct srb_iocb *mbx;
720 int rval = QLA_FUNCTION_FAILED;
721 unsigned long flags;
722 u16 *mb;
723
Quinn Tran3dbec592017-12-28 12:33:40 -0800724 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
725 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800726
Quinn Tran83548fe2017-06-02 09:12:01 -0700727 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800728 "Async-gnlist WWPN %8phC \n", fcport->port_name);
729
730 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800731 fcport->disc_state = DSC_GNL;
732 fcport->last_rscn_gen = fcport->rscn_gen;
733 fcport->last_login_gen = fcport->login_gen;
734
735 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
736 if (vha->gnl.sent) {
737 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tran3dbec592017-12-28 12:33:40 -0800738 return QLA_SUCCESS;
Quinn Tran726b8542017-01-19 22:28:00 -0800739 }
740 vha->gnl.sent = 1;
741 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
742
743 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
744 if (!sp)
745 goto done;
Quinn Tran3dbec592017-12-28 12:33:40 -0800746
747 fcport->flags |= FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800748 sp->type = SRB_MB_IOCB;
749 sp->name = "gnlist";
750 sp->gen1 = fcport->rscn_gen;
751 sp->gen2 = fcport->login_gen;
752
753 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
754
755 mb = sp->u.iocb_cmd.u.mbx.out_mb;
756 mb[0] = MBC_PORT_NODE_NAME_LIST;
757 mb[1] = BIT_2 | BIT_3;
758 mb[2] = MSW(vha->gnl.ldma);
759 mb[3] = LSW(vha->gnl.ldma);
760 mb[6] = MSW(MSD(vha->gnl.ldma));
761 mb[7] = LSW(MSD(vha->gnl.ldma));
762 mb[8] = vha->gnl.size;
763 mb[9] = vha->vp_idx;
764
765 mbx = &sp->u.iocb_cmd;
766 mbx->timeout = qla2x00_async_iocb_timeout;
767
768 sp->done = qla24xx_async_gnl_sp_done;
769
770 rval = qla2x00_start_sp(sp);
771 if (rval != QLA_SUCCESS)
772 goto done_free_sp;
773
Quinn Tran83548fe2017-06-02 09:12:01 -0700774 ql_dbg(ql_dbg_disc, vha, 0x20da,
775 "Async-%s - OUT WWPN %8phC hndl %x\n",
776 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800777
778 return rval;
779
780done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800781 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800782 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800783done:
Quinn Tran726b8542017-01-19 22:28:00 -0800784 return rval;
785}
786
787int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
788{
789 struct qla_work_evt *e;
790
791 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
792 if (!e)
793 return QLA_FUNCTION_FAILED;
794
795 e->u.fcport.fcport = fcport;
Quinn Tran6d6749272017-12-28 12:33:41 -0800796 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800797 return qla2x00_post_work(vha, e);
798}
799
800static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800801void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800802{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800803 struct srb *sp = s;
804 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800805 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800806 fc_port_t *fcport = sp->fcport;
807 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800808 struct event_arg ea;
809
Quinn Tran83548fe2017-06-02 09:12:01 -0700810 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800811 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
812 sp->name, res, fcport->port_name, mb[1], mb[2]);
813
Quinn Tran6d6749272017-12-28 12:33:41 -0800814 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800815
Quinn Tran726b8542017-01-19 22:28:00 -0800816 memset(&ea, 0, sizeof(ea));
817 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800818 ea.fcport = fcport;
819 ea.sp = sp;
820
821 qla2x00_fcport_event_handler(vha, &ea);
822
823 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
824 sp->u.iocb_cmd.u.mbx.in_dma);
825
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800826 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800827}
828
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700829static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
830{
831 struct qla_work_evt *e;
832
833 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
834 if (!e)
835 return QLA_FUNCTION_FAILED;
836
837 e->u.fcport.fcport = fcport;
838
839 return qla2x00_post_work(vha, e);
840}
841
842static void
843qla2x00_async_prli_sp_done(void *ptr, int res)
844{
845 srb_t *sp = ptr;
846 struct scsi_qla_host *vha = sp->vha;
847 struct srb_iocb *lio = &sp->u.iocb_cmd;
848 struct event_arg ea;
849
850 ql_dbg(ql_dbg_disc, vha, 0x2129,
851 "%s %8phC res %d \n", __func__,
852 sp->fcport->port_name, res);
853
854 sp->fcport->flags &= ~FCF_ASYNC_SENT;
855
856 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
857 memset(&ea, 0, sizeof(ea));
858 ea.event = FCME_PRLI_DONE;
859 ea.fcport = sp->fcport;
860 ea.data[0] = lio->u.logio.data[0];
861 ea.data[1] = lio->u.logio.data[1];
862 ea.iop[0] = lio->u.logio.iop[0];
863 ea.iop[1] = lio->u.logio.iop[1];
864 ea.sp = sp;
865
866 qla2x00_fcport_event_handler(vha, &ea);
867 }
868
869 sp->free(sp);
870}
871
872int
873qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
874{
875 srb_t *sp;
876 struct srb_iocb *lio;
877 int rval = QLA_FUNCTION_FAILED;
878
879 if (!vha->flags.online)
880 return rval;
881
882 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700883 fcport->fw_login_state == DSC_LS_PRLI_PEND)
884 return rval;
885
886 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
887 if (!sp)
888 return rval;
889
890 fcport->flags |= FCF_ASYNC_SENT;
891 fcport->logout_completed = 0;
892
893 sp->type = SRB_PRLI_CMD;
894 sp->name = "prli";
895 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
896
897 lio = &sp->u.iocb_cmd;
898 lio->timeout = qla2x00_async_iocb_timeout;
899 sp->done = qla2x00_async_prli_sp_done;
900 lio->u.logio.flags = 0;
901
902 if (fcport->fc4f_nvme)
903 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
904
905 rval = qla2x00_start_sp(sp);
906 if (rval != QLA_SUCCESS) {
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700907 fcport->flags |= FCF_LOGIN_NEEDED;
908 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
909 goto done_free_sp;
910 }
911
912 ql_dbg(ql_dbg_disc, vha, 0x211b,
913 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
914 fcport->port_name, sp->handle, fcport->loop_id,
915 fcport->d_id.b24, fcport->login_retry);
916
917 return rval;
918
919done_free_sp:
920 sp->free(sp);
921 fcport->flags &= ~FCF_ASYNC_SENT;
922 return rval;
923}
924
Quinn Trana07fc0a2017-08-23 15:05:21 -0700925int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800926{
927 struct qla_work_evt *e;
928
929 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
930 if (!e)
931 return QLA_FUNCTION_FAILED;
932
933 e->u.fcport.fcport = fcport;
934 e->u.fcport.opt = opt;
Quinn Tran6d6749272017-12-28 12:33:41 -0800935 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800936 return qla2x00_post_work(vha, e);
937}
938
939int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
940{
941 srb_t *sp;
942 struct srb_iocb *mbx;
943 int rval = QLA_FUNCTION_FAILED;
944 u16 *mb;
945 dma_addr_t pd_dma;
946 struct port_database_24xx *pd;
947 struct qla_hw_data *ha = vha->hw;
948
Quinn Tran3dbec592017-12-28 12:33:40 -0800949 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
950 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800951
Quinn Tran726b8542017-01-19 22:28:00 -0800952 fcport->disc_state = DSC_GPDB;
953
954 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
955 if (!sp)
956 goto done;
957
Quinn Tran3dbec592017-12-28 12:33:40 -0800958 fcport->flags |= FCF_ASYNC_SENT;
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700959 sp->type = SRB_MB_IOCB;
960 sp->name = "gpdb";
961 sp->gen1 = fcport->rscn_gen;
962 sp->gen2 = fcport->login_gen;
963 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
964
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200965 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800966 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700967 ql_log(ql_log_warn, vha, 0xd043,
968 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800969 goto done_free_sp;
970 }
Quinn Tran726b8542017-01-19 22:28:00 -0800971
Quinn Tran726b8542017-01-19 22:28:00 -0800972 mb = sp->u.iocb_cmd.u.mbx.out_mb;
973 mb[0] = MBC_GET_PORT_DATABASE;
974 mb[1] = fcport->loop_id;
975 mb[2] = MSW(pd_dma);
976 mb[3] = LSW(pd_dma);
977 mb[6] = MSW(MSD(pd_dma));
978 mb[7] = LSW(MSD(pd_dma));
979 mb[9] = vha->vp_idx;
980 mb[10] = opt;
981
982 mbx = &sp->u.iocb_cmd;
983 mbx->timeout = qla2x00_async_iocb_timeout;
984 mbx->u.mbx.in = (void *)pd;
985 mbx->u.mbx.in_dma = pd_dma;
986
987 sp->done = qla24xx_async_gpdb_sp_done;
988
989 rval = qla2x00_start_sp(sp);
990 if (rval != QLA_SUCCESS)
991 goto done_free_sp;
992
Quinn Tran83548fe2017-06-02 09:12:01 -0700993 ql_dbg(ql_dbg_disc, vha, 0x20dc,
994 "Async-%s %8phC hndl %x opt %x\n",
995 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800996
997 return rval;
998
999done_free_sp:
1000 if (pd)
1001 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1002
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001003 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -08001004 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -08001005done:
Quinn Tran726b8542017-01-19 22:28:00 -08001006 qla24xx_post_gpdb_work(vha, fcport, opt);
1007 return rval;
1008}
1009
1010static
Quinn Trana4239942017-12-28 12:33:26 -08001011void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001012{
Quinn Tran726b8542017-01-19 22:28:00 -08001013 unsigned long flags;
1014
Quinn Tran726b8542017-01-19 22:28:00 -08001015 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001016 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001017 ea->fcport->deleted = 0;
1018 ea->fcport->logout_on_delete = 1;
1019
1020 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1021 vha->fcport_count++;
1022 ea->fcport->login_succ = 1;
1023
1024 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1025 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001026 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001027 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001028 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001029 vha->fcport_count);
1030
Quinn Trana4239942017-12-28 12:33:26 -08001031 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001032 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001033 if (ea->fcport->id_changed) {
1034 ea->fcport->id_changed = 0;
1035 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1036 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1037 __func__, __LINE__, ea->fcport->port_name,
1038 vha->fcport_count);
1039 qla24xx_post_gfpnid_work(vha, ea->fcport);
1040 } else {
1041 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1042 "%s %d %8phC post gpsc fcp_cnt %d\n",
1043 __func__, __LINE__, ea->fcport->port_name,
1044 vha->fcport_count);
1045 qla24xx_post_gpsc_work(vha, ea->fcport);
1046 }
Quinn Tran726b8542017-01-19 22:28:00 -08001047 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001048 } else if (ea->fcport->login_succ) {
1049 /*
1050 * We have an existing session. A late RSCN delivery
1051 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001052 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001053 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001054 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1055 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001056 __func__, __LINE__, ea->fcport->port_name);
himanshu.madhani@cavium.com8a7eac22018-01-15 20:46:50 -08001057 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001058 }
1059 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001060}
Quinn Tran726b8542017-01-19 22:28:00 -08001061
Quinn Trana4239942017-12-28 12:33:26 -08001062static
1063void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1064{
Quinn Trana4239942017-12-28 12:33:26 -08001065 fc_port_t *fcport = ea->fcport;
1066 struct port_database_24xx *pd;
1067 struct srb *sp = ea->sp;
1068
1069 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1070
1071 fcport->flags &= ~FCF_ASYNC_SENT;
1072
1073 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001074 "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1075 fcport->disc_state, pd->current_login_state, ea->rc);
Quinn Trana4239942017-12-28 12:33:26 -08001076
1077 if (fcport->disc_state == DSC_DELETE_PEND)
1078 return;
1079
1080 switch (pd->current_login_state) {
1081 case PDS_PRLI_COMPLETE:
1082 __qla24xx_parse_gpdb(vha, fcport, pd);
1083 break;
1084 case PDS_PLOGI_PENDING:
1085 case PDS_PLOGI_COMPLETE:
1086 case PDS_PRLI_PENDING:
1087 case PDS_PRLI2_PENDING:
1088 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1089 __func__, __LINE__, fcport->port_name);
1090 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1091 return;
1092 case PDS_LOGO_PENDING:
1093 case PDS_PORT_UNAVAILABLE:
1094 default:
1095 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1096 __func__, __LINE__, fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08001097 qlt_schedule_sess_for_deletion(fcport);
Quinn Trana4239942017-12-28 12:33:26 -08001098 return;
1099 }
1100 __qla24xx_handle_gpdb_event(vha, ea);
1101} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001102
1103static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1104{
1105 u8 login = 0;
Quinn Tran040036b2017-12-28 12:33:38 -08001106 int rc;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001107
1108 if (qla_tgt_mode_enabled(vha))
1109 return;
1110
1111 if (qla_dual_mode_enabled(vha)) {
1112 if (N2N_TOPO(vha->hw)) {
1113 u64 mywwn, wwn;
1114
1115 mywwn = wwn_to_u64(vha->port_name);
1116 wwn = wwn_to_u64(fcport->port_name);
1117 if (mywwn > wwn)
1118 login = 1;
1119 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1120 && time_after_eq(jiffies,
1121 fcport->plogi_nack_done_deadline))
1122 login = 1;
1123 } else {
1124 login = 1;
1125 }
1126 } else {
1127 /* initiator mode */
1128 login = 1;
1129 }
1130
1131 if (login) {
Quinn Tran040036b2017-12-28 12:33:38 -08001132 if (fcport->loop_id == FC_NO_LOOP_ID) {
1133 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1134 rc = qla2x00_find_new_loop_id(vha, fcport);
1135 if (rc) {
1136 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1137 "%s %d %8phC post del sess - out of loopid\n",
1138 __func__, __LINE__, fcport->port_name);
1139 fcport->scan_state = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001140 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran040036b2017-12-28 12:33:38 -08001141 return;
1142 }
1143 }
Quinn Tran9cd883f2017-12-28 12:33:24 -08001144 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1145 "%s %d %8phC post login\n",
1146 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001147 qla2x00_post_async_login_work(vha, fcport, NULL);
1148 }
1149}
1150
Quinn Tran726b8542017-01-19 22:28:00 -08001151int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1152{
Quinn Tranf13515a2017-12-28 12:33:15 -08001153 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001154 u64 wwn;
1155
1156 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001157 "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d retry %d lid %d scan %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001158 __func__, fcport->port_name, fcport->disc_state,
1159 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1160 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001161 fcport->login_gen, fcport->login_retry,
Quinn Trana4239942017-12-28 12:33:26 -08001162 fcport->loop_id, fcport->scan_state);
1163
Quinn Tran726b8542017-01-19 22:28:00 -08001164 if (fcport->login_retry == 0)
1165 return 0;
1166
1167 if (fcport->scan_state != QLA_FCPORT_FOUND)
1168 return 0;
1169
Quinn Tran726b8542017-01-19 22:28:00 -08001170 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001171 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1172 return 0;
1173
Quinn Tran5b334692017-03-15 09:48:48 -07001174 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001175 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1176 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001177 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001178 }
Quinn Tran5b334692017-03-15 09:48:48 -07001179 }
1180
Quinn Tran726b8542017-01-19 22:28:00 -08001181 /* for pure Target Mode. Login will not be initiated */
1182 if (vha->host->active_mode == MODE_TARGET)
1183 return 0;
1184
1185 if (fcport->flags & FCF_ASYNC_SENT) {
1186 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1187 return 0;
1188 }
1189
Quinn Trana4239942017-12-28 12:33:26 -08001190 fcport->login_retry--;
1191
Quinn Tran726b8542017-01-19 22:28:00 -08001192 switch (fcport->disc_state) {
1193 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001194 wwn = wwn_to_u64(fcport->node_name);
1195 if (wwn == 0) {
1196 ql_dbg(ql_dbg_disc, vha, 0xffff,
1197 "%s %d %8phC post GNNID\n",
1198 __func__, __LINE__, fcport->port_name);
1199 qla24xx_post_gnnid_work(vha, fcport);
1200 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001201 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1202 "%s %d %8phC post gnl\n",
1203 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001204 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001205 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001206 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001207 }
1208 break;
1209
1210 case DSC_GNL:
1211 if (fcport->login_pause) {
1212 fcport->last_rscn_gen = fcport->rscn_gen;
1213 fcport->last_login_gen = fcport->login_gen;
1214 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1215 break;
1216 }
1217
Quinn Tran9cd883f2017-12-28 12:33:24 -08001218 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001219 break;
1220
1221 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001222 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1223 "%s %d %8phC post gidpn\n",
1224 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001225 if (N2N_TOPO(vha->hw))
1226 qla_chk_n2n_b4_login(vha, fcport);
1227 else
1228 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001229 break;
1230
1231 case DSC_LOGIN_COMPLETE:
1232 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001233 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001234 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001235 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001236 data[0] = data[1] = 0;
1237 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001238 break;
1239
Darren Trapp1cbc0ef2018-03-20 23:09:37 -07001240 case DSC_LOGIN_PEND:
1241 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1242 qla24xx_post_prli_work(vha, fcport);
1243 break;
1244
Quinn Tran726b8542017-01-19 22:28:00 -08001245 default:
1246 break;
1247 }
1248
1249 return 0;
1250}
1251
1252static
1253void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1254{
1255 fcport->rscn_gen++;
1256
Quinn Tran83548fe2017-06-02 09:12:01 -07001257 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1258 "%s %8phC DS %d LS %d\n",
1259 __func__, fcport->port_name, fcport->disc_state,
1260 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001261
1262 if (fcport->flags & FCF_ASYNC_SENT)
1263 return;
1264
1265 switch (fcport->disc_state) {
1266 case DSC_DELETED:
1267 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001268 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001269 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001270 default:
1271 break;
1272 }
1273}
1274
1275int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001276 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001277{
1278 struct qla_work_evt *e;
1279 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1280 if (!e)
1281 return QLA_FUNCTION_FAILED;
1282
1283 e->u.new_sess.id = *id;
1284 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001285 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001286 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001287 if (node_name)
1288 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001289
1290 return qla2x00_post_work(vha, e);
1291}
1292
1293static
Quinn Tran726b8542017-01-19 22:28:00 -08001294void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1295 struct event_arg *ea)
1296{
1297 fc_port_t *fcport = ea->fcport;
1298
Quinn Tran83548fe2017-06-02 09:12:01 -07001299 ql_dbg(ql_dbg_disc, vha, 0x2102,
1300 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1301 __func__, fcport->port_name, fcport->disc_state,
1302 fcport->fw_login_state, fcport->login_pause,
1303 fcport->deleted, fcport->conflict,
1304 fcport->last_rscn_gen, fcport->rscn_gen,
1305 fcport->last_login_gen, fcport->login_gen,
1306 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001307
1308 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001309 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1310 return;
1311
Quinn Tran5b334692017-03-15 09:48:48 -07001312 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001313 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1314 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001315 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001316 }
Quinn Tran5b334692017-03-15 09:48:48 -07001317 }
1318
Quinn Tran726b8542017-01-19 22:28:00 -08001319 if (fcport->flags & FCF_ASYNC_SENT) {
1320 fcport->login_retry++;
1321 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1322 return;
1323 }
1324
1325 if (fcport->disc_state == DSC_DELETE_PEND) {
1326 fcport->login_retry++;
1327 return;
1328 }
1329
1330 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001331 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001332 __func__, __LINE__, fcport->port_name);
1333
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001334 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001335 return;
1336 }
1337
1338 qla24xx_fcport_handle_login(vha, fcport);
1339}
1340
Quinn Tran41dc5292017-01-19 22:28:03 -08001341void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001342{
Quinn Tranf352eeb2017-12-28 12:33:35 -08001343 fc_port_t *f, *tf;
Quinn Tran41dc5292017-01-19 22:28:03 -08001344 uint32_t id = 0, mask, rid;
Quinn Tranf352eeb2017-12-28 12:33:35 -08001345 unsigned long flags;
Quinn Tran726b8542017-01-19 22:28:00 -08001346
1347 switch (ea->event) {
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001348 case FCME_RSCN:
1349 case FCME_GIDPN_DONE:
1350 case FCME_GPSC_DONE:
1351 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001352 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001353 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1354 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1355 return;
1356 break;
1357 default:
1358 break;
1359 }
1360
1361 switch (ea->event) {
1362 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001363 if (test_bit(UNLOADING, &vha->dpc_flags))
1364 return;
1365
1366 qla24xx_handle_relogin_event(vha, ea);
1367 break;
1368 case FCME_RSCN:
1369 if (test_bit(UNLOADING, &vha->dpc_flags))
1370 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001371 switch (ea->id.b.rsvd_1) {
1372 case RSCN_PORT_ADDR:
Quinn Tranf352eeb2017-12-28 12:33:35 -08001373 spin_lock_irqsave(&vha->work_lock, flags);
1374 if (vha->scan.scan_flags == 0) {
1375 ql_dbg(ql_dbg_disc, vha, 0xffff,
1376 "%s: schedule\n", __func__);
1377 vha->scan.scan_flags |= SF_QUEUED;
1378 schedule_delayed_work(&vha->scan.scan_work, 5);
Quinn Tran726b8542017-01-19 22:28:00 -08001379 }
Quinn Tranf352eeb2017-12-28 12:33:35 -08001380 spin_unlock_irqrestore(&vha->work_lock, flags);
1381
Quinn Tran41dc5292017-01-19 22:28:03 -08001382 break;
1383 case RSCN_AREA_ADDR:
1384 case RSCN_DOM_ADDR:
1385 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1386 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001387 ql_dbg(ql_dbg_async, vha, 0x5044,
1388 "RSCN: Area 0x%06x was affected\n",
1389 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001390 } else {
1391 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001392 ql_dbg(ql_dbg_async, vha, 0x507a,
1393 "RSCN: Domain 0x%06x was affected\n",
1394 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001395 }
1396
1397 rid = ea->id.b24 & mask;
1398 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1399 list) {
1400 id = f->d_id.b24 & mask;
1401 if (rid == id) {
1402 ea->fcport = f;
1403 qla24xx_handle_rscn_event(f, ea);
1404 }
1405 }
1406 break;
1407 case RSCN_FAB_ADDR:
1408 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001409 ql_log(ql_log_warn, vha, 0xd045,
1410 "RSCN: Fabric was affected. Addr format %d\n",
1411 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001412 qla2x00_mark_all_devices_lost(vha, 1);
1413 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1414 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001415 }
1416 break;
1417 case FCME_GIDPN_DONE:
1418 qla24xx_handle_gidpn_event(vha, ea);
1419 break;
1420 case FCME_GNL_DONE:
1421 qla24xx_handle_gnl_done_event(vha, ea);
1422 break;
1423 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001424 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001425 break;
1426 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1427 qla24xx_handle_plogi_done_event(vha, ea);
1428 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001429 case FCME_PRLI_DONE:
1430 qla24xx_handle_prli_done_event(vha, ea);
1431 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001432 case FCME_GPDB_DONE:
1433 qla24xx_handle_gpdb_event(vha, ea);
1434 break;
1435 case FCME_GPNID_DONE:
1436 qla24xx_handle_gpnid_event(vha, ea);
1437 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001438 case FCME_GFFID_DONE:
1439 qla24xx_handle_gffid_event(vha, ea);
1440 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001441 case FCME_ADISC_DONE:
1442 qla24xx_handle_adisc_event(vha, ea);
1443 break;
Quinn Trana4239942017-12-28 12:33:26 -08001444 case FCME_GNNID_DONE:
1445 qla24xx_handle_gnnid_event(vha, ea);
1446 break;
1447 case FCME_GFPNID_DONE:
1448 qla24xx_handle_gfpnid_event(vha, ea);
1449 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001450 default:
1451 BUG_ON(1);
1452 break;
1453 }
1454}
1455
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001456static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001457qla2x00_tmf_iocb_timeout(void *data)
1458{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001459 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001460 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1461
1462 tmf->u.tmf.comp_status = CS_TIMEOUT;
1463 complete(&tmf->u.tmf.comp);
1464}
1465
1466static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001467qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001468{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001469 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001470 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001471
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001472 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001473}
1474
1475int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001476qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001477 uint32_t tag)
1478{
1479 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001480 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001481 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001482 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001483
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001484 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001485 if (!sp)
1486 goto done;
1487
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001488 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001489 sp->type = SRB_TM_CMD;
1490 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001491 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1492 tm_iocb->u.tmf.flags = flags;
1493 tm_iocb->u.tmf.lun = lun;
1494 tm_iocb->u.tmf.data = tag;
1495 sp->done = qla2x00_tmf_sp_done;
1496 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1497 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001498
1499 rval = qla2x00_start_sp(sp);
1500 if (rval != QLA_SUCCESS)
1501 goto done_free_sp;
1502
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001503 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001504 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1505 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1506 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001507
1508 wait_for_completion(&tm_iocb->u.tmf.comp);
1509
1510 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1511 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1512
1513 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1514 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1515 "TM IOCB failed (%x).\n", rval);
1516 }
1517
1518 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1519 flags = tm_iocb->u.tmf.flags;
1520 lun = (uint16_t)tm_iocb->u.tmf.lun;
1521
1522 /* Issue Marker IOCB */
1523 qla2x00_marker(vha, vha->hw->req_q_map[0],
1524 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1525 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1526 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001527
1528done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001529 sp->free(sp);
Quinn Tran6d6749272017-12-28 12:33:41 -08001530 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001531done:
1532 return rval;
1533}
1534
Armen Baloyan4440e462014-02-26 04:15:18 -05001535static void
1536qla24xx_abort_iocb_timeout(void *data)
1537{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001538 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001539 struct srb_iocb *abt = &sp->u.iocb_cmd;
1540
1541 abt->u.abt.comp_status = CS_TIMEOUT;
1542 complete(&abt->u.abt.comp);
1543}
1544
1545static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001546qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001547{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001548 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001549 struct srb_iocb *abt = &sp->u.iocb_cmd;
1550
1551 complete(&abt->u.abt.comp);
1552}
1553
Quinn Tran15f30a52017-03-15 09:48:52 -07001554int
Armen Baloyan4440e462014-02-26 04:15:18 -05001555qla24xx_async_abort_cmd(srb_t *cmd_sp)
1556{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001557 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001558 fc_port_t *fcport = cmd_sp->fcport;
1559 struct srb_iocb *abt_iocb;
1560 srb_t *sp;
1561 int rval = QLA_FUNCTION_FAILED;
1562
1563 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1564 if (!sp)
1565 goto done;
1566
1567 abt_iocb = &sp->u.iocb_cmd;
1568 sp->type = SRB_ABT_CMD;
1569 sp->name = "abort";
1570 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1571 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
Darren Trappb027a5a2018-01-15 20:46:51 -08001572
1573 if (vha->flags.qpairs_available && cmd_sp->qpair)
1574 abt_iocb->u.abt.req_que_no =
1575 cpu_to_le16(cmd_sp->qpair->req->id);
1576 else
1577 abt_iocb->u.abt.req_que_no = cpu_to_le16(vha->req->id);
1578
Armen Baloyan4440e462014-02-26 04:15:18 -05001579 sp->done = qla24xx_abort_sp_done;
1580 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1581 init_completion(&abt_iocb->u.abt.comp);
1582
1583 rval = qla2x00_start_sp(sp);
1584 if (rval != QLA_SUCCESS)
1585 goto done_free_sp;
1586
1587 ql_dbg(ql_dbg_async, vha, 0x507c,
1588 "Abort command issued - hdl=%x, target_id=%x\n",
1589 cmd_sp->handle, fcport->tgt_id);
1590
1591 wait_for_completion(&abt_iocb->u.abt.comp);
1592
1593 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1594 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1595
1596done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001597 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001598done:
1599 return rval;
1600}
1601
1602int
1603qla24xx_async_abort_command(srb_t *sp)
1604{
1605 unsigned long flags = 0;
1606
1607 uint32_t handle;
1608 fc_port_t *fcport = sp->fcport;
1609 struct scsi_qla_host *vha = fcport->vha;
1610 struct qla_hw_data *ha = vha->hw;
1611 struct req_que *req = vha->req;
1612
Darren Trappb027a5a2018-01-15 20:46:51 -08001613 if (vha->flags.qpairs_available && sp->qpair)
1614 req = sp->qpair->req;
1615
Armen Baloyan4440e462014-02-26 04:15:18 -05001616 spin_lock_irqsave(&ha->hardware_lock, flags);
1617 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1618 if (req->outstanding_cmds[handle] == sp)
1619 break;
1620 }
1621 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1622 if (handle == req->num_outstanding_cmds) {
1623 /* Command not found. */
1624 return QLA_FUNCTION_FAILED;
1625 }
1626 if (sp->type == SRB_FXIOCB_DCMD)
1627 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1628 FXDISC_ABORT_IOCTL);
1629
1630 return qla24xx_async_abort_cmd(sp);
1631}
1632
Quinn Tran726b8542017-01-19 22:28:00 -08001633static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001634qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1635{
1636 switch (ea->data[0]) {
1637 case MBS_COMMAND_COMPLETE:
1638 ql_dbg(ql_dbg_disc, vha, 0x2118,
1639 "%s %d %8phC post gpdb\n",
1640 __func__, __LINE__, ea->fcport->port_name);
1641
1642 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1643 ea->fcport->logout_on_delete = 1;
1644 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1645 break;
1646 default:
Darren Trapp1cbc0ef2018-03-20 23:09:37 -07001647 if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
1648 (ea->iop[1] == 0x50000)) { /* reson 5=busy expl:0x0 */
1649 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1650 ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
1651 break;
1652 }
1653
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001654 if (ea->fcport->n2n_flag) {
1655 ql_dbg(ql_dbg_disc, vha, 0x2118,
1656 "%s %d %8phC post fc4 prli\n",
1657 __func__, __LINE__, ea->fcport->port_name);
1658 ea->fcport->fc4f_nvme = 0;
1659 ea->fcport->n2n_flag = 0;
1660 qla24xx_post_prli_work(vha, ea->fcport);
1661 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001662 ql_dbg(ql_dbg_disc, vha, 0x2119,
1663 "%s %d %8phC unhandle event of %x\n",
1664 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1665 break;
1666 }
1667}
1668
1669static void
Quinn Tran726b8542017-01-19 22:28:00 -08001670qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001671{
Quinn Tran726b8542017-01-19 22:28:00 -08001672 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001673 u16 lid;
1674 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001675 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001676 struct fc_port *fcport = ea->fcport;
1677
Quinn Tranf352eeb2017-12-28 12:33:35 -08001678 ql_dbg(ql_dbg_disc, vha, 0xffff,
1679 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1680 __func__, fcport->port_name, fcport->disc_state,
1681 fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1682 ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1683 ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1684
Quinn Trana4239942017-12-28 12:33:26 -08001685 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1686 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1687 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1688 "%s %d %8phC Remote is trying to login\n",
1689 __func__, __LINE__, fcport->port_name);
1690 return;
1691 }
1692
1693 if (fcport->disc_state == DSC_DELETE_PEND)
1694 return;
1695
1696 if (ea->sp->gen2 != fcport->login_gen) {
1697 /* target side must have changed it. */
1698 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001699 "%s %8phC generation changed\n",
1700 __func__, fcport->port_name);
1701 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Trana4239942017-12-28 12:33:26 -08001702 return;
1703 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1704 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1705 __func__, __LINE__, fcport->port_name);
1706 qla24xx_post_gidpn_work(vha, fcport);
1707 return;
1708 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001709
Quinn Tran726b8542017-01-19 22:28:00 -08001710 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001711 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001712 /*
1713 * Driver must validate login state - If PRLI not complete,
1714 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1715 * requests.
1716 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001717 if (ea->fcport->fc4f_nvme) {
1718 ql_dbg(ql_dbg_disc, vha, 0x2117,
1719 "%s %d %8phC post prli\n",
1720 __func__, __LINE__, ea->fcport->port_name);
1721 qla24xx_post_prli_work(vha, ea->fcport);
1722 } else {
1723 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001724 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1725 __func__, __LINE__, ea->fcport->port_name,
1726 ea->fcport->loop_id, ea->fcport->d_id.b24);
1727
1728 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001729 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana084fd62017-12-04 14:45:00 -08001730 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001731 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1732 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001733 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001734 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1735 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1736
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001737 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1738 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001739 break;
1740 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001741 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001742 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1743
1744 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1745 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1746 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001747 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1748 else
Quinn Tran726b8542017-01-19 22:28:00 -08001749 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001750 break;
1751 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001752 /* data[1] = IO PARAM 1 = nport ID */
1753 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1754 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1755 cid.b.al_pa = ea->iop[1] & 0xff;
1756 cid.b.rsvd_1 = 0;
1757
Quinn Tran83548fe2017-06-02 09:12:01 -07001758 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1759 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1760 __func__, __LINE__, ea->fcport->port_name,
1761 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001762
1763 if (IS_SW_RESV_ADDR(cid)) {
1764 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1765 ea->fcport->loop_id = FC_NO_LOOP_ID;
1766 } else {
1767 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001768 }
Quinn Tran726b8542017-01-19 22:28:00 -08001769 qla24xx_post_gnl_work(vha, ea->fcport);
1770 break;
1771 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001772 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1773 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1774 __func__, __LINE__, ea->fcport->port_name,
1775 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1776 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001777
Quinn Trana084fd62017-12-04 14:45:00 -08001778 lid = ea->iop[1] & 0xffff;
1779 qlt_find_sess_invalidate_other(vha,
1780 wwn_to_u64(ea->fcport->port_name),
1781 ea->fcport->d_id, lid, &conflict_fcport);
1782
1783 if (conflict_fcport) {
1784 /*
1785 * Another fcport share the same loop_id/nport id.
1786 * Conflict fcport needs to finish cleanup before this
1787 * fcport can proceed to login.
1788 */
1789 conflict_fcport->conflict = ea->fcport;
1790 ea->fcport->login_pause = 1;
1791
1792 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1793 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1794 __func__, __LINE__, ea->fcport->port_name,
1795 ea->fcport->d_id.b24, lid);
1796 qla2x00_clear_loop_id(ea->fcport);
1797 qla24xx_post_gidpn_work(vha, ea->fcport);
1798 } else {
1799 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1800 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1801 __func__, __LINE__, ea->fcport->port_name,
1802 ea->fcport->d_id.b24, lid);
1803
1804 qla2x00_clear_loop_id(ea->fcport);
1805 set_bit(lid, vha->hw->loop_id_map);
1806 ea->fcport->loop_id = lid;
1807 ea->fcport->keep_nport_handle = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001808 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana084fd62017-12-04 14:45:00 -08001809 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001810 break;
1811 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001812 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001813}
1814
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001815void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001816qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1817 uint16_t *data)
1818{
Quinn Tran726b8542017-01-19 22:28:00 -08001819 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001820 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001821 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001822 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001823}
1824
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001825void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001826qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1827 uint16_t *data)
1828{
1829 if (data[0] == MBS_COMMAND_COMPLETE) {
1830 qla2x00_update_fcport(vha, fcport);
1831
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001832 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001833 }
1834
1835 /* Retry login. */
1836 fcport->flags &= ~FCF_ASYNC_SENT;
1837 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1838 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1839 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001840 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001841
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001842 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001843}
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845/****************************************************************************/
1846/* QLogic ISP2x00 Hardware Support Functions. */
1847/****************************************************************************/
1848
Saurav Kashyapfa492632012-11-21 02:40:29 -05001849static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001850qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1851{
1852 int rval = QLA_SUCCESS;
1853 struct qla_hw_data *ha = vha->hw;
1854 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001855 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001856
1857 qla83xx_idc_lock(vha, 0);
1858
1859 /* SV: TODO: Assign initialization timeout from
1860 * flash-info / other param
1861 */
1862 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1863 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1864
1865 /* Set our fcoe function presence */
1866 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1867 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1868 "Error while setting DRV-Presence.\n");
1869 rval = QLA_FUNCTION_FAILED;
1870 goto exit;
1871 }
1872
1873 /* Decide the reset ownership */
1874 qla83xx_reset_ownership(vha);
1875
1876 /*
1877 * On first protocol driver load:
1878 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1879 * register.
1880 * Others: Check compatibility with current IDC Major version.
1881 */
1882 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1883 if (ha->flags.nic_core_reset_owner) {
1884 /* Set IDC Major version */
1885 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1886 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1887
1888 /* Clearing IDC-Lock-Recovery register */
1889 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1890 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1891 /*
1892 * Clear further IDC participation if we are not compatible with
1893 * the current IDC Major Version.
1894 */
1895 ql_log(ql_log_warn, vha, 0xb07d,
1896 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1897 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1898 __qla83xx_clear_drv_presence(vha);
1899 rval = QLA_FUNCTION_FAILED;
1900 goto exit;
1901 }
1902 /* Each function sets its supported Minor version. */
1903 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1904 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1905 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1906
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001907 if (ha->flags.nic_core_reset_owner) {
1908 memset(config, 0, sizeof(config));
1909 if (!qla81xx_get_port_config(vha, config))
1910 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1911 QLA8XXX_DEV_READY);
1912 }
1913
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001914 rval = qla83xx_idc_state_handler(vha);
1915
1916exit:
1917 qla83xx_idc_unlock(vha, 0);
1918
1919 return rval;
1920}
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922/*
1923* qla2x00_initialize_adapter
1924* Initialize board.
1925*
1926* Input:
1927* ha = adapter block pointer.
1928*
1929* Returns:
1930* 0 = success
1931*/
1932int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001933qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001936 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001937 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001938
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001939 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1940 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001943 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001944 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001945 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001946 ha->flags.pci_channel_io_perm_failure = 0;
1947 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001948 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001949 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1950 atomic_set(&vha->loop_state, LOOP_DOWN);
1951 vha->device_flags = DFLG_NO_CABLE;
1952 vha->dpc_flags = 0;
1953 vha->flags.management_server_logged_in = 0;
1954 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 ha->isp_abort_cnt = 0;
1956 ha->beacon_blink_led = 0;
1957
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001958 set_bit(0, ha->req_qid_map);
1959 set_bit(0, ha->rsp_qid_map);
1960
Chad Dupuiscfb09192011-11-18 09:03:07 -08001961 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001962 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001963 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001965 ql_log(ql_log_warn, vha, 0x0044,
1966 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return (rval);
1968 }
1969
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001970 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001972 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001973 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001974 ql_log(ql_log_fatal, vha, 0x004f,
1975 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001976 return rval;
1977 }
1978
1979 if (IS_QLA8044(ha)) {
1980 qla8044_read_reset_template(vha);
1981
1982 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1983 * If DONRESET_BIT0 is set, drivers should not set dev_state
1984 * to NEED_RESET. But if NEED_RESET is set, drivers should
1985 * should honor the reset. */
1986 if (ql2xdontresethba == 1)
1987 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001988 }
1989
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001990 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001991 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001992 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001993
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001994 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001996 if (ha->flags.disable_serdes) {
1997 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001998 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001999 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07002000 return QLA_FUNCTION_FAILED;
2001 }
2002
Chad Dupuiscfb09192011-11-18 09:03:07 -08002003 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002004 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002006 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2007 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08002008 if (rval)
2009 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002010 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08002011 if (rval)
2012 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002014
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002015 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002016 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002017 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002018 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002019 "Unable to configure ISP84XX.\n");
2020 return QLA_FUNCTION_FAILED;
2021 }
2022 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002023
Quinn Tranead03852017-01-19 22:28:01 -08002024 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002025 rval = qla2x00_init_rings(vha);
2026
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002027 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002029 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07002030 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002031 rval = qla84xx_init_chip(vha);
2032 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002033 ql_log(ql_log_warn, vha, 0x00d4,
2034 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07002035 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002036 }
2037 }
2038
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002039 /* Load the NIC Core f/w if we are the first protocol driver. */
2040 if (IS_QLA8031(ha)) {
2041 rval = qla83xx_nic_core_fw_load(vha);
2042 if (rval)
2043 ql_log(ql_log_warn, vha, 0x0124,
2044 "Error in initializing NIC Core f/w.\n");
2045 }
2046
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002047 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2048 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002049
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002050 if (IS_P3P_TYPE(ha))
2051 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2052 else
2053 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return (rval);
2056}
2057
2058/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002059 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002060 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 *
2062 * Returns 0 on success.
2063 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002064int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002065qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002067 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002068 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002069 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002070 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002073 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002076 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2078
Andrew Vasquez737faec2008-10-24 15:13:45 -07002079 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002081 /* Get PCI bus information. */
2082 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002083 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002084 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2085
2086 return QLA_SUCCESS;
2087}
2088
2089/**
2090 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002091 * @vha: HA context
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002092 *
2093 * Returns 0 on success.
2094 */
2095int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002096qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002097{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002098 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002099 unsigned long flags = 0;
2100 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002101 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002102 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002103
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002104 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002105 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002106
2107 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002108 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002109
2110 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2111 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002112 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002113
2114 /*
2115 * If this is a 2300 card and not 2312, reset the
2116 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2117 * the 2310 also reports itself as a 2300 so we need to get the
2118 * fb revision level -- a 6 indicates it really is a 2300 and
2119 * not a 2310.
2120 */
2121 if (IS_QLA2300(ha)) {
2122 spin_lock_irqsave(&ha->hardware_lock, flags);
2123
2124 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002125 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002126 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002127 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002128 break;
2129
2130 udelay(10);
2131 }
2132
2133 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002134 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2135 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002136
2137 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002138 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002139
2140 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002141 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002142
2143 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002144 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2145 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002146
2147 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002148 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002149 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002150 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002151 break;
2152
2153 udelay(10);
2154 }
2155
2156 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2157 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002158
2159 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2160
Andrew Vasquez737faec2008-10-24 15:13:45 -07002161 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002162
2163 /* Get PCI bus information. */
2164 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002165 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002166 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2167
2168 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002172 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002173 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002174 *
2175 * Returns 0 on success.
2176 */
2177int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002178qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002179{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002180 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002181 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002182 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002183 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002184
2185 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002186 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002187
2188 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002189 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002190 w &= ~PCI_COMMAND_INTX_DISABLE;
2191 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2192
2193 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2194
2195 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002196 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2197 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002198
2199 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002200 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002201 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002202
Andrew Vasquez737faec2008-10-24 15:13:45 -07002203 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002204
Auke Kok44c10132007-06-08 15:46:36 -07002205 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002206
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002207 /* Get PCI bus information. */
2208 spin_lock_irqsave(&ha->hardware_lock, flags);
2209 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2210 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2211
2212 return QLA_SUCCESS;
2213}
2214
2215/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002216 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002217 * @vha: HA context
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002218 *
2219 * Returns 0 on success.
2220 */
2221int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002222qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002223{
2224 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002225 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002226
2227 pci_set_master(ha->pdev);
2228 pci_try_set_mwi(ha->pdev);
2229
2230 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2231 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2232 w &= ~PCI_COMMAND_INTX_DISABLE;
2233 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2234
2235 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002236 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002237 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002238
Andrew Vasquez737faec2008-10-24 15:13:45 -07002239 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002240
2241 ha->chip_revision = ha->pdev->revision;
2242
2243 return QLA_SUCCESS;
2244}
2245
2246/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 * qla2x00_isp_firmware() - Choose firmware image.
Bart Van Assche2db62282018-01-23 16:33:51 -08002248 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 *
2250 * Returns 0 on success.
2251 */
2252static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002253qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002256 uint16_t loop_id, topo, sw_cap;
2257 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002258 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002261 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002264 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002267 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002268 if (rval == QLA_SUCCESS) {
2269 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002270 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002271 &area, &domain, &topo, &sw_cap);
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002275 if (rval)
2276 ql_dbg(ql_dbg_init, vha, 0x007a,
2277 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 return (rval);
2280}
2281
2282/**
2283 * qla2x00_reset_chip() - Reset ISP chip.
Bart Van Assche2db62282018-01-23 16:33:51 -08002284 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 *
2286 * Returns 0 on success.
2287 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002288void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002289qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002292 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002293 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 uint16_t cmd;
2296
Andrew Vasquez85880802009-12-15 21:29:46 -08002297 if (unlikely(pci_channel_offline(ha->pdev)))
2298 return;
2299
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002300 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 spin_lock_irqsave(&ha->hardware_lock, flags);
2303
2304 /* Turn off master enable */
2305 cmd = 0;
2306 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2307 cmd &= ~PCI_COMMAND_MASTER;
2308 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2309
2310 if (!IS_QLA2100(ha)) {
2311 /* Pause RISC. */
2312 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2313 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2314 for (cnt = 0; cnt < 30000; cnt++) {
2315 if ((RD_REG_WORD(&reg->hccr) &
2316 HCCR_RISC_PAUSE) != 0)
2317 break;
2318 udelay(100);
2319 }
2320 } else {
2321 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2322 udelay(10);
2323 }
2324
2325 /* Select FPM registers. */
2326 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2327 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2328
2329 /* FPM Soft Reset. */
2330 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2331 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2332
2333 /* Toggle Fpm Reset. */
2334 if (!IS_QLA2200(ha)) {
2335 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2336 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2337 }
2338
2339 /* Select frame buffer registers. */
2340 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2341 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2342
2343 /* Reset frame buffer FIFOs. */
2344 if (IS_QLA2200(ha)) {
2345 WRT_FB_CMD_REG(ha, reg, 0xa000);
2346 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2347 } else {
2348 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2349
2350 /* Read back fb_cmd until zero or 3 seconds max */
2351 for (cnt = 0; cnt < 3000; cnt++) {
2352 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2353 break;
2354 udelay(100);
2355 }
2356 }
2357
2358 /* Select RISC module registers. */
2359 WRT_REG_WORD(&reg->ctrl_status, 0);
2360 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2361
2362 /* Reset RISC processor. */
2363 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2364 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2365
2366 /* Release RISC processor. */
2367 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2368 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2369 }
2370
2371 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2372 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2373
2374 /* Reset ISP chip. */
2375 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2376
2377 /* Wait for RISC to recover from reset. */
2378 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2379 /*
2380 * It is necessary to for a delay here since the card doesn't
2381 * respond to PCI reads during a reset. On some architectures
2382 * this will result in an MCA.
2383 */
2384 udelay(20);
2385 for (cnt = 30000; cnt; cnt--) {
2386 if ((RD_REG_WORD(&reg->ctrl_status) &
2387 CSR_ISP_SOFT_RESET) == 0)
2388 break;
2389 udelay(100);
2390 }
2391 } else
2392 udelay(10);
2393
2394 /* Reset RISC processor. */
2395 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2396
2397 WRT_REG_WORD(&reg->semaphore, 0);
2398
2399 /* Release RISC processor. */
2400 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2401 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2402
2403 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2404 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002405 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 udelay(100);
2409 }
2410 } else
2411 udelay(100);
2412
2413 /* Turn on master enable */
2414 cmd |= PCI_COMMAND_MASTER;
2415 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2416
2417 /* Disable RISC pause on FPM parity error. */
2418 if (!IS_QLA2100(ha)) {
2419 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2420 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2421 }
2422
2423 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2424}
2425
2426/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002427 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
Bart Van Assche2db62282018-01-23 16:33:51 -08002428 * @vha: HA context
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002429 *
2430 * Returns 0 on success.
2431 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002432static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002433qla81xx_reset_mpi(scsi_qla_host_t *vha)
2434{
2435 uint16_t mb[4] = {0x1010, 0, 1, 0};
2436
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002437 if (!IS_QLA81XX(vha->hw))
2438 return QLA_SUCCESS;
2439
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002440 return qla81xx_write_mpi_register(vha, mb);
2441}
2442
2443/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002444 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Bart Van Assche2db62282018-01-23 16:33:51 -08002445 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002446 *
2447 * Returns 0 on success.
2448 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002449static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002450qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002451{
2452 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002453 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002454 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002455 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002456 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002457 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002458 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002459
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002460 spin_lock_irqsave(&ha->hardware_lock, flags);
2461
2462 /* Reset RISC. */
2463 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2464 for (cnt = 0; cnt < 30000; cnt++) {
2465 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2466 break;
2467
2468 udelay(10);
2469 }
2470
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002471 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2472 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2473
2474 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2475 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2476 RD_REG_DWORD(&reg->hccr),
2477 RD_REG_DWORD(&reg->ctrl_status),
2478 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2479
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002480 WRT_REG_DWORD(&reg->ctrl_status,
2481 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002482 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002483
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002484 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002485
Andrew Vasquez88c26662005-07-08 17:59:26 -07002486 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002487 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002488 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2489 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002490 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002491 if (cnt)
2492 udelay(5);
2493 else
2494 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002495 }
2496
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002497 if (rval == QLA_SUCCESS)
2498 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2499
2500 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2501 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2502 RD_REG_DWORD(&reg->hccr),
2503 RD_REG_DWORD(&reg->mailbox0));
2504
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002505 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002506 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002507 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002508 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002509 if ((RD_REG_DWORD(&reg->ctrl_status) &
2510 CSRX_ISP_SOFT_RESET) == 0)
2511 break;
2512
2513 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002514 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002515 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2516 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2517
2518 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2519 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2520 RD_REG_DWORD(&reg->hccr),
2521 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002522
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002523 /* If required, do an MPI FW reset now */
2524 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2525 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2526 if (++abts_cnt < 5) {
2527 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2528 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2529 } else {
2530 /*
2531 * We exhausted the ISP abort retries. We have to
2532 * set the board offline.
2533 */
2534 abts_cnt = 0;
2535 vha->flags.online = 0;
2536 }
2537 }
2538 }
2539
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002540 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2541 RD_REG_DWORD(&reg->hccr);
2542
2543 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2544 RD_REG_DWORD(&reg->hccr);
2545
2546 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2547 RD_REG_DWORD(&reg->hccr);
2548
Bart Van Assche52c82822015-07-09 07:23:26 -07002549 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002550 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002551 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002552 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002553 if (cnt)
2554 udelay(5);
2555 else
2556 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002557 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002558 if (rval == QLA_SUCCESS)
2559 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2560
2561 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2562 "Host Risc 0x%x, mailbox0 0x%x\n",
2563 RD_REG_DWORD(&reg->hccr),
2564 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002565
2566 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002567
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002568 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2569 "Driver in %s mode\n",
2570 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2571
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002572 if (IS_NOPOLLING_TYPE(ha))
2573 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002574
2575 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002576}
2577
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002578static void
2579qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2580{
2581 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2582
2583 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2584 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2585
2586}
2587
2588static void
2589qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2590{
2591 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2592
2593 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2594 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2595}
2596
2597static void
2598qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2599{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002600 uint32_t wd32 = 0;
2601 uint delta_msec = 100;
2602 uint elapsed_msec = 0;
2603 uint timeout_msec;
2604 ulong n;
2605
Joe Carnucciocc790762015-08-04 13:37:53 -04002606 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2607 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002608 return;
2609
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002610 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2611 udelay(100);
2612
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002613attempt:
2614 timeout_msec = TIMEOUT_SEMAPHORE;
2615 n = timeout_msec / delta_msec;
2616 while (n--) {
2617 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2618 qla25xx_read_risc_sema_reg(vha, &wd32);
2619 if (wd32 & RISC_SEMAPHORE)
2620 break;
2621 msleep(delta_msec);
2622 elapsed_msec += delta_msec;
2623 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2624 goto force;
2625 }
2626
2627 if (!(wd32 & RISC_SEMAPHORE))
2628 goto force;
2629
2630 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2631 goto acquired;
2632
2633 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2634 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2635 n = timeout_msec / delta_msec;
2636 while (n--) {
2637 qla25xx_read_risc_sema_reg(vha, &wd32);
2638 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2639 break;
2640 msleep(delta_msec);
2641 elapsed_msec += delta_msec;
2642 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2643 goto force;
2644 }
2645
2646 if (wd32 & RISC_SEMAPHORE_FORCE)
2647 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2648
2649 goto attempt;
2650
2651force:
2652 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2653
2654acquired:
2655 return;
2656}
2657
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002658/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002659 * qla24xx_reset_chip() - Reset ISP24xx chip.
Bart Van Assche2db62282018-01-23 16:33:51 -08002660 * @vha: HA context
Andrew Vasquez88c26662005-07-08 17:59:26 -07002661 *
2662 * Returns 0 on success.
2663 */
2664void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002665qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002666{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002667 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002668
2669 if (pci_channel_offline(ha->pdev) &&
2670 ha->flags.pci_channel_io_perm_failure) {
2671 return;
2672 }
2673
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002674 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002675
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002676 qla25xx_manipulate_risc_semaphore(vha);
2677
Andrew Vasquez88c26662005-07-08 17:59:26 -07002678 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002679 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002680}
2681
2682/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 * qla2x00_chip_diag() - Test chip for proper operation.
Bart Van Assche2db62282018-01-23 16:33:51 -08002684 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 *
2686 * Returns 0 on success.
2687 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002688int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002689qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002692 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002693 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 unsigned long flags = 0;
2695 uint16_t data;
2696 uint32_t cnt;
2697 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002698 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 /* Assume a failed state */
2701 rval = QLA_FUNCTION_FAILED;
2702
Bart Van Asscheda4704d2018-01-23 16:33:47 -08002703 ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
2704 &reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 spin_lock_irqsave(&ha->hardware_lock, flags);
2707
2708 /* Reset ISP chip. */
2709 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2710
2711 /*
2712 * We need to have a delay here since the card will not respond while
2713 * in reset causing an MCA on some architectures.
2714 */
2715 udelay(20);
2716 data = qla2x00_debounce_register(&reg->ctrl_status);
2717 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2718 udelay(5);
2719 data = RD_REG_WORD(&reg->ctrl_status);
2720 barrier();
2721 }
2722
2723 if (!cnt)
2724 goto chip_diag_failed;
2725
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002726 ql_dbg(ql_dbg_init, vha, 0x007c,
2727 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 /* Reset RISC processor. */
2730 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2731 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2732
2733 /* Workaround for QLA2312 PCI parity error */
2734 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2735 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2736 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2737 udelay(5);
2738 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002739 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 } else
2742 udelay(10);
2743
2744 if (!cnt)
2745 goto chip_diag_failed;
2746
2747 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002748 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2751 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2752 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2753 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2754 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2755 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002756 ql_log(ql_log_warn, vha, 0x0062,
2757 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2758 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 goto chip_diag_failed;
2761 }
2762 ha->product_id[0] = mb[1];
2763 ha->product_id[1] = mb[2];
2764 ha->product_id[2] = mb[3];
2765 ha->product_id[3] = mb[4];
2766
2767 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002768 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2770 else
2771 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002772 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 if (IS_QLA2200(ha) &&
2775 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2776 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002777 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002779 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 ha->fw_transfer_size = 128;
2781 }
2782
2783 /* Wrap Incoming Mailboxes Test. */
2784 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2785
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002786 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002787 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002788 if (rval)
2789 ql_log(ql_log_warn, vha, 0x0080,
2790 "Failed mailbox send register test.\n");
2791 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /* Flag a successful rval */
2793 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 spin_lock_irqsave(&ha->hardware_lock, flags);
2795
2796chip_diag_failed:
2797 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002798 ql_log(ql_log_info, vha, 0x0081,
2799 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2802
2803 return (rval);
2804}
2805
2806/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002807 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
Bart Van Assche2db62282018-01-23 16:33:51 -08002808 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002809 *
2810 * Returns 0 on success.
2811 */
2812int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002813qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002814{
2815 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002816 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002817 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002818
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002819 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002820 return QLA_SUCCESS;
2821
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002822 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002823
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002824 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002825 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002826 ql_log(ql_log_warn, vha, 0x0082,
2827 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002828 } else {
2829 /* Flag a successful rval */
2830 rval = QLA_SUCCESS;
2831 }
2832
2833 return rval;
2834}
2835
Quinn Tranad0a0b02017-12-28 12:33:14 -08002836static void
2837qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002838{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002839 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002840 dma_addr_t tc_dma;
2841 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002842 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002843
Quinn Tranad0a0b02017-12-28 12:33:14 -08002844 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002845 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002846 "%s: Offload Mem is already allocated.\n",
2847 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002848 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002849 }
2850
Quinn Tranad0a0b02017-12-28 12:33:14 -08002851 if (IS_FWI2_CAPABLE(ha)) {
2852 /* Allocate memory for Fibre Channel Event Buffer. */
2853 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2854 !IS_QLA27XX(ha))
2855 goto try_eft;
2856
2857 if (ha->fce)
2858 dma_free_coherent(&ha->pdev->dev,
2859 FCE_SIZE, ha->fce, ha->fce_dma);
2860
2861 /* Allocate memory for Fibre Channel Event Buffer. */
2862 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2863 GFP_KERNEL);
2864 if (!tc) {
2865 ql_log(ql_log_warn, vha, 0x00be,
2866 "Unable to allocate (%d KB) for FCE.\n",
2867 FCE_SIZE / 1024);
2868 goto try_eft;
2869 }
2870
2871 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2872 ha->fce_mb, &ha->fce_bufs);
2873 if (rval) {
2874 ql_log(ql_log_warn, vha, 0x00bf,
2875 "Unable to initialize FCE (%d).\n", rval);
2876 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2877 tc_dma);
2878 ha->flags.fce_enabled = 0;
2879 goto try_eft;
2880 }
2881 ql_dbg(ql_dbg_init, vha, 0x00c0,
2882 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2883
2884 ha->flags.fce_enabled = 1;
2885 ha->fce_dma = tc_dma;
2886 ha->fce = tc;
2887
2888try_eft:
2889 if (ha->eft)
2890 dma_free_coherent(&ha->pdev->dev,
2891 EFT_SIZE, ha->eft, ha->eft_dma);
2892
2893 /* Allocate memory for Extended Trace Buffer. */
2894 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2895 GFP_KERNEL);
2896 if (!tc) {
2897 ql_log(ql_log_warn, vha, 0x00c1,
2898 "Unable to allocate (%d KB) for EFT.\n",
2899 EFT_SIZE / 1024);
2900 goto eft_err;
2901 }
2902
2903 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2904 if (rval) {
2905 ql_log(ql_log_warn, vha, 0x00c2,
2906 "Unable to initialize EFT (%d).\n", rval);
2907 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2908 tc_dma);
2909 goto eft_err;
2910 }
2911 ql_dbg(ql_dbg_init, vha, 0x00c3,
2912 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2913
2914 ha->eft_dma = tc_dma;
2915 ha->eft = tc;
2916 }
2917
2918eft_err:
2919 return;
2920}
2921
2922void
2923qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2924{
2925 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2926 eft_size, fce_size, mq_size;
2927 struct qla_hw_data *ha = vha->hw;
2928 struct req_que *req = ha->req_q_map[0];
2929 struct rsp_que *rsp = ha->rsp_q_map[0];
2930 struct qla2xxx_fw_dump *fw_dump;
2931
Chad Dupuisf73cb692014-02-26 04:15:06 -05002932 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2933 req_q_size = rsp_q_size = 0;
2934
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002935 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2936 fixed_size = sizeof(struct qla2100_fw_dump);
2937 } else if (IS_QLA23XX(ha)) {
2938 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2939 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2940 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002941 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002942 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002943 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2944 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002945 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2946 else if (IS_QLA25XX(ha))
2947 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2948 else
2949 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002950
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002951 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2952 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002953 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002954 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002955 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002956 /*
2957 * Allocate maximum buffer size for all queues.
2958 * Resizing must be done at end-of-dump processing.
2959 */
2960 mq_size += ha->max_req_queues *
2961 (req->length * sizeof(request_t));
2962 mq_size += ha->max_rsp_queues *
2963 (rsp->length * sizeof(response_t));
2964 }
Arun Easi00876ae2013-03-25 02:21:37 -04002965 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002966 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002967 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002968 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2969 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002970 goto try_eft;
2971
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002972 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002973try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002974 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002975 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002976 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002977 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002978
Chad Dupuisf73cb692014-02-26 04:15:06 -05002979 if (IS_QLA27XX(ha)) {
2980 if (!ha->fw_dump_template) {
2981 ql_log(ql_log_warn, vha, 0x00ba,
2982 "Failed missing fwdump template\n");
2983 return;
2984 }
2985 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2986 ql_dbg(ql_dbg_init, vha, 0x00fa,
2987 "-> allocating fwdump (%x bytes)...\n", dump_size);
2988 goto allocate;
2989 }
2990
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002991 req_q_size = req->length * sizeof(request_t);
2992 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002993 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002994 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002995 ha->chain_offset = dump_size;
2996 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002997
Quinn Tranb945e772017-06-13 20:47:29 -07002998 if (ha->exchoffld_buf)
2999 dump_size += sizeof(struct qla2xxx_offld_chain) +
3000 ha->exchoffld_size;
3001 if (ha->exlogin_buf)
3002 dump_size += sizeof(struct qla2xxx_offld_chain) +
3003 ha->exlogin_size;
3004
Chad Dupuisf73cb692014-02-26 04:15:06 -05003005allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08003006 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
3007 fw_dump = vmalloc(dump_size);
3008 if (!fw_dump) {
3009 ql_log(ql_log_warn, vha, 0x00c4,
3010 "Unable to allocate (%d KB) for firmware dump.\n",
3011 dump_size / 1024);
3012 } else {
3013 if (ha->fw_dump)
3014 vfree(ha->fw_dump);
3015 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003016
Quinn Tranad0a0b02017-12-28 12:33:14 -08003017 ha->fw_dump_len = dump_size;
3018 ql_dbg(ql_dbg_init, vha, 0x00c5,
3019 "Allocated (%d KB) for firmware dump.\n",
3020 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07003021
Quinn Tranad0a0b02017-12-28 12:33:14 -08003022 if (IS_QLA27XX(ha))
3023 return;
3024
3025 ha->fw_dump->signature[0] = 'Q';
3026 ha->fw_dump->signature[1] = 'L';
3027 ha->fw_dump->signature[2] = 'G';
3028 ha->fw_dump->signature[3] = 'C';
3029 ha->fw_dump->version = htonl(1);
3030
3031 ha->fw_dump->fixed_size = htonl(fixed_size);
3032 ha->fw_dump->mem_size = htonl(mem_size);
3033 ha->fw_dump->req_q_size = htonl(req_q_size);
3034 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3035
3036 ha->fw_dump->eft_size = htonl(eft_size);
3037 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3038 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3039
3040 ha->fw_dump->header_size =
3041 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003042 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003043 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003044}
3045
Andrew Vasquez18e75552009-06-03 09:55:30 -07003046static int
3047qla81xx_mpi_sync(scsi_qla_host_t *vha)
3048{
3049#define MPS_MASK 0xe0
3050 int rval;
3051 uint16_t dc;
3052 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003053
3054 if (!IS_QLA81XX(vha->hw))
3055 return QLA_SUCCESS;
3056
3057 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3058 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003059 ql_log(ql_log_warn, vha, 0x0105,
3060 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003061 goto done;
3062 }
3063
3064 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3065 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3066 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003067 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003068 goto done_release;
3069 }
3070
3071 dc &= MPS_MASK;
3072 if (dc == (dw & MPS_MASK))
3073 goto done_release;
3074
3075 dw &= ~MPS_MASK;
3076 dw |= dc;
3077 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3078 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003079 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003080 }
3081
3082done_release:
3083 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3084 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003085 ql_log(ql_log_warn, vha, 0x006d,
3086 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003087 }
3088
3089done:
3090 return rval;
3091}
3092
Chad Dupuis8d93f552013-01-30 03:34:37 -05003093int
3094qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3095{
3096 /* Don't try to reallocate the array */
3097 if (req->outstanding_cmds)
3098 return QLA_SUCCESS;
3099
Michael Hernandezd7459522016-12-12 14:40:07 -08003100 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003101 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3102 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003103 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3104 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003105 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003106 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003107 }
3108
3109 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3110 req->num_outstanding_cmds, GFP_KERNEL);
3111
3112 if (!req->outstanding_cmds) {
3113 /*
3114 * Try to allocate a minimal size just so we can get through
3115 * initialization.
3116 */
3117 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3118 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3119 req->num_outstanding_cmds, GFP_KERNEL);
3120
3121 if (!req->outstanding_cmds) {
3122 ql_log(ql_log_fatal, NULL, 0x0126,
3123 "Failed to allocate memory for "
3124 "outstanding_cmds for req_que %p.\n", req);
3125 req->num_outstanding_cmds = 0;
3126 return QLA_FUNCTION_FAILED;
3127 }
3128 }
3129
3130 return QLA_SUCCESS;
3131}
3132
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003133#define PRINT_FIELD(_field, _flag, _str) { \
3134 if (a0->_field & _flag) {\
3135 if (p) {\
3136 strcat(ptr, "|");\
3137 ptr++;\
3138 leftover--;\
3139 } \
3140 len = snprintf(ptr, leftover, "%s", _str); \
3141 p = 1;\
3142 leftover -= len;\
3143 ptr += len; \
3144 } \
3145}
3146
3147static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3148{
3149#define STR_LEN 64
3150 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3151 u8 str[STR_LEN], *ptr, p;
3152 int leftover, len;
3153
3154 memset(str, 0, STR_LEN);
3155 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3156 ql_dbg(ql_dbg_init, vha, 0x015a,
3157 "SFP MFG Name: %s\n", str);
3158
3159 memset(str, 0, STR_LEN);
3160 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3161 ql_dbg(ql_dbg_init, vha, 0x015c,
3162 "SFP Part Name: %s\n", str);
3163
3164 /* media */
3165 memset(str, 0, STR_LEN);
3166 ptr = str;
3167 leftover = STR_LEN;
3168 p = len = 0;
3169 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3170 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3171 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3172 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3173 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3174 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3175 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3176 ql_dbg(ql_dbg_init, vha, 0x0160,
3177 "SFP Media: %s\n", str);
3178
3179 /* link length */
3180 memset(str, 0, STR_LEN);
3181 ptr = str;
3182 leftover = STR_LEN;
3183 p = len = 0;
3184 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3185 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3186 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3187 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3188 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3189 ql_dbg(ql_dbg_init, vha, 0x0196,
3190 "SFP Link Length: %s\n", str);
3191
3192 memset(str, 0, STR_LEN);
3193 ptr = str;
3194 leftover = STR_LEN;
3195 p = len = 0;
3196 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3197 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3198 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3199 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3200 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3201 ql_dbg(ql_dbg_init, vha, 0x016e,
3202 "SFP FC Link Tech: %s\n", str);
3203
3204 if (a0->length_km)
3205 ql_dbg(ql_dbg_init, vha, 0x016f,
3206 "SFP Distant: %d km\n", a0->length_km);
3207 if (a0->length_100m)
3208 ql_dbg(ql_dbg_init, vha, 0x0170,
3209 "SFP Distant: %d m\n", a0->length_100m*100);
3210 if (a0->length_50um_10m)
3211 ql_dbg(ql_dbg_init, vha, 0x0189,
3212 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3213 if (a0->length_62um_10m)
3214 ql_dbg(ql_dbg_init, vha, 0x018a,
3215 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3216 if (a0->length_om4_10m)
3217 ql_dbg(ql_dbg_init, vha, 0x0194,
3218 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3219 if (a0->length_om3_10m)
3220 ql_dbg(ql_dbg_init, vha, 0x0195,
3221 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3222}
3223
3224
3225/*
3226 * Return Code:
3227 * QLA_SUCCESS: no action
3228 * QLA_INTERFACE_ERROR: SFP is not there.
3229 * QLA_FUNCTION_FAILED: detected New SFP
3230 */
3231int
3232qla24xx_detect_sfp(scsi_qla_host_t *vha)
3233{
3234 int rc = QLA_SUCCESS;
3235 struct sff_8247_a0 *a;
3236 struct qla_hw_data *ha = vha->hw;
3237
3238 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3239 goto out;
3240
3241 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3242 if (rc)
3243 goto out;
3244
3245 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3246 qla2xxx_print_sfp_info(vha);
3247
3248 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3249 /* long range */
3250 ha->flags.detected_lr_sfp = 1;
3251
3252 if (a->length_km > 5 || a->length_100m > 50)
3253 ha->long_range_distance = LR_DISTANCE_10K;
3254 else
3255 ha->long_range_distance = LR_DISTANCE_5K;
3256
3257 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3258 ql_dbg(ql_dbg_async, vha, 0x507b,
3259 "Detected Long Range SFP.\n");
3260 } else {
3261 /* short range */
3262 ha->flags.detected_lr_sfp = 0;
3263 if (ha->flags.using_lr_setting)
3264 ql_dbg(ql_dbg_async, vha, 0x5084,
3265 "Detected Short Range SFP.\n");
3266 }
3267
3268 if (!vha->flags.init_done)
3269 rc = QLA_SUCCESS;
3270out:
3271 return rc;
3272}
3273
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003274/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 * qla2x00_setup_chip() - Load and start RISC firmware.
Bart Van Assche2db62282018-01-23 16:33:51 -08003276 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 *
3278 * Returns 0 on success.
3279 */
3280static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003281qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003283 int rval;
3284 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003285 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003286 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3287 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003288 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003289
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003290 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003291 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003292 if (rval == QLA_SUCCESS) {
3293 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003294 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003295 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003296 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003297 }
3298
Andrew Vasquez3db06522008-01-31 12:33:49 -08003299 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3300 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3301 spin_lock_irqsave(&ha->hardware_lock, flags);
3302 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3303 RD_REG_WORD(&reg->hccr);
3304 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Andrew Vasquez18e75552009-06-03 09:55:30 -07003307 qla81xx_mpi_sync(vha);
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003310 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003311 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003312 ql_dbg(ql_dbg_init, vha, 0x00c9,
3313 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003315 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (rval == QLA_SUCCESS) {
3317 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003318 ql_dbg(ql_dbg_init, vha, 0x00ca,
3319 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003321 if (ql2xexlogins)
3322 ha->flags.exlogins_enabled = 1;
3323
Quinn Tran99e1b682017-06-02 09:12:03 -07003324 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003325 ha->flags.exchoffld_enabled = 1;
3326
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003327 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003329 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003330 qla24xx_detect_sfp(vha);
3331
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003332 rval = qla2x00_set_exlogins_buffer(vha);
3333 if (rval != QLA_SUCCESS)
3334 goto failed;
3335
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003336 rval = qla2x00_set_exchoffld_buffer(vha);
3337 if (rval != QLA_SUCCESS)
3338 goto failed;
3339
Giridhar Malavalia9083012010-04-12 17:59:55 -07003340enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003341 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003342 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003343 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003344 else
3345 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003346 if (rval != QLA_SUCCESS)
3347 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003348 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003349 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003350 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003351 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003352 if ((!ha->max_npiv_vports) ||
3353 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003354 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003355 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003356 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003357 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003358 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003359
Chad Dupuis8d93f552013-01-30 03:34:37 -05003360 /*
3361 * Allocate the array of outstanding commands
3362 * now that we know the firmware resources.
3363 */
3364 rval = qla2x00_alloc_outstanding_cmds(ha,
3365 vha->req);
3366 if (rval != QLA_SUCCESS)
3367 goto failed;
3368
Quinn Tranad0a0b02017-12-28 12:33:14 -08003369 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3370 qla2x00_alloc_offload_mem(vha);
3371
3372 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003373 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003374
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003375 } else {
3376 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003379 ql_log(ql_log_fatal, vha, 0x00cd,
3380 "ISP Firmware failed checksum.\n");
3381 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003383 } else
3384 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Andrew Vasquez3db06522008-01-31 12:33:49 -08003386 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3387 /* Enable proper parity. */
3388 spin_lock_irqsave(&ha->hardware_lock, flags);
3389 if (IS_QLA2300(ha))
3390 /* SRAM parity */
3391 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3392 else
3393 /* SRAM, Instruction RAM and GP RAM parity */
3394 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3395 RD_REG_WORD(&reg->hccr);
3396 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3397 }
3398
Chad Dupuisf3982d82014-09-25 05:16:57 -04003399 if (IS_QLA27XX(ha))
3400 ha->flags.fac_supported = 1;
3401 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003402 uint32_t size;
3403
3404 rval = qla81xx_fac_get_sector_size(vha, &size);
3405 if (rval == QLA_SUCCESS) {
3406 ha->flags.fac_supported = 1;
3407 ha->fdt_block_size = size << 2;
3408 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003409 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003410 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3411 ha->fw_major_version, ha->fw_minor_version,
3412 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003413
Chad Dupuisf73cb692014-02-26 04:15:06 -05003414 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003415 ha->flags.fac_supported = 0;
3416 rval = QLA_SUCCESS;
3417 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003418 }
3419 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003420failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003422 ql_log(ql_log_fatal, vha, 0x00cf,
3423 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 }
3425
3426 return (rval);
3427}
3428
3429/**
3430 * qla2x00_init_response_q_entries() - Initializes response queue entries.
Bart Van Assche2db62282018-01-23 16:33:51 -08003431 * @rsp: response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 *
3433 * Beginning of request ring has initialization control block already built
3434 * by nvram config routine.
3435 *
3436 * Returns 0 on success.
3437 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003438void
3439qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
3441 uint16_t cnt;
3442 response_t *pkt;
3443
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003444 rsp->ring_ptr = rsp->ring;
3445 rsp->ring_index = 0;
3446 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003447 pkt = rsp->ring_ptr;
3448 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 pkt->signature = RESPONSE_PROCESSED;
3450 pkt++;
3451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
3454/**
3455 * qla2x00_update_fw_options() - Read and process firmware options.
Bart Van Assche2db62282018-01-23 16:33:51 -08003456 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 *
3458 * Returns 0 on success.
3459 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003460void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003461qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
3463 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003464 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003467 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3470 return;
3471
3472 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003473 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3474 "Serial link options.\n");
3475 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3476 (uint8_t *)&ha->fw_seriallink_options,
3477 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
3479 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3480 if (ha->fw_seriallink_options[3] & BIT_2) {
3481 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3482
3483 /* 1G settings */
3484 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3485 emphasis = (ha->fw_seriallink_options[2] &
3486 (BIT_4 | BIT_3)) >> 3;
3487 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003488 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 rx_sens = (ha->fw_seriallink_options[0] &
3490 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3491 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3492 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3493 if (rx_sens == 0x0)
3494 rx_sens = 0x3;
3495 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3496 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3497 ha->fw_options[10] |= BIT_5 |
3498 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3499 (tx_sens & (BIT_1 | BIT_0));
3500
3501 /* 2G settings */
3502 swing = (ha->fw_seriallink_options[2] &
3503 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3504 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3505 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003506 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 rx_sens = (ha->fw_seriallink_options[1] &
3508 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3509 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3510 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3511 if (rx_sens == 0x0)
3512 rx_sens = 0x3;
3513 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3514 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3515 ha->fw_options[11] |= BIT_5 |
3516 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3517 (tx_sens & (BIT_1 | BIT_0));
3518 }
3519
3520 /* FCP2 options. */
3521 /* Return command IOCBs without waiting for an ABTS to complete. */
3522 ha->fw_options[3] |= BIT_13;
3523
3524 /* LED scheme. */
3525 if (ha->flags.enable_led_scheme)
3526 ha->fw_options[2] |= BIT_12;
3527
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003528 /* Detect ISP6312. */
3529 if (IS_QLA6312(ha))
3530 ha->fw_options[2] |= BIT_13;
3531
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003532 /* Set Retry FLOGI in case of P2P connection */
3533 if (ha->operating_mode == P2P) {
3534 ha->fw_options[2] |= BIT_3;
3535 ql_dbg(ql_dbg_disc, vha, 0x2100,
3536 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3537 __func__, ha->fw_options[2]);
3538 }
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003541 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542}
3543
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003544void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003545qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003546{
3547 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003548 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003549
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003550 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003551 return;
3552
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003553 /* Hold status IOCBs until ABTS response received. */
3554 if (ql2xfwholdabts)
3555 ha->fw_options[3] |= BIT_12;
3556
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003557 /* Set Retry FLOGI in case of P2P connection */
3558 if (ha->operating_mode == P2P) {
3559 ha->fw_options[2] |= BIT_3;
3560 ql_dbg(ql_dbg_disc, vha, 0x2101,
3561 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3562 __func__, ha->fw_options[2]);
3563 }
3564
Quinn Tran41dc5292017-01-19 22:28:03 -08003565 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003566 if (ql2xmvasynctoatio &&
3567 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003568 if (qla_tgt_mode_enabled(vha) ||
3569 qla_dual_mode_enabled(vha))
3570 ha->fw_options[2] |= BIT_11;
3571 else
3572 ha->fw_options[2] &= ~BIT_11;
3573 }
3574
Quinn Tranf7e761f2017-06-02 09:12:02 -07003575 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3576 /*
3577 * Tell FW to track each exchange to prevent
3578 * driver from using stale exchange.
3579 */
3580 if (qla_tgt_mode_enabled(vha) ||
3581 qla_dual_mode_enabled(vha))
3582 ha->fw_options[2] |= BIT_4;
3583 else
3584 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003585
3586 /* Reserve 1/2 of emergency exchanges for ELS.*/
3587 if (qla2xuseresexchforels)
3588 ha->fw_options[2] |= BIT_8;
3589 else
3590 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003591 }
3592
Quinn Tran83548fe2017-06-02 09:12:01 -07003593 ql_dbg(ql_dbg_init, vha, 0x00e8,
3594 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3595 __func__, ha->fw_options[1], ha->fw_options[2],
3596 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003597
3598 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3599 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003600
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003601 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003602 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003603 return;
3604
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003605 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003606 le16_to_cpu(ha->fw_seriallink_options24[1]),
3607 le16_to_cpu(ha->fw_seriallink_options24[2]),
3608 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003609 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003610 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003611 "Unable to update Serial Link options (%x).\n", rval);
3612 }
3613}
3614
3615void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003616qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003617{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003618 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003619 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003620 struct req_que *req = ha->req_q_map[0];
3621 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003622
3623 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003624 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3625 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003626 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3627 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3628 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3629 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3630 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3631 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003632
3633 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3634 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3635 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3636 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3637 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3638}
3639
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003640void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003641qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003642{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003643 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003644 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003645 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3646 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003647 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003648 uint16_t rid = 0;
3649 struct req_que *req = ha->req_q_map[0];
3650 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003651
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003652 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003653 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003654 icb->request_q_outpointer = cpu_to_le16(0);
3655 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003656 icb->request_q_length = cpu_to_le16(req->length);
3657 icb->response_q_length = cpu_to_le16(rsp->length);
3658 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3659 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3660 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3661 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003662
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003663 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003664 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003665 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3666 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3667 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3668
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003669 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003670 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003671
Chad Dupuisf73cb692014-02-26 04:15:06 -05003672 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003673 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3674 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003675 if (ha->flags.msix_enabled) {
3676 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003677 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003678 "Registering vector 0x%x for base que.\n",
3679 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003680 icb->msix = cpu_to_le16(msix->entry);
3681 }
3682 /* Use alternate PCI bus number */
3683 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003684 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003685 /* Use alternate PCI devfn */
3686 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003687 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003688
Anirban Chakraborty31557542009-12-02 10:36:55 -08003689 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003690 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3691 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003692 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003693 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003694 ql_dbg(ql_dbg_init, vha, 0x00fe,
3695 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003696 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003697 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003698 }
Bart Van Asschead950362015-07-09 07:24:08 -07003699 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003700
3701 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3702 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3703 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3704 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3705 } else {
3706 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3707 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3708 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3709 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3710 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003711 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003712
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003713 /* PCI posting */
3714 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003715}
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717/**
3718 * qla2x00_init_rings() - Initializes firmware.
Bart Van Assche2db62282018-01-23 16:33:51 -08003719 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 *
3721 * Beginning of request ring has initialization control block already built
3722 * by nvram config routine.
3723 *
3724 * Returns 0 on success.
3725 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003726int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003727qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 int rval;
3730 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003731 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003732 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003733 struct req_que *req;
3734 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003735 struct mid_init_cb_24xx *mid_init_cb =
3736 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 spin_lock_irqsave(&ha->hardware_lock, flags);
3739
3740 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003741 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003742 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003743 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003744 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003745 req->out_ptr = (void *)(req->ring + req->length);
3746 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003747 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003748 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003750 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003751
3752 /* Initialize firmware. */
3753 req->ring_ptr = req->ring;
3754 req->ring_index = 0;
3755 req->cnt = req->length;
3756 }
3757
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003758 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003759 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003760 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003761 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003762 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3763 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003764 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003765 if (IS_QLAFX00(ha))
3766 qlafx00_init_response_q_entries(rsp);
3767 else
3768 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003771 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3772 ha->tgt.atio_ring_index = 0;
3773 /* Initialize ATIO queue entries */
3774 qlt_init_atio_q_entries(vha);
3775
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003776 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3779
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003780 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3781
3782 if (IS_QLAFX00(ha)) {
3783 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3784 goto next_check;
3785 }
3786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003788 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003790 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003791 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003792 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003793 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003794 }
3795
Andrew Vasquez24a08132009-03-24 09:08:16 -07003796 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003797 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003798 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003799 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003800 ha->flags.dport_enabled =
3801 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3802 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3803 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3804 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003805 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003806 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003807 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003808 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003809 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003810
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003811 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003812next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003814 ql_log(ql_log_fatal, vha, 0x00d2,
3815 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003817 ql_dbg(ql_dbg_init, vha, 0x00d3,
3818 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003819 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 }
3821
3822 return (rval);
3823}
3824
3825/**
3826 * qla2x00_fw_ready() - Waits for firmware ready.
Bart Van Assche2db62282018-01-23 16:33:51 -08003827 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 *
3829 * Returns 0 on success.
3830 */
3831static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003832qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
3834 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003835 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 uint16_t min_wait; /* Minimum wait time if loop is down */
3837 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003838 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003839 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003841 if (IS_QLAFX00(vha->hw))
3842 return qlafx00_fw_ready(vha);
3843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 rval = QLA_SUCCESS;
3845
Chad Dupuis334614912015-04-09 14:59:57 -04003846 /* Time to wait for loop down */
3847 if (IS_P3P_TYPE(ha))
3848 min_wait = 30;
3849 else
3850 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
3852 /*
3853 * Firmware should take at most one RATOV to login, plus 5 seconds for
3854 * our own processing.
3855 */
3856 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3857 wait_time = min_wait;
3858 }
3859
3860 /* Min wait time if loop down */
3861 mtime = jiffies + (min_wait * HZ);
3862
3863 /* wait time before firmware ready */
3864 wtime = jiffies + (wait_time * HZ);
3865
3866 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003867 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003868 ql_log(ql_log_info, vha, 0x801e,
3869 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
3871 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003872 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003873 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003875 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003876 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003878 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003879 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3880 "fw_state=%x 84xx=%x.\n", state[0],
3881 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003882 if ((state[2] & FSTATE_LOGGED_IN) &&
3883 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003884 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3885 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003886
3887 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003888 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003889 if (rval != QLA_SUCCESS) {
3890 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003891 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003892 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003893 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003894 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003895
3896 /* Add time taken to initialize. */
3897 cs84xx_time = jiffies - cs84xx_time;
3898 wtime += cs84xx_time;
3899 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003900 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003901 "Increasing wait time by %ld. "
3902 "New time %ld.\n", cs84xx_time,
3903 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003904 }
3905 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003906 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3907 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003909 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 &ha->login_timeout, &ha->r_a_tov);
3911
3912 rval = QLA_SUCCESS;
3913 break;
3914 }
3915
3916 rval = QLA_FUNCTION_FAILED;
3917
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003918 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003919 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003921 * other than Wait for Login.
3922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003924 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 "Cable is unplugged...\n");
3926
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003927 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 break;
3929 }
3930 }
3931 } else {
3932 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003933 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003934 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 break;
3936 }
3937
3938 if (time_after_eq(jiffies, wtime))
3939 break;
3940
3941 /* Delay for a while */
3942 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 } while (1);
3944
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003945 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003946 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3947 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Chad Dupuiscfb09192011-11-18 09:03:07 -08003949 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003950 ql_log(ql_log_warn, vha, 0x803b,
3951 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 }
3953
3954 return (rval);
3955}
3956
3957/*
3958* qla2x00_configure_hba
3959* Setup adapter context.
3960*
3961* Input:
3962* ha = adapter state pointer.
3963*
3964* Returns:
3965* 0 = success
3966*
3967* Context:
3968* Kernel context.
3969*/
3970static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003971qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
3973 int rval;
3974 uint16_t loop_id;
3975 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003976 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 uint8_t al_pa;
3978 uint8_t area;
3979 uint8_t domain;
3980 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003981 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003982 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003983 port_id_t id;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08003984 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003987 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003988 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003990 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003991 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003992 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003993 ql_dbg(ql_dbg_disc, vha, 0x2008,
3994 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003995 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003996 ql_log(ql_log_warn, vha, 0x2009,
3997 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003998 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3999 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
4000 ql_log(ql_log_warn, vha, 0x1151,
4001 "Doing link init.\n");
4002 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
4003 return rval;
4004 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004005 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08004006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return (rval);
4008 }
4009
4010 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004011 ql_log(ql_log_info, vha, 0x200a,
4012 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 return (QLA_FUNCTION_FAILED);
4014 }
4015
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004016 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 /* initialize */
4019 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4020 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004021 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 switch (topo) {
4024 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004025 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 ha->current_topology = ISP_CFG_NL;
4027 strcpy(connect_type, "(Loop)");
4028 break;
4029
4030 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004031 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004032 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 ha->current_topology = ISP_CFG_FL;
4034 strcpy(connect_type, "(FL_Port)");
4035 break;
4036
4037 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004038 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 ha->operating_mode = P2P;
4040 ha->current_topology = ISP_CFG_N;
4041 strcpy(connect_type, "(N_Port-to-N_Port)");
4042 break;
4043
4044 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004045 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004046 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 ha->operating_mode = P2P;
4048 ha->current_topology = ISP_CFG_F;
4049 strcpy(connect_type, "(F_Port)");
4050 break;
4051
4052 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004053 ql_dbg(ql_dbg_disc, vha, 0x200f,
4054 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 ha->current_topology = ISP_CFG_NL;
4056 strcpy(connect_type, "(Loop)");
4057 break;
4058 }
4059
4060 /* Save Host port and loop ID. */
4061 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004062 id.b.domain = domain;
4063 id.b.area = area;
4064 id.b.al_pa = al_pa;
4065 id.b.rsvd_1 = 0;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004066 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran482c9dc2017-03-15 09:48:54 -07004067 qlt_update_host_map(vha, id);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004068 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004069
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004070 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004071 ql_log(ql_log_info, vha, 0x2010,
4072 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004073 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 return(rval);
4076}
4077
Giridhar Malavalia9083012010-04-12 17:59:55 -07004078inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004079qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4080 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004081{
4082 char *st, *en;
4083 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004084 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004085 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004086 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004087
4088 if (memcmp(model, BINZERO, len) != 0) {
4089 strncpy(ha->model_number, model, len);
4090 st = en = ha->model_number;
4091 en += len - 1;
4092 while (en > st) {
4093 if (*en != 0x20 && *en != 0x00)
4094 break;
4095 *en-- = '\0';
4096 }
4097
4098 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004099 if (use_tbl &&
4100 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004101 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004102 strncpy(ha->model_desc,
4103 qla2x00_model_name[index * 2 + 1],
4104 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004105 } else {
4106 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004107 if (use_tbl &&
4108 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004109 index < QLA_MODEL_NAMES) {
4110 strcpy(ha->model_number,
4111 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004112 strncpy(ha->model_desc,
4113 qla2x00_model_name[index * 2 + 1],
4114 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004115 } else {
4116 strcpy(ha->model_number, def);
4117 }
4118 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004119 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004120 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004121 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004122}
4123
David Miller4e08df32007-04-16 12:37:43 -07004124/* On sparc systems, obtain port and node WWN from firmware
4125 * properties.
4126 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004127static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004128{
4129#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004130 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004131 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004132 struct device_node *dp = pci_device_to_OF_node(pdev);
4133 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004134 int len;
4135
4136 val = of_get_property(dp, "port-wwn", &len);
4137 if (val && len >= WWN_SIZE)
4138 memcpy(nv->port_name, val, WWN_SIZE);
4139
4140 val = of_get_property(dp, "node-wwn", &len);
4141 if (val && len >= WWN_SIZE)
4142 memcpy(nv->node_name, val, WWN_SIZE);
4143#endif
4144}
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146/*
4147* NVRAM configuration for ISP 2xxx
4148*
4149* Input:
4150* ha = adapter block pointer.
4151*
4152* Output:
4153* initialization control block in response_ring
4154* host adapters parameters in host adapter block
4155*
4156* Returns:
4157* 0 = success.
4158*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004159int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004160qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
David Miller4e08df32007-04-16 12:37:43 -07004162 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004163 uint8_t chksum = 0;
4164 uint16_t cnt;
4165 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004166 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004167 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004168 nvram_t *nv = ha->nvram;
4169 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004170 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
David Miller4e08df32007-04-16 12:37:43 -07004172 rval = QLA_SUCCESS;
4173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004175 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 ha->nvram_base = 0;
4177 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4178 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4179 ha->nvram_base = 0x80;
4180
4181 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004182 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004183 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4184 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004186 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4187 "Contents of NVRAM.\n");
4188 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4189 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 /* Bad NVRAM data, set defaults parameters. */
4192 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4193 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4194 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004195 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004196 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004197 "detected: checksum=0x%x id=%c version=0x%x.\n",
4198 chksum, nv->id[0], nv->nvram_version);
4199 ql_log(ql_log_warn, vha, 0x0065,
4200 "Falling back to "
4201 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004202
4203 /*
4204 * Set default initialization control block.
4205 */
4206 memset(nv, 0, ha->nvram_size);
4207 nv->parameter_block_version = ICB_VERSION;
4208
4209 if (IS_QLA23XX(ha)) {
4210 nv->firmware_options[0] = BIT_2 | BIT_1;
4211 nv->firmware_options[1] = BIT_7 | BIT_5;
4212 nv->add_firmware_options[0] = BIT_5;
4213 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004214 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004215 nv->special_options[1] = BIT_7;
4216 } else if (IS_QLA2200(ha)) {
4217 nv->firmware_options[0] = BIT_2 | BIT_1;
4218 nv->firmware_options[1] = BIT_7 | BIT_5;
4219 nv->add_firmware_options[0] = BIT_5;
4220 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004221 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004222 } else if (IS_QLA2100(ha)) {
4223 nv->firmware_options[0] = BIT_3 | BIT_1;
4224 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004225 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004226 }
4227
Bart Van Asschead950362015-07-09 07:24:08 -07004228 nv->max_iocb_allocation = cpu_to_le16(256);
4229 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004230 nv->retry_count = 8;
4231 nv->retry_delay = 1;
4232
4233 nv->port_name[0] = 33;
4234 nv->port_name[3] = 224;
4235 nv->port_name[4] = 139;
4236
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004237 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004238
4239 nv->login_timeout = 4;
4240
4241 /*
4242 * Set default host adapter parameters
4243 */
4244 nv->host_p[1] = BIT_2;
4245 nv->reset_delay = 5;
4246 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004247 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004248 nv->link_down_timeout = 60;
4249
4250 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 }
4252
4253#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4254 /*
4255 * The SN2 does not provide BIOS emulation which means you can't change
4256 * potentially bogus BIOS settings. Force the use of default settings
4257 * for link rate and frame size. Hope that the rest of the settings
4258 * are valid.
4259 */
4260 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004261 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 if (IS_QLA23XX(ha))
4263 nv->special_options[1] = BIT_7;
4264 }
4265#endif
4266
4267 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004268 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270 /*
4271 * Setup driver NVRAM options.
4272 */
4273 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4274 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4275 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4276 nv->firmware_options[1] &= ~BIT_4;
4277
4278 if (IS_QLA23XX(ha)) {
4279 nv->firmware_options[0] |= BIT_2;
4280 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004281 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004282 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
4284 if (IS_QLA2300(ha)) {
4285 if (ha->fb_rev == FPM_2310) {
4286 strcpy(ha->model_number, "QLA2310");
4287 } else {
4288 strcpy(ha->model_number, "QLA2300");
4289 }
4290 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004291 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004292 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 }
4294 } else if (IS_QLA2200(ha)) {
4295 nv->firmware_options[0] |= BIT_2;
4296 /*
4297 * 'Point-to-point preferred, else loop' is not a safe
4298 * connection mode setting.
4299 */
4300 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4301 (BIT_5 | BIT_4)) {
4302 /* Force 'loop preferred, else point-to-point'. */
4303 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4304 nv->add_firmware_options[0] |= BIT_5;
4305 }
4306 strcpy(ha->model_number, "QLA22xx");
4307 } else /*if (IS_QLA2100(ha))*/ {
4308 strcpy(ha->model_number, "QLA2100");
4309 }
4310
4311 /*
4312 * Copy over NVRAM RISC parameter block to initialization control block.
4313 */
4314 dptr1 = (uint8_t *)icb;
4315 dptr2 = (uint8_t *)&nv->parameter_block_version;
4316 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4317 while (cnt--)
4318 *dptr1++ = *dptr2++;
4319
4320 /* Copy 2nd half. */
4321 dptr1 = (uint8_t *)icb->add_firmware_options;
4322 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4323 while (cnt--)
4324 *dptr1++ = *dptr2++;
4325
Andrew Vasquez5341e862006-05-17 15:09:16 -07004326 /* Use alternate WWN? */
4327 if (nv->host_p[1] & BIT_7) {
4328 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4329 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4330 }
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 /* Prepare nodename */
4333 if ((icb->firmware_options[1] & BIT_6) == 0) {
4334 /*
4335 * Firmware will apply the following mask if the nodename was
4336 * not provided.
4337 */
4338 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4339 icb->node_name[0] &= 0xF0;
4340 }
4341
4342 /*
4343 * Set host adapter parameters.
4344 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004345
4346 /*
4347 * BIT_7 in the host-parameters section allows for modification to
4348 * internal driver logging.
4349 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004350 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004351 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4353 /* Always load RISC code on non ISP2[12]00 chips. */
4354 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4355 ha->flags.disable_risc_code_load = 0;
4356 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4357 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4358 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004359 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004360 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 ha->operating_mode =
4363 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4364
4365 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4366 sizeof(ha->fw_seriallink_options));
4367
4368 /* save HBA serial number */
4369 ha->serial0 = icb->port_name[5];
4370 ha->serial1 = icb->port_name[6];
4371 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004372 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4373 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Bart Van Asschead950362015-07-09 07:24:08 -07004375 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 ha->retry_count = nv->retry_count;
4378
4379 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004380 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 nv->login_timeout = ql2xlogintimeout;
4382 if (nv->login_timeout < 4)
4383 nv->login_timeout = 4;
4384 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004386 /* Set minimum RATOV to 100 tenths of a second. */
4387 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 ha->loop_reset_delay = nv->reset_delay;
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 /* Link Down Timeout = 0:
4392 *
4393 * When Port Down timer expires we will start returning
4394 * I/O's to OS with "DID_NO_CONNECT".
4395 *
4396 * Link Down Timeout != 0:
4397 *
4398 * The driver waits for the link to come up after link down
4399 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 if (nv->link_down_timeout == 0) {
4402 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004403 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 } else {
4405 ha->link_down_timeout = nv->link_down_timeout;
4406 ha->loop_down_abort_time =
4407 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 /*
4411 * Need enough time to try and get the port back.
4412 */
4413 ha->port_down_retry_count = nv->port_down_retry_count;
4414 if (qlport_down_retry)
4415 ha->port_down_retry_count = qlport_down_retry;
4416 /* Set login_retry_count */
4417 ha->login_retry_count = nv->retry_count;
4418 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4419 ha->port_down_retry_count > 3)
4420 ha->login_retry_count = ha->port_down_retry_count;
4421 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4422 ha->login_retry_count = ha->port_down_retry_count;
4423 if (ql2xloginretrycount)
4424 ha->login_retry_count = ql2xloginretrycount;
4425
Bart Van Asschead950362015-07-09 07:24:08 -07004426 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 icb->command_resource_count = 0;
4428 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004429 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4432 /* Enable RIO */
4433 icb->firmware_options[0] &= ~BIT_3;
4434 icb->add_firmware_options[0] &=
4435 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4436 icb->add_firmware_options[0] |= BIT_2;
4437 icb->response_accumulation_timer = 3;
4438 icb->interrupt_delay_timer = 5;
4439
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004440 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004442 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004443 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004444 ha->zio_mode = icb->add_firmware_options[0] &
4445 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4446 ha->zio_timer = icb->interrupt_delay_timer ?
4447 icb->interrupt_delay_timer: 2;
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 icb->add_firmware_options[0] &=
4450 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004451 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004452 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004453 ha->zio_mode = QLA_ZIO_MODE_6;
4454
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004455 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004456 "ZIO mode %d enabled; timer delay (%d us).\n",
4457 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004459 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4460 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004461 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
4463 }
4464
David Miller4e08df32007-04-16 12:37:43 -07004465 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004466 ql_log(ql_log_warn, vha, 0x0069,
4467 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004468 }
4469 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470}
4471
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004472static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004473qla2x00_rport_del(void *data)
4474{
4475 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004476 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004477 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004478
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004479 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004480 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004481 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004482 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004483 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004484 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4485 "%s %8phN. rport %p roles %x\n",
4486 __func__, fcport->port_name, rport,
4487 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004488
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004489 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004490 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004491}
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493/**
4494 * qla2x00_alloc_fcport() - Allocate a generic fcport.
Bart Van Assche2db62282018-01-23 16:33:51 -08004495 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 * @flags: allocation flags
4497 *
4498 * Returns a pointer to the allocated fcport, or NULL, if none available.
4499 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004500fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004501qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
4503 fc_port_t *fcport;
4504
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004505 fcport = kzalloc(sizeof(fc_port_t), flags);
4506 if (!fcport)
4507 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
4509 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004510 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 fcport->port_type = FCT_UNKNOWN;
4512 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004513 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004514 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Quinn Tran726b8542017-01-19 22:28:00 -08004516 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4517 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004518 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004519 fcport->disc_state = DSC_DELETED;
4520 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4521 fcport->deleted = QLA_SESS_DELETED;
4522 fcport->login_retry = vha->hw->login_retry_count;
4523 fcport->login_retry = 5;
4524 fcport->logout_on_delete = 1;
4525
4526 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004527 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004528 "Failed to allocate ct_sns request.\n");
4529 kfree(fcport);
4530 fcport = NULL;
4531 }
4532 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4533 INIT_LIST_HEAD(&fcport->gnl_entry);
4534 INIT_LIST_HEAD(&fcport->list);
4535
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004536 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537}
4538
Quinn Tran726b8542017-01-19 22:28:00 -08004539void
4540qla2x00_free_fcport(fc_port_t *fcport)
4541{
4542 if (fcport->ct_desc.ct_sns) {
4543 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4544 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4545 fcport->ct_desc.ct_sns_dma);
4546
4547 fcport->ct_desc.ct_sns = NULL;
4548 }
4549 kfree(fcport);
4550}
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552/*
4553 * qla2x00_configure_loop
4554 * Updates Fibre Channel Device Database with what is actually on loop.
4555 *
4556 * Input:
4557 * ha = adapter block pointer.
4558 *
4559 * Returns:
4560 * 0 = success.
4561 * 1 = error.
4562 * 2 = database was full and device was not configured.
4563 */
4564static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004565qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
4567 int rval;
4568 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004569 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 rval = QLA_SUCCESS;
4571
4572 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004573 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4574 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004576 ql_dbg(ql_dbg_disc, vha, 0x2013,
4577 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 return (rval);
4579 }
4580 }
4581
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004582 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004583 ql_dbg(ql_dbg_disc, vha, 0x2014,
4584 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586 /*
4587 * If we have both an RSCN and PORT UPDATE pending then handle them
4588 * both at the same time.
4589 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004590 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4591 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Michael Hernandez3064ff32009-12-15 21:29:44 -08004593 qla2x00_get_data_rate(vha);
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 /* Determine what we need to do */
4596 if (ha->current_topology == ISP_CFG_FL &&
4597 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 set_bit(RSCN_UPDATE, &flags);
4600
4601 } else if (ha->current_topology == ISP_CFG_F &&
4602 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 set_bit(RSCN_UPDATE, &flags);
4605 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4606
Andrew Vasquez21333b42006-05-17 15:09:56 -07004607 } else if (ha->current_topology == ISP_CFG_N) {
4608 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004609 if (ha->flags.rida_fmt2) {
4610 /* With Rida Format 2, the login is already triggered.
4611 * We know who is on the other side of the wire.
4612 * No need to login to do login to find out or drop into
4613 * qla2x00_configure_local_loop().
4614 */
4615 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4616 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4617 } else {
4618 if (qla_tgt_mode_enabled(vha)) {
4619 /* allow the other side to start the login */
4620 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4621 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4622 }
4623 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004624 } else if (ha->current_topology == ISP_CFG_NL) {
4625 clear_bit(RSCN_UPDATE, &flags);
4626 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004627 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 set_bit(RSCN_UPDATE, &flags);
4630 set_bit(LOCAL_LOOP_UPDATE, &flags);
4631 }
4632
4633 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004634 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4635 ql_dbg(ql_dbg_disc, vha, 0x2015,
4636 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004638 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004639 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 }
4641
4642 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004643 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004644 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004645 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004647 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004648 else
4649 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
4651
4652 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004653 if (atomic_read(&vha->loop_down_timer) ||
4654 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 rval = QLA_FUNCTION_FAILED;
4656 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004657 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004658 ql_dbg(ql_dbg_disc, vha, 0x2069,
4659 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004660 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004661
4662 /*
4663 * Process any ATIO queue entries that came in
4664 * while we weren't online.
4665 */
Quinn Tranead03852017-01-19 22:28:01 -08004666 if (qla_tgt_mode_enabled(vha) ||
4667 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004668 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4669 spin_lock_irqsave(&ha->tgt.atio_lock,
4670 flags);
4671 qlt_24xx_process_atio_queue(vha, 0);
4672 spin_unlock_irqrestore(
4673 &ha->tgt.atio_lock, flags);
4674 } else {
4675 spin_lock_irqsave(&ha->hardware_lock,
4676 flags);
4677 qlt_24xx_process_atio_queue(vha, 1);
4678 spin_unlock_irqrestore(
4679 &ha->hardware_lock, flags);
4680 }
4681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683 }
4684
4685 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004686 ql_dbg(ql_dbg_disc, vha, 0x206a,
4687 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004689 ql_dbg(ql_dbg_disc, vha, 0x206b,
4690 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 }
4692
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004693 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004694 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004696 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004697 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004698 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
4702 return (rval);
4703}
4704
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004705/*
4706 * N2N Login
4707 * Updates Fibre Channel Device Database with local loop devices.
4708 *
4709 * Input:
4710 * ha = adapter block pointer.
4711 *
4712 * Returns:
4713 */
4714static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4715 fc_port_t *fcport)
4716{
4717 struct qla_hw_data *ha = vha->hw;
4718 int res = QLA_SUCCESS, rval;
4719 int greater_wwpn = 0;
4720 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004722 if (ha->current_topology != ISP_CFG_N)
4723 return res;
4724
4725 if (wwn_to_u64(vha->port_name) >
4726 wwn_to_u64(vha->n2n_port_name)) {
4727 ql_dbg(ql_dbg_disc, vha, 0x2002,
4728 "HBA WWPN is greater %llx > target %llx\n",
4729 wwn_to_u64(vha->port_name),
4730 wwn_to_u64(vha->n2n_port_name));
4731 greater_wwpn = 1;
4732 fcport->d_id.b24 = vha->n2n_id;
4733 }
4734
4735 fcport->loop_id = vha->loop_id;
4736 fcport->fc4f_nvme = 0;
4737 fcport->query = 1;
4738
4739 ql_dbg(ql_dbg_disc, vha, 0x4001,
4740 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4741 fcport->d_id.b24, vha->loop_id);
4742
4743 /* Fill in member data. */
4744 if (!greater_wwpn) {
4745 rval = qla2x00_get_port_database(vha, fcport, 0);
4746 ql_dbg(ql_dbg_disc, vha, 0x1051,
4747 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4748 fcport->current_login_state, fcport->last_login_state,
4749 fcport->d_id.b24, fcport->loop_id, rval);
4750
4751 if (((fcport->current_login_state & 0xf) == 0x4) ||
4752 ((fcport->current_login_state & 0xf) == 0x6))
4753 logged_in = 1;
4754 }
4755
4756 if (logged_in || greater_wwpn) {
4757 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4758 qla_nvme_register_hba(vha);
4759
4760 /* Set connected N_Port d_id */
4761 if (vha->flags.nvme_enabled)
4762 fcport->fc4f_nvme = 1;
4763
4764 fcport->scan_state = QLA_FCPORT_FOUND;
4765 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4766 fcport->disc_state = DSC_GNL;
4767 fcport->n2n_flag = 1;
4768 fcport->flags = 3;
4769 vha->hw->flags.gpsc_supported = 0;
4770
4771 if (greater_wwpn) {
4772 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4773 "%s %d PLOGI ELS %8phC\n",
4774 __func__, __LINE__, fcport->port_name);
4775
4776 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4777 fcport, fcport->d_id);
4778 }
4779
4780 if (res != QLA_SUCCESS) {
4781 ql_log(ql_log_info, vha, 0xd04d,
4782 "PLOGI Failed: portid=%06x - retrying\n",
4783 fcport->d_id.b24);
4784 res = QLA_SUCCESS;
4785 } else {
4786 /* State 0x6 means FCP PRLI complete */
4787 if ((fcport->current_login_state & 0xf) == 0x6) {
4788 ql_dbg(ql_dbg_disc, vha, 0x2118,
4789 "%s %d %8phC post GPDB work\n",
4790 __func__, __LINE__, fcport->port_name);
4791 fcport->chip_reset =
4792 vha->hw->base_qpair->chip_reset;
4793 qla24xx_post_gpdb_work(vha, fcport, 0);
4794 } else {
4795 ql_dbg(ql_dbg_disc, vha, 0x2118,
4796 "%s %d %8phC post NVMe PRLI\n",
4797 __func__, __LINE__, fcport->port_name);
4798 qla24xx_post_prli_work(vha, fcport);
4799 }
4800 }
4801 } else {
4802 /* Wait for next database change */
4803 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4804 }
4805
4806 return res;
4807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
4809/*
4810 * qla2x00_configure_local_loop
4811 * Updates Fibre Channel Device Database with local loop devices.
4812 *
4813 * Input:
4814 * ha = adapter block pointer.
4815 *
4816 * Returns:
4817 * 0 = success.
4818 */
4819static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004820qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 int rval, rval2;
4823 int found_devs;
4824 int found;
4825 fc_port_t *fcport, *new_fcport;
4826
4827 uint16_t index;
4828 uint16_t entries;
4829 char *id_iter;
4830 uint16_t loop_id;
4831 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004832 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004833 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
4835 found_devs = 0;
4836 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004837 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004840 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004841 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 &entries);
4843 if (rval != QLA_SUCCESS)
4844 goto cleanup_allocation;
4845
Quinn Tran83548fe2017-06-02 09:12:01 -07004846 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004847 "Entries in ID list (%d).\n", entries);
4848 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4849 (uint8_t *)ha->gid_list,
4850 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
Quinn Tran9cd883f2017-12-28 12:33:24 -08004852 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4853 fcport->scan_state = QLA_FCPORT_SCAN;
4854 }
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004857 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004859 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004860 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 rval = QLA_MEMORY_ALLOC_FAILED;
4862 goto cleanup_allocation;
4863 }
4864 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4865
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004866 /* Inititae N2N login. */
4867 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4868 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4869 if (rval != QLA_SUCCESS)
4870 goto cleanup_allocation;
4871 return QLA_SUCCESS;
4872 }
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 /* Add devices to port list. */
4875 id_iter = (char *)ha->gid_list;
4876 for (index = 0; index < entries; index++) {
4877 domain = ((struct gid_list_info *)id_iter)->domain;
4878 area = ((struct gid_list_info *)id_iter)->area;
4879 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004880 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 loop_id = (uint16_t)
4882 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004883 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 loop_id = le16_to_cpu(
4885 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004886 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
4888 /* Bypass reserved domain fields. */
4889 if ((domain & 0xf0) == 0xf0)
4890 continue;
4891
4892 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004893 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004894 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 continue;
4896
4897 /* Bypass invalid local loop ID. */
4898 if (loop_id > LAST_LOCAL_LOOP_ID)
4899 continue;
4900
Quinn Tran41dc5292017-01-19 22:28:03 -08004901 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 /* Fill in member data. */
4904 new_fcport->d_id.b.domain = domain;
4905 new_fcport->d_id.b.area = area;
4906 new_fcport->d_id.b.al_pa = al_pa;
4907 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004908 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004909
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004910 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004912 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004913 "Failed to retrieve fcport information "
4914 "-- get_port_database=%x, loop_id=0x%04x.\n",
4915 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004916 /* Skip retry if N2N */
4917 if (ha->current_topology != ISP_CFG_N) {
4918 ql_dbg(ql_dbg_disc, vha, 0x2105,
4919 "Scheduling resync.\n");
4920 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4921 continue;
4922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 }
4924
Quinn Tran41dc5292017-01-19 22:28:03 -08004925 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 /* Check for matching device in port list. */
4927 found = 0;
4928 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004929 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 if (memcmp(new_fcport->port_name, fcport->port_name,
4931 WWN_SIZE))
4932 continue;
4933
Shyam Sundarddb9b122009-03-24 09:08:10 -07004934 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 fcport->loop_id = new_fcport->loop_id;
4936 fcport->port_type = new_fcport->port_type;
4937 fcport->d_id.b24 = new_fcport->d_id.b24;
4938 memcpy(fcport->node_name, new_fcport->node_name,
4939 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004940 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 found++;
4942 break;
4943 }
4944
4945 if (!found) {
4946 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004947 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 /* Allocate a new replacement fcport. */
4950 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004951
4952 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4953
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004954 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004957 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004958 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 rval = QLA_MEMORY_ALLOC_FAILED;
4960 goto cleanup_allocation;
4961 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004962 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4964 }
4965
Quinn Tran41dc5292017-01-19 22:28:03 -08004966 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4967
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004968 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004969 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004970
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 found_devs++;
4972 }
4973
Quinn Tran9cd883f2017-12-28 12:33:24 -08004974 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4975 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4976 break;
4977
4978 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4979 if ((qla_dual_mode_enabled(vha) ||
4980 qla_ini_mode_enabled(vha)) &&
4981 atomic_read(&fcport->state) == FCS_ONLINE) {
4982 qla2x00_mark_device_lost(vha, fcport,
4983 ql2xplogiabsentdevice, 0);
4984 if (fcport->loop_id != FC_NO_LOOP_ID &&
4985 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4986 fcport->port_type != FCT_INITIATOR &&
4987 fcport->port_type != FCT_BROADCAST) {
4988 ql_dbg(ql_dbg_disc, vha, 0x20f0,
4989 "%s %d %8phC post del sess\n",
4990 __func__, __LINE__,
4991 fcport->port_name);
4992
Quinn Trand8630bb2017-12-28 12:33:43 -08004993 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004994 continue;
4995 }
4996 }
4997 }
4998
4999 if (fcport->scan_state == QLA_FCPORT_FOUND)
5000 qla24xx_fcport_handle_login(vha, fcport);
5001 }
5002
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08005004 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005007 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005008 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 }
5010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 return (rval);
5012}
5013
5014static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005015qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005016{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005017 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04005018 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005019 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005020
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07005021 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005022 return;
5023
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07005024 if (atomic_read(&fcport->state) != FCS_ONLINE)
5025 return;
5026
Andrew Vasquez39bd9622007-09-20 14:07:34 -07005027 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5028 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005029 return;
5030
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005031 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07005032 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005033 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005034 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005035 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5036 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005037 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005038 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005039 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05005040 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005041 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005042 }
5043}
5044
Quinn Tran726b8542017-01-19 22:28:00 -08005045/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005046static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005047qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005048{
5049 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005050 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005051 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005052
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005053 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5054 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005055 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5056 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5057 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005058 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005059 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005060 ql_log(ql_log_warn, vha, 0x2006,
5061 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005062 return;
5063 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005064
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005065 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005066 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005067 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005068
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005069 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005070
5071 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005072 if (fcport->port_type == FCT_INITIATOR)
5073 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5074 if (fcport->port_type == FCT_TARGET)
5075 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005076
Quinn Tran83548fe2017-06-02 09:12:01 -07005077 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5078 "%s %8phN. rport %p is %s mode\n",
5079 __func__, fcport->port_name, rport,
5080 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005081
Andrew Vasquez77d74142005-07-08 18:00:36 -07005082 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
5085/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005086 * qla2x00_update_fcport
5087 * Updates device on list.
5088 *
5089 * Input:
5090 * ha = adapter block pointer.
5091 * fcport = port structure pointer.
5092 *
5093 * Return:
5094 * 0 - Success
5095 * BIT_0 - error
5096 *
5097 * Context:
5098 * Kernel context.
5099 */
5100void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005101qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005102{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005103 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005104
Quinn Tran726b8542017-01-19 22:28:00 -08005105 if (IS_SW_RESV_ADDR(fcport->d_id))
5106 return;
5107
Quinn Tran83548fe2017-06-02 09:12:01 -07005108 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005109 __func__, fcport->port_name);
5110
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005111 if (IS_QLAFX00(vha->hw)) {
5112 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005113 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005114 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005115 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005116 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005117 fcport->disc_state = DSC_LOGIN_COMPLETE;
5118 fcport->deleted = 0;
5119 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005120
Darren Trappdbe18012018-03-20 23:09:31 -07005121 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5122 qla2x00_iidma_fcport(vha, fcport);
5123
Duane Grigsbye84067d2017-06-21 13:48:43 -07005124 if (fcport->fc4f_nvme) {
5125 qla_nvme_register_remote(vha, fcport);
5126 return;
5127 }
5128
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005129 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005130
5131reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005132 switch (vha->host->active_mode) {
5133 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005134 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005135 break;
5136 case MODE_TARGET:
5137 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5138 !vha->vha_tgt.qla_tgt->tgt_stopped)
5139 qlt_fc_port_added(vha, fcport);
5140 break;
5141 case MODE_DUAL:
5142 qla2x00_reg_remote_port(vha, fcport);
5143 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5144 !vha->vha_tgt.qla_tgt->tgt_stopped)
5145 qlt_fc_port_added(vha, fcport);
5146 break;
5147 default:
5148 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005149 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005150}
5151
5152/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 * qla2x00_configure_fabric
5154 * Setup SNS devices with loop ID's.
5155 *
5156 * Input:
5157 * ha = adapter block pointer.
5158 *
5159 * Returns:
5160 * 0 = success.
5161 * BIT_0 = error
5162 */
5163static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005164qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
Arun Easib3b02e62012-02-09 11:15:39 -08005166 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005167 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005169 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005171 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005172 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
5174 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005175 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005176 loop_id = NPH_F_PORT;
5177 else
5178 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005179 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005181 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005182 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005184 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 return (QLA_SUCCESS);
5186 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005187 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
Quinn Tran41dc5292017-01-19 22:28:03 -08005189
5190 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5191 rval = qla2x00_send_change_request(vha, 0x3, 0);
5192 if (rval != QLA_SUCCESS)
5193 ql_log(ql_log_warn, vha, 0x121,
5194 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5195 rval);
5196 }
5197
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005200 qla2x00_mgmt_svr_login(vha);
5201
Andrew Vasquezcca53352005-08-26 19:08:30 -07005202 /* FDMI support. */
5203 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005204 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5205 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005208 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005209 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5210 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005211 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5212 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5213 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5214 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005215 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005216 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005217 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005218 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5219 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005221 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005222 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005223 if (test_bit(LOOP_RESYNC_NEEDED,
5224 &vha->dpc_flags))
5225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005227 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005229 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005230 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005231 if (test_bit(LOOP_RESYNC_NEEDED,
5232 &vha->dpc_flags))
5233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005235 if (vha->flags.nvme_enabled) {
5236 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5237 ql_dbg(ql_dbg_disc, vha, 0x2049,
5238 "Register NVME FC Type Features failed.\n");
5239 }
5240 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005241 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005243 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005244 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005245 if (test_bit(LOOP_RESYNC_NEEDED,
5246 &vha->dpc_flags))
5247 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005248 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005250 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005251 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005252 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5253 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
5255 }
5256
Joe Carnuccio827210b2013-02-08 01:57:57 -05005257
Alexei Potashnikdf673272015-07-14 16:00:46 -04005258 /* Mark the time right before querying FW for connected ports.
5259 * This process is long, asynchronous and by the time it's done,
5260 * collected information might not be accurate anymore. E.g.
5261 * disconnected port might have re-connected and a brand new
5262 * session has been created. In this case session's generation
5263 * will be newer than discovery_gen. */
5264 qlt_do_generation_tick(vha, &discovery_gen);
5265
Quinn Trana4239942017-12-28 12:33:26 -08005266 if (USE_ASYNC_SCAN(ha)) {
5267 rval = QLA_SUCCESS;
5268 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5269 if (rval)
5270 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5271 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005272 list_for_each_entry(fcport, &vha->vp_fcports, list)
5273 fcport->scan_state = QLA_FCPORT_SCAN;
5274
Quinn Trana4239942017-12-28 12:33:26 -08005275 rval = qla2x00_find_all_fabric_devs(vha);
5276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 if (rval != QLA_SUCCESS)
5278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 } while (0);
5280
Duane Grigsbye84067d2017-06-21 13:48:43 -07005281 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5282 qla_nvme_register_hba(vha);
5283
Quinn Tran726b8542017-01-19 22:28:00 -08005284 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005285 ql_dbg(ql_dbg_disc, vha, 0x2068,
5286 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
5288 return (rval);
5289}
5290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291/*
5292 * qla2x00_find_all_fabric_devs
5293 *
5294 * Input:
5295 * ha = adapter block pointer.
5296 * dev = database device entry pointer.
5297 *
5298 * Returns:
5299 * 0 = success.
5300 *
5301 * Context:
5302 * Kernel context.
5303 */
5304static int
Quinn Tran726b8542017-01-19 22:28:00 -08005305qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306{
5307 int rval;
5308 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005309 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 int found;
5311
5312 sw_info_t *swl;
5313 int swl_idx;
5314 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005315 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005316 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005317 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005318 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
5320 rval = QLA_SUCCESS;
5321
5322 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005323 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005324 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005325 GFP_KERNEL);
5326 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005327 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005329 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005330 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005332 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005333 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005335 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5336 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005337 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005339 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5340 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005341 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005343 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5344 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005345 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5346 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005347 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5348 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005350
5351 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005352 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005353 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005354 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5355 return rval;
5356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 }
5358 swl_idx = 0;
5359
5360 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005361 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005363 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005364 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return (QLA_MEMORY_ALLOC_FAILED);
5366 }
5367 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 /* Set start port ID scan at adapter ID. */
5369 first_dev = 1;
5370 last_dev = 0;
5371
5372 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005373 loop_id = ha->min_external_loopid;
5374 for (; loop_id <= ha->max_loop_id; loop_id++) {
5375 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 continue;
5377
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005378 if (ha->current_topology == ISP_CFG_FL &&
5379 (atomic_read(&vha->loop_down_timer) ||
5380 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005381 atomic_set(&vha->loop_down_timer, 0);
5382 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5383 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 if (swl != NULL) {
5388 if (last_dev) {
5389 wrap.b24 = new_fcport->d_id.b24;
5390 } else {
5391 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5392 memcpy(new_fcport->node_name,
5393 swl[swl_idx].node_name, WWN_SIZE);
5394 memcpy(new_fcport->port_name,
5395 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005396 memcpy(new_fcport->fabric_port_name,
5397 swl[swl_idx].fabric_port_name, WWN_SIZE);
5398 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005399 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005401 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005402 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005403 if (vha->flags.nvme_enabled &&
5404 swl[swl_idx].fc4f_nvme) {
5405 new_fcport->fc4f_nvme =
5406 swl[swl_idx].fc4f_nvme;
5407 ql_log(ql_log_info, vha, 0x2131,
5408 "FOUND: NVME port %8phC as FC Type 28h\n",
5409 new_fcport->port_name);
5410 }
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5413 last_dev = 1;
5414 }
5415 swl_idx++;
5416 }
5417 } else {
5418 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005419 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005421 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005422 "SNS scan failed -- assuming "
5423 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 rval = QLA_SUCCESS;
5425 break;
5426 }
5427 }
5428
5429 /* If wrap on switch device list, exit. */
5430 if (first_dev) {
5431 wrap.b24 = new_fcport->d_id.b24;
5432 first_dev = 0;
5433 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005434 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005435 "Device wrap (%02x%02x%02x).\n",
5436 new_fcport->d_id.b.domain,
5437 new_fcport->d_id.b.area,
5438 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 break;
5440 }
5441
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005442 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005443 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 continue;
5445
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005446 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005447 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5448 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005449
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005450 /* Bypass if same domain and area of adapter. */
5451 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005452 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005453 ISP_CFG_FL)
5454 continue;
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 /* Bypass reserved domain fields. */
5457 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5458 continue;
5459
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005460 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005461 if (ql2xgffidenable &&
5462 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5463 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005464 continue;
5465
Quinn Tran726b8542017-01-19 22:28:00 -08005466 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /* Locate matching device in database. */
5469 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005470 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 if (memcmp(new_fcport->port_name, fcport->port_name,
5472 WWN_SIZE))
5473 continue;
5474
Joe Carnuccio827210b2013-02-08 01:57:57 -05005475 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005476
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 found++;
5478
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005479 /* Update port state. */
5480 memcpy(fcport->fabric_port_name,
5481 new_fcport->fabric_port_name, WWN_SIZE);
5482 fcport->fp_speed = new_fcport->fp_speed;
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005485 * If address the same and state FCS_ONLINE
5486 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 */
5488 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005489 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005490 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 break;
5492 }
5493
5494 /*
5495 * If device was not a fabric device before.
5496 */
5497 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5498 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005499 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 fcport->flags |= (FCF_FABRIC_DEVICE |
5501 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 break;
5503 }
5504
5505 /*
5506 * Port ID changed or device was marked to be updated;
5507 * Log it out if still logged in and mark it for
5508 * relogin later.
5509 */
Quinn Tran726b8542017-01-19 22:28:00 -08005510 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005511 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5512 "port changed FC ID, %8phC"
5513 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5514 fcport->port_name,
5515 fcport->d_id.b.domain,
5516 fcport->d_id.b.area,
5517 fcport->d_id.b.al_pa,
5518 fcport->loop_id,
5519 new_fcport->d_id.b.domain,
5520 new_fcport->d_id.b.area,
5521 new_fcport->d_id.b.al_pa);
5522 fcport->d_id.b24 = new_fcport->d_id.b24;
5523 break;
5524 }
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 fcport->d_id.b24 = new_fcport->d_id.b24;
5527 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 break;
5529 }
5530
Darren Trapp9dd96862018-03-20 23:09:32 -07005531 if (fcport->fc4f_nvme) {
5532 if (fcport->disc_state == DSC_DELETE_PEND) {
5533 fcport->disc_state = DSC_GNL;
5534 vha->fcport_count--;
5535 fcport->login_succ = 0;
5536 }
5537 }
5538
Quinn Tran726b8542017-01-19 22:28:00 -08005539 if (found) {
5540 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005544 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005545 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5546
5547 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
5550 /* Allocate a new replacement fcport. */
5551 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005552 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005554 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005555 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 return (QLA_MEMORY_ALLOC_FAILED);
5557 }
5558 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5559 new_fcport->d_id.b24 = nxt_d_id.b24;
5560 }
5561
Quinn Tran726b8542017-01-19 22:28:00 -08005562 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
Quinn Tran726b8542017-01-19 22:28:00 -08005564 /*
5565 * Logout all previous fabric dev marked lost, except FCP2 devices.
5566 */
5567 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5568 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5569 break;
5570
5571 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5572 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5573 continue;
5574
5575 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5576 if ((qla_dual_mode_enabled(vha) ||
5577 qla_ini_mode_enabled(vha)) &&
5578 atomic_read(&fcport->state) == FCS_ONLINE) {
5579 qla2x00_mark_device_lost(vha, fcport,
5580 ql2xplogiabsentdevice, 0);
5581 if (fcport->loop_id != FC_NO_LOOP_ID &&
5582 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5583 fcport->port_type != FCT_INITIATOR &&
5584 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005585 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005586 "%s %d %8phC post del sess\n",
5587 __func__, __LINE__,
5588 fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08005589 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005590 continue;
5591 }
5592 }
5593 }
5594
5595 if (fcport->scan_state == QLA_FCPORT_FOUND)
5596 qla24xx_fcport_handle_login(vha, fcport);
5597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 return (rval);
5599}
5600
5601/*
5602 * qla2x00_find_new_loop_id
5603 * Scan through our port list and find a new usable loop ID.
5604 *
5605 * Input:
5606 * ha: adapter state pointer.
5607 * dev: port structure pointer.
5608 *
5609 * Returns:
5610 * qla2x00 local function return status code.
5611 *
5612 * Context:
5613 * Kernel context.
5614 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005615int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005616qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
5618 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005619 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005620 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
5622 rval = QLA_SUCCESS;
5623
Chad Dupuis5f16b332012-08-22 14:21:00 -04005624 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Chad Dupuis5f16b332012-08-22 14:21:00 -04005626 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5627 LOOPID_MAP_SIZE);
5628 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5629 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5630 dev->loop_id = FC_NO_LOOP_ID;
5631 rval = QLA_FUNCTION_FAILED;
5632 } else
5633 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Chad Dupuis5f16b332012-08-22 14:21:00 -04005635 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Chad Dupuis5f16b332012-08-22 14:21:00 -04005637 if (rval == QLA_SUCCESS)
5638 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5639 "Assigning new loopid=%x, portid=%x.\n",
5640 dev->loop_id, dev->d_id.b24);
5641 else
5642 ql_log(ql_log_warn, dev->vha, 0x2087,
5643 "No loop_id's available, portid=%x.\n",
5644 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
5646 return (rval);
5647}
5648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650/*
5651 * qla2x00_fabric_login
5652 * Issue fabric login command.
5653 *
5654 * Input:
5655 * ha = adapter block pointer.
5656 * device = pointer to FC device type structure.
5657 *
5658 * Returns:
5659 * 0 - Login successfully
5660 * 1 - Login failed
5661 * 2 - Initiator device
5662 * 3 - Fatal error
5663 */
5664int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005665qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 uint16_t *next_loopid)
5667{
5668 int rval;
5669 int retry;
5670 uint16_t tmp_loopid;
5671 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005672 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 retry = 0;
5675 tmp_loopid = 0;
5676
5677 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005678 ql_dbg(ql_dbg_disc, vha, 0x2000,
5679 "Trying Fabric Login w/loop id 0x%04x for port "
5680 "%02x%02x%02x.\n",
5681 fcport->loop_id, fcport->d_id.b.domain,
5682 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
5684 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005685 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 fcport->d_id.b.domain, fcport->d_id.b.area,
5687 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005688 if (rval != QLA_SUCCESS) {
5689 return rval;
5690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 if (mb[0] == MBS_PORT_ID_USED) {
5692 /*
5693 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005694 * recommends the driver perform an implicit login with
5695 * the specified ID again. The ID we just used is save
5696 * here so we return with an ID that can be tried by
5697 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 */
5699 retry++;
5700 tmp_loopid = fcport->loop_id;
5701 fcport->loop_id = mb[1];
5702
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005703 ql_dbg(ql_dbg_disc, vha, 0x2001,
5704 "Fabric Login: port in use - next loop "
5705 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005707 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
5709 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5710 /*
5711 * Login succeeded.
5712 */
5713 if (retry) {
5714 /* A retry occurred before. */
5715 *next_loopid = tmp_loopid;
5716 } else {
5717 /*
5718 * No retry occurred before. Just increment the
5719 * ID value for next login.
5720 */
5721 *next_loopid = (fcport->loop_id + 1);
5722 }
5723
5724 if (mb[1] & BIT_0) {
5725 fcport->port_type = FCT_INITIATOR;
5726 } else {
5727 fcport->port_type = FCT_TARGET;
5728 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005729 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 }
5731 }
5732
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005733 if (mb[10] & BIT_0)
5734 fcport->supported_classes |= FC_COS_CLASS2;
5735 if (mb[10] & BIT_1)
5736 fcport->supported_classes |= FC_COS_CLASS3;
5737
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005738 if (IS_FWI2_CAPABLE(ha)) {
5739 if (mb[10] & BIT_7)
5740 fcport->flags |=
5741 FCF_CONF_COMP_SUPPORTED;
5742 }
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 rval = QLA_SUCCESS;
5745 break;
5746 } else if (mb[0] == MBS_LOOP_ID_USED) {
5747 /*
5748 * Loop ID already used, try next loop ID.
5749 */
5750 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005751 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 if (rval != QLA_SUCCESS) {
5753 /* Ran out of loop IDs to use */
5754 break;
5755 }
5756 } else if (mb[0] == MBS_COMMAND_ERROR) {
5757 /*
5758 * Firmware possibly timed out during login. If NO
5759 * retries are left to do then the device is declared
5760 * dead.
5761 */
5762 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005763 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005764 fcport->d_id.b.domain, fcport->d_id.b.area,
5765 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005766 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
5768 rval = 1;
5769 break;
5770 } else {
5771 /*
5772 * unrecoverable / not handled error
5773 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005774 ql_dbg(ql_dbg_disc, vha, 0x2002,
5775 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5776 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5777 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5778 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005781 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005782 fcport->d_id.b.domain, fcport->d_id.b.area,
5783 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005784 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005785 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
5787 rval = 3;
5788 break;
5789 }
5790 }
5791
5792 return (rval);
5793}
5794
5795/*
5796 * qla2x00_local_device_login
5797 * Issue local device login command.
5798 *
5799 * Input:
5800 * ha = adapter block pointer.
5801 * loop_id = loop id of device to login to.
5802 *
5803 * Returns (Where's the #define!!!!):
5804 * 0 - Login successfully
5805 * 1 - Login failed
5806 * 3 - Fatal error
5807 */
5808int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005809qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
5811 int rval;
5812 uint16_t mb[MAILBOX_REGISTER_COUNT];
5813
5814 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005815 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 if (rval == QLA_SUCCESS) {
5817 /* Interrogate mailbox registers for any errors */
5818 if (mb[0] == MBS_COMMAND_ERROR)
5819 rval = 1;
5820 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5821 /* device not in PCB table */
5822 rval = 3;
5823 }
5824
5825 return (rval);
5826}
5827
5828/*
5829 * qla2x00_loop_resync
5830 * Resync with fibre channel devices.
5831 *
5832 * Input:
5833 * ha = adapter block pointer.
5834 *
5835 * Returns:
5836 * 0 = success
5837 */
5838int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005839qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005841 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005843 struct req_que *req;
5844 struct rsp_que *rsp;
5845
Michael Hernandezd7459522016-12-12 14:40:07 -08005846 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005847 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005849 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5850 if (vha->flags.online) {
5851 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5853 wait_time = 256;
5854 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005855 if (!IS_QLAFX00(vha->hw)) {
5856 /*
5857 * Issue a marker after FW becomes
5858 * ready.
5859 */
5860 qla2x00_marker(vha, req, rsp, 0, 0,
5861 MK_SYNC_ALL);
5862 vha->marker_needed = 0;
5863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
5865 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005866 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005868 if (IS_QLAFX00(vha->hw))
5869 qlafx00_configure_devices(vha);
5870 else
5871 qla2x00_configure_loop(vha);
5872
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005874 } while (!atomic_read(&vha->loop_down_timer) &&
5875 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5876 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5877 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 }
5880
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005881 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005884 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005885 ql_dbg(ql_dbg_disc, vha, 0x206c,
5886 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
5888 return (rval);
5889}
5890
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005891/*
5892* qla2x00_perform_loop_resync
5893* Description: This function will set the appropriate flags and call
5894* qla2x00_loop_resync. If successful loop will be resynced
5895* Arguments : scsi_qla_host_t pointer
5896* returm : Success or Failure
5897*/
5898
5899int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5900{
5901 int32_t rval = 0;
5902
5903 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5904 /*Configure the flags so that resync happens properly*/
5905 atomic_set(&ha->loop_down_timer, 0);
5906 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5907 atomic_set(&ha->loop_state, LOOP_UP);
5908 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5909 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5910 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5911
5912 rval = qla2x00_loop_resync(ha);
5913 } else
5914 atomic_set(&ha->loop_state, LOOP_DEAD);
5915
5916 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5917 }
5918
5919 return rval;
5920}
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005923qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005924{
5925 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005926 struct scsi_qla_host *vha;
5927 struct qla_hw_data *ha = base_vha->hw;
5928 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005929
Arun Easifeafb7b2010-09-03 14:57:00 -07005930 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005931 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005932 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5933 atomic_inc(&vha->vref_count);
5934 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005935 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005936 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5937 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005938 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005939
Arun Easifeafb7b2010-09-03 14:57:00 -07005940 spin_lock_irqsave(&ha->vport_slock, flags);
5941 }
5942 }
5943 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005944 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005945 }
5946 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005947}
5948
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005949/* Assumes idc_lock always held on entry */
5950void
5951qla83xx_reset_ownership(scsi_qla_host_t *vha)
5952{
5953 struct qla_hw_data *ha = vha->hw;
5954 uint32_t drv_presence, drv_presence_mask;
5955 uint32_t dev_part_info1, dev_part_info2, class_type;
5956 uint32_t class_type_mask = 0x3;
5957 uint16_t fcoe_other_function = 0xffff, i;
5958
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005959 if (IS_QLA8044(ha)) {
5960 drv_presence = qla8044_rd_direct(vha,
5961 QLA8044_CRB_DRV_ACTIVE_INDEX);
5962 dev_part_info1 = qla8044_rd_direct(vha,
5963 QLA8044_CRB_DEV_PART_INFO_INDEX);
5964 dev_part_info2 = qla8044_rd_direct(vha,
5965 QLA8044_CRB_DEV_PART_INFO2);
5966 } else {
5967 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5968 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5969 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5970 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005971 for (i = 0; i < 8; i++) {
5972 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5973 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5974 (i != ha->portnum)) {
5975 fcoe_other_function = i;
5976 break;
5977 }
5978 }
5979 if (fcoe_other_function == 0xffff) {
5980 for (i = 0; i < 8; i++) {
5981 class_type = ((dev_part_info2 >> (i * 4)) &
5982 class_type_mask);
5983 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5984 ((i + 8) != ha->portnum)) {
5985 fcoe_other_function = i + 8;
5986 break;
5987 }
5988 }
5989 }
5990 /*
5991 * Prepare drv-presence mask based on fcoe functions present.
5992 * However consider only valid physical fcoe function numbers (0-15).
5993 */
5994 drv_presence_mask = ~((1 << (ha->portnum)) |
5995 ((fcoe_other_function == 0xffff) ?
5996 0 : (1 << (fcoe_other_function))));
5997
5998 /* We are the reset owner iff:
5999 * - No other protocol drivers present.
6000 * - This is the lowest among fcoe functions. */
6001 if (!(drv_presence & drv_presence_mask) &&
6002 (ha->portnum < fcoe_other_function)) {
6003 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
6004 "This host is Reset owner.\n");
6005 ha->flags.nic_core_reset_owner = 1;
6006 }
6007}
6008
Saurav Kashyapfa492632012-11-21 02:40:29 -05006009static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006010__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6011{
6012 int rval = QLA_SUCCESS;
6013 struct qla_hw_data *ha = vha->hw;
6014 uint32_t drv_ack;
6015
6016 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6017 if (rval == QLA_SUCCESS) {
6018 drv_ack |= (1 << ha->portnum);
6019 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6020 }
6021
6022 return rval;
6023}
6024
Saurav Kashyapfa492632012-11-21 02:40:29 -05006025static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006026__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6027{
6028 int rval = QLA_SUCCESS;
6029 struct qla_hw_data *ha = vha->hw;
6030 uint32_t drv_ack;
6031
6032 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6033 if (rval == QLA_SUCCESS) {
6034 drv_ack &= ~(1 << ha->portnum);
6035 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6036 }
6037
6038 return rval;
6039}
6040
Saurav Kashyapfa492632012-11-21 02:40:29 -05006041static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006042qla83xx_dev_state_to_string(uint32_t dev_state)
6043{
6044 switch (dev_state) {
6045 case QLA8XXX_DEV_COLD:
6046 return "COLD/RE-INIT";
6047 case QLA8XXX_DEV_INITIALIZING:
6048 return "INITIALIZING";
6049 case QLA8XXX_DEV_READY:
6050 return "READY";
6051 case QLA8XXX_DEV_NEED_RESET:
6052 return "NEED RESET";
6053 case QLA8XXX_DEV_NEED_QUIESCENT:
6054 return "NEED QUIESCENT";
6055 case QLA8XXX_DEV_FAILED:
6056 return "FAILED";
6057 case QLA8XXX_DEV_QUIESCENT:
6058 return "QUIESCENT";
6059 default:
6060 return "Unknown";
6061 }
6062}
6063
6064/* Assumes idc-lock always held on entry */
6065void
6066qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6067{
6068 struct qla_hw_data *ha = vha->hw;
6069 uint32_t idc_audit_reg = 0, duration_secs = 0;
6070
6071 switch (audit_type) {
6072 case IDC_AUDIT_TIMESTAMP:
6073 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6074 idc_audit_reg = (ha->portnum) |
6075 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6076 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6077 break;
6078
6079 case IDC_AUDIT_COMPLETION:
6080 duration_secs = ((jiffies_to_msecs(jiffies) -
6081 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6082 idc_audit_reg = (ha->portnum) |
6083 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6084 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6085 break;
6086
6087 default:
6088 ql_log(ql_log_warn, vha, 0xb078,
6089 "Invalid audit type specified.\n");
6090 break;
6091 }
6092}
6093
6094/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006095static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006096qla83xx_initiating_reset(scsi_qla_host_t *vha)
6097{
6098 struct qla_hw_data *ha = vha->hw;
6099 uint32_t idc_control, dev_state;
6100
6101 __qla83xx_get_idc_control(vha, &idc_control);
6102 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6103 ql_log(ql_log_info, vha, 0xb080,
6104 "NIC Core reset has been disabled. idc-control=0x%x\n",
6105 idc_control);
6106 return QLA_FUNCTION_FAILED;
6107 }
6108
6109 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6110 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6111 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6112 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6113 QLA8XXX_DEV_NEED_RESET);
6114 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6115 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6116 } else {
6117 const char *state = qla83xx_dev_state_to_string(dev_state);
6118 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6119
6120 /* SV: XXX: Is timeout required here? */
6121 /* Wait for IDC state change READY -> NEED_RESET */
6122 while (dev_state == QLA8XXX_DEV_READY) {
6123 qla83xx_idc_unlock(vha, 0);
6124 msleep(200);
6125 qla83xx_idc_lock(vha, 0);
6126 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6127 }
6128 }
6129
6130 /* Send IDC ack by writing to drv-ack register */
6131 __qla83xx_set_drv_ack(vha);
6132
6133 return QLA_SUCCESS;
6134}
6135
6136int
6137__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6138{
6139 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6140}
6141
6142int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006143__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6144{
6145 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6146}
6147
Saurav Kashyapfa492632012-11-21 02:40:29 -05006148static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006149qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6150{
6151 uint32_t drv_presence = 0;
6152 struct qla_hw_data *ha = vha->hw;
6153
6154 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6155 if (drv_presence & (1 << ha->portnum))
6156 return QLA_SUCCESS;
6157 else
6158 return QLA_TEST_FAILED;
6159}
6160
6161int
6162qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6163{
6164 int rval = QLA_SUCCESS;
6165 struct qla_hw_data *ha = vha->hw;
6166
6167 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6168 "Entered %s().\n", __func__);
6169
6170 if (vha->device_flags & DFLG_DEV_FAILED) {
6171 ql_log(ql_log_warn, vha, 0xb059,
6172 "Device in unrecoverable FAILED state.\n");
6173 return QLA_FUNCTION_FAILED;
6174 }
6175
6176 qla83xx_idc_lock(vha, 0);
6177
6178 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6179 ql_log(ql_log_warn, vha, 0xb05a,
6180 "Function=0x%x has been removed from IDC participation.\n",
6181 ha->portnum);
6182 rval = QLA_FUNCTION_FAILED;
6183 goto exit;
6184 }
6185
6186 qla83xx_reset_ownership(vha);
6187
6188 rval = qla83xx_initiating_reset(vha);
6189
6190 /*
6191 * Perform reset if we are the reset-owner,
6192 * else wait till IDC state changes to READY/FAILED.
6193 */
6194 if (rval == QLA_SUCCESS) {
6195 rval = qla83xx_idc_state_handler(vha);
6196
6197 if (rval == QLA_SUCCESS)
6198 ha->flags.nic_core_hung = 0;
6199 __qla83xx_clear_drv_ack(vha);
6200 }
6201
6202exit:
6203 qla83xx_idc_unlock(vha, 0);
6204
6205 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6206
6207 return rval;
6208}
6209
Saurav Kashyap81178772012-08-22 14:21:04 -04006210int
6211qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6212{
6213 struct qla_hw_data *ha = vha->hw;
6214 int rval = QLA_FUNCTION_FAILED;
6215
6216 if (!IS_MCTP_CAPABLE(ha)) {
6217 /* This message can be removed from the final version */
6218 ql_log(ql_log_info, vha, 0x506d,
6219 "This board is not MCTP capable\n");
6220 return rval;
6221 }
6222
6223 if (!ha->mctp_dump) {
6224 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6225 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6226
6227 if (!ha->mctp_dump) {
6228 ql_log(ql_log_warn, vha, 0x506e,
6229 "Failed to allocate memory for mctp dump\n");
6230 return rval;
6231 }
6232 }
6233
6234#define MCTP_DUMP_STR_ADDR 0x00000000
6235 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6236 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6237 if (rval != QLA_SUCCESS) {
6238 ql_log(ql_log_warn, vha, 0x506f,
6239 "Failed to capture mctp dump\n");
6240 } else {
6241 ql_log(ql_log_info, vha, 0x5070,
6242 "Mctp dump capture for host (%ld/%p).\n",
6243 vha->host_no, ha->mctp_dump);
6244 ha->mctp_dumped = 1;
6245 }
6246
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006247 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006248 ha->flags.nic_core_reset_hdlr_active = 1;
6249 rval = qla83xx_restart_nic_firmware(vha);
6250 if (rval)
6251 /* NIC Core reset failed. */
6252 ql_log(ql_log_warn, vha, 0x5071,
6253 "Failed to restart nic firmware\n");
6254 else
6255 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6256 "Restarted NIC firmware successfully.\n");
6257 ha->flags.nic_core_reset_hdlr_active = 0;
6258 }
6259
6260 return rval;
6261
6262}
6263
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006264/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006265* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006266* Description: This function will block the new I/Os
6267* Its not aborting any I/Os as context
6268* is not destroyed during quiescence
6269* Arguments: scsi_qla_host_t
6270* return : void
6271*/
6272void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006273qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006274{
6275 struct qla_hw_data *ha = vha->hw;
6276 struct scsi_qla_host *vp;
6277
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006278 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6279 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006280
6281 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6282 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6283 atomic_set(&vha->loop_state, LOOP_DOWN);
6284 qla2x00_mark_all_devices_lost(vha, 0);
6285 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006286 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006287 } else {
6288 if (!atomic_read(&vha->loop_down_timer))
6289 atomic_set(&vha->loop_down_timer,
6290 LOOP_DOWN_TIME);
6291 }
6292 /* Wait for pending cmds to complete */
6293 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6294}
6295
Giridhar Malavalia9083012010-04-12 17:59:55 -07006296void
6297qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6298{
6299 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006300 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006301 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006302 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006303 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006304
Saurav Kashyape46ef002011-02-23 15:27:16 -08006305 /* For ISP82XX, driver waits for completion of the commands.
6306 * online flag should be set.
6307 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006308 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006309 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006310 ha->flags.chip_reset_done = 0;
6311 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006312 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006313
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006314 ql_log(ql_log_info, vha, 0x00af,
6315 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006316
Saurav Kashyape46ef002011-02-23 15:27:16 -08006317 /* For ISP82XX, reset_chip is just disabling interrupts.
6318 * Driver waits for the completion of the commands.
6319 * the interrupts need to be enabled.
6320 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006321 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006322 ha->isp_ops->reset_chip(vha);
6323
Quinn Tran9cd883f2017-12-28 12:33:24 -08006324 SAVE_TOPO(ha);
6325 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006326 ha->flags.n2n_ae = 0;
6327 ha->flags.lip_ae = 0;
6328 ha->current_topology = 0;
6329 ha->flags.fw_started = 0;
6330 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006331 ha->base_qpair->chip_reset++;
6332 for (i = 0; i < ha->max_qpairs; i++) {
6333 if (ha->queue_pair_map[i])
6334 ha->queue_pair_map[i]->chip_reset =
6335 ha->base_qpair->chip_reset;
6336 }
Quinn Tran726b8542017-01-19 22:28:00 -08006337
Giridhar Malavalia9083012010-04-12 17:59:55 -07006338 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6339 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6340 atomic_set(&vha->loop_state, LOOP_DOWN);
6341 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006342
6343 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006344 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006345 atomic_inc(&vp->vref_count);
6346 spin_unlock_irqrestore(&ha->vport_slock, flags);
6347
Giridhar Malavalia9083012010-04-12 17:59:55 -07006348 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006349
6350 spin_lock_irqsave(&ha->vport_slock, flags);
6351 atomic_dec(&vp->vref_count);
6352 }
6353 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006354 } else {
6355 if (!atomic_read(&vha->loop_down_timer))
6356 atomic_set(&vha->loop_down_timer,
6357 LOOP_DOWN_TIME);
6358 }
6359
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006360 /* Clear all async request states across all VPs. */
6361 list_for_each_entry(fcport, &vha->vp_fcports, list)
6362 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6363 spin_lock_irqsave(&ha->vport_slock, flags);
6364 list_for_each_entry(vp, &ha->vp_list, list) {
6365 atomic_inc(&vp->vref_count);
6366 spin_unlock_irqrestore(&ha->vport_slock, flags);
6367
6368 list_for_each_entry(fcport, &vp->vp_fcports, list)
6369 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6370
6371 spin_lock_irqsave(&ha->vport_slock, flags);
6372 atomic_dec(&vp->vref_count);
6373 }
6374 spin_unlock_irqrestore(&ha->vport_slock, flags);
6375
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006376 if (!ha->flags.eeh_busy) {
6377 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006378 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006379 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006380 ql_log(ql_log_info, vha, 0x00b4,
6381 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006382
Saurav Kashyape46ef002011-02-23 15:27:16 -08006383 /* Done waiting for pending commands.
6384 * Reset the online flag.
6385 */
6386 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006389 /* Requeue all commands in outstanding command list. */
6390 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6391 }
Arun Easib6a029e2014-09-25 06:14:52 -04006392 /* memory barrier */
6393 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394}
6395
6396/*
6397* qla2x00_abort_isp
6398* Resets ISP and aborts all outstanding commands.
6399*
6400* Input:
6401* ha = adapter block pointer.
6402*
6403* Returns:
6404* 0 = success
6405*/
6406int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006407qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006409 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006411 struct qla_hw_data *ha = vha->hw;
6412 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006413 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006414 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006416 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006417 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
Santosh Vernekara61712972012-08-22 14:21:13 -04006419 if (IS_QLA8031(ha)) {
6420 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6421 "Clearing fcoe driver presence.\n");
6422 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6423 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6424 "Error while clearing DRV-Presence.\n");
6425 }
6426
Andrew Vasquez85880802009-12-15 21:29:46 -08006427 if (unlikely(pci_channel_offline(ha->pdev) &&
6428 ha->flags.pci_channel_io_perm_failure)) {
6429 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6430 status = 0;
6431 return status;
6432 }
6433
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006434 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006435
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006436 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006438 if (!qla2x00_restart_isp(vha)) {
6439 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006441 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 /*
6443 * Issue marker command only when we are going
6444 * to start the I/O .
6445 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006446 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 }
6448
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006449 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006451 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006453 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006454 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006455
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006456 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6457 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006458 if (ha->fce) {
6459 ha->flags.fce_enabled = 1;
6460 memset(ha->fce, 0,
6461 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006462 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006463 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6464 &ha->fce_bufs);
6465 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006466 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006467 "Unable to reinitialize FCE "
6468 "(%d).\n", rval);
6469 ha->flags.fce_enabled = 0;
6470 }
6471 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006472
6473 if (ha->eft) {
6474 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006475 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006476 ha->eft_dma, EFT_NUM_BUFFERS);
6477 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006478 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006479 "Unable to reinitialize EFT "
6480 "(%d).\n", rval);
6481 }
6482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006484 vha->flags.online = 1;
6485 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006487 ql_log(ql_log_fatal, vha, 0x8035,
6488 "ISP error recover failed - "
6489 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006490 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 * The next call disables the board
6492 * completely.
6493 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006494 ha->isp_ops->reset_adapter(vha);
6495 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006497 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 status = 0;
6499 } else { /* schedule another ISP abort */
6500 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006501 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6502 "ISP abort - retry remaining %d.\n",
6503 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 status = 1;
6505 }
6506 } else {
6507 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006508 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6509 "ISP error recovery - retrying (%d) "
6510 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006511 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 status = 1;
6513 }
6514 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006515
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 }
6517
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006518 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006519 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006520 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006521 spin_lock_irqsave(&ha->vport_slock, flags);
6522 list_for_each_entry(vp, &ha->vp_list, list) {
6523 if (vp->vp_idx) {
6524 atomic_inc(&vp->vref_count);
6525 spin_unlock_irqrestore(&ha->vport_slock, flags);
6526
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006527 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006528
6529 spin_lock_irqsave(&ha->vport_slock, flags);
6530 atomic_dec(&vp->vref_count);
6531 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006532 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006533 spin_unlock_irqrestore(&ha->vport_slock, flags);
6534
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006535 if (IS_QLA8031(ha)) {
6536 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6537 "Setting back fcoe driver presence.\n");
6538 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6539 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6540 "Error while setting DRV-Presence.\n");
6541 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006542 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006543 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6544 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 }
6546
6547 return(status);
6548}
6549
6550/*
6551* qla2x00_restart_isp
6552* restarts the ISP after a reset
6553*
6554* Input:
6555* ha = adapter block pointer.
6556*
6557* Returns:
6558* 0 = success
6559*/
6560static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006561qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006563 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006564 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006565 struct req_que *req = ha->req_q_map[0];
6566 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
6568 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006569 if (qla2x00_isp_firmware(vha)) {
6570 vha->flags.online = 0;
6571 status = ha->isp_ops->chip_diag(vha);
6572 if (!status)
6573 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 }
6575
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006576 if (!status && !(status = qla2x00_init_rings(vha))) {
6577 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006578 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006579
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006580 /* Initialize the queues in use */
6581 qla25xx_init_queues(ha);
6582
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006583 status = qla2x00_fw_ready(vha);
6584 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006585 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006586 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006587 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 }
6589
6590 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006591 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 }
6594 return (status);
6595}
6596
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006597static int
6598qla25xx_init_queues(struct qla_hw_data *ha)
6599{
6600 struct rsp_que *rsp = NULL;
6601 struct req_que *req = NULL;
6602 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6603 int ret = -1;
6604 int i;
6605
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006606 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006607 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006608 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006609 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006610 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006611 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006612 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6613 "%s Rsp que: %d init failed.\n",
6614 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006615 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006616 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6617 "%s Rsp que: %d inited.\n",
6618 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006619 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006620 }
6621 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006622 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006623 if (req && test_bit(i, ha->req_qid_map)) {
6624 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006625 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006626 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006627 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006628 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6629 "%s Req que: %d init failed.\n",
6630 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006631 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006632 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6633 "%s Req que: %d inited.\n",
6634 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006635 }
6636 }
6637 return ret;
6638}
6639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640/*
6641* qla2x00_reset_adapter
6642* Reset adapter.
6643*
6644* Input:
6645* ha = adapter block pointer.
6646*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006647void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006648qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649{
6650 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006651 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006652 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006654 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006655 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 spin_lock_irqsave(&ha->hardware_lock, flags);
6658 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6659 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6660 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6661 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6662 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6663}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006664
6665void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006666qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006667{
6668 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006669 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006670 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6671
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006672 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006673 return;
6674
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006675 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006676 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006677
6678 spin_lock_irqsave(&ha->hardware_lock, flags);
6679 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6680 RD_REG_DWORD(&reg->hccr);
6681 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6682 RD_REG_DWORD(&reg->hccr);
6683 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006684
6685 if (IS_NOPOLLING_TYPE(ha))
6686 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006687}
6688
David Miller4e08df32007-04-16 12:37:43 -07006689/* On sparc systems, obtain port and node WWN from firmware
6690 * properties.
6691 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006692static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6693 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006694{
6695#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006696 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006697 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006698 struct device_node *dp = pci_device_to_OF_node(pdev);
6699 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006700 int len;
6701
6702 val = of_get_property(dp, "port-wwn", &len);
6703 if (val && len >= WWN_SIZE)
6704 memcpy(nv->port_name, val, WWN_SIZE);
6705
6706 val = of_get_property(dp, "node-wwn", &len);
6707 if (val && len >= WWN_SIZE)
6708 memcpy(nv->node_name, val, WWN_SIZE);
6709#endif
6710}
6711
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006712int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006713qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006714{
David Miller4e08df32007-04-16 12:37:43 -07006715 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006716 struct init_cb_24xx *icb;
6717 struct nvram_24xx *nv;
6718 uint32_t *dptr;
6719 uint8_t *dptr1, *dptr2;
6720 uint32_t chksum;
6721 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006722 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006723
David Miller4e08df32007-04-16 12:37:43 -07006724 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006725 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006726 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006727
6728 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006729 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006730 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6731 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6732 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006733 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006734 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6735 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006736
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006737 ha->nvram_size = sizeof(struct nvram_24xx);
6738 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006739
Seokmann Ju281afe12007-07-26 13:43:34 -07006740 /* Get VPD data into cache */
6741 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006742 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006743 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6744
6745 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006746 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006747 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006748 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006749 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6750 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006751
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006752 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6753 "Contents of NVRAM\n");
6754 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6755 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006756
6757 /* Bad NVRAM data, set defaults parameters. */
6758 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6759 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006760 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006761 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006762 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006763 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006764 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6765 ql_log(ql_log_warn, vha, 0x006c,
6766 "Falling back to functioning (yet invalid -- WWPN) "
6767 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006768
6769 /*
6770 * Set default initialization control block.
6771 */
6772 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006773 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6774 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006775 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006776 nv->execution_throttle = cpu_to_le16(0xFFFF);
6777 nv->exchange_count = cpu_to_le16(0);
6778 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006779 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006780 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006781 nv->port_name[2] = 0x00;
6782 nv->port_name[3] = 0xe0;
6783 nv->port_name[4] = 0x8b;
6784 nv->port_name[5] = 0x1c;
6785 nv->port_name[6] = 0x55;
6786 nv->port_name[7] = 0x86;
6787 nv->node_name[0] = 0x20;
6788 nv->node_name[1] = 0x00;
6789 nv->node_name[2] = 0x00;
6790 nv->node_name[3] = 0xe0;
6791 nv->node_name[4] = 0x8b;
6792 nv->node_name[5] = 0x1c;
6793 nv->node_name[6] = 0x55;
6794 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006795 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006796 nv->login_retry_count = cpu_to_le16(8);
6797 nv->interrupt_delay_timer = cpu_to_le16(0);
6798 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006799 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006800 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6801 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6802 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6803 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6804 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6805 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006806 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006807 nv->max_luns_per_target = cpu_to_le16(128);
6808 nv->port_down_retry_count = cpu_to_le16(30);
6809 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006810
6811 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006812 }
6813
Quinn Tran726b8542017-01-19 22:28:00 -08006814 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006815 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006816 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006817 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006818 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006819 }
6820
6821 qlt_24xx_config_nvram_stage1(vha, nv);
6822
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006823 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006824 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006825
6826 /* Copy 1st segment. */
6827 dptr1 = (uint8_t *)icb;
6828 dptr2 = (uint8_t *)&nv->version;
6829 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6830 while (cnt--)
6831 *dptr1++ = *dptr2++;
6832
6833 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006834 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006835
6836 /* Copy 2nd segment. */
6837 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6838 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6839 cnt = (uint8_t *)&icb->reserved_3 -
6840 (uint8_t *)&icb->interrupt_delay_timer;
6841 while (cnt--)
6842 *dptr1++ = *dptr2++;
6843
6844 /*
6845 * Setup driver NVRAM options.
6846 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006847 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006848 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006849
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006850 qlt_24xx_config_nvram_stage2(vha, icb);
6851
Bart Van Asschead950362015-07-09 07:24:08 -07006852 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006853 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006854 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6855 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6856 }
6857
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006858 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006859 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006860 /*
6861 * Firmware will apply the following mask if the nodename was
6862 * not provided.
6863 */
6864 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6865 icb->node_name[0] &= 0xF0;
6866 }
6867
6868 /* Set host adapter parameters. */
6869 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006870 ha->flags.enable_lip_reset = 0;
6871 ha->flags.enable_lip_full_login =
6872 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6873 ha->flags.enable_target_reset =
6874 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006875 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006876 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006877
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006878 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6879 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006880
6881 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6882 sizeof(ha->fw_seriallink_options24));
6883
6884 /* save HBA serial number */
6885 ha->serial0 = icb->port_name[5];
6886 ha->serial1 = icb->port_name[6];
6887 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006888 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6889 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006890
Bart Van Asschead950362015-07-09 07:24:08 -07006891 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006892
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006893 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6894
6895 /* Set minimum login_timeout to 4 seconds. */
6896 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6897 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6898 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006899 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006900 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006901
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006902 /* Set minimum RATOV to 100 tenths of a second. */
6903 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006904
6905 ha->loop_reset_delay = nv->reset_delay;
6906
6907 /* Link Down Timeout = 0:
6908 *
6909 * When Port Down timer expires we will start returning
6910 * I/O's to OS with "DID_NO_CONNECT".
6911 *
6912 * Link Down Timeout != 0:
6913 *
6914 * The driver waits for the link to come up after link down
6915 * before returning I/Os to OS with "DID_NO_CONNECT".
6916 */
6917 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6918 ha->loop_down_abort_time =
6919 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6920 } else {
6921 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6922 ha->loop_down_abort_time =
6923 (LOOP_DOWN_TIME - ha->link_down_timeout);
6924 }
6925
6926 /* Need enough time to try and get the port back. */
6927 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6928 if (qlport_down_retry)
6929 ha->port_down_retry_count = qlport_down_retry;
6930
6931 /* Set login_retry_count */
6932 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6933 if (ha->port_down_retry_count ==
6934 le16_to_cpu(nv->port_down_retry_count) &&
6935 ha->port_down_retry_count > 3)
6936 ha->login_retry_count = ha->port_down_retry_count;
6937 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6938 ha->login_retry_count = ha->port_down_retry_count;
6939 if (ql2xloginretrycount)
6940 ha->login_retry_count = ql2xloginretrycount;
6941
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006942 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006943 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006944 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6945 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6946 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6947 le16_to_cpu(icb->interrupt_delay_timer): 2;
6948 }
Bart Van Asschead950362015-07-09 07:24:08 -07006949 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006950 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006951 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006952 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006953 ha->zio_mode = QLA_ZIO_MODE_6;
6954
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006955 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006956 "ZIO mode %d enabled; timer delay (%d us).\n",
6957 ha->zio_mode, ha->zio_timer * 100);
6958
6959 icb->firmware_options_2 |= cpu_to_le32(
6960 (uint32_t)ha->zio_mode);
6961 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006962 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006963 }
6964
David Miller4e08df32007-04-16 12:37:43 -07006965 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006966 ql_log(ql_log_warn, vha, 0x0070,
6967 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006968 }
6969 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006970}
6971
Sawan Chandak4243c112016-01-27 12:03:31 -05006972uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6973{
6974 struct qla27xx_image_status pri_image_status, sec_image_status;
6975 uint8_t valid_pri_image, valid_sec_image;
6976 uint32_t *wptr;
6977 uint32_t cnt, chksum, size;
6978 struct qla_hw_data *ha = vha->hw;
6979
6980 valid_pri_image = valid_sec_image = 1;
6981 ha->active_image = 0;
6982 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6983
6984 if (!ha->flt_region_img_status_pri) {
6985 valid_pri_image = 0;
6986 goto check_sec_image;
6987 }
6988
6989 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6990 ha->flt_region_img_status_pri, size);
6991
6992 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6993 ql_dbg(ql_dbg_init, vha, 0x018b,
6994 "Primary image signature (0x%x) not valid\n",
6995 pri_image_status.signature);
6996 valid_pri_image = 0;
6997 goto check_sec_image;
6998 }
6999
7000 wptr = (uint32_t *)(&pri_image_status);
7001 cnt = size;
7002
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007003 for (chksum = 0; cnt--; wptr++)
7004 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08007005
Sawan Chandak4243c112016-01-27 12:03:31 -05007006 if (chksum) {
7007 ql_dbg(ql_dbg_init, vha, 0x018c,
7008 "Checksum validation failed for primary image (0x%x)\n",
7009 chksum);
7010 valid_pri_image = 0;
7011 }
7012
7013check_sec_image:
7014 if (!ha->flt_region_img_status_sec) {
7015 valid_sec_image = 0;
7016 goto check_valid_image;
7017 }
7018
7019 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7020 ha->flt_region_img_status_sec, size);
7021
7022 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7023 ql_dbg(ql_dbg_init, vha, 0x018d,
7024 "Secondary image signature(0x%x) not valid\n",
7025 sec_image_status.signature);
7026 valid_sec_image = 0;
7027 goto check_valid_image;
7028 }
7029
7030 wptr = (uint32_t *)(&sec_image_status);
7031 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007032 for (chksum = 0; cnt--; wptr++)
7033 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05007034 if (chksum) {
7035 ql_dbg(ql_dbg_init, vha, 0x018e,
7036 "Checksum validation failed for secondary image (0x%x)\n",
7037 chksum);
7038 valid_sec_image = 0;
7039 }
7040
7041check_valid_image:
7042 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7043 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7044 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7045 if (!ha->active_image ||
7046 pri_image_status.generation_number <
7047 sec_image_status.generation_number)
7048 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7049 }
7050
7051 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7052 ha->active_image == 0 ? "default bootld and fw" :
7053 ha->active_image == 1 ? "primary" :
7054 ha->active_image == 2 ? "secondary" :
7055 "Invalid");
7056
7057 return ha->active_image;
7058}
7059
Adrian Bunk413975a2006-06-30 02:33:06 -07007060static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007061qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7062 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007063{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007064 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007065 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007066 uint32_t *dcode, dlen;
7067 uint32_t risc_addr;
7068 uint32_t risc_size;
7069 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007070 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007071 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007072
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007073 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007074 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007075
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007076 rval = QLA_SUCCESS;
7077
7078 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007079 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007080 *srisc_addr = 0;
7081
Sawan Chandak4243c112016-01-27 12:03:31 -05007082 if (IS_QLA27XX(ha) &&
7083 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7084 faddr = ha->flt_region_fw_sec;
7085
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007086 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007087 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007088 for (i = 0; i < 4; i++)
7089 dcode[i] = be32_to_cpu(dcode[i]);
7090 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7091 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7092 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7093 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007094 ql_log(ql_log_fatal, vha, 0x008c,
7095 "Unable to verify the integrity of flash firmware "
7096 "image.\n");
7097 ql_log(ql_log_fatal, vha, 0x008d,
7098 "Firmware data: %08x %08x %08x %08x.\n",
7099 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007100
7101 return QLA_FUNCTION_FAILED;
7102 }
7103
7104 while (segments && rval == QLA_SUCCESS) {
7105 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007106 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007107
7108 risc_addr = be32_to_cpu(dcode[2]);
7109 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7110 risc_size = be32_to_cpu(dcode[3]);
7111
7112 fragment = 0;
7113 while (risc_size > 0 && rval == QLA_SUCCESS) {
7114 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7115 if (dlen > risc_size)
7116 dlen = risc_size;
7117
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007118 ql_dbg(ql_dbg_init, vha, 0x008e,
7119 "Loading risc segment@ risc addr %x "
7120 "number of dwords 0x%x offset 0x%x.\n",
7121 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007122
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007123 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007124 for (i = 0; i < dlen; i++)
7125 dcode[i] = swab32(dcode[i]);
7126
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007127 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007128 dlen);
7129 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007130 ql_log(ql_log_fatal, vha, 0x008f,
7131 "Failed to load segment %d of firmware.\n",
7132 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007133 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007134 }
7135
7136 faddr += dlen;
7137 risc_addr += dlen;
7138 risc_size -= dlen;
7139 fragment++;
7140 }
7141
7142 /* Next segment. */
7143 segments--;
7144 }
7145
Chad Dupuisf73cb692014-02-26 04:15:06 -05007146 if (!IS_QLA27XX(ha))
7147 return rval;
7148
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 ql_dbg(ql_dbg_init, vha, 0x0161,
7155 "Loading fwdump template from %x\n", faddr);
7156 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7157 risc_size = be32_to_cpu(dcode[2]);
7158 ql_dbg(ql_dbg_init, vha, 0x0162,
7159 "-> array size %x dwords\n", risc_size);
7160 if (risc_size == 0 || risc_size == ~0)
7161 goto default_template;
7162
7163 dlen = (risc_size - 8) * sizeof(*dcode);
7164 ql_dbg(ql_dbg_init, vha, 0x0163,
7165 "-> template allocating %x bytes...\n", dlen);
7166 ha->fw_dump_template = vmalloc(dlen);
7167 if (!ha->fw_dump_template) {
7168 ql_log(ql_log_warn, vha, 0x0164,
7169 "Failed fwdump template allocate %x bytes.\n", risc_size);
7170 goto default_template;
7171 }
7172
7173 faddr += 7;
7174 risc_size -= 8;
7175 dcode = ha->fw_dump_template;
7176 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7177 for (i = 0; i < risc_size; i++)
7178 dcode[i] = le32_to_cpu(dcode[i]);
7179
7180 if (!qla27xx_fwdt_template_valid(dcode)) {
7181 ql_log(ql_log_warn, vha, 0x0165,
7182 "Failed fwdump template validate\n");
7183 goto default_template;
7184 }
7185
7186 dlen = qla27xx_fwdt_template_size(dcode);
7187 ql_dbg(ql_dbg_init, vha, 0x0166,
7188 "-> template size %x bytes\n", dlen);
7189 if (dlen > risc_size * sizeof(*dcode)) {
7190 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007191 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007192 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007193 goto default_template;
7194 }
7195 ha->fw_dump_template_len = dlen;
7196 return rval;
7197
7198default_template:
7199 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7200 if (ha->fw_dump_template)
7201 vfree(ha->fw_dump_template);
7202 ha->fw_dump_template = NULL;
7203 ha->fw_dump_template_len = 0;
7204
7205 dlen = qla27xx_fwdt_template_default_size();
7206 ql_dbg(ql_dbg_init, vha, 0x0169,
7207 "-> template allocating %x bytes...\n", dlen);
7208 ha->fw_dump_template = vmalloc(dlen);
7209 if (!ha->fw_dump_template) {
7210 ql_log(ql_log_warn, vha, 0x016a,
7211 "Failed fwdump template allocate %x bytes.\n", risc_size);
7212 goto failed_template;
7213 }
7214
7215 dcode = ha->fw_dump_template;
7216 risc_size = dlen / sizeof(*dcode);
7217 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7218 for (i = 0; i < risc_size; i++)
7219 dcode[i] = be32_to_cpu(dcode[i]);
7220
7221 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7222 ql_log(ql_log_warn, vha, 0x016b,
7223 "Failed fwdump template validate\n");
7224 goto failed_template;
7225 }
7226
7227 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7228 ql_dbg(ql_dbg_init, vha, 0x016c,
7229 "-> template size %x bytes\n", dlen);
7230 ha->fw_dump_template_len = dlen;
7231 return rval;
7232
7233failed_template:
7234 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7235 if (ha->fw_dump_template)
7236 vfree(ha->fw_dump_template);
7237 ha->fw_dump_template = NULL;
7238 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007239 return rval;
7240}
7241
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007242#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007243
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007244int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007245qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007246{
7247 int rval;
7248 int i, fragment;
7249 uint16_t *wcode, *fwcode;
7250 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7251 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007252 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007253 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007254
7255 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007256 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007257 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007258 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007259 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007260 ql_log(ql_log_info, vha, 0x0084,
7261 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007262 return QLA_FUNCTION_FAILED;
7263 }
7264
7265 rval = QLA_SUCCESS;
7266
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007267 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007268 *srisc_addr = 0;
7269 fwcode = (uint16_t *)blob->fw->data;
7270 fwclen = 0;
7271
7272 /* Validate firmware image by checking version. */
7273 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007274 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007275 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007276 blob->fw->size);
7277 goto fail_fw_integrity;
7278 }
7279 for (i = 0; i < 4; i++)
7280 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7281 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7282 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7283 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007284 ql_log(ql_log_fatal, vha, 0x0086,
7285 "Unable to verify integrity of firmware image.\n");
7286 ql_log(ql_log_fatal, vha, 0x0087,
7287 "Firmware data: %04x %04x %04x %04x.\n",
7288 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007289 goto fail_fw_integrity;
7290 }
7291
7292 seg = blob->segs;
7293 while (*seg && rval == QLA_SUCCESS) {
7294 risc_addr = *seg;
7295 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7296 risc_size = be16_to_cpu(fwcode[3]);
7297
7298 /* Validate firmware image size. */
7299 fwclen += risc_size * sizeof(uint16_t);
7300 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007301 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007302 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007303 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007304 goto fail_fw_integrity;
7305 }
7306
7307 fragment = 0;
7308 while (risc_size > 0 && rval == QLA_SUCCESS) {
7309 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7310 if (wlen > risc_size)
7311 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007312 ql_dbg(ql_dbg_init, vha, 0x0089,
7313 "Loading risc segment@ risc addr %x number of "
7314 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007315
7316 for (i = 0; i < wlen; i++)
7317 wcode[i] = swab16(fwcode[i]);
7318
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007319 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007320 wlen);
7321 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007322 ql_log(ql_log_fatal, vha, 0x008a,
7323 "Failed to load segment %d of firmware.\n",
7324 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007325 break;
7326 }
7327
7328 fwcode += wlen;
7329 risc_addr += wlen;
7330 risc_size -= wlen;
7331 fragment++;
7332 }
7333
7334 /* Next segment. */
7335 seg++;
7336 }
7337 return rval;
7338
7339fail_fw_integrity:
7340 return QLA_FUNCTION_FAILED;
7341}
7342
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007343static int
7344qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007345{
7346 int rval;
7347 int segments, fragment;
7348 uint32_t *dcode, dlen;
7349 uint32_t risc_addr;
7350 uint32_t risc_size;
7351 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007352 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007353 const uint32_t *fwcode;
7354 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007355 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007356 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007357
Andrew Vasquez54333832005-11-09 15:49:04 -08007358 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007359 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007360 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007361 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007362 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007363 ql_log(ql_log_warn, vha, 0x0091,
7364 "Firmware images can be retrieved from: "
7365 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007366
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007367 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007368 }
7369
Chad Dupuiscfb09192011-11-18 09:03:07 -08007370 ql_dbg(ql_dbg_init, vha, 0x0092,
7371 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007372
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007373 rval = QLA_SUCCESS;
7374
7375 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007376 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007377 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007378 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007379 fwclen = 0;
7380
7381 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007382 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007383 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007384 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007385 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007386 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007387 }
7388 for (i = 0; i < 4; i++)
7389 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7390 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7391 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7392 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7393 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007394 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007395 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007396 blob->fw->size);
7397 ql_log(ql_log_fatal, vha, 0x0095,
7398 "Firmware data: %08x %08x %08x %08x.\n",
7399 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007400 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007401 }
7402
7403 while (segments && rval == QLA_SUCCESS) {
7404 risc_addr = be32_to_cpu(fwcode[2]);
7405 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7406 risc_size = be32_to_cpu(fwcode[3]);
7407
7408 /* Validate firmware image size. */
7409 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007410 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007411 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007412 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007413 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007414 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007415 }
7416
7417 fragment = 0;
7418 while (risc_size > 0 && rval == QLA_SUCCESS) {
7419 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7420 if (dlen > risc_size)
7421 dlen = risc_size;
7422
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007423 ql_dbg(ql_dbg_init, vha, 0x0097,
7424 "Loading risc segment@ risc addr %x "
7425 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007426
7427 for (i = 0; i < dlen; i++)
7428 dcode[i] = swab32(fwcode[i]);
7429
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007430 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007431 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007432 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007433 ql_log(ql_log_fatal, vha, 0x0098,
7434 "Failed to load segment %d of firmware.\n",
7435 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007436 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007437 }
7438
7439 fwcode += dlen;
7440 risc_addr += dlen;
7441 risc_size -= dlen;
7442 fragment++;
7443 }
7444
7445 /* Next segment. */
7446 segments--;
7447 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007448
7449 if (!IS_QLA27XX(ha))
7450 return rval;
7451
7452 if (ha->fw_dump_template)
7453 vfree(ha->fw_dump_template);
7454 ha->fw_dump_template = NULL;
7455 ha->fw_dump_template_len = 0;
7456
7457 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007458 "Loading fwdump template from %x\n",
7459 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007460 risc_size = be32_to_cpu(fwcode[2]);
7461 ql_dbg(ql_dbg_init, vha, 0x172,
7462 "-> array size %x dwords\n", risc_size);
7463 if (risc_size == 0 || risc_size == ~0)
7464 goto default_template;
7465
7466 dlen = (risc_size - 8) * sizeof(*fwcode);
7467 ql_dbg(ql_dbg_init, vha, 0x0173,
7468 "-> template allocating %x bytes...\n", dlen);
7469 ha->fw_dump_template = vmalloc(dlen);
7470 if (!ha->fw_dump_template) {
7471 ql_log(ql_log_warn, vha, 0x0174,
7472 "Failed fwdump template allocate %x bytes.\n", risc_size);
7473 goto default_template;
7474 }
7475
7476 fwcode += 7;
7477 risc_size -= 8;
7478 dcode = ha->fw_dump_template;
7479 for (i = 0; i < risc_size; i++)
7480 dcode[i] = le32_to_cpu(fwcode[i]);
7481
7482 if (!qla27xx_fwdt_template_valid(dcode)) {
7483 ql_log(ql_log_warn, vha, 0x0175,
7484 "Failed fwdump template validate\n");
7485 goto default_template;
7486 }
7487
7488 dlen = qla27xx_fwdt_template_size(dcode);
7489 ql_dbg(ql_dbg_init, vha, 0x0176,
7490 "-> template size %x bytes\n", dlen);
7491 if (dlen > risc_size * sizeof(*fwcode)) {
7492 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007493 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007494 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007495 goto default_template;
7496 }
7497 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007498 return rval;
7499
Chad Dupuisf73cb692014-02-26 04:15:06 -05007500default_template:
7501 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7502 if (ha->fw_dump_template)
7503 vfree(ha->fw_dump_template);
7504 ha->fw_dump_template = NULL;
7505 ha->fw_dump_template_len = 0;
7506
7507 dlen = qla27xx_fwdt_template_default_size();
7508 ql_dbg(ql_dbg_init, vha, 0x0179,
7509 "-> template allocating %x bytes...\n", dlen);
7510 ha->fw_dump_template = vmalloc(dlen);
7511 if (!ha->fw_dump_template) {
7512 ql_log(ql_log_warn, vha, 0x017a,
7513 "Failed fwdump template allocate %x bytes.\n", risc_size);
7514 goto failed_template;
7515 }
7516
7517 dcode = ha->fw_dump_template;
7518 risc_size = dlen / sizeof(*fwcode);
7519 fwcode = qla27xx_fwdt_template_default();
7520 for (i = 0; i < risc_size; i++)
7521 dcode[i] = be32_to_cpu(fwcode[i]);
7522
7523 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7524 ql_log(ql_log_warn, vha, 0x017b,
7525 "Failed fwdump template validate\n");
7526 goto failed_template;
7527 }
7528
7529 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7530 ql_dbg(ql_dbg_init, vha, 0x017c,
7531 "-> template size %x bytes\n", dlen);
7532 ha->fw_dump_template_len = dlen;
7533 return rval;
7534
7535failed_template:
7536 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7537 if (ha->fw_dump_template)
7538 vfree(ha->fw_dump_template);
7539 ha->fw_dump_template = NULL;
7540 ha->fw_dump_template_len = 0;
7541 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007542}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007543
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007544int
7545qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7546{
7547 int rval;
7548
Andrew Vasqueze337d902009-04-06 22:33:49 -07007549 if (ql2xfwloadbin == 1)
7550 return qla81xx_load_risc(vha, srisc_addr);
7551
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007552 /*
7553 * FW Load priority:
7554 * 1) Firmware via request-firmware interface (.bin file).
7555 * 2) Firmware residing in flash.
7556 */
7557 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7558 if (rval == QLA_SUCCESS)
7559 return rval;
7560
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007561 return qla24xx_load_risc_flash(vha, srisc_addr,
7562 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007563}
7564
7565int
7566qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7567{
7568 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007569 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007570
Andrew Vasqueze337d902009-04-06 22:33:49 -07007571 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007572 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007573
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007574 /*
7575 * FW Load priority:
7576 * 1) Firmware residing in flash.
7577 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007578 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007579 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007580 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007581 if (rval == QLA_SUCCESS)
7582 return rval;
7583
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007584try_blob_fw:
7585 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7586 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7587 return rval;
7588
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007589 ql_log(ql_log_info, vha, 0x0099,
7590 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007591 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7592 if (rval != QLA_SUCCESS)
7593 return rval;
7594
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007595 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007596 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007597 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007598}
7599
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007600void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007601qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007602{
7603 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007604 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007605
Andrew Vasquez85880802009-12-15 21:29:46 -08007606 if (ha->flags.pci_channel_io_perm_failure)
7607 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007608 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007609 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007610 if (!ha->fw_major_version)
7611 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007612 if (!ha->flags.fw_started)
7613 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007614
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007615 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007616 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007617 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007618 ha->isp_ops->reset_chip(vha);
7619 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007620 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007621 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007622 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007623 ql_log(ql_log_info, vha, 0x8015,
7624 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007625 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007626 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007627
Quinn Tran4b60c822017-06-13 20:47:21 -07007628 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007629 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007630}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007631
7632int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007633qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007634{
7635 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007636 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007637 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007638 struct qla_hw_data *ha = vha->hw;
7639 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007640 struct req_que *req;
7641 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007642
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007643 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007644 return -EINVAL;
7645
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007646 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007647 if (vha->qpair)
7648 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007649 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007650 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007651 rsp = req->rsp;
7652
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007653 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007654 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007655 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007656 }
7657
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007658 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007659
7660 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007661 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7662 BIT_1);
7663 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7664 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7665 ql_dbg(ql_dbg_init, vha, 0x0120,
7666 "Failed SNS login: loop_id=%x, rval2=%d\n",
7667 NPH_SNS, rval2);
7668 else
7669 ql_dbg(ql_dbg_init, vha, 0x0103,
7670 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7671 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7672 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007673 return (QLA_FUNCTION_FAILED);
7674 }
7675
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007676 atomic_set(&vha->loop_down_timer, 0);
7677 atomic_set(&vha->loop_state, LOOP_UP);
7678 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7679 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7680 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007681
7682 return rval;
7683}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007684
7685/* 84XX Support **************************************************************/
7686
7687static LIST_HEAD(qla_cs84xx_list);
7688static DEFINE_MUTEX(qla_cs84xx_mutex);
7689
7690static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007691qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007692{
7693 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007694 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007695
7696 mutex_lock(&qla_cs84xx_mutex);
7697
7698 /* Find any shared 84xx chip. */
7699 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7700 if (cs84xx->bus == ha->pdev->bus) {
7701 kref_get(&cs84xx->kref);
7702 goto done;
7703 }
7704 }
7705
7706 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7707 if (!cs84xx)
7708 goto done;
7709
7710 kref_init(&cs84xx->kref);
7711 spin_lock_init(&cs84xx->access_lock);
7712 mutex_init(&cs84xx->fw_update_mutex);
7713 cs84xx->bus = ha->pdev->bus;
7714
7715 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7716done:
7717 mutex_unlock(&qla_cs84xx_mutex);
7718 return cs84xx;
7719}
7720
7721static void
7722__qla84xx_chip_release(struct kref *kref)
7723{
7724 struct qla_chip_state_84xx *cs84xx =
7725 container_of(kref, struct qla_chip_state_84xx, kref);
7726
7727 mutex_lock(&qla_cs84xx_mutex);
7728 list_del(&cs84xx->list);
7729 mutex_unlock(&qla_cs84xx_mutex);
7730 kfree(cs84xx);
7731}
7732
7733void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007734qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007735{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007736 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007737 if (ha->cs84xx)
7738 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7739}
7740
7741static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007742qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007743{
7744 int rval;
7745 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007746 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007747
7748 mutex_lock(&ha->cs84xx->fw_update_mutex);
7749
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007750 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007751
7752 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7753
7754 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7755 QLA_SUCCESS;
7756}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007757
7758/* 81XX Support **************************************************************/
7759
7760int
7761qla81xx_nvram_config(scsi_qla_host_t *vha)
7762{
7763 int rval;
7764 struct init_cb_81xx *icb;
7765 struct nvram_81xx *nv;
7766 uint32_t *dptr;
7767 uint8_t *dptr1, *dptr2;
7768 uint32_t chksum;
7769 uint16_t cnt;
7770 struct qla_hw_data *ha = vha->hw;
7771
7772 rval = QLA_SUCCESS;
7773 icb = (struct init_cb_81xx *)ha->init_cb;
7774 nv = ha->nvram;
7775
7776 /* Determine NVRAM starting address. */
7777 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007778 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007779 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7780 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007781
7782 /* Get VPD data into cache */
7783 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007784 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7785 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007786
7787 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007788 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007789 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007790 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007791 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7792 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007793
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007794 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7795 "Contents of NVRAM:\n");
7796 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7797 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007798
7799 /* Bad NVRAM data, set defaults parameters. */
7800 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7801 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007802 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007803 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007804 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007805 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007806 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007807 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007808 ql_log(ql_log_info, vha, 0x0074,
7809 "Falling back to functioning (yet invalid -- WWPN) "
7810 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007811
7812 /*
7813 * Set default initialization control block.
7814 */
7815 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007816 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7817 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007818 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007819 nv->execution_throttle = cpu_to_le16(0xFFFF);
7820 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007821 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007822 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007823 nv->port_name[2] = 0x00;
7824 nv->port_name[3] = 0xe0;
7825 nv->port_name[4] = 0x8b;
7826 nv->port_name[5] = 0x1c;
7827 nv->port_name[6] = 0x55;
7828 nv->port_name[7] = 0x86;
7829 nv->node_name[0] = 0x20;
7830 nv->node_name[1] = 0x00;
7831 nv->node_name[2] = 0x00;
7832 nv->node_name[3] = 0xe0;
7833 nv->node_name[4] = 0x8b;
7834 nv->node_name[5] = 0x1c;
7835 nv->node_name[6] = 0x55;
7836 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007837 nv->login_retry_count = cpu_to_le16(8);
7838 nv->interrupt_delay_timer = cpu_to_le16(0);
7839 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007840 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007841 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7842 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7843 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7844 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7845 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7846 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007847 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007848 nv->max_luns_per_target = cpu_to_le16(128);
7849 nv->port_down_retry_count = cpu_to_le16(30);
7850 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007851 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007852 nv->enode_mac[1] = 0xC0;
7853 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007854 nv->enode_mac[3] = 0x04;
7855 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007856 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007857
7858 rval = 1;
7859 }
7860
Arun Easi9e522cd2012-08-22 14:21:31 -04007861 if (IS_T10_PI_CAPABLE(ha))
7862 nv->frame_payload_size &= ~7;
7863
Arun Easiaa230bc2013-01-30 03:34:39 -05007864 qlt_81xx_config_nvram_stage1(vha, nv);
7865
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007866 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007867 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007868
7869 /* Copy 1st segment. */
7870 dptr1 = (uint8_t *)icb;
7871 dptr2 = (uint8_t *)&nv->version;
7872 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7873 while (cnt--)
7874 *dptr1++ = *dptr2++;
7875
7876 icb->login_retry_count = nv->login_retry_count;
7877
7878 /* Copy 2nd segment. */
7879 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7880 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7881 cnt = (uint8_t *)&icb->reserved_5 -
7882 (uint8_t *)&icb->interrupt_delay_timer;
7883 while (cnt--)
7884 *dptr1++ = *dptr2++;
7885
7886 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7887 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7888 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007889 icb->enode_mac[0] = 0x00;
7890 icb->enode_mac[1] = 0xC0;
7891 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007892 icb->enode_mac[3] = 0x04;
7893 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007894 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007895 }
7896
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007897 /* Use extended-initialization control block. */
7898 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7899
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007900 /*
7901 * Setup driver NVRAM options.
7902 */
7903 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007904 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007905
Arun Easiaa230bc2013-01-30 03:34:39 -05007906 qlt_81xx_config_nvram_stage2(vha, icb);
7907
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007908 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007909 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007910 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7911 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7912 }
7913
7914 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007915 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007916 /*
7917 * Firmware will apply the following mask if the nodename was
7918 * not provided.
7919 */
7920 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7921 icb->node_name[0] &= 0xF0;
7922 }
7923
7924 /* Set host adapter parameters. */
7925 ha->flags.disable_risc_code_load = 0;
7926 ha->flags.enable_lip_reset = 0;
7927 ha->flags.enable_lip_full_login =
7928 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7929 ha->flags.enable_target_reset =
7930 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7931 ha->flags.enable_led_scheme = 0;
7932 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7933
7934 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7935 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7936
7937 /* save HBA serial number */
7938 ha->serial0 = icb->port_name[5];
7939 ha->serial1 = icb->port_name[6];
7940 ha->serial2 = icb->port_name[7];
7941 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7942 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7943
Bart Van Asschead950362015-07-09 07:24:08 -07007944 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007945
7946 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7947
7948 /* Set minimum login_timeout to 4 seconds. */
7949 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7950 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7951 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007952 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007953 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007954
7955 /* Set minimum RATOV to 100 tenths of a second. */
7956 ha->r_a_tov = 100;
7957
7958 ha->loop_reset_delay = nv->reset_delay;
7959
7960 /* Link Down Timeout = 0:
7961 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007962 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007963 * I/O's to OS with "DID_NO_CONNECT".
7964 *
7965 * Link Down Timeout != 0:
7966 *
7967 * The driver waits for the link to come up after link down
7968 * before returning I/Os to OS with "DID_NO_CONNECT".
7969 */
7970 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7971 ha->loop_down_abort_time =
7972 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7973 } else {
7974 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7975 ha->loop_down_abort_time =
7976 (LOOP_DOWN_TIME - ha->link_down_timeout);
7977 }
7978
7979 /* Need enough time to try and get the port back. */
7980 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7981 if (qlport_down_retry)
7982 ha->port_down_retry_count = qlport_down_retry;
7983
7984 /* Set login_retry_count */
7985 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7986 if (ha->port_down_retry_count ==
7987 le16_to_cpu(nv->port_down_retry_count) &&
7988 ha->port_down_retry_count > 3)
7989 ha->login_retry_count = ha->port_down_retry_count;
7990 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7991 ha->login_retry_count = ha->port_down_retry_count;
7992 if (ql2xloginretrycount)
7993 ha->login_retry_count = ql2xloginretrycount;
7994
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007995 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007996 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007997 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007998
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007999 /* Enable ZIO. */
8000 if (!vha->flags.init_done) {
8001 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
8002 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
8003 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
8004 le16_to_cpu(icb->interrupt_delay_timer): 2;
8005 }
Bart Van Asschead950362015-07-09 07:24:08 -07008006 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008007 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
8008 vha->flags.process_response_queue = 0;
8009 if (ha->zio_mode != QLA_ZIO_DISABLED) {
8010 ha->zio_mode = QLA_ZIO_MODE_6;
8011
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008012 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008013 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008014 ha->zio_mode,
8015 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008016
8017 icb->firmware_options_2 |= cpu_to_le32(
8018 (uint32_t)ha->zio_mode);
8019 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8020 vha->flags.process_response_queue = 1;
8021 }
8022
Quinn Tran41dc5292017-01-19 22:28:03 -08008023 /* enable RIDA Format2 */
8024 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
8025 icb->firmware_options_3 |= BIT_0;
8026
Duane Grigsbyedd05de2017-10-13 09:34:06 -07008027 if (IS_QLA27XX(ha)) {
8028 icb->firmware_options_3 |= BIT_8;
8029 ql_dbg(ql_log_info, vha, 0x0075,
8030 "Enabling direct connection.\n");
8031 }
8032
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008033 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008034 ql_log(ql_log_warn, vha, 0x0076,
8035 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008036 }
8037 return (rval);
8038}
8039
Giridhar Malavalia9083012010-04-12 17:59:55 -07008040int
8041qla82xx_restart_isp(scsi_qla_host_t *vha)
8042{
8043 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008044 struct qla_hw_data *ha = vha->hw;
8045 struct req_que *req = ha->req_q_map[0];
8046 struct rsp_que *rsp = ha->rsp_q_map[0];
8047 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008048 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008049
8050 status = qla2x00_init_rings(vha);
8051 if (!status) {
8052 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8053 ha->flags.chip_reset_done = 1;
8054
8055 status = qla2x00_fw_ready(vha);
8056 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008057 /* Issue a marker after FW becomes ready. */
8058 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008059 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008060 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008061 }
8062
8063 /* if no cable then assume it's good */
8064 if ((vha->device_flags & DFLG_NO_CABLE))
8065 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008066 }
8067
8068 if (!status) {
8069 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8070
8071 if (!atomic_read(&vha->loop_down_timer)) {
8072 /*
8073 * Issue marker command only when we are going
8074 * to start the I/O .
8075 */
8076 vha->marker_needed = 1;
8077 }
8078
Giridhar Malavalia9083012010-04-12 17:59:55 -07008079 ha->isp_ops->enable_intrs(ha);
8080
8081 ha->isp_abort_cnt = 0;
8082 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8083
Saurav Kashyap53296782011-05-10 11:30:06 -07008084 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008085 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008086
Giridhar Malavalia9083012010-04-12 17:59:55 -07008087 if (ha->fce) {
8088 ha->flags.fce_enabled = 1;
8089 memset(ha->fce, 0,
8090 fce_calc_size(ha->fce_bufs));
8091 rval = qla2x00_enable_fce_trace(vha,
8092 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8093 &ha->fce_bufs);
8094 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008095 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008096 "Unable to reinitialize FCE (%d).\n",
8097 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008098 ha->flags.fce_enabled = 0;
8099 }
8100 }
8101
8102 if (ha->eft) {
8103 memset(ha->eft, 0, EFT_SIZE);
8104 rval = qla2x00_enable_eft_trace(vha,
8105 ha->eft_dma, EFT_NUM_BUFFERS);
8106 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008107 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008108 "Unable to reinitialize EFT (%d).\n",
8109 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008110 }
8111 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008112 }
8113
8114 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008115 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008116 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008117
8118 spin_lock_irqsave(&ha->vport_slock, flags);
8119 list_for_each_entry(vp, &ha->vp_list, list) {
8120 if (vp->vp_idx) {
8121 atomic_inc(&vp->vref_count);
8122 spin_unlock_irqrestore(&ha->vport_slock, flags);
8123
Giridhar Malavalia9083012010-04-12 17:59:55 -07008124 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008125
8126 spin_lock_irqsave(&ha->vport_slock, flags);
8127 atomic_dec(&vp->vref_count);
8128 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008129 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008130 spin_unlock_irqrestore(&ha->vport_slock, flags);
8131
Giridhar Malavalia9083012010-04-12 17:59:55 -07008132 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008133 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008134 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008135 }
8136
8137 return status;
8138}
8139
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008140void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008141qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008142{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008143 struct qla_hw_data *ha = vha->hw;
8144
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008145 /* Hold status IOCBs until ABTS response received. */
8146 if (ql2xfwholdabts)
8147 ha->fw_options[3] |= BIT_12;
8148
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008149 /* Set Retry FLOGI in case of P2P connection */
8150 if (ha->operating_mode == P2P) {
8151 ha->fw_options[2] |= BIT_3;
8152 ql_dbg(ql_dbg_disc, vha, 0x2103,
8153 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8154 __func__, ha->fw_options[2]);
8155 }
8156
Quinn Tran41dc5292017-01-19 22:28:03 -08008157 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8158 if (ql2xmvasynctoatio) {
8159 if (qla_tgt_mode_enabled(vha) ||
8160 qla_dual_mode_enabled(vha))
8161 ha->fw_options[2] |= BIT_11;
8162 else
8163 ha->fw_options[2] &= ~BIT_11;
8164 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008165
Quinn Tranf7e761f2017-06-02 09:12:02 -07008166 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008167 qla_dual_mode_enabled(vha)) {
8168 /* FW auto send SCSI status during */
8169 ha->fw_options[1] |= BIT_8;
8170 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8171
8172 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008173 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008174 } else {
8175 ha->fw_options[1] &= ~BIT_8;
8176 ha->fw_options[10] &= 0x00ff;
8177
Quinn Tranf7e761f2017-06-02 09:12:02 -07008178 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008179 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008180
Quinn Tran41dc5292017-01-19 22:28:03 -08008181 if (ql2xetsenable) {
8182 /* Enable ETS Burst. */
8183 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8184 ha->fw_options[2] |= BIT_9;
8185 }
8186
Quinn Tran83548fe2017-06-02 09:12:01 -07008187 ql_dbg(ql_dbg_init, vha, 0x00e9,
8188 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8189 __func__, ha->fw_options[1], ha->fw_options[2],
8190 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008191
Andrew Vasquezae97c912010-02-18 10:07:28 -08008192 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008193}
Sarang Radke09ff7012010-03-19 17:03:59 -07008194
8195/*
8196 * qla24xx_get_fcp_prio
8197 * Gets the fcp cmd priority value for the logged in port.
8198 * Looks for a match of the port descriptors within
8199 * each of the fcp prio config entries. If a match is found,
8200 * the tag (priority) value is returned.
8201 *
8202 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008203 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008204 * fcport = port structure pointer.
8205 *
8206 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008207 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008208 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008209 *
8210 * Context:
8211 * Kernel context
8212 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008213static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008214qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8215{
8216 int i, entries;
8217 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008218 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008219 uint32_t pid1, pid2;
8220 uint64_t wwn1, wwn2;
8221 struct qla_fcp_prio_entry *pri_entry;
8222 struct qla_hw_data *ha = vha->hw;
8223
8224 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008225 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008226
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008227 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008228 entries = ha->fcp_prio_cfg->num_entries;
8229 pri_entry = &ha->fcp_prio_cfg->entry[0];
8230
8231 for (i = 0; i < entries; i++) {
8232 pid_match = wwn_match = 0;
8233
8234 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8235 pri_entry++;
8236 continue;
8237 }
8238
8239 /* check source pid for a match */
8240 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8241 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8242 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8243 if (pid1 == INVALID_PORT_ID)
8244 pid_match++;
8245 else if (pid1 == pid2)
8246 pid_match++;
8247 }
8248
8249 /* check destination pid for a match */
8250 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8251 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8252 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8253 if (pid1 == INVALID_PORT_ID)
8254 pid_match++;
8255 else if (pid1 == pid2)
8256 pid_match++;
8257 }
8258
8259 /* check source WWN for a match */
8260 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8261 wwn1 = wwn_to_u64(vha->port_name);
8262 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8263 if (wwn2 == (uint64_t)-1)
8264 wwn_match++;
8265 else if (wwn1 == wwn2)
8266 wwn_match++;
8267 }
8268
8269 /* check destination WWN for a match */
8270 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8271 wwn1 = wwn_to_u64(fcport->port_name);
8272 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8273 if (wwn2 == (uint64_t)-1)
8274 wwn_match++;
8275 else if (wwn1 == wwn2)
8276 wwn_match++;
8277 }
8278
8279 if (pid_match == 2 || wwn_match == 2) {
8280 /* Found a matching entry */
8281 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8282 priority = pri_entry->tag;
8283 break;
8284 }
8285
8286 pri_entry++;
8287 }
8288
8289 return priority;
8290}
8291
8292/*
8293 * qla24xx_update_fcport_fcp_prio
8294 * Activates fcp priority for the logged in fc port
8295 *
8296 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008297 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008298 * fcp = port structure pointer.
8299 *
8300 * Return:
8301 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8302 *
8303 * Context:
8304 * Kernel context.
8305 */
8306int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008307qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008308{
8309 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008310 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008311 uint16_t mb[5];
8312
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008313 if (fcport->port_type != FCT_TARGET ||
8314 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008315 return QLA_FUNCTION_FAILED;
8316
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008317 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008318 if (priority < 0)
8319 return QLA_FUNCTION_FAILED;
8320
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008321 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008322 fcport->fcp_prio = priority & 0xf;
8323 return QLA_SUCCESS;
8324 }
8325
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008326 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008327 if (ret == QLA_SUCCESS) {
8328 if (fcport->fcp_prio != priority)
8329 ql_dbg(ql_dbg_user, vha, 0x709e,
8330 "Updated FCP_CMND priority - value=%d loop_id=%d "
8331 "port_id=%02x%02x%02x.\n", priority,
8332 fcport->loop_id, fcport->d_id.b.domain,
8333 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008334 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008335 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008336 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008337 "Unable to update FCP_CMND priority - ret=0x%x for "
8338 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8339 fcport->d_id.b.domain, fcport->d_id.b.area,
8340 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008341 return ret;
8342}
8343
8344/*
8345 * qla24xx_update_all_fcp_prio
8346 * Activates fcp priority for all the logged in ports
8347 *
8348 * Input:
8349 * ha = adapter block pointer.
8350 *
8351 * Return:
8352 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8353 *
8354 * Context:
8355 * Kernel context.
8356 */
8357int
8358qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8359{
8360 int ret;
8361 fc_port_t *fcport;
8362
8363 ret = QLA_FUNCTION_FAILED;
8364 /* We need to set priority for all logged in ports */
8365 list_for_each_entry(fcport, &vha->vp_fcports, list)
8366 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8367
8368 return ret;
8369}
Michael Hernandezd7459522016-12-12 14:40:07 -08008370
Quinn Tran82de8022017-06-13 20:47:17 -07008371struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8372 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008373{
8374 int rsp_id = 0;
8375 int req_id = 0;
8376 int i;
8377 struct qla_hw_data *ha = vha->hw;
8378 uint16_t qpair_id = 0;
8379 struct qla_qpair *qpair = NULL;
8380 struct qla_msix_entry *msix;
8381
8382 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8383 ql_log(ql_log_warn, vha, 0x00181,
8384 "FW/Driver is not multi-queue capable.\n");
8385 return NULL;
8386 }
8387
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008388 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008389 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8390 if (qpair == NULL) {
8391 ql_log(ql_log_warn, vha, 0x0182,
8392 "Failed to allocate memory for queue pair.\n");
8393 return NULL;
8394 }
8395 memset(qpair, 0, sizeof(struct qla_qpair));
8396
8397 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008398 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008399 qpair->qp_lock_ptr = &qpair->qp_lock;
8400 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008401 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008402
8403 /* Assign available que pair id */
8404 mutex_lock(&ha->mq_lock);
8405 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008406 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008407 mutex_unlock(&ha->mq_lock);
8408 ql_log(ql_log_warn, vha, 0x0183,
8409 "No resources to create additional q pair.\n");
8410 goto fail_qid_map;
8411 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008412 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008413 set_bit(qpair_id, ha->qpair_qid_map);
8414 ha->queue_pair_map[qpair_id] = qpair;
8415 qpair->id = qpair_id;
8416 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008417 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008418 INIT_LIST_HEAD(&qpair->hints_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008419 qpair->chip_reset = ha->base_qpair->chip_reset;
8420 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8421 qpair->enable_explicit_conf =
8422 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008423
8424 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008425 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008426 if (msix->in_use)
8427 continue;
8428 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008429 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008430 "Vector %x selected for qpair\n", msix->vector);
8431 break;
8432 }
8433 if (!qpair->msix) {
8434 ql_log(ql_log_warn, vha, 0x0184,
8435 "Out of MSI-X vectors!.\n");
8436 goto fail_msix;
8437 }
8438
8439 qpair->msix->in_use = 1;
8440 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008441 qpair->pdev = ha->pdev;
8442 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8443 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008444
8445 mutex_unlock(&ha->mq_lock);
8446
8447 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008448 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008449 if (!rsp_id) {
8450 ql_log(ql_log_warn, vha, 0x0185,
8451 "Failed to create response queue.\n");
8452 goto fail_rsp;
8453 }
8454
8455 qpair->rsp = ha->rsp_q_map[rsp_id];
8456
8457 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008458 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8459 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008460 if (!req_id) {
8461 ql_log(ql_log_warn, vha, 0x0186,
8462 "Failed to create request queue.\n");
8463 goto fail_req;
8464 }
8465
8466 qpair->req = ha->req_q_map[req_id];
8467 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008468 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008469 /* init qpair to this cpu. Will adjust at run time. */
8470 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008471
8472 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8473 if (ha->fw_attributes & BIT_4)
8474 qpair->difdix_supported = 1;
8475 }
8476
8477 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8478 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008479 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008480 "Failed to create srb mempool for qpair %d\n",
8481 qpair->id);
8482 goto fail_mempool;
8483 }
8484
8485 /* Mark as online */
8486 qpair->online = 1;
8487
8488 if (!vha->flags.qpairs_available)
8489 vha->flags.qpairs_available = 1;
8490
8491 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8492 "Request/Response queue pair created, id %d\n",
8493 qpair->id);
8494 ql_dbg(ql_dbg_init, vha, 0x0187,
8495 "Request/Response queue pair created, id %d\n",
8496 qpair->id);
8497 }
8498 return qpair;
8499
8500fail_mempool:
8501fail_req:
8502 qla25xx_delete_rsp_que(vha, qpair->rsp);
8503fail_rsp:
8504 mutex_lock(&ha->mq_lock);
8505 qpair->msix->in_use = 0;
8506 list_del(&qpair->qp_list_elem);
8507 if (list_empty(&vha->qp_list))
8508 vha->flags.qpairs_available = 0;
8509fail_msix:
8510 ha->queue_pair_map[qpair_id] = NULL;
8511 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008512 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008513 mutex_unlock(&ha->mq_lock);
8514fail_qid_map:
8515 kfree(qpair);
8516 return NULL;
8517}
8518
8519int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8520{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008521 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008522 struct qla_hw_data *ha = qpair->hw;
8523
8524 qpair->delete_in_progress = 1;
8525 while (atomic_read(&qpair->ref_count))
8526 msleep(500);
8527
8528 ret = qla25xx_delete_req_que(vha, qpair->req);
8529 if (ret != QLA_SUCCESS)
8530 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008531
Michael Hernandezd7459522016-12-12 14:40:07 -08008532 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8533 if (ret != QLA_SUCCESS)
8534 goto fail;
8535
8536 mutex_lock(&ha->mq_lock);
8537 ha->queue_pair_map[qpair->id] = NULL;
8538 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008539 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008540 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008541 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008542 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008543 vha->flags.qpairs_req_created = 0;
8544 vha->flags.qpairs_rsp_created = 0;
8545 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008546 mempool_destroy(qpair->srb_mempool);
8547 kfree(qpair);
8548 mutex_unlock(&ha->mq_lock);
8549
8550 return QLA_SUCCESS;
8551fail:
8552 return ret;
8553}