blob: be4c67b465b80faa2bf126c1abed8afcab6b554e [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 *);
Harihara Kadayam4d4df192008-04-03 13:13:26 -070044
Andrew Vasquezac280b62009-08-20 11:06:05 -070045/* SRB Extensions ---------------------------------------------------------- */
46
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080047void
Kees Cook8e5f4ba2017-09-03 13:23:32 -070048qla2x00_sp_timeout(struct timer_list *t)
Andrew Vasquezac280b62009-08-20 11:06:05 -070049{
Kees Cook8e5f4ba2017-09-03 13:23:32 -070050 srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
Madhuranath Iyengar49163922010-05-04 15:01:28 -070051 struct srb_iocb *iocb;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080052 scsi_qla_host_t *vha = sp->vha;
Andrew Vasquezac280b62009-08-20 11:06:05 -070053 struct req_que *req;
54 unsigned long flags;
55
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080056 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
57 req = vha->hw->req_q_map[0];
Andrew Vasquezac280b62009-08-20 11:06:05 -070058 req->outstanding_cmds[sp->handle] = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080059 iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar49163922010-05-04 15:01:28 -070060 iocb->timeout(sp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080061 sp->free(sp);
62 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -070063}
64
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080065void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080066qla2x00_sp_free(void *ptr)
Andrew Vasquezac280b62009-08-20 11:06:05 -070067{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080068 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080069 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -070070
Chad Dupuis4d97cc52010-10-15 11:27:41 -070071 del_timer(&iocb->timer);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080072 qla2x00_rel_sp(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -070073}
74
Andrew Vasquezac280b62009-08-20 11:06:05 -070075/* Asynchronous Login/Logout Routines -------------------------------------- */
76
Saurav Kashyapa9b6f722012-08-22 14:21:01 -040077unsigned long
Andrew Vasquez5b914902010-05-28 15:08:30 -070078qla2x00_get_async_timeout(struct scsi_qla_host *vha)
79{
80 unsigned long tmo;
81 struct qla_hw_data *ha = vha->hw;
82
83 /* Firmware should use switch negotiated r_a_tov for timeout. */
84 tmo = ha->r_a_tov / 10 * 2;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040085 if (IS_QLAFX00(ha)) {
86 tmo = FX00_DEF_RATOV * 2;
87 } else if (!IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez5b914902010-05-28 15:08:30 -070088 /*
89 * Except for earlier ISPs where the timeout is seeded from the
90 * initialization control block.
91 */
92 tmo = ha->login_timeout;
93 }
94 return tmo;
95}
Andrew Vasquezac280b62009-08-20 11:06:05 -070096
Quinn Tran726b8542017-01-19 22:28:00 -080097void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080098qla2x00_async_iocb_timeout(void *data)
Andrew Vasquezac280b62009-08-20 11:06:05 -070099{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800100 srb_t *sp = data;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700101 fc_port_t *fcport = sp->fcport;
Quinn Tran726b8542017-01-19 22:28:00 -0800102 struct srb_iocb *lio = &sp->u.iocb_cmd;
103 struct event_arg ea;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700104
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700105 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
Quinn Tran726b8542017-01-19 22:28:00 -0800106 "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
107 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700108
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700109 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800110
111 switch (sp->type) {
112 case SRB_LOGIN_CMD:
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700113 /* Retry as needed. */
114 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
115 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
116 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800117 memset(&ea, 0, sizeof(ea));
118 ea.event = FCME_PLOGI_DONE;
119 ea.fcport = sp->fcport;
120 ea.data[0] = lio->u.logio.data[0];
121 ea.data[1] = lio->u.logio.data[1];
122 ea.sp = sp;
123 qla24xx_handle_plogi_done_event(fcport->vha, &ea);
124 break;
125 case SRB_LOGOUT_CMD:
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400126 qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800127 break;
128 case SRB_CT_PTHRU_CMD:
129 case SRB_MB_IOCB:
130 case SRB_NACK_PLOGI:
131 case SRB_NACK_PRLI:
132 case SRB_NACK_LOGO:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800133 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800134 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700135 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700136}
137
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700138static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800139qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700140{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800141 srb_t *sp = ptr;
142 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800143 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800144 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700145
Quinn Tran83548fe2017-06-02 09:12:01 -0700146 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800147 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800148
149 sp->fcport->flags &= ~FCF_ASYNC_SENT;
150 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
151 memset(&ea, 0, sizeof(ea));
152 ea.event = FCME_PLOGI_DONE;
153 ea.fcport = sp->fcport;
154 ea.data[0] = lio->u.logio.data[0];
155 ea.data[1] = lio->u.logio.data[1];
156 ea.iop[0] = lio->u.logio.iop[0];
157 ea.iop[1] = lio->u.logio.iop[1];
158 ea.sp = sp;
159 qla2x00_fcport_event_handler(vha, &ea);
160 }
161
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800162 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700163}
164
Andrew Vasquezac280b62009-08-20 11:06:05 -0700165int
166qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
167 uint16_t *data)
168{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700169 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700170 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800171 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700172
Quinn Tran726b8542017-01-19 22:28:00 -0800173 if (!vha->flags.online)
174 goto done;
175
176 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
177 (fcport->fw_login_state == DSC_LS_PLOGI_COMP) ||
178 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
179 goto done;
180
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800181 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700182 if (!sp)
183 goto done;
184
Quinn Tran726b8542017-01-19 22:28:00 -0800185 fcport->flags |= FCF_ASYNC_SENT;
186 fcport->logout_completed = 0;
187
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800188 sp->type = SRB_LOGIN_CMD;
189 sp->name = "login";
190 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
191
192 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700193 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800194 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700195 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700196
197 if (fcport->fc4f_nvme)
198 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
199
Andrew Vasquezac280b62009-08-20 11:06:05 -0700200 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700201 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700202 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500203 if (rval != QLA_SUCCESS) {
204 fcport->flags &= ~FCF_ASYNC_SENT;
205 fcport->flags |= FCF_LOGIN_NEEDED;
206 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700207 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500208 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700209
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700210 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800211 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
212 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800213 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
214 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700215 return rval;
216
217done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800218 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700219done:
Quinn Tran726b8542017-01-19 22:28:00 -0800220 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700221 return rval;
222}
223
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700224static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800225qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700226{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800227 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800228 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700229
Quinn Tran726b8542017-01-19 22:28:00 -0800230 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800231 if (!test_bit(UNLOADING, &sp->vha->dpc_flags))
232 qla2x00_post_async_logout_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800233 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800234 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700235}
236
Andrew Vasquezac280b62009-08-20 11:06:05 -0700237int
238qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
239{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700240 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700241 struct srb_iocb *lio;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700242 int rval;
243
244 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800245 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800246 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700247 if (!sp)
248 goto done;
249
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800250 sp->type = SRB_LOGOUT_CMD;
251 sp->name = "logout";
252 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
253
254 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700255 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800256 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700257 rval = qla2x00_start_sp(sp);
258 if (rval != QLA_SUCCESS)
259 goto done_free_sp;
260
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700261 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800262 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800263 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800264 fcport->d_id.b.area, fcport->d_id.b.al_pa,
265 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700266 return rval;
267
268done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800269 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700270done:
Quinn Tran726b8542017-01-19 22:28:00 -0800271 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700272 return rval;
273}
274
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700275static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800276qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700277{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800278 srb_t *sp = ptr;
279 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800280 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700281
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800282 if (!test_bit(UNLOADING, &vha->dpc_flags))
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800283 qla2x00_post_async_adisc_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800284 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800285 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700286}
287
288int
289qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
290 uint16_t *data)
291{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700292 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700293 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700294 int rval;
295
296 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800297 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800298 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700299 if (!sp)
300 goto done;
301
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800302 sp->type = SRB_ADISC_CMD;
303 sp->name = "adisc";
304 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
305
306 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700307 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800308 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700309 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700310 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700311 rval = qla2x00_start_sp(sp);
312 if (rval != QLA_SUCCESS)
313 goto done_free_sp;
314
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700315 ql_dbg(ql_dbg_disc, vha, 0x206f,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800316 "Async-adisc - hdl=%x loopid=%x portid=%02x%02x%02x.\n",
317 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
318 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700319 return rval;
320
321done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800322 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700323done:
Quinn Tran726b8542017-01-19 22:28:00 -0800324 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700325 return rval;
326}
327
Quinn Tran726b8542017-01-19 22:28:00 -0800328static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
329 struct event_arg *ea)
330{
331 fc_port_t *fcport, *conflict_fcport;
332 struct get_name_list_extended *e;
333 u16 i, n, found = 0, loop_id;
334 port_id_t id;
335 u64 wwn;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700336 u8 opt = 0, current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800337
338 fcport = ea->fcport;
339
340 if (ea->rc) { /* rval */
341 if (fcport->login_retry == 0) {
342 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700343 ql_dbg(ql_dbg_disc, vha, 0x20de,
344 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
345 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800346 }
347 return;
348 }
349
350 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700351 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800352 "%s %8phC rscn gen changed rscn %d|%d \n",
353 __func__, fcport->port_name,
354 fcport->last_rscn_gen, fcport->rscn_gen);
355 qla24xx_post_gidpn_work(vha, fcport);
356 return;
357 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700358 ql_dbg(ql_dbg_disc, vha, 0x20e0,
359 "%s %8phC login gen changed login %d|%d\n",
360 __func__, fcport->port_name,
361 fcport->last_login_gen, fcport->login_gen);
Quinn Tran726b8542017-01-19 22:28:00 -0800362 return;
363 }
364
365 n = ea->data[0] / sizeof(struct get_name_list_extended);
366
Quinn Tran83548fe2017-06-02 09:12:01 -0700367 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800368 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
369 __func__, __LINE__, fcport->port_name, n,
370 fcport->d_id.b.domain, fcport->d_id.b.area,
371 fcport->d_id.b.al_pa, fcport->loop_id);
372
373 for (i = 0; i < n; i++) {
374 e = &vha->gnl.l[i];
375 wwn = wwn_to_u64(e->port_name);
376
377 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
378 continue;
379
380 found = 1;
381 id.b.domain = e->port_id[2];
382 id.b.area = e->port_id[1];
383 id.b.al_pa = e->port_id[0];
384 id.b.rsvd_1 = 0;
385
386 loop_id = le16_to_cpu(e->nport_handle);
387 loop_id = (loop_id & 0x7fff);
388
Quinn Tran83548fe2017-06-02 09:12:01 -0700389 ql_dbg(ql_dbg_disc, vha, 0x20e2,
390 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
391 __func__, fcport->port_name,
392 e->current_login_state, fcport->fw_login_state,
393 id.b.domain, id.b.area, id.b.al_pa,
394 fcport->d_id.b.domain, fcport->d_id.b.area,
395 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800396
397 if ((id.b24 != fcport->d_id.b24) ||
398 ((fcport->loop_id != FC_NO_LOOP_ID) &&
399 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700400 ql_dbg(ql_dbg_disc, vha, 0x20e3,
401 "%s %d %8phC post del sess\n",
402 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800403 qlt_schedule_sess_for_deletion(fcport, 1);
404 return;
405 }
406
407 fcport->loop_id = loop_id;
408
409 wwn = wwn_to_u64(fcport->port_name);
410 qlt_find_sess_invalidate_other(vha, wwn,
411 id, loop_id, &conflict_fcport);
412
413 if (conflict_fcport) {
414 /*
415 * Another share fcport share the same loop_id &
416 * nport id. Conflict fcport needs to finish
417 * cleanup before this fcport can proceed to login.
418 */
419 conflict_fcport->conflict = fcport;
420 fcport->login_pause = 1;
421 }
422
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700423 if (fcport->fc4f_nvme)
424 current_login_state = e->current_login_state >> 4;
425 else
426 current_login_state = e->current_login_state & 0xf;
427
428 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800429 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700430 ql_dbg(ql_dbg_disc, vha, 0x20e4,
431 "%s %d %8phC post gpdb\n",
432 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800433 opt = PDO_FORCE_ADISC;
434 qla24xx_post_gpdb_work(vha, fcport, opt);
435 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800436 case DSC_LS_PORT_UNAVAIL:
437 default:
438 if (fcport->loop_id == FC_NO_LOOP_ID) {
439 qla2x00_find_new_loop_id(vha, fcport);
440 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
441 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700442 ql_dbg(ql_dbg_disc, vha, 0x20e5,
443 "%s %d %8phC\n",
444 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800445 qla24xx_fcport_handle_login(vha, fcport);
446 break;
447 }
448 }
449
450 if (!found) {
451 /* fw has no record of this port */
452 if (fcport->loop_id == FC_NO_LOOP_ID) {
453 qla2x00_find_new_loop_id(vha, fcport);
454 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
455 } else {
456 for (i = 0; i < n; i++) {
457 e = &vha->gnl.l[i];
458 id.b.domain = e->port_id[0];
459 id.b.area = e->port_id[1];
460 id.b.al_pa = e->port_id[2];
461 id.b.rsvd_1 = 0;
462 loop_id = le16_to_cpu(e->nport_handle);
463
464 if (fcport->d_id.b24 == id.b24) {
465 conflict_fcport =
466 qla2x00_find_fcport_by_wwpn(vha,
467 e->port_name, 0);
468
Quinn Tran83548fe2017-06-02 09:12:01 -0700469 ql_dbg(ql_dbg_disc, vha, 0x20e6,
Quinn Tran726b8542017-01-19 22:28:00 -0800470 "%s %d %8phC post del sess\n",
471 __func__, __LINE__,
472 conflict_fcport->port_name);
473 qlt_schedule_sess_for_deletion
474 (conflict_fcport, 1);
475 }
476
477 if (fcport->loop_id == loop_id) {
478 /* FW already picked this loop id for another fcport */
479 qla2x00_find_new_loop_id(vha, fcport);
480 }
481 }
482 }
483 qla24xx_fcport_handle_login(vha, fcport);
484 }
485} /* gnl_event */
486
487static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800488qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800489{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800490 struct srb *sp = s;
491 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800492 unsigned long flags;
493 struct fc_port *fcport = NULL, *tf;
494 u16 i, n = 0, loop_id;
495 struct event_arg ea;
496 struct get_name_list_extended *e;
497 u64 wwn;
498 struct list_head h;
499
Quinn Tran83548fe2017-06-02 09:12:01 -0700500 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800501 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
502 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
503 sp->u.iocb_cmd.u.mbx.in_mb[2]);
504
505 memset(&ea, 0, sizeof(ea));
506 ea.sp = sp;
507 ea.rc = res;
508 ea.event = FCME_GNL_DONE;
509
510 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
511 sizeof(struct get_name_list_extended)) {
512 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
513 sizeof(struct get_name_list_extended);
514 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
515 }
516
517 for (i = 0; i < n; i++) {
518 e = &vha->gnl.l[i];
519 loop_id = le16_to_cpu(e->nport_handle);
520 /* mask out reserve bit */
521 loop_id = (loop_id & 0x7fff);
522 set_bit(loop_id, vha->hw->loop_id_map);
523 wwn = wwn_to_u64(e->port_name);
524
Quinn Tran83548fe2017-06-02 09:12:01 -0700525 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800526 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
527 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
528 e->port_id[0], e->current_login_state, e->last_login_state,
529 (loop_id & 0x7fff));
530 }
531
532 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
533 vha->gnl.sent = 0;
534
535 INIT_LIST_HEAD(&h);
536 fcport = tf = NULL;
537 if (!list_empty(&vha->gnl.fcports))
538 list_splice_init(&vha->gnl.fcports, &h);
539
540 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
541 list_del_init(&fcport->gnl_entry);
542 fcport->flags &= ~FCF_ASYNC_SENT;
543 ea.fcport = fcport;
544
545 qla2x00_fcport_event_handler(vha, &ea);
546 }
547
548 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
549
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800550 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800551}
552
553int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
554{
555 srb_t *sp;
556 struct srb_iocb *mbx;
557 int rval = QLA_FUNCTION_FAILED;
558 unsigned long flags;
559 u16 *mb;
560
561 if (!vha->flags.online)
562 goto done;
563
Quinn Tran83548fe2017-06-02 09:12:01 -0700564 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800565 "Async-gnlist WWPN %8phC \n", fcport->port_name);
566
567 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
568 fcport->flags |= FCF_ASYNC_SENT;
569 fcport->disc_state = DSC_GNL;
570 fcport->last_rscn_gen = fcport->rscn_gen;
571 fcport->last_login_gen = fcport->login_gen;
572
573 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
574 if (vha->gnl.sent) {
575 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
576 rval = QLA_SUCCESS;
577 goto done;
578 }
579 vha->gnl.sent = 1;
580 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
581
582 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
583 if (!sp)
584 goto done;
585 sp->type = SRB_MB_IOCB;
586 sp->name = "gnlist";
587 sp->gen1 = fcport->rscn_gen;
588 sp->gen2 = fcport->login_gen;
589
590 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
591
592 mb = sp->u.iocb_cmd.u.mbx.out_mb;
593 mb[0] = MBC_PORT_NODE_NAME_LIST;
594 mb[1] = BIT_2 | BIT_3;
595 mb[2] = MSW(vha->gnl.ldma);
596 mb[3] = LSW(vha->gnl.ldma);
597 mb[6] = MSW(MSD(vha->gnl.ldma));
598 mb[7] = LSW(MSD(vha->gnl.ldma));
599 mb[8] = vha->gnl.size;
600 mb[9] = vha->vp_idx;
601
602 mbx = &sp->u.iocb_cmd;
603 mbx->timeout = qla2x00_async_iocb_timeout;
604
605 sp->done = qla24xx_async_gnl_sp_done;
606
607 rval = qla2x00_start_sp(sp);
608 if (rval != QLA_SUCCESS)
609 goto done_free_sp;
610
Quinn Tran83548fe2017-06-02 09:12:01 -0700611 ql_dbg(ql_dbg_disc, vha, 0x20da,
612 "Async-%s - OUT WWPN %8phC hndl %x\n",
613 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800614
615 return rval;
616
617done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800618 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800619done:
620 fcport->flags &= ~FCF_ASYNC_SENT;
621 return rval;
622}
623
624int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
625{
626 struct qla_work_evt *e;
627
628 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
629 if (!e)
630 return QLA_FUNCTION_FAILED;
631
632 e->u.fcport.fcport = fcport;
633 return qla2x00_post_work(vha, e);
634}
635
636static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800637void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800638{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800639 struct srb *sp = s;
640 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800641 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800642 struct port_database_24xx *pd;
643 fc_port_t *fcport = sp->fcport;
644 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
645 int rval = QLA_SUCCESS;
646 struct event_arg ea;
647
Quinn Tran83548fe2017-06-02 09:12:01 -0700648 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800649 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
650 sp->name, res, fcport->port_name, mb[1], mb[2]);
651
652 fcport->flags &= ~FCF_ASYNC_SENT;
653
654 if (res) {
655 rval = res;
656 goto gpd_error_out;
657 }
658
659 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
660
Quinn Tran15f30a52017-03-15 09:48:52 -0700661 rval = __qla24xx_parse_gpdb(vha, fcport, pd);
Quinn Tran726b8542017-01-19 22:28:00 -0800662
663gpd_error_out:
664 memset(&ea, 0, sizeof(ea));
665 ea.event = FCME_GPDB_DONE;
666 ea.rc = rval;
667 ea.fcport = fcport;
668 ea.sp = sp;
669
670 qla2x00_fcport_event_handler(vha, &ea);
671
672 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
673 sp->u.iocb_cmd.u.mbx.in_dma);
674
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800675 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800676}
677
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700678static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
679{
680 struct qla_work_evt *e;
681
682 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
683 if (!e)
684 return QLA_FUNCTION_FAILED;
685
686 e->u.fcport.fcport = fcport;
687
688 return qla2x00_post_work(vha, e);
689}
690
691static void
692qla2x00_async_prli_sp_done(void *ptr, int res)
693{
694 srb_t *sp = ptr;
695 struct scsi_qla_host *vha = sp->vha;
696 struct srb_iocb *lio = &sp->u.iocb_cmd;
697 struct event_arg ea;
698
699 ql_dbg(ql_dbg_disc, vha, 0x2129,
700 "%s %8phC res %d \n", __func__,
701 sp->fcport->port_name, res);
702
703 sp->fcport->flags &= ~FCF_ASYNC_SENT;
704
705 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
706 memset(&ea, 0, sizeof(ea));
707 ea.event = FCME_PRLI_DONE;
708 ea.fcport = sp->fcport;
709 ea.data[0] = lio->u.logio.data[0];
710 ea.data[1] = lio->u.logio.data[1];
711 ea.iop[0] = lio->u.logio.iop[0];
712 ea.iop[1] = lio->u.logio.iop[1];
713 ea.sp = sp;
714
715 qla2x00_fcport_event_handler(vha, &ea);
716 }
717
718 sp->free(sp);
719}
720
721int
722qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
723{
724 srb_t *sp;
725 struct srb_iocb *lio;
726 int rval = QLA_FUNCTION_FAILED;
727
728 if (!vha->flags.online)
729 return rval;
730
731 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
732 fcport->fw_login_state == DSC_LS_PLOGI_COMP ||
733 fcport->fw_login_state == DSC_LS_PRLI_PEND)
734 return rval;
735
736 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
737 if (!sp)
738 return rval;
739
740 fcport->flags |= FCF_ASYNC_SENT;
741 fcport->logout_completed = 0;
742
743 sp->type = SRB_PRLI_CMD;
744 sp->name = "prli";
745 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
746
747 lio = &sp->u.iocb_cmd;
748 lio->timeout = qla2x00_async_iocb_timeout;
749 sp->done = qla2x00_async_prli_sp_done;
750 lio->u.logio.flags = 0;
751
752 if (fcport->fc4f_nvme)
753 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
754
755 rval = qla2x00_start_sp(sp);
756 if (rval != QLA_SUCCESS) {
757 fcport->flags &= ~FCF_ASYNC_SENT;
758 fcport->flags |= FCF_LOGIN_NEEDED;
759 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
760 goto done_free_sp;
761 }
762
763 ql_dbg(ql_dbg_disc, vha, 0x211b,
764 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
765 fcport->port_name, sp->handle, fcport->loop_id,
766 fcport->d_id.b24, fcport->login_retry);
767
768 return rval;
769
770done_free_sp:
771 sp->free(sp);
772 fcport->flags &= ~FCF_ASYNC_SENT;
773 return rval;
774}
775
Quinn Trana07fc0a2017-08-23 15:05:21 -0700776int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800777{
778 struct qla_work_evt *e;
779
780 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
781 if (!e)
782 return QLA_FUNCTION_FAILED;
783
784 e->u.fcport.fcport = fcport;
785 e->u.fcport.opt = opt;
786 return qla2x00_post_work(vha, e);
787}
788
789int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
790{
791 srb_t *sp;
792 struct srb_iocb *mbx;
793 int rval = QLA_FUNCTION_FAILED;
794 u16 *mb;
795 dma_addr_t pd_dma;
796 struct port_database_24xx *pd;
797 struct qla_hw_data *ha = vha->hw;
798
799 if (!vha->flags.online)
800 goto done;
801
802 fcport->flags |= FCF_ASYNC_SENT;
803 fcport->disc_state = DSC_GPDB;
804
805 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
806 if (!sp)
807 goto done;
808
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700809 sp->type = SRB_MB_IOCB;
810 sp->name = "gpdb";
811 sp->gen1 = fcport->rscn_gen;
812 sp->gen2 = fcport->login_gen;
813 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
814
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200815 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800816 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700817 ql_log(ql_log_warn, vha, 0xd043,
818 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800819 goto done_free_sp;
820 }
Quinn Tran726b8542017-01-19 22:28:00 -0800821
Quinn Tran726b8542017-01-19 22:28:00 -0800822 mb = sp->u.iocb_cmd.u.mbx.out_mb;
823 mb[0] = MBC_GET_PORT_DATABASE;
824 mb[1] = fcport->loop_id;
825 mb[2] = MSW(pd_dma);
826 mb[3] = LSW(pd_dma);
827 mb[6] = MSW(MSD(pd_dma));
828 mb[7] = LSW(MSD(pd_dma));
829 mb[9] = vha->vp_idx;
830 mb[10] = opt;
831
832 mbx = &sp->u.iocb_cmd;
833 mbx->timeout = qla2x00_async_iocb_timeout;
834 mbx->u.mbx.in = (void *)pd;
835 mbx->u.mbx.in_dma = pd_dma;
836
837 sp->done = qla24xx_async_gpdb_sp_done;
838
839 rval = qla2x00_start_sp(sp);
840 if (rval != QLA_SUCCESS)
841 goto done_free_sp;
842
Quinn Tran83548fe2017-06-02 09:12:01 -0700843 ql_dbg(ql_dbg_disc, vha, 0x20dc,
844 "Async-%s %8phC hndl %x opt %x\n",
845 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800846
847 return rval;
848
849done_free_sp:
850 if (pd)
851 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
852
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800853 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800854done:
855 fcport->flags &= ~FCF_ASYNC_SENT;
856 qla24xx_post_gpdb_work(vha, fcport, opt);
857 return rval;
858}
859
860static
861void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
862{
863 int rval = ea->rc;
864 fc_port_t *fcport = ea->fcport;
865 unsigned long flags;
866
867 fcport->flags &= ~FCF_ASYNC_SENT;
868
Quinn Tran83548fe2017-06-02 09:12:01 -0700869 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tran726b8542017-01-19 22:28:00 -0800870 "%s %8phC DS %d LS %d rval %d\n", __func__, fcport->port_name,
871 fcport->disc_state, fcport->fw_login_state, rval);
872
873 if (ea->sp->gen2 != fcport->login_gen) {
874 /* target side must have changed it. */
Quinn Tran83548fe2017-06-02 09:12:01 -0700875 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran726b8542017-01-19 22:28:00 -0800876 "%s %8phC generation changed rscn %d|%d login %d|%d \n",
877 __func__, fcport->port_name, fcport->last_rscn_gen,
878 fcport->rscn_gen, fcport->last_login_gen,
879 fcport->login_gen);
880 return;
881 } else if (ea->sp->gen1 != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700882 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -0800883 __func__, __LINE__, fcport->port_name);
884 qla24xx_post_gidpn_work(vha, fcport);
885 return;
886 }
887
888 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700889 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
Quinn Tran726b8542017-01-19 22:28:00 -0800890 __func__, __LINE__, fcport->port_name);
891 qlt_schedule_sess_for_deletion_lock(fcport);
892 return;
893 }
894
895 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
896 ea->fcport->login_gen++;
897 ea->fcport->deleted = 0;
898 ea->fcport->logout_on_delete = 1;
899
900 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
901 vha->fcport_count++;
902 ea->fcport->login_succ = 1;
903
904 if (!IS_IIDMA_CAPABLE(vha->hw) ||
905 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700906 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -0800907 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
908 __func__, __LINE__, fcport->port_name,
909 vha->fcport_count);
910
911 qla24xx_post_upd_fcport_work(vha, fcport);
912 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700913 ql_dbg(ql_dbg_disc, vha, 0x20d7,
Quinn Tran726b8542017-01-19 22:28:00 -0800914 "%s %d %8phC post gpsc fcp_cnt %d\n",
915 __func__, __LINE__, fcport->port_name,
916 vha->fcport_count);
917
918 qla24xx_post_gpsc_work(vha, fcport);
919 }
920 }
921 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
922} /* gpdb event */
923
924int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
925{
926 if (fcport->login_retry == 0)
927 return 0;
928
929 if (fcport->scan_state != QLA_FCPORT_FOUND)
930 return 0;
931
Quinn Tran83548fe2017-06-02 09:12:01 -0700932 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tran726b8542017-01-19 22:28:00 -0800933 "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d|%d retry %d lid %d\n",
934 __func__, fcport->port_name, fcport->disc_state,
935 fcport->fw_login_state, fcport->login_pause, fcport->flags,
936 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
937 fcport->last_login_gen, fcport->login_gen, fcport->login_retry,
938 fcport->loop_id);
939
940 fcport->login_retry--;
941
942 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -0800943 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
944 return 0;
945
Quinn Tran5b334692017-03-15 09:48:48 -0700946 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
947 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline))
948 return 0;
949 }
950
Quinn Tran726b8542017-01-19 22:28:00 -0800951 /* for pure Target Mode. Login will not be initiated */
952 if (vha->host->active_mode == MODE_TARGET)
953 return 0;
954
955 if (fcport->flags & FCF_ASYNC_SENT) {
956 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
957 return 0;
958 }
959
960 switch (fcport->disc_state) {
961 case DSC_DELETED:
962 if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700963 ql_dbg(ql_dbg_disc, vha, 0x20bd,
964 "%s %d %8phC post gnl\n",
965 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800966 qla24xx_async_gnl(vha, fcport);
967 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700968 ql_dbg(ql_dbg_disc, vha, 0x20bf,
969 "%s %d %8phC post login\n",
970 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800971 fcport->disc_state = DSC_LOGIN_PEND;
972 qla2x00_post_async_login_work(vha, fcport, NULL);
973 }
974 break;
975
976 case DSC_GNL:
977 if (fcport->login_pause) {
978 fcport->last_rscn_gen = fcport->rscn_gen;
979 fcport->last_login_gen = fcport->login_gen;
980 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
981 break;
982 }
983
984 if (fcport->flags & FCF_FCP2_DEVICE) {
985 u8 opt = PDO_FORCE_ADISC;
986
Quinn Tran83548fe2017-06-02 09:12:01 -0700987 ql_dbg(ql_dbg_disc, vha, 0x20c9,
988 "%s %d %8phC post gpdb\n",
989 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800990
991 fcport->disc_state = DSC_GPDB;
992 qla24xx_post_gpdb_work(vha, fcport, opt);
993 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700994 ql_dbg(ql_dbg_disc, vha, 0x20cf,
995 "%s %d %8phC post login\n",
996 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800997 fcport->disc_state = DSC_LOGIN_PEND;
998 qla2x00_post_async_login_work(vha, fcport, NULL);
999 }
1000
1001 break;
1002
1003 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001004 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1005 "%s %d %8phC post gidpn\n",
1006 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08001007
1008 qla24xx_post_gidpn_work(vha, fcport);
1009 break;
1010
1011 case DSC_LOGIN_COMPLETE:
1012 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001013 ql_dbg(ql_dbg_disc, vha, 0x20d1,
1014 "%s %d %8phC post gpdb\n",
1015 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -08001016
1017 qla24xx_post_gpdb_work(vha, fcport, PDO_FORCE_ADISC);
1018 break;
1019
1020 default:
1021 break;
1022 }
1023
1024 return 0;
1025}
1026
1027static
1028void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1029{
1030 fcport->rscn_gen++;
1031
Quinn Tran83548fe2017-06-02 09:12:01 -07001032 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1033 "%s %8phC DS %d LS %d\n",
1034 __func__, fcport->port_name, fcport->disc_state,
1035 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001036
1037 if (fcport->flags & FCF_ASYNC_SENT)
1038 return;
1039
1040 switch (fcport->disc_state) {
1041 case DSC_DELETED:
1042 case DSC_LOGIN_COMPLETE:
1043 qla24xx_post_gidpn_work(fcport->vha, fcport);
1044 break;
1045
1046 default:
1047 break;
1048 }
1049}
1050
1051int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
1052 u8 *port_name, void *pla)
1053{
1054 struct qla_work_evt *e;
1055 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1056 if (!e)
1057 return QLA_FUNCTION_FAILED;
1058
1059 e->u.new_sess.id = *id;
1060 e->u.new_sess.pla = pla;
1061 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
1062
1063 return qla2x00_post_work(vha, e);
1064}
1065
1066static
1067int qla24xx_handle_delete_done_event(scsi_qla_host_t *vha,
1068 struct event_arg *ea)
1069{
1070 fc_port_t *fcport = ea->fcport;
1071
1072 if (test_bit(UNLOADING, &vha->dpc_flags))
1073 return 0;
1074
1075 switch (vha->host->active_mode) {
1076 case MODE_INITIATOR:
1077 case MODE_DUAL:
1078 if (fcport->scan_state == QLA_FCPORT_FOUND)
1079 qla24xx_fcport_handle_login(vha, fcport);
1080 break;
1081
1082 case MODE_TARGET:
1083 default:
1084 /* no-op */
1085 break;
1086 }
1087
1088 return 0;
1089}
1090
1091static
1092void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1093 struct event_arg *ea)
1094{
1095 fc_port_t *fcport = ea->fcport;
1096
1097 if (fcport->scan_state != QLA_FCPORT_FOUND) {
1098 fcport->login_retry++;
1099 return;
1100 }
1101
Quinn Tran83548fe2017-06-02 09:12:01 -07001102 ql_dbg(ql_dbg_disc, vha, 0x2102,
1103 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1104 __func__, fcport->port_name, fcport->disc_state,
1105 fcport->fw_login_state, fcport->login_pause,
1106 fcport->deleted, fcport->conflict,
1107 fcport->last_rscn_gen, fcport->rscn_gen,
1108 fcport->last_login_gen, fcport->login_gen,
1109 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001110
1111 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001112 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1113 return;
1114
Quinn Tran5b334692017-03-15 09:48:48 -07001115 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1116 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline))
1117 return;
1118 }
1119
Quinn Tran726b8542017-01-19 22:28:00 -08001120 if (fcport->flags & FCF_ASYNC_SENT) {
1121 fcport->login_retry++;
1122 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1123 return;
1124 }
1125
1126 if (fcport->disc_state == DSC_DELETE_PEND) {
1127 fcport->login_retry++;
1128 return;
1129 }
1130
1131 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001132 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001133 __func__, __LINE__, fcport->port_name);
1134
1135 qla24xx_async_gidpn(vha, fcport);
1136 return;
1137 }
1138
1139 qla24xx_fcport_handle_login(vha, fcport);
1140}
1141
Quinn Tran41dc5292017-01-19 22:28:03 -08001142void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001143{
Quinn Tran41dc5292017-01-19 22:28:03 -08001144 fc_port_t *fcport, *f, *tf;
1145 uint32_t id = 0, mask, rid;
Quinn Tran726b8542017-01-19 22:28:00 -08001146 int rc;
1147
1148 switch (ea->event) {
1149 case FCME_RELOGIN:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001150 case FCME_RSCN:
1151 case FCME_GIDPN_DONE:
1152 case FCME_GPSC_DONE:
1153 case FCME_GPNID_DONE:
1154 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1155 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1156 return;
1157 break;
1158 default:
1159 break;
1160 }
1161
1162 switch (ea->event) {
1163 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001164 if (test_bit(UNLOADING, &vha->dpc_flags))
1165 return;
1166
1167 qla24xx_handle_relogin_event(vha, ea);
1168 break;
1169 case FCME_RSCN:
1170 if (test_bit(UNLOADING, &vha->dpc_flags))
1171 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001172 switch (ea->id.b.rsvd_1) {
1173 case RSCN_PORT_ADDR:
1174 fcport = qla2x00_find_fcport_by_nportid(vha, &ea->id, 1);
1175 if (!fcport) {
1176 /* cable moved */
1177 rc = qla24xx_post_gpnid_work(vha, &ea->id);
1178 if (rc) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001179 ql_log(ql_log_warn, vha, 0xd044,
1180 "RSCN GPNID work failed %02x%02x%02x\n",
1181 ea->id.b.domain, ea->id.b.area,
1182 ea->id.b.al_pa);
Quinn Tran41dc5292017-01-19 22:28:03 -08001183 }
1184 } else {
1185 ea->fcport = fcport;
1186 qla24xx_handle_rscn_event(fcport, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001187 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001188 break;
1189 case RSCN_AREA_ADDR:
1190 case RSCN_DOM_ADDR:
1191 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1192 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001193 ql_dbg(ql_dbg_async, vha, 0x5044,
1194 "RSCN: Area 0x%06x was affected\n",
1195 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001196 } else {
1197 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001198 ql_dbg(ql_dbg_async, vha, 0x507a,
1199 "RSCN: Domain 0x%06x was affected\n",
1200 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001201 }
1202
1203 rid = ea->id.b24 & mask;
1204 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1205 list) {
1206 id = f->d_id.b24 & mask;
1207 if (rid == id) {
1208 ea->fcport = f;
1209 qla24xx_handle_rscn_event(f, ea);
1210 }
1211 }
1212 break;
1213 case RSCN_FAB_ADDR:
1214 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001215 ql_log(ql_log_warn, vha, 0xd045,
1216 "RSCN: Fabric was affected. Addr format %d\n",
1217 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001218 qla2x00_mark_all_devices_lost(vha, 1);
1219 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1220 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001221 }
1222 break;
1223 case FCME_GIDPN_DONE:
1224 qla24xx_handle_gidpn_event(vha, ea);
1225 break;
1226 case FCME_GNL_DONE:
1227 qla24xx_handle_gnl_done_event(vha, ea);
1228 break;
1229 case FCME_GPSC_DONE:
1230 qla24xx_post_upd_fcport_work(vha, ea->fcport);
1231 break;
1232 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1233 qla24xx_handle_plogi_done_event(vha, ea);
1234 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001235 case FCME_PRLI_DONE:
1236 qla24xx_handle_prli_done_event(vha, ea);
1237 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001238 case FCME_GPDB_DONE:
1239 qla24xx_handle_gpdb_event(vha, ea);
1240 break;
1241 case FCME_GPNID_DONE:
1242 qla24xx_handle_gpnid_event(vha, ea);
1243 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001244 case FCME_GFFID_DONE:
1245 qla24xx_handle_gffid_event(vha, ea);
1246 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001247 case FCME_DELETE_DONE:
1248 qla24xx_handle_delete_done_event(vha, ea);
1249 break;
1250 default:
1251 BUG_ON(1);
1252 break;
1253 }
1254}
1255
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001256static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001257qla2x00_tmf_iocb_timeout(void *data)
1258{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001259 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001260 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1261
1262 tmf->u.tmf.comp_status = CS_TIMEOUT;
1263 complete(&tmf->u.tmf.comp);
1264}
1265
1266static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001267qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001268{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001269 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001270 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001271
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001272 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001273}
1274
1275int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001276qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001277 uint32_t tag)
1278{
1279 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001280 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001281 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001282 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001283
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001284 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001285 if (!sp)
1286 goto done;
1287
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001288 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001289 sp->type = SRB_TM_CMD;
1290 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001291 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1292 tm_iocb->u.tmf.flags = flags;
1293 tm_iocb->u.tmf.lun = lun;
1294 tm_iocb->u.tmf.data = tag;
1295 sp->done = qla2x00_tmf_sp_done;
1296 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1297 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001298
1299 rval = qla2x00_start_sp(sp);
1300 if (rval != QLA_SUCCESS)
1301 goto done_free_sp;
1302
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001303 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001304 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1305 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1306 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001307
1308 wait_for_completion(&tm_iocb->u.tmf.comp);
1309
1310 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1311 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1312
1313 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1314 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1315 "TM IOCB failed (%x).\n", rval);
1316 }
1317
1318 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1319 flags = tm_iocb->u.tmf.flags;
1320 lun = (uint16_t)tm_iocb->u.tmf.lun;
1321
1322 /* Issue Marker IOCB */
1323 qla2x00_marker(vha, vha->hw->req_q_map[0],
1324 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1325 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1326 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001327
1328done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001329 sp->free(sp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001330done:
1331 return rval;
1332}
1333
Armen Baloyan4440e462014-02-26 04:15:18 -05001334static void
1335qla24xx_abort_iocb_timeout(void *data)
1336{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001337 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001338 struct srb_iocb *abt = &sp->u.iocb_cmd;
1339
1340 abt->u.abt.comp_status = CS_TIMEOUT;
1341 complete(&abt->u.abt.comp);
1342}
1343
1344static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001345qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001346{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001347 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001348 struct srb_iocb *abt = &sp->u.iocb_cmd;
1349
1350 complete(&abt->u.abt.comp);
1351}
1352
Quinn Tran15f30a52017-03-15 09:48:52 -07001353int
Armen Baloyan4440e462014-02-26 04:15:18 -05001354qla24xx_async_abort_cmd(srb_t *cmd_sp)
1355{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001356 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001357 fc_port_t *fcport = cmd_sp->fcport;
1358 struct srb_iocb *abt_iocb;
1359 srb_t *sp;
1360 int rval = QLA_FUNCTION_FAILED;
1361
1362 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1363 if (!sp)
1364 goto done;
1365
1366 abt_iocb = &sp->u.iocb_cmd;
1367 sp->type = SRB_ABT_CMD;
1368 sp->name = "abort";
1369 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1370 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1371 sp->done = qla24xx_abort_sp_done;
1372 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1373 init_completion(&abt_iocb->u.abt.comp);
1374
1375 rval = qla2x00_start_sp(sp);
1376 if (rval != QLA_SUCCESS)
1377 goto done_free_sp;
1378
1379 ql_dbg(ql_dbg_async, vha, 0x507c,
1380 "Abort command issued - hdl=%x, target_id=%x\n",
1381 cmd_sp->handle, fcport->tgt_id);
1382
1383 wait_for_completion(&abt_iocb->u.abt.comp);
1384
1385 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1386 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1387
1388done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001389 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001390done:
1391 return rval;
1392}
1393
1394int
1395qla24xx_async_abort_command(srb_t *sp)
1396{
1397 unsigned long flags = 0;
1398
1399 uint32_t handle;
1400 fc_port_t *fcport = sp->fcport;
1401 struct scsi_qla_host *vha = fcport->vha;
1402 struct qla_hw_data *ha = vha->hw;
1403 struct req_que *req = vha->req;
1404
1405 spin_lock_irqsave(&ha->hardware_lock, flags);
1406 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1407 if (req->outstanding_cmds[handle] == sp)
1408 break;
1409 }
1410 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1411 if (handle == req->num_outstanding_cmds) {
1412 /* Command not found. */
1413 return QLA_FUNCTION_FAILED;
1414 }
1415 if (sp->type == SRB_FXIOCB_DCMD)
1416 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1417 FXDISC_ABORT_IOCTL);
1418
1419 return qla24xx_async_abort_cmd(sp);
1420}
1421
Quinn Tran726b8542017-01-19 22:28:00 -08001422static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001423qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1424{
1425 switch (ea->data[0]) {
1426 case MBS_COMMAND_COMPLETE:
1427 ql_dbg(ql_dbg_disc, vha, 0x2118,
1428 "%s %d %8phC post gpdb\n",
1429 __func__, __LINE__, ea->fcport->port_name);
1430
1431 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1432 ea->fcport->logout_on_delete = 1;
1433 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1434 break;
1435 default:
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001436 if (ea->fcport->n2n_flag) {
1437 ql_dbg(ql_dbg_disc, vha, 0x2118,
1438 "%s %d %8phC post fc4 prli\n",
1439 __func__, __LINE__, ea->fcport->port_name);
1440 ea->fcport->fc4f_nvme = 0;
1441 ea->fcport->n2n_flag = 0;
1442 qla24xx_post_prli_work(vha, ea->fcport);
1443 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001444 ql_dbg(ql_dbg_disc, vha, 0x2119,
1445 "%s %d %8phC unhandle event of %x\n",
1446 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1447 break;
1448 }
1449}
1450
1451static void
Quinn Tran726b8542017-01-19 22:28:00 -08001452qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001453{
Quinn Tran726b8542017-01-19 22:28:00 -08001454 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001455 u16 lid;
1456 struct fc_port *conflict_fcport;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001457
Quinn Tran726b8542017-01-19 22:28:00 -08001458 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001459 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001460 /*
1461 * Driver must validate login state - If PRLI not complete,
1462 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1463 * requests.
1464 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001465 if (ea->fcport->fc4f_nvme) {
1466 ql_dbg(ql_dbg_disc, vha, 0x2117,
1467 "%s %d %8phC post prli\n",
1468 __func__, __LINE__, ea->fcport->port_name);
1469 qla24xx_post_prli_work(vha, ea->fcport);
1470 } else {
1471 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001472 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1473 __func__, __LINE__, ea->fcport->port_name,
1474 ea->fcport->loop_id, ea->fcport->d_id.b24);
1475
1476 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1477 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001478 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1479 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001480 ea->fcport->send_els_logo = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001481 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1482 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001483 break;
1484 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001485 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001486 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1487
1488 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1489 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1490 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001491 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1492 else
Quinn Tran726b8542017-01-19 22:28:00 -08001493 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001494 break;
1495 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001496 /* data[1] = IO PARAM 1 = nport ID */
1497 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1498 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1499 cid.b.al_pa = ea->iop[1] & 0xff;
1500 cid.b.rsvd_1 = 0;
1501
Quinn Tran83548fe2017-06-02 09:12:01 -07001502 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1503 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1504 __func__, __LINE__, ea->fcport->port_name,
1505 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001506
1507 if (IS_SW_RESV_ADDR(cid)) {
1508 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1509 ea->fcport->loop_id = FC_NO_LOOP_ID;
1510 } else {
1511 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001512 }
Quinn Tran726b8542017-01-19 22:28:00 -08001513 qla24xx_post_gnl_work(vha, ea->fcport);
1514 break;
1515 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001516 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1517 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1518 __func__, __LINE__, ea->fcport->port_name,
1519 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1520 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001521
Quinn Trana084fd62017-12-04 14:45:00 -08001522 lid = ea->iop[1] & 0xffff;
1523 qlt_find_sess_invalidate_other(vha,
1524 wwn_to_u64(ea->fcport->port_name),
1525 ea->fcport->d_id, lid, &conflict_fcport);
1526
1527 if (conflict_fcport) {
1528 /*
1529 * Another fcport share the same loop_id/nport id.
1530 * Conflict fcport needs to finish cleanup before this
1531 * fcport can proceed to login.
1532 */
1533 conflict_fcport->conflict = ea->fcport;
1534 ea->fcport->login_pause = 1;
1535
1536 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1537 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1538 __func__, __LINE__, ea->fcport->port_name,
1539 ea->fcport->d_id.b24, lid);
1540 qla2x00_clear_loop_id(ea->fcport);
1541 qla24xx_post_gidpn_work(vha, ea->fcport);
1542 } else {
1543 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1544 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1545 __func__, __LINE__, ea->fcport->port_name,
1546 ea->fcport->d_id.b24, lid);
1547
1548 qla2x00_clear_loop_id(ea->fcport);
1549 set_bit(lid, vha->hw->loop_id_map);
1550 ea->fcport->loop_id = lid;
1551 ea->fcport->keep_nport_handle = 0;
1552 qlt_schedule_sess_for_deletion(ea->fcport, false);
1553 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001554 break;
1555 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001556 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001557}
1558
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001559void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001560qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1561 uint16_t *data)
1562{
Quinn Tran726b8542017-01-19 22:28:00 -08001563 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001564 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001565 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001566 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001567}
1568
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001569void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001570qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1571 uint16_t *data)
1572{
1573 if (data[0] == MBS_COMMAND_COMPLETE) {
1574 qla2x00_update_fcport(vha, fcport);
1575
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001576 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001577 }
1578
1579 /* Retry login. */
1580 fcport->flags &= ~FCF_ASYNC_SENT;
1581 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1582 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1583 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001584 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001585
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001586 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001587}
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/****************************************************************************/
1590/* QLogic ISP2x00 Hardware Support Functions. */
1591/****************************************************************************/
1592
Saurav Kashyapfa492632012-11-21 02:40:29 -05001593static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001594qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1595{
1596 int rval = QLA_SUCCESS;
1597 struct qla_hw_data *ha = vha->hw;
1598 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001599 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001600
1601 qla83xx_idc_lock(vha, 0);
1602
1603 /* SV: TODO: Assign initialization timeout from
1604 * flash-info / other param
1605 */
1606 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1607 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1608
1609 /* Set our fcoe function presence */
1610 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1611 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1612 "Error while setting DRV-Presence.\n");
1613 rval = QLA_FUNCTION_FAILED;
1614 goto exit;
1615 }
1616
1617 /* Decide the reset ownership */
1618 qla83xx_reset_ownership(vha);
1619
1620 /*
1621 * On first protocol driver load:
1622 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1623 * register.
1624 * Others: Check compatibility with current IDC Major version.
1625 */
1626 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1627 if (ha->flags.nic_core_reset_owner) {
1628 /* Set IDC Major version */
1629 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1630 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1631
1632 /* Clearing IDC-Lock-Recovery register */
1633 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1634 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1635 /*
1636 * Clear further IDC participation if we are not compatible with
1637 * the current IDC Major Version.
1638 */
1639 ql_log(ql_log_warn, vha, 0xb07d,
1640 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1641 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1642 __qla83xx_clear_drv_presence(vha);
1643 rval = QLA_FUNCTION_FAILED;
1644 goto exit;
1645 }
1646 /* Each function sets its supported Minor version. */
1647 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1648 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1649 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1650
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001651 if (ha->flags.nic_core_reset_owner) {
1652 memset(config, 0, sizeof(config));
1653 if (!qla81xx_get_port_config(vha, config))
1654 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1655 QLA8XXX_DEV_READY);
1656 }
1657
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001658 rval = qla83xx_idc_state_handler(vha);
1659
1660exit:
1661 qla83xx_idc_unlock(vha, 0);
1662
1663 return rval;
1664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/*
1667* qla2x00_initialize_adapter
1668* Initialize board.
1669*
1670* Input:
1671* ha = adapter block pointer.
1672*
1673* Returns:
1674* 0 = success
1675*/
1676int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001677qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001680 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001681 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001682
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001683 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1684 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001687 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001688 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001689 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001690 ha->flags.pci_channel_io_perm_failure = 0;
1691 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001692 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001693 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1694 atomic_set(&vha->loop_state, LOOP_DOWN);
1695 vha->device_flags = DFLG_NO_CABLE;
1696 vha->dpc_flags = 0;
1697 vha->flags.management_server_logged_in = 0;
1698 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 ha->isp_abort_cnt = 0;
1700 ha->beacon_blink_led = 0;
1701
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001702 set_bit(0, ha->req_qid_map);
1703 set_bit(0, ha->rsp_qid_map);
1704
Chad Dupuiscfb09192011-11-18 09:03:07 -08001705 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001706 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001707 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001709 ql_log(ql_log_warn, vha, 0x0044,
1710 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return (rval);
1712 }
1713
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001714 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001716 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001717 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001718 ql_log(ql_log_fatal, vha, 0x004f,
1719 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001720 return rval;
1721 }
1722
1723 if (IS_QLA8044(ha)) {
1724 qla8044_read_reset_template(vha);
1725
1726 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1727 * If DONRESET_BIT0 is set, drivers should not set dev_state
1728 * to NEED_RESET. But if NEED_RESET is set, drivers should
1729 * should honor the reset. */
1730 if (ql2xdontresethba == 1)
1731 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001732 }
1733
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001734 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001735 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001736 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001737
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001738 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001740 if (ha->flags.disable_serdes) {
1741 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001742 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001743 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001744 return QLA_FUNCTION_FAILED;
1745 }
1746
Chad Dupuiscfb09192011-11-18 09:03:07 -08001747 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001748 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001750 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1751 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001752 if (rval)
1753 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001754 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001755 if (rval)
1756 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001758
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001759 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001760 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001761 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001762 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001763 "Unable to configure ISP84XX.\n");
1764 return QLA_FUNCTION_FAILED;
1765 }
1766 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001767
Quinn Tranead03852017-01-19 22:28:01 -08001768 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001769 rval = qla2x00_init_rings(vha);
1770
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001771 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001773 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001774 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001775 rval = qla84xx_init_chip(vha);
1776 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001777 ql_log(ql_log_warn, vha, 0x00d4,
1778 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07001779 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001780 }
1781 }
1782
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001783 /* Load the NIC Core f/w if we are the first protocol driver. */
1784 if (IS_QLA8031(ha)) {
1785 rval = qla83xx_nic_core_fw_load(vha);
1786 if (rval)
1787 ql_log(ql_log_warn, vha, 0x0124,
1788 "Error in initializing NIC Core f/w.\n");
1789 }
1790
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05001791 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
1792 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07001793
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04001794 if (IS_P3P_TYPE(ha))
1795 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
1796 else
1797 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return (rval);
1800}
1801
1802/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001803 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * @ha: HA context
1805 *
1806 * Returns 0 on success.
1807 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001808int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001809qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Andrew Vasqueza157b102007-05-07 07:43:01 -07001811 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001812 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001813 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001814 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07001817 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07001820 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1822
Andrew Vasquez737faec2008-10-24 15:13:45 -07001823 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001825 /* Get PCI bus information. */
1826 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07001827 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001828 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1829
1830 return QLA_SUCCESS;
1831}
1832
1833/**
1834 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
1835 * @ha: HA context
1836 *
1837 * Returns 0 on success.
1838 */
1839int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001840qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001841{
Andrew Vasqueza157b102007-05-07 07:43:01 -07001842 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001843 unsigned long flags = 0;
1844 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001845 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001846 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001847
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001848 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07001849 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001850
1851 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07001852 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001853
1854 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1855 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07001856 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001857
1858 /*
1859 * If this is a 2300 card and not 2312, reset the
1860 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
1861 * the 2310 also reports itself as a 2300 so we need to get the
1862 * fb revision level -- a 6 indicates it really is a 2300 and
1863 * not a 2310.
1864 */
1865 if (IS_QLA2300(ha)) {
1866 spin_lock_irqsave(&ha->hardware_lock, flags);
1867
1868 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001869 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001870 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07001871 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001872 break;
1873
1874 udelay(10);
1875 }
1876
1877 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001878 WRT_REG_WORD(&reg->ctrl_status, 0x20);
1879 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001880
1881 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001882 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001883
1884 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07001885 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001886
1887 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001888 WRT_REG_WORD(&reg->ctrl_status, 0x0);
1889 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001890
1891 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001892 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001893 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07001894 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001895 break;
1896
1897 udelay(10);
1898 }
1899
1900 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1901 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001902
1903 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
1904
Andrew Vasquez737faec2008-10-24 15:13:45 -07001905 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001906
1907 /* Get PCI bus information. */
1908 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07001909 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001910 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1911
1912 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001916 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
1917 * @ha: HA context
1918 *
1919 * Returns 0 on success.
1920 */
1921int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001922qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001923{
Andrew Vasqueza157b102007-05-07 07:43:01 -07001924 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001925 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001926 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001927 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001928
1929 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07001930 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001931
1932 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07001933 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001934 w &= ~PCI_COMMAND_INTX_DISABLE;
1935 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1936
1937 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
1938
1939 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07001940 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
1941 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001942
1943 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07001944 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04001945 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001946
Andrew Vasquez737faec2008-10-24 15:13:45 -07001947 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001948
Auke Kok44c10132007-06-08 15:46:36 -07001949 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08001950
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001951 /* Get PCI bus information. */
1952 spin_lock_irqsave(&ha->hardware_lock, flags);
1953 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
1954 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1955
1956 return QLA_SUCCESS;
1957}
1958
1959/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001960 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
1961 * @ha: HA context
1962 *
1963 * Returns 0 on success.
1964 */
1965int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001966qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001967{
1968 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001969 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001970
1971 pci_set_master(ha->pdev);
1972 pci_try_set_mwi(ha->pdev);
1973
1974 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
1975 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1976 w &= ~PCI_COMMAND_INTX_DISABLE;
1977 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1978
1979 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07001980 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04001981 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001982
Andrew Vasquez737faec2008-10-24 15:13:45 -07001983 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001984
1985 ha->chip_revision = ha->pdev->revision;
1986
1987 return QLA_SUCCESS;
1988}
1989
1990/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 * qla2x00_isp_firmware() - Choose firmware image.
1992 * @ha: HA context
1993 *
1994 * Returns 0 on success.
1995 */
1996static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001997qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002000 uint16_t loop_id, topo, sw_cap;
2001 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002002 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002005 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002008 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002011 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002012 if (rval == QLA_SUCCESS) {
2013 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002014 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002015 &area, &domain, &topo, &sw_cap);
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002019 if (rval)
2020 ql_dbg(ql_dbg_init, vha, 0x007a,
2021 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 return (rval);
2024}
2025
2026/**
2027 * qla2x00_reset_chip() - Reset ISP chip.
2028 * @ha: HA context
2029 *
2030 * Returns 0 on success.
2031 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002032void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002033qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002036 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002037 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 uint16_t cmd;
2040
Andrew Vasquez85880802009-12-15 21:29:46 -08002041 if (unlikely(pci_channel_offline(ha->pdev)))
2042 return;
2043
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002044 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 spin_lock_irqsave(&ha->hardware_lock, flags);
2047
2048 /* Turn off master enable */
2049 cmd = 0;
2050 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2051 cmd &= ~PCI_COMMAND_MASTER;
2052 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2053
2054 if (!IS_QLA2100(ha)) {
2055 /* Pause RISC. */
2056 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2057 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2058 for (cnt = 0; cnt < 30000; cnt++) {
2059 if ((RD_REG_WORD(&reg->hccr) &
2060 HCCR_RISC_PAUSE) != 0)
2061 break;
2062 udelay(100);
2063 }
2064 } else {
2065 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2066 udelay(10);
2067 }
2068
2069 /* Select FPM registers. */
2070 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2071 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2072
2073 /* FPM Soft Reset. */
2074 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2075 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2076
2077 /* Toggle Fpm Reset. */
2078 if (!IS_QLA2200(ha)) {
2079 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2080 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2081 }
2082
2083 /* Select frame buffer registers. */
2084 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2085 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2086
2087 /* Reset frame buffer FIFOs. */
2088 if (IS_QLA2200(ha)) {
2089 WRT_FB_CMD_REG(ha, reg, 0xa000);
2090 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2091 } else {
2092 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2093
2094 /* Read back fb_cmd until zero or 3 seconds max */
2095 for (cnt = 0; cnt < 3000; cnt++) {
2096 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2097 break;
2098 udelay(100);
2099 }
2100 }
2101
2102 /* Select RISC module registers. */
2103 WRT_REG_WORD(&reg->ctrl_status, 0);
2104 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2105
2106 /* Reset RISC processor. */
2107 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2108 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2109
2110 /* Release RISC processor. */
2111 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2112 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2113 }
2114
2115 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2116 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2117
2118 /* Reset ISP chip. */
2119 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2120
2121 /* Wait for RISC to recover from reset. */
2122 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2123 /*
2124 * It is necessary to for a delay here since the card doesn't
2125 * respond to PCI reads during a reset. On some architectures
2126 * this will result in an MCA.
2127 */
2128 udelay(20);
2129 for (cnt = 30000; cnt; cnt--) {
2130 if ((RD_REG_WORD(&reg->ctrl_status) &
2131 CSR_ISP_SOFT_RESET) == 0)
2132 break;
2133 udelay(100);
2134 }
2135 } else
2136 udelay(10);
2137
2138 /* Reset RISC processor. */
2139 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2140
2141 WRT_REG_WORD(&reg->semaphore, 0);
2142
2143 /* Release RISC processor. */
2144 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2145 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2146
2147 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2148 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002149 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 udelay(100);
2153 }
2154 } else
2155 udelay(100);
2156
2157 /* Turn on master enable */
2158 cmd |= PCI_COMMAND_MASTER;
2159 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2160
2161 /* Disable RISC pause on FPM parity error. */
2162 if (!IS_QLA2100(ha)) {
2163 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2164 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2165 }
2166
2167 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2168}
2169
2170/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002171 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2172 *
2173 * Returns 0 on success.
2174 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002175static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002176qla81xx_reset_mpi(scsi_qla_host_t *vha)
2177{
2178 uint16_t mb[4] = {0x1010, 0, 1, 0};
2179
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002180 if (!IS_QLA81XX(vha->hw))
2181 return QLA_SUCCESS;
2182
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002183 return qla81xx_write_mpi_register(vha, mb);
2184}
2185
2186/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002187 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002188 * @ha: HA context
2189 *
2190 * Returns 0 on success.
2191 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002192static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002193qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002194{
2195 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002196 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002197 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002198 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002199 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002200 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002201 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002202
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002203 spin_lock_irqsave(&ha->hardware_lock, flags);
2204
2205 /* Reset RISC. */
2206 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2207 for (cnt = 0; cnt < 30000; cnt++) {
2208 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2209 break;
2210
2211 udelay(10);
2212 }
2213
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002214 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2215 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2216
2217 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2218 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2219 RD_REG_DWORD(&reg->hccr),
2220 RD_REG_DWORD(&reg->ctrl_status),
2221 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2222
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002223 WRT_REG_DWORD(&reg->ctrl_status,
2224 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002225 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002226
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002227 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002228
Andrew Vasquez88c26662005-07-08 17:59:26 -07002229 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002230 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002231 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2232 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002233 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002234 if (cnt)
2235 udelay(5);
2236 else
2237 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002238 }
2239
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002240 if (rval == QLA_SUCCESS)
2241 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2242
2243 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2244 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2245 RD_REG_DWORD(&reg->hccr),
2246 RD_REG_DWORD(&reg->mailbox0));
2247
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002248 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002249 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002250 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002251 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002252 if ((RD_REG_DWORD(&reg->ctrl_status) &
2253 CSRX_ISP_SOFT_RESET) == 0)
2254 break;
2255
2256 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002257 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002258 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2259 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2260
2261 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2262 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2263 RD_REG_DWORD(&reg->hccr),
2264 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002265
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002266 /* If required, do an MPI FW reset now */
2267 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2268 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2269 if (++abts_cnt < 5) {
2270 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2271 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2272 } else {
2273 /*
2274 * We exhausted the ISP abort retries. We have to
2275 * set the board offline.
2276 */
2277 abts_cnt = 0;
2278 vha->flags.online = 0;
2279 }
2280 }
2281 }
2282
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002283 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2284 RD_REG_DWORD(&reg->hccr);
2285
2286 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2287 RD_REG_DWORD(&reg->hccr);
2288
2289 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2290 RD_REG_DWORD(&reg->hccr);
2291
Bart Van Assche52c82822015-07-09 07:23:26 -07002292 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002293 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002294 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002295 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002296 if (cnt)
2297 udelay(5);
2298 else
2299 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002300 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002301 if (rval == QLA_SUCCESS)
2302 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2303
2304 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2305 "Host Risc 0x%x, mailbox0 0x%x\n",
2306 RD_REG_DWORD(&reg->hccr),
2307 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002308
2309 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002310
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002311 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2312 "Driver in %s mode\n",
2313 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2314
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002315 if (IS_NOPOLLING_TYPE(ha))
2316 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002317
2318 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002319}
2320
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002321static void
2322qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2323{
2324 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2325
2326 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2327 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2328
2329}
2330
2331static void
2332qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2333{
2334 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2335
2336 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2337 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2338}
2339
2340static void
2341qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2342{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002343 uint32_t wd32 = 0;
2344 uint delta_msec = 100;
2345 uint elapsed_msec = 0;
2346 uint timeout_msec;
2347 ulong n;
2348
Joe Carnucciocc790762015-08-04 13:37:53 -04002349 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2350 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002351 return;
2352
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002353 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2354 udelay(100);
2355
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002356attempt:
2357 timeout_msec = TIMEOUT_SEMAPHORE;
2358 n = timeout_msec / delta_msec;
2359 while (n--) {
2360 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2361 qla25xx_read_risc_sema_reg(vha, &wd32);
2362 if (wd32 & RISC_SEMAPHORE)
2363 break;
2364 msleep(delta_msec);
2365 elapsed_msec += delta_msec;
2366 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2367 goto force;
2368 }
2369
2370 if (!(wd32 & RISC_SEMAPHORE))
2371 goto force;
2372
2373 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2374 goto acquired;
2375
2376 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2377 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2378 n = timeout_msec / delta_msec;
2379 while (n--) {
2380 qla25xx_read_risc_sema_reg(vha, &wd32);
2381 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2382 break;
2383 msleep(delta_msec);
2384 elapsed_msec += delta_msec;
2385 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2386 goto force;
2387 }
2388
2389 if (wd32 & RISC_SEMAPHORE_FORCE)
2390 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2391
2392 goto attempt;
2393
2394force:
2395 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2396
2397acquired:
2398 return;
2399}
2400
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002401/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002402 * qla24xx_reset_chip() - Reset ISP24xx chip.
2403 * @ha: HA context
2404 *
2405 * Returns 0 on success.
2406 */
2407void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002408qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002409{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002410 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002411
2412 if (pci_channel_offline(ha->pdev) &&
2413 ha->flags.pci_channel_io_perm_failure) {
2414 return;
2415 }
2416
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002417 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002418
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002419 qla25xx_manipulate_risc_semaphore(vha);
2420
Andrew Vasquez88c26662005-07-08 17:59:26 -07002421 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002422 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002423}
2424
2425/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 * qla2x00_chip_diag() - Test chip for proper operation.
2427 * @ha: HA context
2428 *
2429 * Returns 0 on success.
2430 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002431int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002432qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
2434 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002435 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002436 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 unsigned long flags = 0;
2438 uint16_t data;
2439 uint32_t cnt;
2440 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002441 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* Assume a failed state */
2444 rval = QLA_FUNCTION_FAILED;
2445
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002446 ql_dbg(ql_dbg_init, vha, 0x007b,
2447 "Testing device at %lx.\n", (u_long)&reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 spin_lock_irqsave(&ha->hardware_lock, flags);
2450
2451 /* Reset ISP chip. */
2452 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2453
2454 /*
2455 * We need to have a delay here since the card will not respond while
2456 * in reset causing an MCA on some architectures.
2457 */
2458 udelay(20);
2459 data = qla2x00_debounce_register(&reg->ctrl_status);
2460 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2461 udelay(5);
2462 data = RD_REG_WORD(&reg->ctrl_status);
2463 barrier();
2464 }
2465
2466 if (!cnt)
2467 goto chip_diag_failed;
2468
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002469 ql_dbg(ql_dbg_init, vha, 0x007c,
2470 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /* Reset RISC processor. */
2473 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2474 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2475
2476 /* Workaround for QLA2312 PCI parity error */
2477 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2478 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2479 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2480 udelay(5);
2481 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002482 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
2484 } else
2485 udelay(10);
2486
2487 if (!cnt)
2488 goto chip_diag_failed;
2489
2490 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002491 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2494 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2495 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2496 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2497 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2498 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002499 ql_log(ql_log_warn, vha, 0x0062,
2500 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2501 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 goto chip_diag_failed;
2504 }
2505 ha->product_id[0] = mb[1];
2506 ha->product_id[1] = mb[2];
2507 ha->product_id[2] = mb[3];
2508 ha->product_id[3] = mb[4];
2509
2510 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002511 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2513 else
2514 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002515 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 if (IS_QLA2200(ha) &&
2518 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2519 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002520 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002522 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 ha->fw_transfer_size = 128;
2524 }
2525
2526 /* Wrap Incoming Mailboxes Test. */
2527 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2528
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002529 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002530 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002531 if (rval)
2532 ql_log(ql_log_warn, vha, 0x0080,
2533 "Failed mailbox send register test.\n");
2534 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /* Flag a successful rval */
2536 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 spin_lock_irqsave(&ha->hardware_lock, flags);
2538
2539chip_diag_failed:
2540 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002541 ql_log(ql_log_info, vha, 0x0081,
2542 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2545
2546 return (rval);
2547}
2548
2549/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002550 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2551 * @ha: HA context
2552 *
2553 * Returns 0 on success.
2554 */
2555int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002556qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002557{
2558 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002559 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002560 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002561
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002562 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002563 return QLA_SUCCESS;
2564
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002565 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002566
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002567 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002568 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002569 ql_log(ql_log_warn, vha, 0x0082,
2570 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002571 } else {
2572 /* Flag a successful rval */
2573 rval = QLA_SUCCESS;
2574 }
2575
2576 return rval;
2577}
2578
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002579void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002580qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002581{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002582 int rval;
2583 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002584 eft_size, fce_size, mq_size;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002585 dma_addr_t tc_dma;
2586 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002587 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002588 struct req_que *req = ha->req_q_map[0];
2589 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002590
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002591 if (ha->fw_dump) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002592 ql_dbg(ql_dbg_init, vha, 0x00bd,
2593 "Firmware dump already allocated.\n");
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002594 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002595 }
2596
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002597 ha->fw_dumped = 0;
Hiral Patel61f098d2014-04-11 16:54:21 -04002598 ha->fw_dump_cap_flags = 0;
Chad Dupuisf73cb692014-02-26 04:15:06 -05002599 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2600 req_q_size = rsp_q_size = 0;
2601
2602 if (IS_QLA27XX(ha))
2603 goto try_fce;
2604
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002605 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2606 fixed_size = sizeof(struct qla2100_fw_dump);
2607 } else if (IS_QLA23XX(ha)) {
2608 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2609 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2610 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002611 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002612 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002613 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2614 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002615 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2616 else if (IS_QLA25XX(ha))
2617 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2618 else
2619 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002620
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002621 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2622 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002623 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002624 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002625 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002626 /*
2627 * Allocate maximum buffer size for all queues.
2628 * Resizing must be done at end-of-dump processing.
2629 */
2630 mq_size += ha->max_req_queues *
2631 (req->length * sizeof(request_t));
2632 mq_size += ha->max_rsp_queues *
2633 (rsp->length * sizeof(response_t));
2634 }
Arun Easi00876ae2013-03-25 02:21:37 -04002635 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002636 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002637 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002638 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2639 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002640 goto try_eft;
2641
Chad Dupuisf73cb692014-02-26 04:15:06 -05002642try_fce:
2643 if (ha->fce)
2644 dma_free_coherent(&ha->pdev->dev,
2645 FCE_SIZE, ha->fce, ha->fce_dma);
2646
2647 /* Allocate memory for Fibre Channel Event Buffer. */
Joe Perches0ea85b52014-06-15 13:37:51 -07002648 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2649 GFP_KERNEL);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002650 if (!tc) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002651 ql_log(ql_log_warn, vha, 0x00be,
2652 "Unable to allocate (%d KB) for FCE.\n",
2653 FCE_SIZE / 1024);
Anirban Chakraborty17d98632008-12-18 10:06:15 -08002654 goto try_eft;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002655 }
2656
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002657 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002658 ha->fce_mb, &ha->fce_bufs);
2659 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002660 ql_log(ql_log_warn, vha, 0x00bf,
2661 "Unable to initialize FCE (%d).\n", rval);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002662 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2663 tc_dma);
2664 ha->flags.fce_enabled = 0;
Anirban Chakraborty17d98632008-12-18 10:06:15 -08002665 goto try_eft;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002666 }
Chad Dupuiscfb09192011-11-18 09:03:07 -08002667 ql_dbg(ql_dbg_init, vha, 0x00c0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002668 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002669
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002670 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002671 ha->flags.fce_enabled = 1;
2672 ha->fce_dma = tc_dma;
2673 ha->fce = tc;
Chad Dupuisf73cb692014-02-26 04:15:06 -05002674
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002675try_eft:
Chad Dupuisf73cb692014-02-26 04:15:06 -05002676 if (ha->eft)
2677 dma_free_coherent(&ha->pdev->dev,
2678 EFT_SIZE, ha->eft, ha->eft_dma);
2679
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002680 /* Allocate memory for Extended Trace Buffer. */
Joe Perches0ea85b52014-06-15 13:37:51 -07002681 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2682 GFP_KERNEL);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002683 if (!tc) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002684 ql_log(ql_log_warn, vha, 0x00c1,
2685 "Unable to allocate (%d KB) for EFT.\n",
2686 EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002687 goto cont_alloc;
2688 }
2689
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002690 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002691 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002692 ql_log(ql_log_warn, vha, 0x00c2,
2693 "Unable to initialize EFT (%d).\n", rval);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002694 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2695 tc_dma);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002696 goto cont_alloc;
2697 }
Chad Dupuiscfb09192011-11-18 09:03:07 -08002698 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002699 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002700
2701 eft_size = EFT_SIZE;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002702 ha->eft_dma = tc_dma;
2703 ha->eft = tc;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002704 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002705
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002706cont_alloc:
Chad Dupuisf73cb692014-02-26 04:15:06 -05002707 if (IS_QLA27XX(ha)) {
2708 if (!ha->fw_dump_template) {
2709 ql_log(ql_log_warn, vha, 0x00ba,
2710 "Failed missing fwdump template\n");
2711 return;
2712 }
2713 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2714 ql_dbg(ql_dbg_init, vha, 0x00fa,
2715 "-> allocating fwdump (%x bytes)...\n", dump_size);
2716 goto allocate;
2717 }
2718
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002719 req_q_size = req->length * sizeof(request_t);
2720 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002721 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002722 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002723 ha->chain_offset = dump_size;
2724 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002725
Quinn Tranb945e772017-06-13 20:47:29 -07002726 if (ha->exchoffld_buf)
2727 dump_size += sizeof(struct qla2xxx_offld_chain) +
2728 ha->exchoffld_size;
2729 if (ha->exlogin_buf)
2730 dump_size += sizeof(struct qla2xxx_offld_chain) +
2731 ha->exlogin_size;
2732
Chad Dupuisf73cb692014-02-26 04:15:06 -05002733allocate:
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002734 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002735 if (!ha->fw_dump) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002736 ql_log(ql_log_warn, vha, 0x00c4,
2737 "Unable to allocate (%d KB) for firmware dump.\n",
2738 dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002739
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002740 if (ha->fce) {
2741 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2742 ha->fce_dma);
2743 ha->fce = NULL;
2744 ha->fce_dma = 0;
2745 }
2746
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002747 if (ha->eft) {
2748 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
2749 ha->eft_dma);
2750 ha->eft = NULL;
2751 ha->eft_dma = 0;
2752 }
2753 return;
2754 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002755 ha->fw_dump_len = dump_size;
Chad Dupuiscfb09192011-11-18 09:03:07 -08002756 ql_dbg(ql_dbg_init, vha, 0x00c5,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002757 "Allocated (%d KB) for firmware dump.\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002758
Chad Dupuisf73cb692014-02-26 04:15:06 -05002759 if (IS_QLA27XX(ha))
2760 return;
2761
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002762 ha->fw_dump->signature[0] = 'Q';
2763 ha->fw_dump->signature[1] = 'L';
2764 ha->fw_dump->signature[2] = 'G';
2765 ha->fw_dump->signature[3] = 'C';
Bart Van Asschead950362015-07-09 07:24:08 -07002766 ha->fw_dump->version = htonl(1);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002767
2768 ha->fw_dump->fixed_size = htonl(fixed_size);
2769 ha->fw_dump->mem_size = htonl(mem_size);
2770 ha->fw_dump->req_q_size = htonl(req_q_size);
2771 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
2772
2773 ha->fw_dump->eft_size = htonl(eft_size);
2774 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
2775 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
2776
2777 ha->fw_dump->header_size =
2778 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002779}
2780
Andrew Vasquez18e75552009-06-03 09:55:30 -07002781static int
2782qla81xx_mpi_sync(scsi_qla_host_t *vha)
2783{
2784#define MPS_MASK 0xe0
2785 int rval;
2786 uint16_t dc;
2787 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07002788
2789 if (!IS_QLA81XX(vha->hw))
2790 return QLA_SUCCESS;
2791
2792 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
2793 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002794 ql_log(ql_log_warn, vha, 0x0105,
2795 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002796 goto done;
2797 }
2798
2799 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
2800 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
2801 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002802 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002803 goto done_release;
2804 }
2805
2806 dc &= MPS_MASK;
2807 if (dc == (dw & MPS_MASK))
2808 goto done_release;
2809
2810 dw &= ~MPS_MASK;
2811 dw |= dc;
2812 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
2813 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002814 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002815 }
2816
2817done_release:
2818 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
2819 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002820 ql_log(ql_log_warn, vha, 0x006d,
2821 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002822 }
2823
2824done:
2825 return rval;
2826}
2827
Chad Dupuis8d93f552013-01-30 03:34:37 -05002828int
2829qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
2830{
2831 /* Don't try to reallocate the array */
2832 if (req->outstanding_cmds)
2833 return QLA_SUCCESS;
2834
Michael Hernandezd7459522016-12-12 14:40:07 -08002835 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05002836 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
2837 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05002838 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
2839 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002840 else
Quinn Tran03e8c682015-12-17 14:56:59 -05002841 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002842 }
2843
2844 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
2845 req->num_outstanding_cmds, GFP_KERNEL);
2846
2847 if (!req->outstanding_cmds) {
2848 /*
2849 * Try to allocate a minimal size just so we can get through
2850 * initialization.
2851 */
2852 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
2853 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
2854 req->num_outstanding_cmds, GFP_KERNEL);
2855
2856 if (!req->outstanding_cmds) {
2857 ql_log(ql_log_fatal, NULL, 0x0126,
2858 "Failed to allocate memory for "
2859 "outstanding_cmds for req_que %p.\n", req);
2860 req->num_outstanding_cmds = 0;
2861 return QLA_FUNCTION_FAILED;
2862 }
2863 }
2864
2865 return QLA_SUCCESS;
2866}
2867
Quinn Trane4e3a2c2017-08-23 15:05:07 -07002868#define PRINT_FIELD(_field, _flag, _str) { \
2869 if (a0->_field & _flag) {\
2870 if (p) {\
2871 strcat(ptr, "|");\
2872 ptr++;\
2873 leftover--;\
2874 } \
2875 len = snprintf(ptr, leftover, "%s", _str); \
2876 p = 1;\
2877 leftover -= len;\
2878 ptr += len; \
2879 } \
2880}
2881
2882static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
2883{
2884#define STR_LEN 64
2885 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
2886 u8 str[STR_LEN], *ptr, p;
2887 int leftover, len;
2888
2889 memset(str, 0, STR_LEN);
2890 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
2891 ql_dbg(ql_dbg_init, vha, 0x015a,
2892 "SFP MFG Name: %s\n", str);
2893
2894 memset(str, 0, STR_LEN);
2895 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
2896 ql_dbg(ql_dbg_init, vha, 0x015c,
2897 "SFP Part Name: %s\n", str);
2898
2899 /* media */
2900 memset(str, 0, STR_LEN);
2901 ptr = str;
2902 leftover = STR_LEN;
2903 p = len = 0;
2904 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
2905 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
2906 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
2907 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
2908 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
2909 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
2910 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
2911 ql_dbg(ql_dbg_init, vha, 0x0160,
2912 "SFP Media: %s\n", str);
2913
2914 /* link length */
2915 memset(str, 0, STR_LEN);
2916 ptr = str;
2917 leftover = STR_LEN;
2918 p = len = 0;
2919 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
2920 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
2921 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
2922 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
2923 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
2924 ql_dbg(ql_dbg_init, vha, 0x0196,
2925 "SFP Link Length: %s\n", str);
2926
2927 memset(str, 0, STR_LEN);
2928 ptr = str;
2929 leftover = STR_LEN;
2930 p = len = 0;
2931 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
2932 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
2933 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
2934 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
2935 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
2936 ql_dbg(ql_dbg_init, vha, 0x016e,
2937 "SFP FC Link Tech: %s\n", str);
2938
2939 if (a0->length_km)
2940 ql_dbg(ql_dbg_init, vha, 0x016f,
2941 "SFP Distant: %d km\n", a0->length_km);
2942 if (a0->length_100m)
2943 ql_dbg(ql_dbg_init, vha, 0x0170,
2944 "SFP Distant: %d m\n", a0->length_100m*100);
2945 if (a0->length_50um_10m)
2946 ql_dbg(ql_dbg_init, vha, 0x0189,
2947 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
2948 if (a0->length_62um_10m)
2949 ql_dbg(ql_dbg_init, vha, 0x018a,
2950 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
2951 if (a0->length_om4_10m)
2952 ql_dbg(ql_dbg_init, vha, 0x0194,
2953 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
2954 if (a0->length_om3_10m)
2955 ql_dbg(ql_dbg_init, vha, 0x0195,
2956 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
2957}
2958
2959
2960/*
2961 * Return Code:
2962 * QLA_SUCCESS: no action
2963 * QLA_INTERFACE_ERROR: SFP is not there.
2964 * QLA_FUNCTION_FAILED: detected New SFP
2965 */
2966int
2967qla24xx_detect_sfp(scsi_qla_host_t *vha)
2968{
2969 int rc = QLA_SUCCESS;
2970 struct sff_8247_a0 *a;
2971 struct qla_hw_data *ha = vha->hw;
2972
2973 if (!AUTO_DETECT_SFP_SUPPORT(vha))
2974 goto out;
2975
2976 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
2977 if (rc)
2978 goto out;
2979
2980 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
2981 qla2xxx_print_sfp_info(vha);
2982
2983 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
2984 /* long range */
2985 ha->flags.detected_lr_sfp = 1;
2986
2987 if (a->length_km > 5 || a->length_100m > 50)
2988 ha->long_range_distance = LR_DISTANCE_10K;
2989 else
2990 ha->long_range_distance = LR_DISTANCE_5K;
2991
2992 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
2993 ql_dbg(ql_dbg_async, vha, 0x507b,
2994 "Detected Long Range SFP.\n");
2995 } else {
2996 /* short range */
2997 ha->flags.detected_lr_sfp = 0;
2998 if (ha->flags.using_lr_setting)
2999 ql_dbg(ql_dbg_async, vha, 0x5084,
3000 "Detected Short Range SFP.\n");
3001 }
3002
3003 if (!vha->flags.init_done)
3004 rc = QLA_SUCCESS;
3005out:
3006 return rc;
3007}
3008
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003009/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 * qla2x00_setup_chip() - Load and start RISC firmware.
3011 * @ha: HA context
3012 *
3013 * Returns 0 on success.
3014 */
3015static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003016qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003018 int rval;
3019 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003020 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003021 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3022 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003023 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003024
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003025 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003026 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003027 if (rval == QLA_SUCCESS) {
3028 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003029 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003030 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003031 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003032 }
3033
Andrew Vasquez3db06522008-01-31 12:33:49 -08003034 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3035 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3036 spin_lock_irqsave(&ha->hardware_lock, flags);
3037 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3038 RD_REG_WORD(&reg->hccr);
3039 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Andrew Vasquez18e75552009-06-03 09:55:30 -07003042 qla81xx_mpi_sync(vha);
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003045 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003046 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003047 ql_dbg(ql_dbg_init, vha, 0x00c9,
3048 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003050 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (rval == QLA_SUCCESS) {
3052 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003053 ql_dbg(ql_dbg_init, vha, 0x00ca,
3054 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003056 if (ql2xexlogins)
3057 ha->flags.exlogins_enabled = 1;
3058
Quinn Tran99e1b682017-06-02 09:12:03 -07003059 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003060 ha->flags.exchoffld_enabled = 1;
3061
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003062 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003064 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003065 qla24xx_detect_sfp(vha);
3066
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003067 rval = qla2x00_set_exlogins_buffer(vha);
3068 if (rval != QLA_SUCCESS)
3069 goto failed;
3070
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003071 rval = qla2x00_set_exchoffld_buffer(vha);
3072 if (rval != QLA_SUCCESS)
3073 goto failed;
3074
Giridhar Malavalia9083012010-04-12 17:59:55 -07003075enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003076 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003077 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003078 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003079 else
3080 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003081 if (rval != QLA_SUCCESS)
3082 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003083 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003084 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003085 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003086 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003087 if ((!ha->max_npiv_vports) ||
3088 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003089 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003090 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003091 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003092 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003093 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003094
Chad Dupuis8d93f552013-01-30 03:34:37 -05003095 /*
3096 * Allocate the array of outstanding commands
3097 * now that we know the firmware resources.
3098 */
3099 rval = qla2x00_alloc_outstanding_cmds(ha,
3100 vha->req);
3101 if (rval != QLA_SUCCESS)
3102 goto failed;
3103
Saurav Kashyapbe5ea3c2011-11-18 09:02:11 -08003104 if (!fw_major_version && ql2xallocfwdump
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003105 && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003106 qla2x00_alloc_fw_dump(vha);
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003107 } else {
3108 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003111 ql_log(ql_log_fatal, vha, 0x00cd,
3112 "ISP Firmware failed checksum.\n");
3113 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003115 } else
3116 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Andrew Vasquez3db06522008-01-31 12:33:49 -08003118 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3119 /* Enable proper parity. */
3120 spin_lock_irqsave(&ha->hardware_lock, flags);
3121 if (IS_QLA2300(ha))
3122 /* SRAM parity */
3123 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3124 else
3125 /* SRAM, Instruction RAM and GP RAM parity */
3126 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3127 RD_REG_WORD(&reg->hccr);
3128 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3129 }
3130
Chad Dupuisf3982d82014-09-25 05:16:57 -04003131 if (IS_QLA27XX(ha))
3132 ha->flags.fac_supported = 1;
3133 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003134 uint32_t size;
3135
3136 rval = qla81xx_fac_get_sector_size(vha, &size);
3137 if (rval == QLA_SUCCESS) {
3138 ha->flags.fac_supported = 1;
3139 ha->fdt_block_size = size << 2;
3140 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003141 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003142 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3143 ha->fw_major_version, ha->fw_minor_version,
3144 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003145
Chad Dupuisf73cb692014-02-26 04:15:06 -05003146 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003147 ha->flags.fac_supported = 0;
3148 rval = QLA_SUCCESS;
3149 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003150 }
3151 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003152failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003154 ql_log(ql_log_fatal, vha, 0x00cf,
3155 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
3157
3158 return (rval);
3159}
3160
3161/**
3162 * qla2x00_init_response_q_entries() - Initializes response queue entries.
3163 * @ha: HA context
3164 *
3165 * Beginning of request ring has initialization control block already built
3166 * by nvram config routine.
3167 *
3168 * Returns 0 on success.
3169 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003170void
3171qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 uint16_t cnt;
3174 response_t *pkt;
3175
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003176 rsp->ring_ptr = rsp->ring;
3177 rsp->ring_index = 0;
3178 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003179 pkt = rsp->ring_ptr;
3180 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 pkt->signature = RESPONSE_PROCESSED;
3182 pkt++;
3183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
3186/**
3187 * qla2x00_update_fw_options() - Read and process firmware options.
3188 * @ha: HA context
3189 *
3190 * Returns 0 on success.
3191 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003192void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003193qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194{
3195 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003196 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003199 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3202 return;
3203
3204 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003205 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3206 "Serial link options.\n");
3207 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3208 (uint8_t *)&ha->fw_seriallink_options,
3209 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3212 if (ha->fw_seriallink_options[3] & BIT_2) {
3213 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3214
3215 /* 1G settings */
3216 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3217 emphasis = (ha->fw_seriallink_options[2] &
3218 (BIT_4 | BIT_3)) >> 3;
3219 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003220 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 rx_sens = (ha->fw_seriallink_options[0] &
3222 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3223 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3224 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3225 if (rx_sens == 0x0)
3226 rx_sens = 0x3;
3227 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3228 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3229 ha->fw_options[10] |= BIT_5 |
3230 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3231 (tx_sens & (BIT_1 | BIT_0));
3232
3233 /* 2G settings */
3234 swing = (ha->fw_seriallink_options[2] &
3235 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3236 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3237 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003238 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 rx_sens = (ha->fw_seriallink_options[1] &
3240 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3241 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3242 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3243 if (rx_sens == 0x0)
3244 rx_sens = 0x3;
3245 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3246 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3247 ha->fw_options[11] |= BIT_5 |
3248 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3249 (tx_sens & (BIT_1 | BIT_0));
3250 }
3251
3252 /* FCP2 options. */
3253 /* Return command IOCBs without waiting for an ABTS to complete. */
3254 ha->fw_options[3] |= BIT_13;
3255
3256 /* LED scheme. */
3257 if (ha->flags.enable_led_scheme)
3258 ha->fw_options[2] |= BIT_12;
3259
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003260 /* Detect ISP6312. */
3261 if (IS_QLA6312(ha))
3262 ha->fw_options[2] |= BIT_13;
3263
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003264 /* Set Retry FLOGI in case of P2P connection */
3265 if (ha->operating_mode == P2P) {
3266 ha->fw_options[2] |= BIT_3;
3267 ql_dbg(ql_dbg_disc, vha, 0x2100,
3268 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3269 __func__, ha->fw_options[2]);
3270 }
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003273 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003276void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003277qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003278{
3279 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003280 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003281
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003282 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003283 return;
3284
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003285 /* Hold status IOCBs until ABTS response received. */
3286 if (ql2xfwholdabts)
3287 ha->fw_options[3] |= BIT_12;
3288
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003289 /* Set Retry FLOGI in case of P2P connection */
3290 if (ha->operating_mode == P2P) {
3291 ha->fw_options[2] |= BIT_3;
3292 ql_dbg(ql_dbg_disc, vha, 0x2101,
3293 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3294 __func__, ha->fw_options[2]);
3295 }
3296
Quinn Tran41dc5292017-01-19 22:28:03 -08003297 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003298 if (ql2xmvasynctoatio &&
3299 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003300 if (qla_tgt_mode_enabled(vha) ||
3301 qla_dual_mode_enabled(vha))
3302 ha->fw_options[2] |= BIT_11;
3303 else
3304 ha->fw_options[2] &= ~BIT_11;
3305 }
3306
Quinn Tranf7e761f2017-06-02 09:12:02 -07003307 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3308 /*
3309 * Tell FW to track each exchange to prevent
3310 * driver from using stale exchange.
3311 */
3312 if (qla_tgt_mode_enabled(vha) ||
3313 qla_dual_mode_enabled(vha))
3314 ha->fw_options[2] |= BIT_4;
3315 else
3316 ha->fw_options[2] &= ~BIT_4;
3317 }
3318
Quinn Tran83548fe2017-06-02 09:12:01 -07003319 ql_dbg(ql_dbg_init, vha, 0x00e8,
3320 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3321 __func__, ha->fw_options[1], ha->fw_options[2],
3322 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003323
3324 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3325 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003326
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003327 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003328 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003329 return;
3330
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003331 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003332 le16_to_cpu(ha->fw_seriallink_options24[1]),
3333 le16_to_cpu(ha->fw_seriallink_options24[2]),
3334 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003335 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003336 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003337 "Unable to update Serial Link options (%x).\n", rval);
3338 }
3339}
3340
3341void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003342qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003343{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003344 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003345 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003346 struct req_que *req = ha->req_q_map[0];
3347 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003348
3349 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003350 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3351 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003352 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3353 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3354 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3355 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3356 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3357 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003358
3359 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3360 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3361 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3362 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3363 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3364}
3365
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003366void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003367qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003368{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003369 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003370 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003371 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3372 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003373 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003374 uint16_t rid = 0;
3375 struct req_que *req = ha->req_q_map[0];
3376 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003377
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003378 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003379 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003380 icb->request_q_outpointer = cpu_to_le16(0);
3381 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003382 icb->request_q_length = cpu_to_le16(req->length);
3383 icb->response_q_length = cpu_to_le16(rsp->length);
3384 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3385 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3386 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3387 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003388
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003389 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003390 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003391 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3392 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3393 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3394
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003395 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003396 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003397
Chad Dupuisf73cb692014-02-26 04:15:06 -05003398 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003399 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3400 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003401 if (ha->flags.msix_enabled) {
3402 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003403 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003404 "Registering vector 0x%x for base que.\n",
3405 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003406 icb->msix = cpu_to_le16(msix->entry);
3407 }
3408 /* Use alternate PCI bus number */
3409 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003410 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003411 /* Use alternate PCI devfn */
3412 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003413 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003414
Anirban Chakraborty31557542009-12-02 10:36:55 -08003415 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003416 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3417 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003418 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003419 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003420 ql_dbg(ql_dbg_init, vha, 0x00fe,
3421 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003422 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003423 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003424 }
Bart Van Asschead950362015-07-09 07:24:08 -07003425 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003426
3427 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3428 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3429 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3430 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3431 } else {
3432 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3433 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3434 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3435 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3436 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003437 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003438
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003439 /* PCI posting */
3440 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003441}
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443/**
3444 * qla2x00_init_rings() - Initializes firmware.
3445 * @ha: HA context
3446 *
3447 * Beginning of request ring has initialization control block already built
3448 * by nvram config routine.
3449 *
3450 * Returns 0 on success.
3451 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003452int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003453qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454{
3455 int rval;
3456 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003457 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003458 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003459 struct req_que *req;
3460 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003461 struct mid_init_cb_24xx *mid_init_cb =
3462 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
3464 spin_lock_irqsave(&ha->hardware_lock, flags);
3465
3466 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003467 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003468 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003469 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003470 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003471 req->out_ptr = (void *)(req->ring + req->length);
3472 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003473 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003474 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003476 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003477
3478 /* Initialize firmware. */
3479 req->ring_ptr = req->ring;
3480 req->ring_index = 0;
3481 req->cnt = req->length;
3482 }
3483
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003484 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003485 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003486 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003487 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003488 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3489 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003490 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003491 if (IS_QLAFX00(ha))
3492 qlafx00_init_response_q_entries(rsp);
3493 else
3494 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003497 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3498 ha->tgt.atio_ring_index = 0;
3499 /* Initialize ATIO queue entries */
3500 qlt_init_atio_q_entries(vha);
3501
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003502 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3505
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003506 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3507
3508 if (IS_QLAFX00(ha)) {
3509 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3510 goto next_check;
3511 }
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003514 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003516 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003517 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003518 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003519 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003520 }
3521
Andrew Vasquez24a08132009-03-24 09:08:16 -07003522 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003523 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003524 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003525 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003526 ha->flags.dport_enabled =
3527 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3528 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3529 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3530 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003531 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003532 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003533 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003534 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003535 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003536
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003537 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003538next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003540 ql_log(ql_log_fatal, vha, 0x00d2,
3541 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003543 ql_dbg(ql_dbg_init, vha, 0x00d3,
3544 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003545 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
3547
3548 return (rval);
3549}
3550
3551/**
3552 * qla2x00_fw_ready() - Waits for firmware ready.
3553 * @ha: HA context
3554 *
3555 * Returns 0 on success.
3556 */
3557static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003558qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
3560 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003561 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 uint16_t min_wait; /* Minimum wait time if loop is down */
3563 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003564 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003565 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003567 if (IS_QLAFX00(vha->hw))
3568 return qlafx00_fw_ready(vha);
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 rval = QLA_SUCCESS;
3571
Chad Dupuis334614912015-04-09 14:59:57 -04003572 /* Time to wait for loop down */
3573 if (IS_P3P_TYPE(ha))
3574 min_wait = 30;
3575 else
3576 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 /*
3579 * Firmware should take at most one RATOV to login, plus 5 seconds for
3580 * our own processing.
3581 */
3582 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3583 wait_time = min_wait;
3584 }
3585
3586 /* Min wait time if loop down */
3587 mtime = jiffies + (min_wait * HZ);
3588
3589 /* wait time before firmware ready */
3590 wtime = jiffies + (wait_time * HZ);
3591
3592 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003593 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003594 ql_log(ql_log_info, vha, 0x801e,
3595 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003598 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003599 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003601 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003602 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003604 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003605 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3606 "fw_state=%x 84xx=%x.\n", state[0],
3607 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003608 if ((state[2] & FSTATE_LOGGED_IN) &&
3609 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003610 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3611 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003612
3613 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003614 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003615 if (rval != QLA_SUCCESS) {
3616 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003617 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003618 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003619 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003620 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003621
3622 /* Add time taken to initialize. */
3623 cs84xx_time = jiffies - cs84xx_time;
3624 wtime += cs84xx_time;
3625 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003626 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003627 "Increasing wait time by %ld. "
3628 "New time %ld.\n", cs84xx_time,
3629 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003630 }
3631 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003632 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3633 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003635 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 &ha->login_timeout, &ha->r_a_tov);
3637
3638 rval = QLA_SUCCESS;
3639 break;
3640 }
3641
3642 rval = QLA_FUNCTION_FAILED;
3643
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003644 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003645 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003647 * other than Wait for Login.
3648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003650 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 "Cable is unplugged...\n");
3652
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003653 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 break;
3655 }
3656 }
3657 } else {
3658 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003659 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003660 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 break;
3662 }
3663
3664 if (time_after_eq(jiffies, wtime))
3665 break;
3666
3667 /* Delay for a while */
3668 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 } while (1);
3670
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003671 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003672 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3673 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Chad Dupuiscfb09192011-11-18 09:03:07 -08003675 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003676 ql_log(ql_log_warn, vha, 0x803b,
3677 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
3679
3680 return (rval);
3681}
3682
3683/*
3684* qla2x00_configure_hba
3685* Setup adapter context.
3686*
3687* Input:
3688* ha = adapter state pointer.
3689*
3690* Returns:
3691* 0 = success
3692*
3693* Context:
3694* Kernel context.
3695*/
3696static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003697qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
3699 int rval;
3700 uint16_t loop_id;
3701 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003702 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 uint8_t al_pa;
3704 uint8_t area;
3705 uint8_t domain;
3706 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003707 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003708 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003709 port_id_t id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003712 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003713 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003715 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003716 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003717 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003718 ql_dbg(ql_dbg_disc, vha, 0x2008,
3719 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003720 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003721 ql_log(ql_log_warn, vha, 0x2009,
3722 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003723 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3724 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3725 ql_log(ql_log_warn, vha, 0x1151,
3726 "Doing link init.\n");
3727 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3728 return rval;
3729 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003730 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return (rval);
3733 }
3734
3735 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003736 ql_log(ql_log_info, vha, 0x200a,
3737 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 return (QLA_FUNCTION_FAILED);
3739 }
3740
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003741 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 /* initialize */
3744 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3745 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003746 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
3748 switch (topo) {
3749 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003750 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 ha->current_topology = ISP_CFG_NL;
3752 strcpy(connect_type, "(Loop)");
3753 break;
3754
3755 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003756 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003757 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 ha->current_topology = ISP_CFG_FL;
3759 strcpy(connect_type, "(FL_Port)");
3760 break;
3761
3762 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003763 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 ha->operating_mode = P2P;
3765 ha->current_topology = ISP_CFG_N;
3766 strcpy(connect_type, "(N_Port-to-N_Port)");
3767 break;
3768
3769 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003770 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003771 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 ha->operating_mode = P2P;
3773 ha->current_topology = ISP_CFG_F;
3774 strcpy(connect_type, "(F_Port)");
3775 break;
3776
3777 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003778 ql_dbg(ql_dbg_disc, vha, 0x200f,
3779 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 ha->current_topology = ISP_CFG_NL;
3781 strcpy(connect_type, "(Loop)");
3782 break;
3783 }
3784
3785 /* Save Host port and loop ID. */
3786 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07003787 id.b.domain = domain;
3788 id.b.area = area;
3789 id.b.al_pa = al_pa;
3790 id.b.rsvd_1 = 0;
3791 qlt_update_host_map(vha, id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003792
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003793 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003794 ql_log(ql_log_info, vha, 0x2010,
3795 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003796 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return(rval);
3799}
3800
Giridhar Malavalia9083012010-04-12 17:59:55 -07003801inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003802qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
3803 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003804{
3805 char *st, *en;
3806 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003807 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07003808 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003809 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003810
3811 if (memcmp(model, BINZERO, len) != 0) {
3812 strncpy(ha->model_number, model, len);
3813 st = en = ha->model_number;
3814 en += len - 1;
3815 while (en > st) {
3816 if (*en != 0x20 && *en != 0x00)
3817 break;
3818 *en-- = '\0';
3819 }
3820
3821 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07003822 if (use_tbl &&
3823 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003824 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003825 strncpy(ha->model_desc,
3826 qla2x00_model_name[index * 2 + 1],
3827 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003828 } else {
3829 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07003830 if (use_tbl &&
3831 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003832 index < QLA_MODEL_NAMES) {
3833 strcpy(ha->model_number,
3834 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003835 strncpy(ha->model_desc,
3836 qla2x00_model_name[index * 2 + 1],
3837 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003838 } else {
3839 strcpy(ha->model_number, def);
3840 }
3841 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003842 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003843 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003844 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003845}
3846
David Miller4e08df32007-04-16 12:37:43 -07003847/* On sparc systems, obtain port and node WWN from firmware
3848 * properties.
3849 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003850static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07003851{
3852#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003853 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07003854 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003855 struct device_node *dp = pci_device_to_OF_node(pdev);
3856 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003857 int len;
3858
3859 val = of_get_property(dp, "port-wwn", &len);
3860 if (val && len >= WWN_SIZE)
3861 memcpy(nv->port_name, val, WWN_SIZE);
3862
3863 val = of_get_property(dp, "node-wwn", &len);
3864 if (val && len >= WWN_SIZE)
3865 memcpy(nv->node_name, val, WWN_SIZE);
3866#endif
3867}
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869/*
3870* NVRAM configuration for ISP 2xxx
3871*
3872* Input:
3873* ha = adapter block pointer.
3874*
3875* Output:
3876* initialization control block in response_ring
3877* host adapters parameters in host adapter block
3878*
3879* Returns:
3880* 0 = success.
3881*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003882int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003883qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
David Miller4e08df32007-04-16 12:37:43 -07003885 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003886 uint8_t chksum = 0;
3887 uint16_t cnt;
3888 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003889 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003890 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07003891 nvram_t *nv = ha->nvram;
3892 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003893 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
David Miller4e08df32007-04-16 12:37:43 -07003895 rval = QLA_SUCCESS;
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003898 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 ha->nvram_base = 0;
3900 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
3901 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
3902 ha->nvram_base = 0x80;
3903
3904 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003905 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003906 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
3907 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003909 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
3910 "Contents of NVRAM.\n");
3911 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
3912 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 /* Bad NVRAM data, set defaults parameters. */
3915 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
3916 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
3917 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003918 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04003919 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003920 "detected: checksum=0x%x id=%c version=0x%x.\n",
3921 chksum, nv->id[0], nv->nvram_version);
3922 ql_log(ql_log_warn, vha, 0x0065,
3923 "Falling back to "
3924 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07003925
3926 /*
3927 * Set default initialization control block.
3928 */
3929 memset(nv, 0, ha->nvram_size);
3930 nv->parameter_block_version = ICB_VERSION;
3931
3932 if (IS_QLA23XX(ha)) {
3933 nv->firmware_options[0] = BIT_2 | BIT_1;
3934 nv->firmware_options[1] = BIT_7 | BIT_5;
3935 nv->add_firmware_options[0] = BIT_5;
3936 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003937 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07003938 nv->special_options[1] = BIT_7;
3939 } else if (IS_QLA2200(ha)) {
3940 nv->firmware_options[0] = BIT_2 | BIT_1;
3941 nv->firmware_options[1] = BIT_7 | BIT_5;
3942 nv->add_firmware_options[0] = BIT_5;
3943 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003944 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07003945 } else if (IS_QLA2100(ha)) {
3946 nv->firmware_options[0] = BIT_3 | BIT_1;
3947 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003948 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07003949 }
3950
Bart Van Asschead950362015-07-09 07:24:08 -07003951 nv->max_iocb_allocation = cpu_to_le16(256);
3952 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07003953 nv->retry_count = 8;
3954 nv->retry_delay = 1;
3955
3956 nv->port_name[0] = 33;
3957 nv->port_name[3] = 224;
3958 nv->port_name[4] = 139;
3959
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003960 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07003961
3962 nv->login_timeout = 4;
3963
3964 /*
3965 * Set default host adapter parameters
3966 */
3967 nv->host_p[1] = BIT_2;
3968 nv->reset_delay = 5;
3969 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07003970 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07003971 nv->link_down_timeout = 60;
3972
3973 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 }
3975
3976#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3977 /*
3978 * The SN2 does not provide BIOS emulation which means you can't change
3979 * potentially bogus BIOS settings. Force the use of default settings
3980 * for link rate and frame size. Hope that the rest of the settings
3981 * are valid.
3982 */
3983 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04003984 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 if (IS_QLA23XX(ha))
3986 nv->special_options[1] = BIT_7;
3987 }
3988#endif
3989
3990 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003991 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 /*
3994 * Setup driver NVRAM options.
3995 */
3996 nv->firmware_options[0] |= (BIT_6 | BIT_1);
3997 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
3998 nv->firmware_options[1] |= (BIT_5 | BIT_0);
3999 nv->firmware_options[1] &= ~BIT_4;
4000
4001 if (IS_QLA23XX(ha)) {
4002 nv->firmware_options[0] |= BIT_2;
4003 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004004 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004005 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 if (IS_QLA2300(ha)) {
4008 if (ha->fb_rev == FPM_2310) {
4009 strcpy(ha->model_number, "QLA2310");
4010 } else {
4011 strcpy(ha->model_number, "QLA2300");
4012 }
4013 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004014 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004015 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
4017 } else if (IS_QLA2200(ha)) {
4018 nv->firmware_options[0] |= BIT_2;
4019 /*
4020 * 'Point-to-point preferred, else loop' is not a safe
4021 * connection mode setting.
4022 */
4023 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4024 (BIT_5 | BIT_4)) {
4025 /* Force 'loop preferred, else point-to-point'. */
4026 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4027 nv->add_firmware_options[0] |= BIT_5;
4028 }
4029 strcpy(ha->model_number, "QLA22xx");
4030 } else /*if (IS_QLA2100(ha))*/ {
4031 strcpy(ha->model_number, "QLA2100");
4032 }
4033
4034 /*
4035 * Copy over NVRAM RISC parameter block to initialization control block.
4036 */
4037 dptr1 = (uint8_t *)icb;
4038 dptr2 = (uint8_t *)&nv->parameter_block_version;
4039 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4040 while (cnt--)
4041 *dptr1++ = *dptr2++;
4042
4043 /* Copy 2nd half. */
4044 dptr1 = (uint8_t *)icb->add_firmware_options;
4045 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4046 while (cnt--)
4047 *dptr1++ = *dptr2++;
4048
Andrew Vasquez5341e862006-05-17 15:09:16 -07004049 /* Use alternate WWN? */
4050 if (nv->host_p[1] & BIT_7) {
4051 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4052 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4053 }
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 /* Prepare nodename */
4056 if ((icb->firmware_options[1] & BIT_6) == 0) {
4057 /*
4058 * Firmware will apply the following mask if the nodename was
4059 * not provided.
4060 */
4061 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4062 icb->node_name[0] &= 0xF0;
4063 }
4064
4065 /*
4066 * Set host adapter parameters.
4067 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004068
4069 /*
4070 * BIT_7 in the host-parameters section allows for modification to
4071 * internal driver logging.
4072 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004073 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004074 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4076 /* Always load RISC code on non ISP2[12]00 chips. */
4077 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4078 ha->flags.disable_risc_code_load = 0;
4079 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4080 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4081 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004082 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004083 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 ha->operating_mode =
4086 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4087
4088 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4089 sizeof(ha->fw_seriallink_options));
4090
4091 /* save HBA serial number */
4092 ha->serial0 = icb->port_name[5];
4093 ha->serial1 = icb->port_name[6];
4094 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004095 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4096 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Bart Van Asschead950362015-07-09 07:24:08 -07004098 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 ha->retry_count = nv->retry_count;
4101
4102 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004103 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 nv->login_timeout = ql2xlogintimeout;
4105 if (nv->login_timeout < 4)
4106 nv->login_timeout = 4;
4107 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004109 /* Set minimum RATOV to 100 tenths of a second. */
4110 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 ha->loop_reset_delay = nv->reset_delay;
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 /* Link Down Timeout = 0:
4115 *
4116 * When Port Down timer expires we will start returning
4117 * I/O's to OS with "DID_NO_CONNECT".
4118 *
4119 * Link Down Timeout != 0:
4120 *
4121 * The driver waits for the link to come up after link down
4122 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 if (nv->link_down_timeout == 0) {
4125 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004126 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 } else {
4128 ha->link_down_timeout = nv->link_down_timeout;
4129 ha->loop_down_abort_time =
4130 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 /*
4134 * Need enough time to try and get the port back.
4135 */
4136 ha->port_down_retry_count = nv->port_down_retry_count;
4137 if (qlport_down_retry)
4138 ha->port_down_retry_count = qlport_down_retry;
4139 /* Set login_retry_count */
4140 ha->login_retry_count = nv->retry_count;
4141 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4142 ha->port_down_retry_count > 3)
4143 ha->login_retry_count = ha->port_down_retry_count;
4144 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4145 ha->login_retry_count = ha->port_down_retry_count;
4146 if (ql2xloginretrycount)
4147 ha->login_retry_count = ql2xloginretrycount;
4148
Bart Van Asschead950362015-07-09 07:24:08 -07004149 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 icb->command_resource_count = 0;
4151 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004152 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4155 /* Enable RIO */
4156 icb->firmware_options[0] &= ~BIT_3;
4157 icb->add_firmware_options[0] &=
4158 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4159 icb->add_firmware_options[0] |= BIT_2;
4160 icb->response_accumulation_timer = 3;
4161 icb->interrupt_delay_timer = 5;
4162
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004163 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004165 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004166 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004167 ha->zio_mode = icb->add_firmware_options[0] &
4168 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4169 ha->zio_timer = icb->interrupt_delay_timer ?
4170 icb->interrupt_delay_timer: 2;
4171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 icb->add_firmware_options[0] &=
4173 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004174 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004175 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004176 ha->zio_mode = QLA_ZIO_MODE_6;
4177
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004178 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004179 "ZIO mode %d enabled; timer delay (%d us).\n",
4180 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004182 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4183 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004184 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
4186 }
4187
David Miller4e08df32007-04-16 12:37:43 -07004188 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004189 ql_log(ql_log_warn, vha, 0x0069,
4190 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004191 }
4192 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193}
4194
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004195static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004196qla2x00_rport_del(void *data)
4197{
4198 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004199 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004200 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004201
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004202 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004203 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004204 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004205 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004206 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004207 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4208 "%s %8phN. rport %p roles %x\n",
4209 __func__, fcport->port_name, rport,
4210 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004211
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004212 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004213 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004214}
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216/**
4217 * qla2x00_alloc_fcport() - Allocate a generic fcport.
4218 * @ha: HA context
4219 * @flags: allocation flags
4220 *
4221 * Returns a pointer to the allocated fcport, or NULL, if none available.
4222 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004223fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004224qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 fc_port_t *fcport;
4227
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004228 fcport = kzalloc(sizeof(fc_port_t), flags);
4229 if (!fcport)
4230 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
4232 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004233 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 fcport->port_type = FCT_UNKNOWN;
4235 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004236 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004237 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Quinn Tran726b8542017-01-19 22:28:00 -08004239 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4240 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004241 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004242 fcport->disc_state = DSC_DELETED;
4243 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4244 fcport->deleted = QLA_SESS_DELETED;
4245 fcport->login_retry = vha->hw->login_retry_count;
4246 fcport->login_retry = 5;
4247 fcport->logout_on_delete = 1;
4248
4249 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004250 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004251 "Failed to allocate ct_sns request.\n");
4252 kfree(fcport);
4253 fcport = NULL;
4254 }
4255 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4256 INIT_LIST_HEAD(&fcport->gnl_entry);
4257 INIT_LIST_HEAD(&fcport->list);
4258
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004259 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
Quinn Tran726b8542017-01-19 22:28:00 -08004262void
4263qla2x00_free_fcport(fc_port_t *fcport)
4264{
4265 if (fcport->ct_desc.ct_sns) {
4266 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4267 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4268 fcport->ct_desc.ct_sns_dma);
4269
4270 fcport->ct_desc.ct_sns = NULL;
4271 }
4272 kfree(fcport);
4273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275/*
4276 * qla2x00_configure_loop
4277 * Updates Fibre Channel Device Database with what is actually on loop.
4278 *
4279 * Input:
4280 * ha = adapter block pointer.
4281 *
4282 * Returns:
4283 * 0 = success.
4284 * 1 = error.
4285 * 2 = database was full and device was not configured.
4286 */
4287static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004288qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
4290 int rval;
4291 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004292 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 rval = QLA_SUCCESS;
4294
4295 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004296 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4297 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004299 ql_dbg(ql_dbg_disc, vha, 0x2013,
4300 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return (rval);
4302 }
4303 }
4304
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004305 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004306 ql_dbg(ql_dbg_disc, vha, 0x2014,
4307 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
4309 /*
4310 * If we have both an RSCN and PORT UPDATE pending then handle them
4311 * both at the same time.
4312 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004313 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4314 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Michael Hernandez3064ff32009-12-15 21:29:44 -08004316 qla2x00_get_data_rate(vha);
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 /* Determine what we need to do */
4319 if (ha->current_topology == ISP_CFG_FL &&
4320 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 set_bit(RSCN_UPDATE, &flags);
4323
4324 } else if (ha->current_topology == ISP_CFG_F &&
4325 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 set_bit(RSCN_UPDATE, &flags);
4328 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4329
Andrew Vasquez21333b42006-05-17 15:09:56 -07004330 } else if (ha->current_topology == ISP_CFG_N) {
4331 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08004332 } else if (ha->current_topology == ISP_CFG_NL) {
4333 clear_bit(RSCN_UPDATE, &flags);
4334 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004335 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 set_bit(RSCN_UPDATE, &flags);
4338 set_bit(LOCAL_LOOP_UPDATE, &flags);
4339 }
4340
4341 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004342 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4343 ql_dbg(ql_dbg_disc, vha, 0x2015,
4344 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004346 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004347 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 }
4349
4350 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004351 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004352 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004353 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004355 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004356 else
4357 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 }
4359
4360 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004361 if (atomic_read(&vha->loop_down_timer) ||
4362 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 rval = QLA_FUNCTION_FAILED;
4364 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004365 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004366 ql_dbg(ql_dbg_disc, vha, 0x2069,
4367 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004368 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004369
4370 /*
4371 * Process any ATIO queue entries that came in
4372 * while we weren't online.
4373 */
Quinn Tranead03852017-01-19 22:28:01 -08004374 if (qla_tgt_mode_enabled(vha) ||
4375 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004376 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4377 spin_lock_irqsave(&ha->tgt.atio_lock,
4378 flags);
4379 qlt_24xx_process_atio_queue(vha, 0);
4380 spin_unlock_irqrestore(
4381 &ha->tgt.atio_lock, flags);
4382 } else {
4383 spin_lock_irqsave(&ha->hardware_lock,
4384 flags);
4385 qlt_24xx_process_atio_queue(vha, 1);
4386 spin_unlock_irqrestore(
4387 &ha->hardware_lock, flags);
4388 }
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 }
4391 }
4392
4393 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004394 ql_dbg(ql_dbg_disc, vha, 0x206a,
4395 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004397 ql_dbg(ql_dbg_disc, vha, 0x206b,
4398 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
4400
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004401 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004402 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004404 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004405 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004406 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
4409
4410 return (rval);
4411}
4412
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004413/*
4414 * N2N Login
4415 * Updates Fibre Channel Device Database with local loop devices.
4416 *
4417 * Input:
4418 * ha = adapter block pointer.
4419 *
4420 * Returns:
4421 */
4422static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4423 fc_port_t *fcport)
4424{
4425 struct qla_hw_data *ha = vha->hw;
4426 int res = QLA_SUCCESS, rval;
4427 int greater_wwpn = 0;
4428 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004430 if (ha->current_topology != ISP_CFG_N)
4431 return res;
4432
4433 if (wwn_to_u64(vha->port_name) >
4434 wwn_to_u64(vha->n2n_port_name)) {
4435 ql_dbg(ql_dbg_disc, vha, 0x2002,
4436 "HBA WWPN is greater %llx > target %llx\n",
4437 wwn_to_u64(vha->port_name),
4438 wwn_to_u64(vha->n2n_port_name));
4439 greater_wwpn = 1;
4440 fcport->d_id.b24 = vha->n2n_id;
4441 }
4442
4443 fcport->loop_id = vha->loop_id;
4444 fcport->fc4f_nvme = 0;
4445 fcport->query = 1;
4446
4447 ql_dbg(ql_dbg_disc, vha, 0x4001,
4448 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4449 fcport->d_id.b24, vha->loop_id);
4450
4451 /* Fill in member data. */
4452 if (!greater_wwpn) {
4453 rval = qla2x00_get_port_database(vha, fcport, 0);
4454 ql_dbg(ql_dbg_disc, vha, 0x1051,
4455 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4456 fcport->current_login_state, fcport->last_login_state,
4457 fcport->d_id.b24, fcport->loop_id, rval);
4458
4459 if (((fcport->current_login_state & 0xf) == 0x4) ||
4460 ((fcport->current_login_state & 0xf) == 0x6))
4461 logged_in = 1;
4462 }
4463
4464 if (logged_in || greater_wwpn) {
4465 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4466 qla_nvme_register_hba(vha);
4467
4468 /* Set connected N_Port d_id */
4469 if (vha->flags.nvme_enabled)
4470 fcport->fc4f_nvme = 1;
4471
4472 fcport->scan_state = QLA_FCPORT_FOUND;
4473 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4474 fcport->disc_state = DSC_GNL;
4475 fcport->n2n_flag = 1;
4476 fcport->flags = 3;
4477 vha->hw->flags.gpsc_supported = 0;
4478
4479 if (greater_wwpn) {
4480 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4481 "%s %d PLOGI ELS %8phC\n",
4482 __func__, __LINE__, fcport->port_name);
4483
4484 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4485 fcport, fcport->d_id);
4486 }
4487
4488 if (res != QLA_SUCCESS) {
4489 ql_log(ql_log_info, vha, 0xd04d,
4490 "PLOGI Failed: portid=%06x - retrying\n",
4491 fcport->d_id.b24);
4492 res = QLA_SUCCESS;
4493 } else {
4494 /* State 0x6 means FCP PRLI complete */
4495 if ((fcport->current_login_state & 0xf) == 0x6) {
4496 ql_dbg(ql_dbg_disc, vha, 0x2118,
4497 "%s %d %8phC post GPDB work\n",
4498 __func__, __LINE__, fcport->port_name);
4499 fcport->chip_reset =
4500 vha->hw->base_qpair->chip_reset;
4501 qla24xx_post_gpdb_work(vha, fcport, 0);
4502 } else {
4503 ql_dbg(ql_dbg_disc, vha, 0x2118,
4504 "%s %d %8phC post NVMe PRLI\n",
4505 __func__, __LINE__, fcport->port_name);
4506 qla24xx_post_prli_work(vha, fcport);
4507 }
4508 }
4509 } else {
4510 /* Wait for next database change */
4511 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4512 }
4513
4514 return res;
4515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
4517/*
4518 * qla2x00_configure_local_loop
4519 * Updates Fibre Channel Device Database with local loop devices.
4520 *
4521 * Input:
4522 * ha = adapter block pointer.
4523 *
4524 * Returns:
4525 * 0 = success.
4526 */
4527static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004528qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 int rval, rval2;
4531 int found_devs;
4532 int found;
4533 fc_port_t *fcport, *new_fcport;
4534
4535 uint16_t index;
4536 uint16_t entries;
4537 char *id_iter;
4538 uint16_t loop_id;
4539 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004540 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004541 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
4543 found_devs = 0;
4544 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004545 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004548 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004549 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 &entries);
4551 if (rval != QLA_SUCCESS)
4552 goto cleanup_allocation;
4553
Quinn Tran83548fe2017-06-02 09:12:01 -07004554 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004555 "Entries in ID list (%d).\n", entries);
4556 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4557 (uint8_t *)ha->gid_list,
4558 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004561 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004563 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004564 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 rval = QLA_MEMORY_ALLOC_FAILED;
4566 goto cleanup_allocation;
4567 }
4568 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4569
4570 /*
4571 * Mark local devices that were present with FCF_DEVICE_LOST for now.
4572 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004573 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (atomic_read(&fcport->state) == FCS_ONLINE &&
4575 fcport->port_type != FCT_BROADCAST &&
4576 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
4577
Quinn Tran83548fe2017-06-02 09:12:01 -07004578 ql_dbg(ql_dbg_disc, vha, 0x2096,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004579 "Marking port lost loop_id=0x%04x.\n",
4580 fcport->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Quinn Tran41dc5292017-01-19 22:28:03 -08004582 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 }
4584 }
4585
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004586 /* Inititae N2N login. */
4587 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4588 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4589 if (rval != QLA_SUCCESS)
4590 goto cleanup_allocation;
4591 return QLA_SUCCESS;
4592 }
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 /* Add devices to port list. */
4595 id_iter = (char *)ha->gid_list;
4596 for (index = 0; index < entries; index++) {
4597 domain = ((struct gid_list_info *)id_iter)->domain;
4598 area = ((struct gid_list_info *)id_iter)->area;
4599 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004600 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 loop_id = (uint16_t)
4602 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004603 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 loop_id = le16_to_cpu(
4605 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004606 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608 /* Bypass reserved domain fields. */
4609 if ((domain & 0xf0) == 0xf0)
4610 continue;
4611
4612 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004613 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004614 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 continue;
4616
4617 /* Bypass invalid local loop ID. */
4618 if (loop_id > LAST_LOCAL_LOOP_ID)
4619 continue;
4620
Quinn Tran41dc5292017-01-19 22:28:03 -08004621 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 /* Fill in member data. */
4624 new_fcport->d_id.b.domain = domain;
4625 new_fcport->d_id.b.area = area;
4626 new_fcport->d_id.b.al_pa = al_pa;
4627 new_fcport->loop_id = loop_id;
Quinn Tran41dc5292017-01-19 22:28:03 -08004628
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004629 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004631 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004632 "Failed to retrieve fcport information "
4633 "-- get_port_database=%x, loop_id=0x%04x.\n",
4634 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004635 /* Skip retry if N2N */
4636 if (ha->current_topology != ISP_CFG_N) {
4637 ql_dbg(ql_dbg_disc, vha, 0x2105,
4638 "Scheduling resync.\n");
4639 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4640 continue;
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
4643
Quinn Tran41dc5292017-01-19 22:28:03 -08004644 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 /* Check for matching device in port list. */
4646 found = 0;
4647 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004648 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (memcmp(new_fcport->port_name, fcport->port_name,
4650 WWN_SIZE))
4651 continue;
4652
Shyam Sundarddb9b122009-03-24 09:08:10 -07004653 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 fcport->loop_id = new_fcport->loop_id;
4655 fcport->port_type = new_fcport->port_type;
4656 fcport->d_id.b24 = new_fcport->d_id.b24;
4657 memcpy(fcport->node_name, new_fcport->node_name,
4658 WWN_SIZE);
4659
Quinn Tran41dc5292017-01-19 22:28:03 -08004660 if (!fcport->login_succ) {
4661 vha->fcport_count++;
4662 fcport->login_succ = 1;
4663 fcport->disc_state = DSC_LOGIN_COMPLETE;
4664 }
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 found++;
4667 break;
4668 }
4669
4670 if (!found) {
4671 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004672 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
4674 /* Allocate a new replacement fcport. */
4675 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004676 if (!fcport->login_succ) {
4677 vha->fcport_count++;
4678 fcport->login_succ = 1;
4679 fcport->disc_state = DSC_LOGIN_COMPLETE;
4680 }
4681
4682 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4683
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004684 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004687 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004688 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 rval = QLA_MEMORY_ALLOC_FAILED;
4690 goto cleanup_allocation;
4691 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004692 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4694 }
4695
Quinn Tran41dc5292017-01-19 22:28:03 -08004696 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4697
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004698 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004699 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004700
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004701 qla2x00_update_fcport(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
4703 found_devs++;
4704 }
4705
4706cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004707 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004710 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004711 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 return (rval);
4715}
4716
4717static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004718qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004719{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004720 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004721 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004722 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004723
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004724 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004725 return;
4726
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07004727 if (atomic_read(&fcport->state) != FCS_ONLINE)
4728 return;
4729
Andrew Vasquez39bd9622007-09-20 14:07:34 -07004730 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
4731 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004732 return;
4733
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004734 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004735 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004736 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004737 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004738 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
4739 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004740 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004741 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004742 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05004743 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004744 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004745 }
4746}
4747
Quinn Tran726b8542017-01-19 22:28:00 -08004748/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01004749static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004750qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05004751{
4752 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05004753 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004754 unsigned long flags;
8482e1182005-04-17 15:04:54 -05004755
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07004756 rport_ids.node_name = wwn_to_u64(fcport->node_name);
4757 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05004758 rport_ids.port_id = fcport->d_id.b.domain << 16 |
4759 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
4760 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004761 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07004762 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004763 ql_log(ql_log_warn, vha, 0x2006,
4764 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07004765 return;
4766 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004767
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004768 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004769 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004770 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004771
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004772 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07004773
4774 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05004775 if (fcport->port_type == FCT_INITIATOR)
4776 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4777 if (fcport->port_type == FCT_TARGET)
4778 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08004779
Quinn Tran83548fe2017-06-02 09:12:01 -07004780 ql_dbg(ql_dbg_disc, vha, 0x20ee,
4781 "%s %8phN. rport %p is %s mode\n",
4782 __func__, fcport->port_name, rport,
4783 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08004784
Andrew Vasquez77d74142005-07-08 18:00:36 -07004785 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05004786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788/*
Adrian Bunk23be3312006-11-24 02:46:01 +01004789 * qla2x00_update_fcport
4790 * Updates device on list.
4791 *
4792 * Input:
4793 * ha = adapter block pointer.
4794 * fcport = port structure pointer.
4795 *
4796 * Return:
4797 * 0 - Success
4798 * BIT_0 - error
4799 *
4800 * Context:
4801 * Kernel context.
4802 */
4803void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004804qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01004805{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004806 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004807
Quinn Tran726b8542017-01-19 22:28:00 -08004808 if (IS_SW_RESV_ADDR(fcport->d_id))
4809 return;
4810
Quinn Tran83548fe2017-06-02 09:12:01 -07004811 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08004812 __func__, fcport->port_name);
4813
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004814 if (IS_QLAFX00(vha->hw)) {
4815 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004816 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004817 }
Adrian Bunk23be3312006-11-24 02:46:01 +01004818 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07004819 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08004820 fcport->disc_state = DSC_LOGIN_COMPLETE;
4821 fcport->deleted = 0;
4822 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01004823
Duane Grigsbye84067d2017-06-21 13:48:43 -07004824 if (fcport->fc4f_nvme) {
4825 qla_nvme_register_remote(vha, fcport);
4826 return;
4827 }
4828
Joe Carnuccio1f93da522012-11-21 02:40:38 -05004829 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004830 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08004831 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004832
4833reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08004834 switch (vha->host->active_mode) {
4835 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004836 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08004837 break;
4838 case MODE_TARGET:
4839 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
4840 !vha->vha_tgt.qla_tgt->tgt_stopped)
4841 qlt_fc_port_added(vha, fcport);
4842 break;
4843 case MODE_DUAL:
4844 qla2x00_reg_remote_port(vha, fcport);
4845 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
4846 !vha->vha_tgt.qla_tgt->tgt_stopped)
4847 qlt_fc_port_added(vha, fcport);
4848 break;
4849 default:
4850 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004851 }
Adrian Bunk23be3312006-11-24 02:46:01 +01004852}
4853
4854/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 * qla2x00_configure_fabric
4856 * Setup SNS devices with loop ID's.
4857 *
4858 * Input:
4859 * ha = adapter block pointer.
4860 *
4861 * Returns:
4862 * 0 = success.
4863 * BIT_0 = error
4864 */
4865static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004866qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867{
Arun Easib3b02e62012-02-09 11:15:39 -08004868 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08004869 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004871 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004873 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004874 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07004877 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004878 loop_id = NPH_F_PORT;
4879 else
4880 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004881 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004883 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004884 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004886 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 return (QLA_SUCCESS);
4888 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004889 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Quinn Tran41dc5292017-01-19 22:28:03 -08004891
4892 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
4893 rval = qla2x00_send_change_request(vha, 0x3, 0);
4894 if (rval != QLA_SUCCESS)
4895 ql_log(ql_log_warn, vha, 0x121,
4896 "Failed to enable receiving of RSCN requests: 0x%x.\n",
4897 rval);
4898 }
4899
4900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 do {
Quinn Tran726b8542017-01-19 22:28:00 -08004902 qla2x00_mgmt_svr_login(vha);
4903
Andrew Vasquezcca53352005-08-26 19:08:30 -07004904 /* FDMI support. */
4905 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004906 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
4907 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07004908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07004910 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08004911 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
4912 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07004913 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
4914 ql_dbg(ql_dbg_disc, vha, 0x20a1,
4915 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
4916 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08004917 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05004918 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08004919 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004920 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
4921 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004923 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004924 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004925 if (test_bit(LOOP_RESYNC_NEEDED,
4926 &vha->dpc_flags))
4927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07004929 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004931 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004932 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004933 if (test_bit(LOOP_RESYNC_NEEDED,
4934 &vha->dpc_flags))
4935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07004937 if (vha->flags.nvme_enabled) {
4938 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
4939 ql_dbg(ql_dbg_disc, vha, 0x2049,
4940 "Register NVME FC Type Features failed.\n");
4941 }
4942 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004943 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004945 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004946 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004947 if (test_bit(LOOP_RESYNC_NEEDED,
4948 &vha->dpc_flags))
4949 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004950 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004952 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01004953 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004954 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957 }
4958
Joe Carnuccio827210b2013-02-08 01:57:57 -05004959 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4960 fcport->scan_state = QLA_FCPORT_SCAN;
4961 }
4962
Alexei Potashnikdf673272015-07-14 16:00:46 -04004963 /* Mark the time right before querying FW for connected ports.
4964 * This process is long, asynchronous and by the time it's done,
4965 * collected information might not be accurate anymore. E.g.
4966 * disconnected port might have re-connected and a brand new
4967 * session has been created. In this case session's generation
4968 * will be newer than discovery_gen. */
4969 qlt_do_generation_tick(vha, &discovery_gen);
4970
Quinn Tran726b8542017-01-19 22:28:00 -08004971 rval = qla2x00_find_all_fabric_devs(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 if (rval != QLA_SUCCESS)
4973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 } while (0);
4975
Duane Grigsbye84067d2017-06-21 13:48:43 -07004976 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4977 qla_nvme_register_hba(vha);
4978
Quinn Tran726b8542017-01-19 22:28:00 -08004979 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004980 ql_dbg(ql_dbg_disc, vha, 0x2068,
4981 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 return (rval);
4984}
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986/*
4987 * qla2x00_find_all_fabric_devs
4988 *
4989 * Input:
4990 * ha = adapter block pointer.
4991 * dev = database device entry pointer.
4992 *
4993 * Returns:
4994 * 0 = success.
4995 *
4996 * Context:
4997 * Kernel context.
4998 */
4999static int
Quinn Tran726b8542017-01-19 22:28:00 -08005000qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001{
5002 int rval;
5003 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005004 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 int found;
5006
5007 sw_info_t *swl;
5008 int swl_idx;
5009 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005010 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005011 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005012 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005013 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
5015 rval = QLA_SUCCESS;
5016
5017 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005018 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005019 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005020 GFP_KERNEL);
5021 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005022 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005024 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005025 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005027 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005028 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005030 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5031 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005032 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005034 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5035 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005036 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005038 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5039 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005040 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5041 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005042 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5043 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005045
5046 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005047 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005048 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005049 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5050 return rval;
5051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
5053 swl_idx = 0;
5054
5055 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005056 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005058 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005059 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return (QLA_MEMORY_ALLOC_FAILED);
5061 }
5062 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 /* Set start port ID scan at adapter ID. */
5064 first_dev = 1;
5065 last_dev = 0;
5066
5067 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005068 loop_id = ha->min_external_loopid;
5069 for (; loop_id <= ha->max_loop_id; loop_id++) {
5070 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 continue;
5072
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005073 if (ha->current_topology == ISP_CFG_FL &&
5074 (atomic_read(&vha->loop_down_timer) ||
5075 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005076 atomic_set(&vha->loop_down_timer, 0);
5077 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5078 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
5082 if (swl != NULL) {
5083 if (last_dev) {
5084 wrap.b24 = new_fcport->d_id.b24;
5085 } else {
5086 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5087 memcpy(new_fcport->node_name,
5088 swl[swl_idx].node_name, WWN_SIZE);
5089 memcpy(new_fcport->port_name,
5090 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005091 memcpy(new_fcport->fabric_port_name,
5092 swl[swl_idx].fabric_port_name, WWN_SIZE);
5093 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005094 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005096 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005097 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005098 if (vha->flags.nvme_enabled &&
5099 swl[swl_idx].fc4f_nvme) {
5100 new_fcport->fc4f_nvme =
5101 swl[swl_idx].fc4f_nvme;
5102 ql_log(ql_log_info, vha, 0x2131,
5103 "FOUND: NVME port %8phC as FC Type 28h\n",
5104 new_fcport->port_name);
5105 }
5106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5108 last_dev = 1;
5109 }
5110 swl_idx++;
5111 }
5112 } else {
5113 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005114 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005116 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005117 "SNS scan failed -- assuming "
5118 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 rval = QLA_SUCCESS;
5120 break;
5121 }
5122 }
5123
5124 /* If wrap on switch device list, exit. */
5125 if (first_dev) {
5126 wrap.b24 = new_fcport->d_id.b24;
5127 first_dev = 0;
5128 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005129 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005130 "Device wrap (%02x%02x%02x).\n",
5131 new_fcport->d_id.b.domain,
5132 new_fcport->d_id.b.area,
5133 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 break;
5135 }
5136
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005137 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005138 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 continue;
5140
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005141 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005142 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5143 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005144
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005145 /* Bypass if same domain and area of adapter. */
5146 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005147 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005148 ISP_CFG_FL)
5149 continue;
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 /* Bypass reserved domain fields. */
5152 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5153 continue;
5154
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005155 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005156 if (ql2xgffidenable &&
5157 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5158 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005159 continue;
5160
Quinn Tran726b8542017-01-19 22:28:00 -08005161 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 /* Locate matching device in database. */
5164 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005165 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (memcmp(new_fcport->port_name, fcport->port_name,
5167 WWN_SIZE))
5168 continue;
5169
Joe Carnuccio827210b2013-02-08 01:57:57 -05005170 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 found++;
5173
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005174 /* Update port state. */
5175 memcpy(fcport->fabric_port_name,
5176 new_fcport->fabric_port_name, WWN_SIZE);
5177 fcport->fp_speed = new_fcport->fp_speed;
5178
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005180 * If address the same and state FCS_ONLINE
5181 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 */
5183 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005184 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005185 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 break;
5187 }
5188
5189 /*
5190 * If device was not a fabric device before.
5191 */
5192 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5193 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005194 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 fcport->flags |= (FCF_FABRIC_DEVICE |
5196 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 break;
5198 }
5199
5200 /*
5201 * Port ID changed or device was marked to be updated;
5202 * Log it out if still logged in and mark it for
5203 * relogin later.
5204 */
Quinn Tran726b8542017-01-19 22:28:00 -08005205 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005206 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5207 "port changed FC ID, %8phC"
5208 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5209 fcport->port_name,
5210 fcport->d_id.b.domain,
5211 fcport->d_id.b.area,
5212 fcport->d_id.b.al_pa,
5213 fcport->loop_id,
5214 new_fcport->d_id.b.domain,
5215 new_fcport->d_id.b.area,
5216 new_fcport->d_id.b.al_pa);
5217 fcport->d_id.b24 = new_fcport->d_id.b24;
5218 break;
5219 }
5220
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 fcport->d_id.b24 = new_fcport->d_id.b24;
5222 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 break;
5224 }
5225
Quinn Tran726b8542017-01-19 22:28:00 -08005226 if (found) {
5227 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005231 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005232 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5233
5234 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
5237 /* Allocate a new replacement fcport. */
5238 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005239 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005241 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005242 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 return (QLA_MEMORY_ALLOC_FAILED);
5244 }
5245 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5246 new_fcport->d_id.b24 = nxt_d_id.b24;
5247 }
5248
Quinn Tran726b8542017-01-19 22:28:00 -08005249 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Quinn Tran726b8542017-01-19 22:28:00 -08005251 /*
5252 * Logout all previous fabric dev marked lost, except FCP2 devices.
5253 */
5254 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5255 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5256 break;
5257
5258 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5259 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5260 continue;
5261
5262 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5263 if ((qla_dual_mode_enabled(vha) ||
5264 qla_ini_mode_enabled(vha)) &&
5265 atomic_read(&fcport->state) == FCS_ONLINE) {
5266 qla2x00_mark_device_lost(vha, fcport,
5267 ql2xplogiabsentdevice, 0);
5268 if (fcport->loop_id != FC_NO_LOOP_ID &&
5269 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5270 fcport->port_type != FCT_INITIATOR &&
5271 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005272 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005273 "%s %d %8phC post del sess\n",
5274 __func__, __LINE__,
5275 fcport->port_name);
5276
5277 qlt_schedule_sess_for_deletion_lock
5278 (fcport);
5279 continue;
5280 }
5281 }
5282 }
5283
5284 if (fcport->scan_state == QLA_FCPORT_FOUND)
5285 qla24xx_fcport_handle_login(vha, fcport);
5286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 return (rval);
5288}
5289
5290/*
5291 * qla2x00_find_new_loop_id
5292 * Scan through our port list and find a new usable loop ID.
5293 *
5294 * Input:
5295 * ha: adapter state pointer.
5296 * dev: port structure pointer.
5297 *
5298 * Returns:
5299 * qla2x00 local function return status code.
5300 *
5301 * Context:
5302 * Kernel context.
5303 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005304int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005305qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306{
5307 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005308 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005309 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
5311 rval = QLA_SUCCESS;
5312
Chad Dupuis5f16b332012-08-22 14:21:00 -04005313 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
Chad Dupuis5f16b332012-08-22 14:21:00 -04005315 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5316 LOOPID_MAP_SIZE);
5317 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5318 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5319 dev->loop_id = FC_NO_LOOP_ID;
5320 rval = QLA_FUNCTION_FAILED;
5321 } else
5322 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Chad Dupuis5f16b332012-08-22 14:21:00 -04005324 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Chad Dupuis5f16b332012-08-22 14:21:00 -04005326 if (rval == QLA_SUCCESS)
5327 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5328 "Assigning new loopid=%x, portid=%x.\n",
5329 dev->loop_id, dev->d_id.b24);
5330 else
5331 ql_log(ql_log_warn, dev->vha, 0x2087,
5332 "No loop_id's available, portid=%x.\n",
5333 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
5335 return (rval);
5336}
5337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339/*
5340 * qla2x00_fabric_login
5341 * Issue fabric login command.
5342 *
5343 * Input:
5344 * ha = adapter block pointer.
5345 * device = pointer to FC device type structure.
5346 *
5347 * Returns:
5348 * 0 - Login successfully
5349 * 1 - Login failed
5350 * 2 - Initiator device
5351 * 3 - Fatal error
5352 */
5353int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005354qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 uint16_t *next_loopid)
5356{
5357 int rval;
5358 int retry;
5359 uint16_t tmp_loopid;
5360 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005361 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
5363 retry = 0;
5364 tmp_loopid = 0;
5365
5366 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005367 ql_dbg(ql_dbg_disc, vha, 0x2000,
5368 "Trying Fabric Login w/loop id 0x%04x for port "
5369 "%02x%02x%02x.\n",
5370 fcport->loop_id, fcport->d_id.b.domain,
5371 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
5373 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005374 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 fcport->d_id.b.domain, fcport->d_id.b.area,
5376 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005377 if (rval != QLA_SUCCESS) {
5378 return rval;
5379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 if (mb[0] == MBS_PORT_ID_USED) {
5381 /*
5382 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005383 * recommends the driver perform an implicit login with
5384 * the specified ID again. The ID we just used is save
5385 * here so we return with an ID that can be tried by
5386 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 */
5388 retry++;
5389 tmp_loopid = fcport->loop_id;
5390 fcport->loop_id = mb[1];
5391
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005392 ql_dbg(ql_dbg_disc, vha, 0x2001,
5393 "Fabric Login: port in use - next loop "
5394 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005396 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
5398 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5399 /*
5400 * Login succeeded.
5401 */
5402 if (retry) {
5403 /* A retry occurred before. */
5404 *next_loopid = tmp_loopid;
5405 } else {
5406 /*
5407 * No retry occurred before. Just increment the
5408 * ID value for next login.
5409 */
5410 *next_loopid = (fcport->loop_id + 1);
5411 }
5412
5413 if (mb[1] & BIT_0) {
5414 fcport->port_type = FCT_INITIATOR;
5415 } else {
5416 fcport->port_type = FCT_TARGET;
5417 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005418 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 }
5420 }
5421
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005422 if (mb[10] & BIT_0)
5423 fcport->supported_classes |= FC_COS_CLASS2;
5424 if (mb[10] & BIT_1)
5425 fcport->supported_classes |= FC_COS_CLASS3;
5426
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005427 if (IS_FWI2_CAPABLE(ha)) {
5428 if (mb[10] & BIT_7)
5429 fcport->flags |=
5430 FCF_CONF_COMP_SUPPORTED;
5431 }
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 rval = QLA_SUCCESS;
5434 break;
5435 } else if (mb[0] == MBS_LOOP_ID_USED) {
5436 /*
5437 * Loop ID already used, try next loop ID.
5438 */
5439 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005440 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 if (rval != QLA_SUCCESS) {
5442 /* Ran out of loop IDs to use */
5443 break;
5444 }
5445 } else if (mb[0] == MBS_COMMAND_ERROR) {
5446 /*
5447 * Firmware possibly timed out during login. If NO
5448 * retries are left to do then the device is declared
5449 * dead.
5450 */
5451 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005452 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005453 fcport->d_id.b.domain, fcport->d_id.b.area,
5454 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005455 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 rval = 1;
5458 break;
5459 } else {
5460 /*
5461 * unrecoverable / not handled error
5462 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005463 ql_dbg(ql_dbg_disc, vha, 0x2002,
5464 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5465 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5466 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5467 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
5469 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005470 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005471 fcport->d_id.b.domain, fcport->d_id.b.area,
5472 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005473 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005474 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
5476 rval = 3;
5477 break;
5478 }
5479 }
5480
5481 return (rval);
5482}
5483
5484/*
5485 * qla2x00_local_device_login
5486 * Issue local device login command.
5487 *
5488 * Input:
5489 * ha = adapter block pointer.
5490 * loop_id = loop id of device to login to.
5491 *
5492 * Returns (Where's the #define!!!!):
5493 * 0 - Login successfully
5494 * 1 - Login failed
5495 * 3 - Fatal error
5496 */
5497int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005498qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
5500 int rval;
5501 uint16_t mb[MAILBOX_REGISTER_COUNT];
5502
5503 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005504 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 if (rval == QLA_SUCCESS) {
5506 /* Interrogate mailbox registers for any errors */
5507 if (mb[0] == MBS_COMMAND_ERROR)
5508 rval = 1;
5509 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5510 /* device not in PCB table */
5511 rval = 3;
5512 }
5513
5514 return (rval);
5515}
5516
5517/*
5518 * qla2x00_loop_resync
5519 * Resync with fibre channel devices.
5520 *
5521 * Input:
5522 * ha = adapter block pointer.
5523 *
5524 * Returns:
5525 * 0 = success
5526 */
5527int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005528qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005530 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005532 struct req_que *req;
5533 struct rsp_que *rsp;
5534
Michael Hernandezd7459522016-12-12 14:40:07 -08005535 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005536 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005538 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5539 if (vha->flags.online) {
5540 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5542 wait_time = 256;
5543 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005544 if (!IS_QLAFX00(vha->hw)) {
5545 /*
5546 * Issue a marker after FW becomes
5547 * ready.
5548 */
5549 qla2x00_marker(vha, req, rsp, 0, 0,
5550 MK_SYNC_ALL);
5551 vha->marker_needed = 0;
5552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
5554 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005555 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005557 if (IS_QLAFX00(vha->hw))
5558 qlafx00_configure_devices(vha);
5559 else
5560 qla2x00_configure_loop(vha);
5561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005563 } while (!atomic_read(&vha->loop_down_timer) &&
5564 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5565 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5566 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 }
5569
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005570 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005573 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005574 ql_dbg(ql_dbg_disc, vha, 0x206c,
5575 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 return (rval);
5578}
5579
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005580/*
5581* qla2x00_perform_loop_resync
5582* Description: This function will set the appropriate flags and call
5583* qla2x00_loop_resync. If successful loop will be resynced
5584* Arguments : scsi_qla_host_t pointer
5585* returm : Success or Failure
5586*/
5587
5588int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5589{
5590 int32_t rval = 0;
5591
5592 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5593 /*Configure the flags so that resync happens properly*/
5594 atomic_set(&ha->loop_down_timer, 0);
5595 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5596 atomic_set(&ha->loop_state, LOOP_UP);
5597 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5598 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5599 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5600
5601 rval = qla2x00_loop_resync(ha);
5602 } else
5603 atomic_set(&ha->loop_state, LOOP_DEAD);
5604
5605 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5606 }
5607
5608 return rval;
5609}
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005612qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005613{
5614 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005615 struct scsi_qla_host *vha;
5616 struct qla_hw_data *ha = base_vha->hw;
5617 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005618
Arun Easifeafb7b2010-09-03 14:57:00 -07005619 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005620 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005621 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5622 atomic_inc(&vha->vref_count);
5623 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005624 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005625 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5626 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005627 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005628
Arun Easifeafb7b2010-09-03 14:57:00 -07005629 spin_lock_irqsave(&ha->vport_slock, flags);
5630 }
5631 }
5632 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005633 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005634 }
5635 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005636}
5637
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005638/* Assumes idc_lock always held on entry */
5639void
5640qla83xx_reset_ownership(scsi_qla_host_t *vha)
5641{
5642 struct qla_hw_data *ha = vha->hw;
5643 uint32_t drv_presence, drv_presence_mask;
5644 uint32_t dev_part_info1, dev_part_info2, class_type;
5645 uint32_t class_type_mask = 0x3;
5646 uint16_t fcoe_other_function = 0xffff, i;
5647
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005648 if (IS_QLA8044(ha)) {
5649 drv_presence = qla8044_rd_direct(vha,
5650 QLA8044_CRB_DRV_ACTIVE_INDEX);
5651 dev_part_info1 = qla8044_rd_direct(vha,
5652 QLA8044_CRB_DEV_PART_INFO_INDEX);
5653 dev_part_info2 = qla8044_rd_direct(vha,
5654 QLA8044_CRB_DEV_PART_INFO2);
5655 } else {
5656 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5657 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5658 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5659 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005660 for (i = 0; i < 8; i++) {
5661 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5662 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5663 (i != ha->portnum)) {
5664 fcoe_other_function = i;
5665 break;
5666 }
5667 }
5668 if (fcoe_other_function == 0xffff) {
5669 for (i = 0; i < 8; i++) {
5670 class_type = ((dev_part_info2 >> (i * 4)) &
5671 class_type_mask);
5672 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5673 ((i + 8) != ha->portnum)) {
5674 fcoe_other_function = i + 8;
5675 break;
5676 }
5677 }
5678 }
5679 /*
5680 * Prepare drv-presence mask based on fcoe functions present.
5681 * However consider only valid physical fcoe function numbers (0-15).
5682 */
5683 drv_presence_mask = ~((1 << (ha->portnum)) |
5684 ((fcoe_other_function == 0xffff) ?
5685 0 : (1 << (fcoe_other_function))));
5686
5687 /* We are the reset owner iff:
5688 * - No other protocol drivers present.
5689 * - This is the lowest among fcoe functions. */
5690 if (!(drv_presence & drv_presence_mask) &&
5691 (ha->portnum < fcoe_other_function)) {
5692 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5693 "This host is Reset owner.\n");
5694 ha->flags.nic_core_reset_owner = 1;
5695 }
5696}
5697
Saurav Kashyapfa492632012-11-21 02:40:29 -05005698static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005699__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5700{
5701 int rval = QLA_SUCCESS;
5702 struct qla_hw_data *ha = vha->hw;
5703 uint32_t drv_ack;
5704
5705 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5706 if (rval == QLA_SUCCESS) {
5707 drv_ack |= (1 << ha->portnum);
5708 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5709 }
5710
5711 return rval;
5712}
5713
Saurav Kashyapfa492632012-11-21 02:40:29 -05005714static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005715__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5716{
5717 int rval = QLA_SUCCESS;
5718 struct qla_hw_data *ha = vha->hw;
5719 uint32_t drv_ack;
5720
5721 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5722 if (rval == QLA_SUCCESS) {
5723 drv_ack &= ~(1 << ha->portnum);
5724 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5725 }
5726
5727 return rval;
5728}
5729
Saurav Kashyapfa492632012-11-21 02:40:29 -05005730static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005731qla83xx_dev_state_to_string(uint32_t dev_state)
5732{
5733 switch (dev_state) {
5734 case QLA8XXX_DEV_COLD:
5735 return "COLD/RE-INIT";
5736 case QLA8XXX_DEV_INITIALIZING:
5737 return "INITIALIZING";
5738 case QLA8XXX_DEV_READY:
5739 return "READY";
5740 case QLA8XXX_DEV_NEED_RESET:
5741 return "NEED RESET";
5742 case QLA8XXX_DEV_NEED_QUIESCENT:
5743 return "NEED QUIESCENT";
5744 case QLA8XXX_DEV_FAILED:
5745 return "FAILED";
5746 case QLA8XXX_DEV_QUIESCENT:
5747 return "QUIESCENT";
5748 default:
5749 return "Unknown";
5750 }
5751}
5752
5753/* Assumes idc-lock always held on entry */
5754void
5755qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
5756{
5757 struct qla_hw_data *ha = vha->hw;
5758 uint32_t idc_audit_reg = 0, duration_secs = 0;
5759
5760 switch (audit_type) {
5761 case IDC_AUDIT_TIMESTAMP:
5762 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
5763 idc_audit_reg = (ha->portnum) |
5764 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
5765 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
5766 break;
5767
5768 case IDC_AUDIT_COMPLETION:
5769 duration_secs = ((jiffies_to_msecs(jiffies) -
5770 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
5771 idc_audit_reg = (ha->portnum) |
5772 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
5773 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
5774 break;
5775
5776 default:
5777 ql_log(ql_log_warn, vha, 0xb078,
5778 "Invalid audit type specified.\n");
5779 break;
5780 }
5781}
5782
5783/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05005784static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005785qla83xx_initiating_reset(scsi_qla_host_t *vha)
5786{
5787 struct qla_hw_data *ha = vha->hw;
5788 uint32_t idc_control, dev_state;
5789
5790 __qla83xx_get_idc_control(vha, &idc_control);
5791 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
5792 ql_log(ql_log_info, vha, 0xb080,
5793 "NIC Core reset has been disabled. idc-control=0x%x\n",
5794 idc_control);
5795 return QLA_FUNCTION_FAILED;
5796 }
5797
5798 /* Set NEED-RESET iff in READY state and we are the reset-owner */
5799 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5800 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
5801 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
5802 QLA8XXX_DEV_NEED_RESET);
5803 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
5804 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
5805 } else {
5806 const char *state = qla83xx_dev_state_to_string(dev_state);
5807 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
5808
5809 /* SV: XXX: Is timeout required here? */
5810 /* Wait for IDC state change READY -> NEED_RESET */
5811 while (dev_state == QLA8XXX_DEV_READY) {
5812 qla83xx_idc_unlock(vha, 0);
5813 msleep(200);
5814 qla83xx_idc_lock(vha, 0);
5815 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5816 }
5817 }
5818
5819 /* Send IDC ack by writing to drv-ack register */
5820 __qla83xx_set_drv_ack(vha);
5821
5822 return QLA_SUCCESS;
5823}
5824
5825int
5826__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
5827{
5828 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
5829}
5830
5831int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005832__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
5833{
5834 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
5835}
5836
Saurav Kashyapfa492632012-11-21 02:40:29 -05005837static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005838qla83xx_check_driver_presence(scsi_qla_host_t *vha)
5839{
5840 uint32_t drv_presence = 0;
5841 struct qla_hw_data *ha = vha->hw;
5842
5843 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5844 if (drv_presence & (1 << ha->portnum))
5845 return QLA_SUCCESS;
5846 else
5847 return QLA_TEST_FAILED;
5848}
5849
5850int
5851qla83xx_nic_core_reset(scsi_qla_host_t *vha)
5852{
5853 int rval = QLA_SUCCESS;
5854 struct qla_hw_data *ha = vha->hw;
5855
5856 ql_dbg(ql_dbg_p3p, vha, 0xb058,
5857 "Entered %s().\n", __func__);
5858
5859 if (vha->device_flags & DFLG_DEV_FAILED) {
5860 ql_log(ql_log_warn, vha, 0xb059,
5861 "Device in unrecoverable FAILED state.\n");
5862 return QLA_FUNCTION_FAILED;
5863 }
5864
5865 qla83xx_idc_lock(vha, 0);
5866
5867 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
5868 ql_log(ql_log_warn, vha, 0xb05a,
5869 "Function=0x%x has been removed from IDC participation.\n",
5870 ha->portnum);
5871 rval = QLA_FUNCTION_FAILED;
5872 goto exit;
5873 }
5874
5875 qla83xx_reset_ownership(vha);
5876
5877 rval = qla83xx_initiating_reset(vha);
5878
5879 /*
5880 * Perform reset if we are the reset-owner,
5881 * else wait till IDC state changes to READY/FAILED.
5882 */
5883 if (rval == QLA_SUCCESS) {
5884 rval = qla83xx_idc_state_handler(vha);
5885
5886 if (rval == QLA_SUCCESS)
5887 ha->flags.nic_core_hung = 0;
5888 __qla83xx_clear_drv_ack(vha);
5889 }
5890
5891exit:
5892 qla83xx_idc_unlock(vha, 0);
5893
5894 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
5895
5896 return rval;
5897}
5898
Saurav Kashyap81178772012-08-22 14:21:04 -04005899int
5900qla2xxx_mctp_dump(scsi_qla_host_t *vha)
5901{
5902 struct qla_hw_data *ha = vha->hw;
5903 int rval = QLA_FUNCTION_FAILED;
5904
5905 if (!IS_MCTP_CAPABLE(ha)) {
5906 /* This message can be removed from the final version */
5907 ql_log(ql_log_info, vha, 0x506d,
5908 "This board is not MCTP capable\n");
5909 return rval;
5910 }
5911
5912 if (!ha->mctp_dump) {
5913 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
5914 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
5915
5916 if (!ha->mctp_dump) {
5917 ql_log(ql_log_warn, vha, 0x506e,
5918 "Failed to allocate memory for mctp dump\n");
5919 return rval;
5920 }
5921 }
5922
5923#define MCTP_DUMP_STR_ADDR 0x00000000
5924 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
5925 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
5926 if (rval != QLA_SUCCESS) {
5927 ql_log(ql_log_warn, vha, 0x506f,
5928 "Failed to capture mctp dump\n");
5929 } else {
5930 ql_log(ql_log_info, vha, 0x5070,
5931 "Mctp dump capture for host (%ld/%p).\n",
5932 vha->host_no, ha->mctp_dump);
5933 ha->mctp_dumped = 1;
5934 }
5935
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04005936 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04005937 ha->flags.nic_core_reset_hdlr_active = 1;
5938 rval = qla83xx_restart_nic_firmware(vha);
5939 if (rval)
5940 /* NIC Core reset failed. */
5941 ql_log(ql_log_warn, vha, 0x5071,
5942 "Failed to restart nic firmware\n");
5943 else
5944 ql_dbg(ql_dbg_p3p, vha, 0xb084,
5945 "Restarted NIC firmware successfully.\n");
5946 ha->flags.nic_core_reset_hdlr_active = 0;
5947 }
5948
5949 return rval;
5950
5951}
5952
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005953/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005954* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005955* Description: This function will block the new I/Os
5956* Its not aborting any I/Os as context
5957* is not destroyed during quiescence
5958* Arguments: scsi_qla_host_t
5959* return : void
5960*/
5961void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005962qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005963{
5964 struct qla_hw_data *ha = vha->hw;
5965 struct scsi_qla_host *vp;
5966
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005967 ql_dbg(ql_dbg_dpc, vha, 0x401d,
5968 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005969
5970 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
5971 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
5972 atomic_set(&vha->loop_state, LOOP_DOWN);
5973 qla2x00_mark_all_devices_lost(vha, 0);
5974 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005975 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005976 } else {
5977 if (!atomic_read(&vha->loop_down_timer))
5978 atomic_set(&vha->loop_down_timer,
5979 LOOP_DOWN_TIME);
5980 }
5981 /* Wait for pending cmds to complete */
5982 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
5983}
5984
Giridhar Malavalia9083012010-04-12 17:59:55 -07005985void
5986qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
5987{
5988 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005989 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07005990 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08005991 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005992 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005993
Saurav Kashyape46ef002011-02-23 15:27:16 -08005994 /* For ISP82XX, driver waits for completion of the commands.
5995 * online flag should be set.
5996 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005997 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08005998 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005999 ha->flags.chip_reset_done = 0;
6000 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006001 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006002
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006003 ql_log(ql_log_info, vha, 0x00af,
6004 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006005
Saurav Kashyape46ef002011-02-23 15:27:16 -08006006 /* For ISP82XX, reset_chip is just disabling interrupts.
6007 * Driver waits for the completion of the commands.
6008 * the interrupts need to be enabled.
6009 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006010 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006011 ha->isp_ops->reset_chip(vha);
6012
Quinn Tranec7193e2017-03-15 09:48:55 -07006013 ha->flags.n2n_ae = 0;
6014 ha->flags.lip_ae = 0;
6015 ha->current_topology = 0;
6016 ha->flags.fw_started = 0;
6017 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006018 ha->base_qpair->chip_reset++;
6019 for (i = 0; i < ha->max_qpairs; i++) {
6020 if (ha->queue_pair_map[i])
6021 ha->queue_pair_map[i]->chip_reset =
6022 ha->base_qpair->chip_reset;
6023 }
Quinn Tran726b8542017-01-19 22:28:00 -08006024
Giridhar Malavalia9083012010-04-12 17:59:55 -07006025 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6026 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6027 atomic_set(&vha->loop_state, LOOP_DOWN);
6028 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006029
6030 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006031 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006032 atomic_inc(&vp->vref_count);
6033 spin_unlock_irqrestore(&ha->vport_slock, flags);
6034
Giridhar Malavalia9083012010-04-12 17:59:55 -07006035 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006036
6037 spin_lock_irqsave(&ha->vport_slock, flags);
6038 atomic_dec(&vp->vref_count);
6039 }
6040 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006041 } else {
6042 if (!atomic_read(&vha->loop_down_timer))
6043 atomic_set(&vha->loop_down_timer,
6044 LOOP_DOWN_TIME);
6045 }
6046
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006047 /* Clear all async request states across all VPs. */
6048 list_for_each_entry(fcport, &vha->vp_fcports, list)
6049 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6050 spin_lock_irqsave(&ha->vport_slock, flags);
6051 list_for_each_entry(vp, &ha->vp_list, list) {
6052 atomic_inc(&vp->vref_count);
6053 spin_unlock_irqrestore(&ha->vport_slock, flags);
6054
6055 list_for_each_entry(fcport, &vp->vp_fcports, list)
6056 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6057
6058 spin_lock_irqsave(&ha->vport_slock, flags);
6059 atomic_dec(&vp->vref_count);
6060 }
6061 spin_unlock_irqrestore(&ha->vport_slock, flags);
6062
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006063 if (!ha->flags.eeh_busy) {
6064 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006065 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006066 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006067 ql_log(ql_log_info, vha, 0x00b4,
6068 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006069
Saurav Kashyape46ef002011-02-23 15:27:16 -08006070 /* Done waiting for pending commands.
6071 * Reset the online flag.
6072 */
6073 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006076 /* Requeue all commands in outstanding command list. */
6077 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6078 }
Arun Easib6a029e2014-09-25 06:14:52 -04006079 /* memory barrier */
6080 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081}
6082
6083/*
6084* qla2x00_abort_isp
6085* Resets ISP and aborts all outstanding commands.
6086*
6087* Input:
6088* ha = adapter block pointer.
6089*
6090* Returns:
6091* 0 = success
6092*/
6093int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006094qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006096 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006098 struct qla_hw_data *ha = vha->hw;
6099 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006100 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006101 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006103 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006104 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Santosh Vernekara61712972012-08-22 14:21:13 -04006106 if (IS_QLA8031(ha)) {
6107 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6108 "Clearing fcoe driver presence.\n");
6109 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6110 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6111 "Error while clearing DRV-Presence.\n");
6112 }
6113
Andrew Vasquez85880802009-12-15 21:29:46 -08006114 if (unlikely(pci_channel_offline(ha->pdev) &&
6115 ha->flags.pci_channel_io_perm_failure)) {
6116 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6117 status = 0;
6118 return status;
6119 }
6120
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006121 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006122
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006123 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006125 if (!qla2x00_restart_isp(vha)) {
6126 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006128 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 /*
6130 * Issue marker command only when we are going
6131 * to start the I/O .
6132 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006133 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 }
6135
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006136 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006138 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006140 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006141 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006142
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006143 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6144 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006145 if (ha->fce) {
6146 ha->flags.fce_enabled = 1;
6147 memset(ha->fce, 0,
6148 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006149 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006150 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6151 &ha->fce_bufs);
6152 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006153 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006154 "Unable to reinitialize FCE "
6155 "(%d).\n", rval);
6156 ha->flags.fce_enabled = 0;
6157 }
6158 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006159
6160 if (ha->eft) {
6161 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006162 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006163 ha->eft_dma, EFT_NUM_BUFFERS);
6164 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006165 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006166 "Unable to reinitialize EFT "
6167 "(%d).\n", rval);
6168 }
6169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006171 vha->flags.online = 1;
6172 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006174 ql_log(ql_log_fatal, vha, 0x8035,
6175 "ISP error recover failed - "
6176 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006177 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 * The next call disables the board
6179 * completely.
6180 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006181 ha->isp_ops->reset_adapter(vha);
6182 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006184 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 status = 0;
6186 } else { /* schedule another ISP abort */
6187 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006188 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6189 "ISP abort - retry remaining %d.\n",
6190 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 status = 1;
6192 }
6193 } else {
6194 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006195 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6196 "ISP error recovery - retrying (%d) "
6197 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006198 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 status = 1;
6200 }
6201 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 }
6204
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006205 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006206 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006207 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006208 spin_lock_irqsave(&ha->vport_slock, flags);
6209 list_for_each_entry(vp, &ha->vp_list, list) {
6210 if (vp->vp_idx) {
6211 atomic_inc(&vp->vref_count);
6212 spin_unlock_irqrestore(&ha->vport_slock, flags);
6213
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006214 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006215
6216 spin_lock_irqsave(&ha->vport_slock, flags);
6217 atomic_dec(&vp->vref_count);
6218 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006219 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006220 spin_unlock_irqrestore(&ha->vport_slock, flags);
6221
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006222 if (IS_QLA8031(ha)) {
6223 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6224 "Setting back fcoe driver presence.\n");
6225 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6226 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6227 "Error while setting DRV-Presence.\n");
6228 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006229 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006230 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6231 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 }
6233
6234 return(status);
6235}
6236
6237/*
6238* qla2x00_restart_isp
6239* restarts the ISP after a reset
6240*
6241* Input:
6242* ha = adapter block pointer.
6243*
6244* Returns:
6245* 0 = success
6246*/
6247static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006248qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006250 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006251 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006252 struct req_que *req = ha->req_q_map[0];
6253 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
6255 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006256 if (qla2x00_isp_firmware(vha)) {
6257 vha->flags.online = 0;
6258 status = ha->isp_ops->chip_diag(vha);
6259 if (!status)
6260 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 }
6262
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006263 if (!status && !(status = qla2x00_init_rings(vha))) {
6264 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006265 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006266
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006267 /* Initialize the queues in use */
6268 qla25xx_init_queues(ha);
6269
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006270 status = qla2x00_fw_ready(vha);
6271 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006272 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006273 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006274 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 }
6276
6277 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006278 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 }
6281 return (status);
6282}
6283
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006284static int
6285qla25xx_init_queues(struct qla_hw_data *ha)
6286{
6287 struct rsp_que *rsp = NULL;
6288 struct req_que *req = NULL;
6289 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6290 int ret = -1;
6291 int i;
6292
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006293 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006294 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006295 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006296 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006297 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006298 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006299 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6300 "%s Rsp que: %d init failed.\n",
6301 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006302 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006303 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6304 "%s Rsp que: %d inited.\n",
6305 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006306 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006307 }
6308 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006309 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006310 if (req && test_bit(i, ha->req_qid_map)) {
6311 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006312 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006313 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006314 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006315 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6316 "%s Req que: %d init failed.\n",
6317 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006318 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006319 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6320 "%s Req que: %d inited.\n",
6321 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006322 }
6323 }
6324 return ret;
6325}
6326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327/*
6328* qla2x00_reset_adapter
6329* Reset adapter.
6330*
6331* Input:
6332* ha = adapter block pointer.
6333*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006334void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006335qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
6337 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006338 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006339 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006341 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006342 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 spin_lock_irqsave(&ha->hardware_lock, flags);
6345 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6346 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6347 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6348 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6349 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6350}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006351
6352void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006353qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006354{
6355 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006356 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006357 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6358
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006359 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006360 return;
6361
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006362 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006363 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006364
6365 spin_lock_irqsave(&ha->hardware_lock, flags);
6366 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6367 RD_REG_DWORD(&reg->hccr);
6368 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6369 RD_REG_DWORD(&reg->hccr);
6370 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006371
6372 if (IS_NOPOLLING_TYPE(ha))
6373 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006374}
6375
David Miller4e08df32007-04-16 12:37:43 -07006376/* On sparc systems, obtain port and node WWN from firmware
6377 * properties.
6378 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006379static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6380 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006381{
6382#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006383 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006384 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006385 struct device_node *dp = pci_device_to_OF_node(pdev);
6386 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006387 int len;
6388
6389 val = of_get_property(dp, "port-wwn", &len);
6390 if (val && len >= WWN_SIZE)
6391 memcpy(nv->port_name, val, WWN_SIZE);
6392
6393 val = of_get_property(dp, "node-wwn", &len);
6394 if (val && len >= WWN_SIZE)
6395 memcpy(nv->node_name, val, WWN_SIZE);
6396#endif
6397}
6398
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006399int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006400qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006401{
David Miller4e08df32007-04-16 12:37:43 -07006402 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006403 struct init_cb_24xx *icb;
6404 struct nvram_24xx *nv;
6405 uint32_t *dptr;
6406 uint8_t *dptr1, *dptr2;
6407 uint32_t chksum;
6408 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006409 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006410
David Miller4e08df32007-04-16 12:37:43 -07006411 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006412 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006413 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006414
6415 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006416 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006417 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6418 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6419 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006420 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006421 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6422 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006423
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006424 ha->nvram_size = sizeof(struct nvram_24xx);
6425 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006426
Seokmann Ju281afe12007-07-26 13:43:34 -07006427 /* Get VPD data into cache */
6428 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006429 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006430 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6431
6432 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006433 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006434 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006435 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006436 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6437 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006438
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006439 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6440 "Contents of NVRAM\n");
6441 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6442 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006443
6444 /* Bad NVRAM data, set defaults parameters. */
6445 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6446 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006447 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006448 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006449 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006450 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006451 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6452 ql_log(ql_log_warn, vha, 0x006c,
6453 "Falling back to functioning (yet invalid -- WWPN) "
6454 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006455
6456 /*
6457 * Set default initialization control block.
6458 */
6459 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006460 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6461 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006462 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006463 nv->execution_throttle = cpu_to_le16(0xFFFF);
6464 nv->exchange_count = cpu_to_le16(0);
6465 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006466 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006467 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006468 nv->port_name[2] = 0x00;
6469 nv->port_name[3] = 0xe0;
6470 nv->port_name[4] = 0x8b;
6471 nv->port_name[5] = 0x1c;
6472 nv->port_name[6] = 0x55;
6473 nv->port_name[7] = 0x86;
6474 nv->node_name[0] = 0x20;
6475 nv->node_name[1] = 0x00;
6476 nv->node_name[2] = 0x00;
6477 nv->node_name[3] = 0xe0;
6478 nv->node_name[4] = 0x8b;
6479 nv->node_name[5] = 0x1c;
6480 nv->node_name[6] = 0x55;
6481 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006482 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006483 nv->login_retry_count = cpu_to_le16(8);
6484 nv->interrupt_delay_timer = cpu_to_le16(0);
6485 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006486 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006487 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6488 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6489 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6490 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6491 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6492 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006493 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006494 nv->max_luns_per_target = cpu_to_le16(128);
6495 nv->port_down_retry_count = cpu_to_le16(30);
6496 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006497
6498 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006499 }
6500
Quinn Tran726b8542017-01-19 22:28:00 -08006501 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006502 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006503 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006504 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006505 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006506 }
6507
6508 qlt_24xx_config_nvram_stage1(vha, nv);
6509
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006510 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006511 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006512
6513 /* Copy 1st segment. */
6514 dptr1 = (uint8_t *)icb;
6515 dptr2 = (uint8_t *)&nv->version;
6516 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6517 while (cnt--)
6518 *dptr1++ = *dptr2++;
6519
6520 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006521 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006522
6523 /* Copy 2nd segment. */
6524 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6525 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6526 cnt = (uint8_t *)&icb->reserved_3 -
6527 (uint8_t *)&icb->interrupt_delay_timer;
6528 while (cnt--)
6529 *dptr1++ = *dptr2++;
6530
6531 /*
6532 * Setup driver NVRAM options.
6533 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006534 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006535 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006536
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006537 qlt_24xx_config_nvram_stage2(vha, icb);
6538
Bart Van Asschead950362015-07-09 07:24:08 -07006539 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006540 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006541 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6542 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6543 }
6544
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006545 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006546 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006547 /*
6548 * Firmware will apply the following mask if the nodename was
6549 * not provided.
6550 */
6551 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6552 icb->node_name[0] &= 0xF0;
6553 }
6554
6555 /* Set host adapter parameters. */
6556 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006557 ha->flags.enable_lip_reset = 0;
6558 ha->flags.enable_lip_full_login =
6559 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6560 ha->flags.enable_target_reset =
6561 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006562 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006563 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006564
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006565 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6566 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006567
6568 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6569 sizeof(ha->fw_seriallink_options24));
6570
6571 /* save HBA serial number */
6572 ha->serial0 = icb->port_name[5];
6573 ha->serial1 = icb->port_name[6];
6574 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006575 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6576 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006577
Bart Van Asschead950362015-07-09 07:24:08 -07006578 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006579
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006580 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6581
6582 /* Set minimum login_timeout to 4 seconds. */
6583 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6584 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6585 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006586 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006587 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006588
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006589 /* Set minimum RATOV to 100 tenths of a second. */
6590 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006591
6592 ha->loop_reset_delay = nv->reset_delay;
6593
6594 /* Link Down Timeout = 0:
6595 *
6596 * When Port Down timer expires we will start returning
6597 * I/O's to OS with "DID_NO_CONNECT".
6598 *
6599 * Link Down Timeout != 0:
6600 *
6601 * The driver waits for the link to come up after link down
6602 * before returning I/Os to OS with "DID_NO_CONNECT".
6603 */
6604 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6605 ha->loop_down_abort_time =
6606 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6607 } else {
6608 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6609 ha->loop_down_abort_time =
6610 (LOOP_DOWN_TIME - ha->link_down_timeout);
6611 }
6612
6613 /* Need enough time to try and get the port back. */
6614 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6615 if (qlport_down_retry)
6616 ha->port_down_retry_count = qlport_down_retry;
6617
6618 /* Set login_retry_count */
6619 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6620 if (ha->port_down_retry_count ==
6621 le16_to_cpu(nv->port_down_retry_count) &&
6622 ha->port_down_retry_count > 3)
6623 ha->login_retry_count = ha->port_down_retry_count;
6624 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6625 ha->login_retry_count = ha->port_down_retry_count;
6626 if (ql2xloginretrycount)
6627 ha->login_retry_count = ql2xloginretrycount;
6628
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006629 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006630 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006631 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6632 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6633 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6634 le16_to_cpu(icb->interrupt_delay_timer): 2;
6635 }
Bart Van Asschead950362015-07-09 07:24:08 -07006636 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006637 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006638 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006639 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006640 ha->zio_mode = QLA_ZIO_MODE_6;
6641
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006642 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006643 "ZIO mode %d enabled; timer delay (%d us).\n",
6644 ha->zio_mode, ha->zio_timer * 100);
6645
6646 icb->firmware_options_2 |= cpu_to_le32(
6647 (uint32_t)ha->zio_mode);
6648 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006649 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006650 }
6651
David Miller4e08df32007-04-16 12:37:43 -07006652 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006653 ql_log(ql_log_warn, vha, 0x0070,
6654 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006655 }
6656 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006657}
6658
Sawan Chandak4243c112016-01-27 12:03:31 -05006659uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6660{
6661 struct qla27xx_image_status pri_image_status, sec_image_status;
6662 uint8_t valid_pri_image, valid_sec_image;
6663 uint32_t *wptr;
6664 uint32_t cnt, chksum, size;
6665 struct qla_hw_data *ha = vha->hw;
6666
6667 valid_pri_image = valid_sec_image = 1;
6668 ha->active_image = 0;
6669 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6670
6671 if (!ha->flt_region_img_status_pri) {
6672 valid_pri_image = 0;
6673 goto check_sec_image;
6674 }
6675
6676 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6677 ha->flt_region_img_status_pri, size);
6678
6679 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6680 ql_dbg(ql_dbg_init, vha, 0x018b,
6681 "Primary image signature (0x%x) not valid\n",
6682 pri_image_status.signature);
6683 valid_pri_image = 0;
6684 goto check_sec_image;
6685 }
6686
6687 wptr = (uint32_t *)(&pri_image_status);
6688 cnt = size;
6689
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006690 for (chksum = 0; cnt--; wptr++)
6691 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006692
Sawan Chandak4243c112016-01-27 12:03:31 -05006693 if (chksum) {
6694 ql_dbg(ql_dbg_init, vha, 0x018c,
6695 "Checksum validation failed for primary image (0x%x)\n",
6696 chksum);
6697 valid_pri_image = 0;
6698 }
6699
6700check_sec_image:
6701 if (!ha->flt_region_img_status_sec) {
6702 valid_sec_image = 0;
6703 goto check_valid_image;
6704 }
6705
6706 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6707 ha->flt_region_img_status_sec, size);
6708
6709 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6710 ql_dbg(ql_dbg_init, vha, 0x018d,
6711 "Secondary image signature(0x%x) not valid\n",
6712 sec_image_status.signature);
6713 valid_sec_image = 0;
6714 goto check_valid_image;
6715 }
6716
6717 wptr = (uint32_t *)(&sec_image_status);
6718 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006719 for (chksum = 0; cnt--; wptr++)
6720 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05006721 if (chksum) {
6722 ql_dbg(ql_dbg_init, vha, 0x018e,
6723 "Checksum validation failed for secondary image (0x%x)\n",
6724 chksum);
6725 valid_sec_image = 0;
6726 }
6727
6728check_valid_image:
6729 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
6730 ha->active_image = QLA27XX_PRIMARY_IMAGE;
6731 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
6732 if (!ha->active_image ||
6733 pri_image_status.generation_number <
6734 sec_image_status.generation_number)
6735 ha->active_image = QLA27XX_SECONDARY_IMAGE;
6736 }
6737
6738 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
6739 ha->active_image == 0 ? "default bootld and fw" :
6740 ha->active_image == 1 ? "primary" :
6741 ha->active_image == 2 ? "secondary" :
6742 "Invalid");
6743
6744 return ha->active_image;
6745}
6746
Adrian Bunk413975a2006-06-30 02:33:06 -07006747static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006748qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
6749 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006750{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006751 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006752 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006753 uint32_t *dcode, dlen;
6754 uint32_t risc_addr;
6755 uint32_t risc_size;
6756 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006757 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006758 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006759
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006760 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08006761 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006762
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006763 rval = QLA_SUCCESS;
6764
6765 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006766 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006767 *srisc_addr = 0;
6768
Sawan Chandak4243c112016-01-27 12:03:31 -05006769 if (IS_QLA27XX(ha) &&
6770 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
6771 faddr = ha->flt_region_fw_sec;
6772
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006773 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006774 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006775 for (i = 0; i < 4; i++)
6776 dcode[i] = be32_to_cpu(dcode[i]);
6777 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
6778 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
6779 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
6780 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006781 ql_log(ql_log_fatal, vha, 0x008c,
6782 "Unable to verify the integrity of flash firmware "
6783 "image.\n");
6784 ql_log(ql_log_fatal, vha, 0x008d,
6785 "Firmware data: %08x %08x %08x %08x.\n",
6786 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006787
6788 return QLA_FUNCTION_FAILED;
6789 }
6790
6791 while (segments && rval == QLA_SUCCESS) {
6792 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006793 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006794
6795 risc_addr = be32_to_cpu(dcode[2]);
6796 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
6797 risc_size = be32_to_cpu(dcode[3]);
6798
6799 fragment = 0;
6800 while (risc_size > 0 && rval == QLA_SUCCESS) {
6801 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
6802 if (dlen > risc_size)
6803 dlen = risc_size;
6804
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006805 ql_dbg(ql_dbg_init, vha, 0x008e,
6806 "Loading risc segment@ risc addr %x "
6807 "number of dwords 0x%x offset 0x%x.\n",
6808 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006809
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006810 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006811 for (i = 0; i < dlen; i++)
6812 dcode[i] = swab32(dcode[i]);
6813
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006814 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006815 dlen);
6816 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006817 ql_log(ql_log_fatal, vha, 0x008f,
6818 "Failed to load segment %d of firmware.\n",
6819 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04006820 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006821 }
6822
6823 faddr += dlen;
6824 risc_addr += dlen;
6825 risc_size -= dlen;
6826 fragment++;
6827 }
6828
6829 /* Next segment. */
6830 segments--;
6831 }
6832
Chad Dupuisf73cb692014-02-26 04:15:06 -05006833 if (!IS_QLA27XX(ha))
6834 return rval;
6835
6836 if (ha->fw_dump_template)
6837 vfree(ha->fw_dump_template);
6838 ha->fw_dump_template = NULL;
6839 ha->fw_dump_template_len = 0;
6840
6841 ql_dbg(ql_dbg_init, vha, 0x0161,
6842 "Loading fwdump template from %x\n", faddr);
6843 qla24xx_read_flash_data(vha, dcode, faddr, 7);
6844 risc_size = be32_to_cpu(dcode[2]);
6845 ql_dbg(ql_dbg_init, vha, 0x0162,
6846 "-> array size %x dwords\n", risc_size);
6847 if (risc_size == 0 || risc_size == ~0)
6848 goto default_template;
6849
6850 dlen = (risc_size - 8) * sizeof(*dcode);
6851 ql_dbg(ql_dbg_init, vha, 0x0163,
6852 "-> template allocating %x bytes...\n", dlen);
6853 ha->fw_dump_template = vmalloc(dlen);
6854 if (!ha->fw_dump_template) {
6855 ql_log(ql_log_warn, vha, 0x0164,
6856 "Failed fwdump template allocate %x bytes.\n", risc_size);
6857 goto default_template;
6858 }
6859
6860 faddr += 7;
6861 risc_size -= 8;
6862 dcode = ha->fw_dump_template;
6863 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
6864 for (i = 0; i < risc_size; i++)
6865 dcode[i] = le32_to_cpu(dcode[i]);
6866
6867 if (!qla27xx_fwdt_template_valid(dcode)) {
6868 ql_log(ql_log_warn, vha, 0x0165,
6869 "Failed fwdump template validate\n");
6870 goto default_template;
6871 }
6872
6873 dlen = qla27xx_fwdt_template_size(dcode);
6874 ql_dbg(ql_dbg_init, vha, 0x0166,
6875 "-> template size %x bytes\n", dlen);
6876 if (dlen > risc_size * sizeof(*dcode)) {
6877 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07006878 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07006879 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006880 goto default_template;
6881 }
6882 ha->fw_dump_template_len = dlen;
6883 return rval;
6884
6885default_template:
6886 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
6887 if (ha->fw_dump_template)
6888 vfree(ha->fw_dump_template);
6889 ha->fw_dump_template = NULL;
6890 ha->fw_dump_template_len = 0;
6891
6892 dlen = qla27xx_fwdt_template_default_size();
6893 ql_dbg(ql_dbg_init, vha, 0x0169,
6894 "-> template allocating %x bytes...\n", dlen);
6895 ha->fw_dump_template = vmalloc(dlen);
6896 if (!ha->fw_dump_template) {
6897 ql_log(ql_log_warn, vha, 0x016a,
6898 "Failed fwdump template allocate %x bytes.\n", risc_size);
6899 goto failed_template;
6900 }
6901
6902 dcode = ha->fw_dump_template;
6903 risc_size = dlen / sizeof(*dcode);
6904 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
6905 for (i = 0; i < risc_size; i++)
6906 dcode[i] = be32_to_cpu(dcode[i]);
6907
6908 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
6909 ql_log(ql_log_warn, vha, 0x016b,
6910 "Failed fwdump template validate\n");
6911 goto failed_template;
6912 }
6913
6914 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
6915 ql_dbg(ql_dbg_init, vha, 0x016c,
6916 "-> template size %x bytes\n", dlen);
6917 ha->fw_dump_template_len = dlen;
6918 return rval;
6919
6920failed_template:
6921 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
6922 if (ha->fw_dump_template)
6923 vfree(ha->fw_dump_template);
6924 ha->fw_dump_template = NULL;
6925 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006926 return rval;
6927}
6928
Giridhar Malavalie9454a82013-02-08 01:57:47 -05006929#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006930
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006931int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006932qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08006933{
6934 int rval;
6935 int i, fragment;
6936 uint16_t *wcode, *fwcode;
6937 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
6938 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006939 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006940 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08006941
6942 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006943 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08006944 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006945 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01006946 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006947 ql_log(ql_log_info, vha, 0x0084,
6948 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08006949 return QLA_FUNCTION_FAILED;
6950 }
6951
6952 rval = QLA_SUCCESS;
6953
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006954 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08006955 *srisc_addr = 0;
6956 fwcode = (uint16_t *)blob->fw->data;
6957 fwclen = 0;
6958
6959 /* Validate firmware image by checking version. */
6960 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006961 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006962 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08006963 blob->fw->size);
6964 goto fail_fw_integrity;
6965 }
6966 for (i = 0; i < 4; i++)
6967 wcode[i] = be16_to_cpu(fwcode[i + 4]);
6968 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
6969 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
6970 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006971 ql_log(ql_log_fatal, vha, 0x0086,
6972 "Unable to verify integrity of firmware image.\n");
6973 ql_log(ql_log_fatal, vha, 0x0087,
6974 "Firmware data: %04x %04x %04x %04x.\n",
6975 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08006976 goto fail_fw_integrity;
6977 }
6978
6979 seg = blob->segs;
6980 while (*seg && rval == QLA_SUCCESS) {
6981 risc_addr = *seg;
6982 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
6983 risc_size = be16_to_cpu(fwcode[3]);
6984
6985 /* Validate firmware image size. */
6986 fwclen += risc_size * sizeof(uint16_t);
6987 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006988 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08006989 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006990 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08006991 goto fail_fw_integrity;
6992 }
6993
6994 fragment = 0;
6995 while (risc_size > 0 && rval == QLA_SUCCESS) {
6996 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
6997 if (wlen > risc_size)
6998 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006999 ql_dbg(ql_dbg_init, vha, 0x0089,
7000 "Loading risc segment@ risc addr %x number of "
7001 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007002
7003 for (i = 0; i < wlen; i++)
7004 wcode[i] = swab16(fwcode[i]);
7005
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007006 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007007 wlen);
7008 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007009 ql_log(ql_log_fatal, vha, 0x008a,
7010 "Failed to load segment %d of firmware.\n",
7011 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007012 break;
7013 }
7014
7015 fwcode += wlen;
7016 risc_addr += wlen;
7017 risc_size -= wlen;
7018 fragment++;
7019 }
7020
7021 /* Next segment. */
7022 seg++;
7023 }
7024 return rval;
7025
7026fail_fw_integrity:
7027 return QLA_FUNCTION_FAILED;
7028}
7029
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007030static int
7031qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007032{
7033 int rval;
7034 int segments, fragment;
7035 uint32_t *dcode, dlen;
7036 uint32_t risc_addr;
7037 uint32_t risc_size;
7038 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007039 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007040 const uint32_t *fwcode;
7041 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007042 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007043 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007044
Andrew Vasquez54333832005-11-09 15:49:04 -08007045 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007046 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007047 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007048 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007049 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007050 ql_log(ql_log_warn, vha, 0x0091,
7051 "Firmware images can be retrieved from: "
7052 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007053
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007054 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007055 }
7056
Chad Dupuiscfb09192011-11-18 09:03:07 -08007057 ql_dbg(ql_dbg_init, vha, 0x0092,
7058 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007059
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007060 rval = QLA_SUCCESS;
7061
7062 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007063 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007064 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007065 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007066 fwclen = 0;
7067
7068 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007069 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007070 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007071 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007072 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007073 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007074 }
7075 for (i = 0; i < 4; i++)
7076 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7077 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7078 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7079 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7080 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007081 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007082 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007083 blob->fw->size);
7084 ql_log(ql_log_fatal, vha, 0x0095,
7085 "Firmware data: %08x %08x %08x %08x.\n",
7086 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007087 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007088 }
7089
7090 while (segments && rval == QLA_SUCCESS) {
7091 risc_addr = be32_to_cpu(fwcode[2]);
7092 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7093 risc_size = be32_to_cpu(fwcode[3]);
7094
7095 /* Validate firmware image size. */
7096 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007097 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007098 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007099 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007100 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007101 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007102 }
7103
7104 fragment = 0;
7105 while (risc_size > 0 && rval == QLA_SUCCESS) {
7106 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7107 if (dlen > risc_size)
7108 dlen = risc_size;
7109
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007110 ql_dbg(ql_dbg_init, vha, 0x0097,
7111 "Loading risc segment@ risc addr %x "
7112 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007113
7114 for (i = 0; i < dlen; i++)
7115 dcode[i] = swab32(fwcode[i]);
7116
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007117 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007118 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007119 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007120 ql_log(ql_log_fatal, vha, 0x0098,
7121 "Failed to load segment %d of firmware.\n",
7122 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007123 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007124 }
7125
7126 fwcode += dlen;
7127 risc_addr += dlen;
7128 risc_size -= dlen;
7129 fragment++;
7130 }
7131
7132 /* Next segment. */
7133 segments--;
7134 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007135
7136 if (!IS_QLA27XX(ha))
7137 return rval;
7138
7139 if (ha->fw_dump_template)
7140 vfree(ha->fw_dump_template);
7141 ha->fw_dump_template = NULL;
7142 ha->fw_dump_template_len = 0;
7143
7144 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007145 "Loading fwdump template from %x\n",
7146 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007147 risc_size = be32_to_cpu(fwcode[2]);
7148 ql_dbg(ql_dbg_init, vha, 0x172,
7149 "-> array size %x dwords\n", risc_size);
7150 if (risc_size == 0 || risc_size == ~0)
7151 goto default_template;
7152
7153 dlen = (risc_size - 8) * sizeof(*fwcode);
7154 ql_dbg(ql_dbg_init, vha, 0x0173,
7155 "-> template allocating %x bytes...\n", dlen);
7156 ha->fw_dump_template = vmalloc(dlen);
7157 if (!ha->fw_dump_template) {
7158 ql_log(ql_log_warn, vha, 0x0174,
7159 "Failed fwdump template allocate %x bytes.\n", risc_size);
7160 goto default_template;
7161 }
7162
7163 fwcode += 7;
7164 risc_size -= 8;
7165 dcode = ha->fw_dump_template;
7166 for (i = 0; i < risc_size; i++)
7167 dcode[i] = le32_to_cpu(fwcode[i]);
7168
7169 if (!qla27xx_fwdt_template_valid(dcode)) {
7170 ql_log(ql_log_warn, vha, 0x0175,
7171 "Failed fwdump template validate\n");
7172 goto default_template;
7173 }
7174
7175 dlen = qla27xx_fwdt_template_size(dcode);
7176 ql_dbg(ql_dbg_init, vha, 0x0176,
7177 "-> template size %x bytes\n", dlen);
7178 if (dlen > risc_size * sizeof(*fwcode)) {
7179 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007180 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007181 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007182 goto default_template;
7183 }
7184 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007185 return rval;
7186
Chad Dupuisf73cb692014-02-26 04:15:06 -05007187default_template:
7188 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7189 if (ha->fw_dump_template)
7190 vfree(ha->fw_dump_template);
7191 ha->fw_dump_template = NULL;
7192 ha->fw_dump_template_len = 0;
7193
7194 dlen = qla27xx_fwdt_template_default_size();
7195 ql_dbg(ql_dbg_init, vha, 0x0179,
7196 "-> template allocating %x bytes...\n", dlen);
7197 ha->fw_dump_template = vmalloc(dlen);
7198 if (!ha->fw_dump_template) {
7199 ql_log(ql_log_warn, vha, 0x017a,
7200 "Failed fwdump template allocate %x bytes.\n", risc_size);
7201 goto failed_template;
7202 }
7203
7204 dcode = ha->fw_dump_template;
7205 risc_size = dlen / sizeof(*fwcode);
7206 fwcode = qla27xx_fwdt_template_default();
7207 for (i = 0; i < risc_size; i++)
7208 dcode[i] = be32_to_cpu(fwcode[i]);
7209
7210 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7211 ql_log(ql_log_warn, vha, 0x017b,
7212 "Failed fwdump template validate\n");
7213 goto failed_template;
7214 }
7215
7216 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7217 ql_dbg(ql_dbg_init, vha, 0x017c,
7218 "-> template size %x bytes\n", dlen);
7219 ha->fw_dump_template_len = dlen;
7220 return rval;
7221
7222failed_template:
7223 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7224 if (ha->fw_dump_template)
7225 vfree(ha->fw_dump_template);
7226 ha->fw_dump_template = NULL;
7227 ha->fw_dump_template_len = 0;
7228 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007229}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007230
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007231int
7232qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7233{
7234 int rval;
7235
Andrew Vasqueze337d902009-04-06 22:33:49 -07007236 if (ql2xfwloadbin == 1)
7237 return qla81xx_load_risc(vha, srisc_addr);
7238
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007239 /*
7240 * FW Load priority:
7241 * 1) Firmware via request-firmware interface (.bin file).
7242 * 2) Firmware residing in flash.
7243 */
7244 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7245 if (rval == QLA_SUCCESS)
7246 return rval;
7247
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007248 return qla24xx_load_risc_flash(vha, srisc_addr,
7249 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007250}
7251
7252int
7253qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7254{
7255 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007256 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007257
Andrew Vasqueze337d902009-04-06 22:33:49 -07007258 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007259 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007260
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007261 /*
7262 * FW Load priority:
7263 * 1) Firmware residing in flash.
7264 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007265 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007266 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007267 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007268 if (rval == QLA_SUCCESS)
7269 return rval;
7270
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007271try_blob_fw:
7272 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7273 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7274 return rval;
7275
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007276 ql_log(ql_log_info, vha, 0x0099,
7277 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007278 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7279 if (rval != QLA_SUCCESS)
7280 return rval;
7281
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007282 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007283 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007284 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007285}
7286
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007287void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007288qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007289{
7290 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007291 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007292
Andrew Vasquez85880802009-12-15 21:29:46 -08007293 if (ha->flags.pci_channel_io_perm_failure)
7294 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007295 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007296 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007297 if (!ha->fw_major_version)
7298 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007299 if (!ha->flags.fw_started)
7300 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007301
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007302 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007303 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007304 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007305 ha->isp_ops->reset_chip(vha);
7306 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007307 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007308 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007309 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007310 ql_log(ql_log_info, vha, 0x8015,
7311 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007312 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007313 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007314
Quinn Tran4b60c822017-06-13 20:47:21 -07007315 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007316 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007317}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007318
7319int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007320qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007321{
7322 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007323 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007324 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007325 struct qla_hw_data *ha = vha->hw;
7326 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007327 struct req_que *req;
7328 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007329
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007330 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007331 return -EINVAL;
7332
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007333 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007334 if (vha->qpair)
7335 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007336 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007337 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007338 rsp = req->rsp;
7339
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007340 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007341 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007342 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007343 }
7344
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007345 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007346
7347 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007348 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7349 BIT_1);
7350 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7351 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7352 ql_dbg(ql_dbg_init, vha, 0x0120,
7353 "Failed SNS login: loop_id=%x, rval2=%d\n",
7354 NPH_SNS, rval2);
7355 else
7356 ql_dbg(ql_dbg_init, vha, 0x0103,
7357 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7358 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7359 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007360 return (QLA_FUNCTION_FAILED);
7361 }
7362
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007363 atomic_set(&vha->loop_down_timer, 0);
7364 atomic_set(&vha->loop_state, LOOP_UP);
7365 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7366 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7367 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007368
7369 return rval;
7370}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007371
7372/* 84XX Support **************************************************************/
7373
7374static LIST_HEAD(qla_cs84xx_list);
7375static DEFINE_MUTEX(qla_cs84xx_mutex);
7376
7377static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007378qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007379{
7380 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007381 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007382
7383 mutex_lock(&qla_cs84xx_mutex);
7384
7385 /* Find any shared 84xx chip. */
7386 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7387 if (cs84xx->bus == ha->pdev->bus) {
7388 kref_get(&cs84xx->kref);
7389 goto done;
7390 }
7391 }
7392
7393 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7394 if (!cs84xx)
7395 goto done;
7396
7397 kref_init(&cs84xx->kref);
7398 spin_lock_init(&cs84xx->access_lock);
7399 mutex_init(&cs84xx->fw_update_mutex);
7400 cs84xx->bus = ha->pdev->bus;
7401
7402 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7403done:
7404 mutex_unlock(&qla_cs84xx_mutex);
7405 return cs84xx;
7406}
7407
7408static void
7409__qla84xx_chip_release(struct kref *kref)
7410{
7411 struct qla_chip_state_84xx *cs84xx =
7412 container_of(kref, struct qla_chip_state_84xx, kref);
7413
7414 mutex_lock(&qla_cs84xx_mutex);
7415 list_del(&cs84xx->list);
7416 mutex_unlock(&qla_cs84xx_mutex);
7417 kfree(cs84xx);
7418}
7419
7420void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007421qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007422{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007423 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007424 if (ha->cs84xx)
7425 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7426}
7427
7428static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007429qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007430{
7431 int rval;
7432 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007433 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007434
7435 mutex_lock(&ha->cs84xx->fw_update_mutex);
7436
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007437 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007438
7439 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7440
7441 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7442 QLA_SUCCESS;
7443}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007444
7445/* 81XX Support **************************************************************/
7446
7447int
7448qla81xx_nvram_config(scsi_qla_host_t *vha)
7449{
7450 int rval;
7451 struct init_cb_81xx *icb;
7452 struct nvram_81xx *nv;
7453 uint32_t *dptr;
7454 uint8_t *dptr1, *dptr2;
7455 uint32_t chksum;
7456 uint16_t cnt;
7457 struct qla_hw_data *ha = vha->hw;
7458
7459 rval = QLA_SUCCESS;
7460 icb = (struct init_cb_81xx *)ha->init_cb;
7461 nv = ha->nvram;
7462
7463 /* Determine NVRAM starting address. */
7464 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007465 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007466 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7467 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007468
7469 /* Get VPD data into cache */
7470 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007471 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7472 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007473
7474 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007475 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007476 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007477 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007478 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7479 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007480
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007481 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7482 "Contents of NVRAM:\n");
7483 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7484 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007485
7486 /* Bad NVRAM data, set defaults parameters. */
7487 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7488 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007489 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007490 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007491 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007492 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007493 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007494 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007495 ql_log(ql_log_info, vha, 0x0074,
7496 "Falling back to functioning (yet invalid -- WWPN) "
7497 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007498
7499 /*
7500 * Set default initialization control block.
7501 */
7502 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007503 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7504 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007505 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007506 nv->execution_throttle = cpu_to_le16(0xFFFF);
7507 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007508 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007509 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007510 nv->port_name[2] = 0x00;
7511 nv->port_name[3] = 0xe0;
7512 nv->port_name[4] = 0x8b;
7513 nv->port_name[5] = 0x1c;
7514 nv->port_name[6] = 0x55;
7515 nv->port_name[7] = 0x86;
7516 nv->node_name[0] = 0x20;
7517 nv->node_name[1] = 0x00;
7518 nv->node_name[2] = 0x00;
7519 nv->node_name[3] = 0xe0;
7520 nv->node_name[4] = 0x8b;
7521 nv->node_name[5] = 0x1c;
7522 nv->node_name[6] = 0x55;
7523 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007524 nv->login_retry_count = cpu_to_le16(8);
7525 nv->interrupt_delay_timer = cpu_to_le16(0);
7526 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007527 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007528 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7529 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7530 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7531 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7532 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7533 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007534 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007535 nv->max_luns_per_target = cpu_to_le16(128);
7536 nv->port_down_retry_count = cpu_to_le16(30);
7537 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007538 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007539 nv->enode_mac[1] = 0xC0;
7540 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007541 nv->enode_mac[3] = 0x04;
7542 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007543 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007544
7545 rval = 1;
7546 }
7547
Arun Easi9e522cd2012-08-22 14:21:31 -04007548 if (IS_T10_PI_CAPABLE(ha))
7549 nv->frame_payload_size &= ~7;
7550
Arun Easiaa230bc2013-01-30 03:34:39 -05007551 qlt_81xx_config_nvram_stage1(vha, nv);
7552
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007553 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007554 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007555
7556 /* Copy 1st segment. */
7557 dptr1 = (uint8_t *)icb;
7558 dptr2 = (uint8_t *)&nv->version;
7559 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7560 while (cnt--)
7561 *dptr1++ = *dptr2++;
7562
7563 icb->login_retry_count = nv->login_retry_count;
7564
7565 /* Copy 2nd segment. */
7566 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7567 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7568 cnt = (uint8_t *)&icb->reserved_5 -
7569 (uint8_t *)&icb->interrupt_delay_timer;
7570 while (cnt--)
7571 *dptr1++ = *dptr2++;
7572
7573 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7574 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7575 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007576 icb->enode_mac[0] = 0x00;
7577 icb->enode_mac[1] = 0xC0;
7578 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007579 icb->enode_mac[3] = 0x04;
7580 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007581 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007582 }
7583
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007584 /* Use extended-initialization control block. */
7585 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7586
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007587 /*
7588 * Setup driver NVRAM options.
7589 */
7590 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007591 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007592
Arun Easiaa230bc2013-01-30 03:34:39 -05007593 qlt_81xx_config_nvram_stage2(vha, icb);
7594
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007595 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007596 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007597 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7598 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7599 }
7600
7601 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007602 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007603 /*
7604 * Firmware will apply the following mask if the nodename was
7605 * not provided.
7606 */
7607 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7608 icb->node_name[0] &= 0xF0;
7609 }
7610
7611 /* Set host adapter parameters. */
7612 ha->flags.disable_risc_code_load = 0;
7613 ha->flags.enable_lip_reset = 0;
7614 ha->flags.enable_lip_full_login =
7615 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7616 ha->flags.enable_target_reset =
7617 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7618 ha->flags.enable_led_scheme = 0;
7619 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7620
7621 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7622 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7623
7624 /* save HBA serial number */
7625 ha->serial0 = icb->port_name[5];
7626 ha->serial1 = icb->port_name[6];
7627 ha->serial2 = icb->port_name[7];
7628 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7629 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7630
Bart Van Asschead950362015-07-09 07:24:08 -07007631 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007632
7633 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7634
7635 /* Set minimum login_timeout to 4 seconds. */
7636 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7637 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7638 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007639 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007640 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007641
7642 /* Set minimum RATOV to 100 tenths of a second. */
7643 ha->r_a_tov = 100;
7644
7645 ha->loop_reset_delay = nv->reset_delay;
7646
7647 /* Link Down Timeout = 0:
7648 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007649 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007650 * I/O's to OS with "DID_NO_CONNECT".
7651 *
7652 * Link Down Timeout != 0:
7653 *
7654 * The driver waits for the link to come up after link down
7655 * before returning I/Os to OS with "DID_NO_CONNECT".
7656 */
7657 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7658 ha->loop_down_abort_time =
7659 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7660 } else {
7661 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7662 ha->loop_down_abort_time =
7663 (LOOP_DOWN_TIME - ha->link_down_timeout);
7664 }
7665
7666 /* Need enough time to try and get the port back. */
7667 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7668 if (qlport_down_retry)
7669 ha->port_down_retry_count = qlport_down_retry;
7670
7671 /* Set login_retry_count */
7672 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7673 if (ha->port_down_retry_count ==
7674 le16_to_cpu(nv->port_down_retry_count) &&
7675 ha->port_down_retry_count > 3)
7676 ha->login_retry_count = ha->port_down_retry_count;
7677 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7678 ha->login_retry_count = ha->port_down_retry_count;
7679 if (ql2xloginretrycount)
7680 ha->login_retry_count = ql2xloginretrycount;
7681
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007682 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007683 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007684 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007685
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007686 /* Enable ZIO. */
7687 if (!vha->flags.init_done) {
7688 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7689 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7690 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7691 le16_to_cpu(icb->interrupt_delay_timer): 2;
7692 }
Bart Van Asschead950362015-07-09 07:24:08 -07007693 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007694 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7695 vha->flags.process_response_queue = 0;
7696 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7697 ha->zio_mode = QLA_ZIO_MODE_6;
7698
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007699 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007700 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007701 ha->zio_mode,
7702 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007703
7704 icb->firmware_options_2 |= cpu_to_le32(
7705 (uint32_t)ha->zio_mode);
7706 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7707 vha->flags.process_response_queue = 1;
7708 }
7709
Quinn Tran41dc5292017-01-19 22:28:03 -08007710 /* enable RIDA Format2 */
7711 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7712 icb->firmware_options_3 |= BIT_0;
7713
Duane Grigsbyedd05de2017-10-13 09:34:06 -07007714 if (IS_QLA27XX(ha)) {
7715 icb->firmware_options_3 |= BIT_8;
7716 ql_dbg(ql_log_info, vha, 0x0075,
7717 "Enabling direct connection.\n");
7718 }
7719
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007720 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007721 ql_log(ql_log_warn, vha, 0x0076,
7722 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007723 }
7724 return (rval);
7725}
7726
Giridhar Malavalia9083012010-04-12 17:59:55 -07007727int
7728qla82xx_restart_isp(scsi_qla_host_t *vha)
7729{
7730 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007731 struct qla_hw_data *ha = vha->hw;
7732 struct req_que *req = ha->req_q_map[0];
7733 struct rsp_que *rsp = ha->rsp_q_map[0];
7734 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07007735 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007736
7737 status = qla2x00_init_rings(vha);
7738 if (!status) {
7739 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7740 ha->flags.chip_reset_done = 1;
7741
7742 status = qla2x00_fw_ready(vha);
7743 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07007744 /* Issue a marker after FW becomes ready. */
7745 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007746 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04007747 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007748 }
7749
7750 /* if no cable then assume it's good */
7751 if ((vha->device_flags & DFLG_NO_CABLE))
7752 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007753 }
7754
7755 if (!status) {
7756 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7757
7758 if (!atomic_read(&vha->loop_down_timer)) {
7759 /*
7760 * Issue marker command only when we are going
7761 * to start the I/O .
7762 */
7763 vha->marker_needed = 1;
7764 }
7765
Giridhar Malavalia9083012010-04-12 17:59:55 -07007766 ha->isp_ops->enable_intrs(ha);
7767
7768 ha->isp_abort_cnt = 0;
7769 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7770
Saurav Kashyap53296782011-05-10 11:30:06 -07007771 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07007772 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07007773
Giridhar Malavalia9083012010-04-12 17:59:55 -07007774 if (ha->fce) {
7775 ha->flags.fce_enabled = 1;
7776 memset(ha->fce, 0,
7777 fce_calc_size(ha->fce_bufs));
7778 rval = qla2x00_enable_fce_trace(vha,
7779 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
7780 &ha->fce_bufs);
7781 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007782 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007783 "Unable to reinitialize FCE (%d).\n",
7784 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007785 ha->flags.fce_enabled = 0;
7786 }
7787 }
7788
7789 if (ha->eft) {
7790 memset(ha->eft, 0, EFT_SIZE);
7791 rval = qla2x00_enable_eft_trace(vha,
7792 ha->eft_dma, EFT_NUM_BUFFERS);
7793 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007794 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007795 "Unable to reinitialize EFT (%d).\n",
7796 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007797 }
7798 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07007799 }
7800
7801 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007802 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007803 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07007804
7805 spin_lock_irqsave(&ha->vport_slock, flags);
7806 list_for_each_entry(vp, &ha->vp_list, list) {
7807 if (vp->vp_idx) {
7808 atomic_inc(&vp->vref_count);
7809 spin_unlock_irqrestore(&ha->vport_slock, flags);
7810
Giridhar Malavalia9083012010-04-12 17:59:55 -07007811 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07007812
7813 spin_lock_irqsave(&ha->vport_slock, flags);
7814 atomic_dec(&vp->vref_count);
7815 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07007816 }
Arun Easifeafb7b2010-09-03 14:57:00 -07007817 spin_unlock_irqrestore(&ha->vport_slock, flags);
7818
Giridhar Malavalia9083012010-04-12 17:59:55 -07007819 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007820 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007821 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07007822 }
7823
7824 return status;
7825}
7826
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007827void
Andrew Vasquezae97c912010-02-18 10:07:28 -08007828qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007829{
Andrew Vasquezae97c912010-02-18 10:07:28 -08007830 struct qla_hw_data *ha = vha->hw;
7831
Himanshu Madhanif198caf2016-01-27 12:03:30 -05007832 /* Hold status IOCBs until ABTS response received. */
7833 if (ql2xfwholdabts)
7834 ha->fw_options[3] |= BIT_12;
7835
Giridhar Malavali088d09d2016-07-06 11:14:20 -04007836 /* Set Retry FLOGI in case of P2P connection */
7837 if (ha->operating_mode == P2P) {
7838 ha->fw_options[2] |= BIT_3;
7839 ql_dbg(ql_dbg_disc, vha, 0x2103,
7840 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
7841 __func__, ha->fw_options[2]);
7842 }
7843
Quinn Tran41dc5292017-01-19 22:28:03 -08007844 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
7845 if (ql2xmvasynctoatio) {
7846 if (qla_tgt_mode_enabled(vha) ||
7847 qla_dual_mode_enabled(vha))
7848 ha->fw_options[2] |= BIT_11;
7849 else
7850 ha->fw_options[2] &= ~BIT_11;
7851 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08007852
Quinn Tranf7e761f2017-06-02 09:12:02 -07007853 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07007854 qla_dual_mode_enabled(vha)) {
7855 /* FW auto send SCSI status during */
7856 ha->fw_options[1] |= BIT_8;
7857 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
7858
7859 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07007860 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07007861 } else {
7862 ha->fw_options[1] &= ~BIT_8;
7863 ha->fw_options[10] &= 0x00ff;
7864
Quinn Tranf7e761f2017-06-02 09:12:02 -07007865 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07007866 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07007867
Quinn Tran41dc5292017-01-19 22:28:03 -08007868 if (ql2xetsenable) {
7869 /* Enable ETS Burst. */
7870 memset(ha->fw_options, 0, sizeof(ha->fw_options));
7871 ha->fw_options[2] |= BIT_9;
7872 }
7873
Quinn Tran83548fe2017-06-02 09:12:01 -07007874 ql_dbg(ql_dbg_init, vha, 0x00e9,
7875 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
7876 __func__, ha->fw_options[1], ha->fw_options[2],
7877 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08007878
Andrew Vasquezae97c912010-02-18 10:07:28 -08007879 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007880}
Sarang Radke09ff7012010-03-19 17:03:59 -07007881
7882/*
7883 * qla24xx_get_fcp_prio
7884 * Gets the fcp cmd priority value for the logged in port.
7885 * Looks for a match of the port descriptors within
7886 * each of the fcp prio config entries. If a match is found,
7887 * the tag (priority) value is returned.
7888 *
7889 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007890 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07007891 * fcport = port structure pointer.
7892 *
7893 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07007894 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007895 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07007896 *
7897 * Context:
7898 * Kernel context
7899 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007900static int
Sarang Radke09ff7012010-03-19 17:03:59 -07007901qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
7902{
7903 int i, entries;
7904 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007905 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07007906 uint32_t pid1, pid2;
7907 uint64_t wwn1, wwn2;
7908 struct qla_fcp_prio_entry *pri_entry;
7909 struct qla_hw_data *ha = vha->hw;
7910
7911 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007912 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07007913
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007914 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07007915 entries = ha->fcp_prio_cfg->num_entries;
7916 pri_entry = &ha->fcp_prio_cfg->entry[0];
7917
7918 for (i = 0; i < entries; i++) {
7919 pid_match = wwn_match = 0;
7920
7921 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
7922 pri_entry++;
7923 continue;
7924 }
7925
7926 /* check source pid for a match */
7927 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
7928 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
7929 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
7930 if (pid1 == INVALID_PORT_ID)
7931 pid_match++;
7932 else if (pid1 == pid2)
7933 pid_match++;
7934 }
7935
7936 /* check destination pid for a match */
7937 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
7938 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
7939 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
7940 if (pid1 == INVALID_PORT_ID)
7941 pid_match++;
7942 else if (pid1 == pid2)
7943 pid_match++;
7944 }
7945
7946 /* check source WWN for a match */
7947 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
7948 wwn1 = wwn_to_u64(vha->port_name);
7949 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
7950 if (wwn2 == (uint64_t)-1)
7951 wwn_match++;
7952 else if (wwn1 == wwn2)
7953 wwn_match++;
7954 }
7955
7956 /* check destination WWN for a match */
7957 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
7958 wwn1 = wwn_to_u64(fcport->port_name);
7959 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
7960 if (wwn2 == (uint64_t)-1)
7961 wwn_match++;
7962 else if (wwn1 == wwn2)
7963 wwn_match++;
7964 }
7965
7966 if (pid_match == 2 || wwn_match == 2) {
7967 /* Found a matching entry */
7968 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
7969 priority = pri_entry->tag;
7970 break;
7971 }
7972
7973 pri_entry++;
7974 }
7975
7976 return priority;
7977}
7978
7979/*
7980 * qla24xx_update_fcport_fcp_prio
7981 * Activates fcp priority for the logged in fc port
7982 *
7983 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007984 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07007985 * fcp = port structure pointer.
7986 *
7987 * Return:
7988 * QLA_SUCCESS or QLA_FUNCTION_FAILED
7989 *
7990 * Context:
7991 * Kernel context.
7992 */
7993int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007994qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07007995{
7996 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007997 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07007998 uint16_t mb[5];
7999
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008000 if (fcport->port_type != FCT_TARGET ||
8001 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008002 return QLA_FUNCTION_FAILED;
8003
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008004 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008005 if (priority < 0)
8006 return QLA_FUNCTION_FAILED;
8007
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008008 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008009 fcport->fcp_prio = priority & 0xf;
8010 return QLA_SUCCESS;
8011 }
8012
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008013 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008014 if (ret == QLA_SUCCESS) {
8015 if (fcport->fcp_prio != priority)
8016 ql_dbg(ql_dbg_user, vha, 0x709e,
8017 "Updated FCP_CMND priority - value=%d loop_id=%d "
8018 "port_id=%02x%02x%02x.\n", priority,
8019 fcport->loop_id, fcport->d_id.b.domain,
8020 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008021 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008022 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008023 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008024 "Unable to update FCP_CMND priority - ret=0x%x for "
8025 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8026 fcport->d_id.b.domain, fcport->d_id.b.area,
8027 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008028 return ret;
8029}
8030
8031/*
8032 * qla24xx_update_all_fcp_prio
8033 * Activates fcp priority for all the logged in ports
8034 *
8035 * Input:
8036 * ha = adapter block pointer.
8037 *
8038 * Return:
8039 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8040 *
8041 * Context:
8042 * Kernel context.
8043 */
8044int
8045qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8046{
8047 int ret;
8048 fc_port_t *fcport;
8049
8050 ret = QLA_FUNCTION_FAILED;
8051 /* We need to set priority for all logged in ports */
8052 list_for_each_entry(fcport, &vha->vp_fcports, list)
8053 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8054
8055 return ret;
8056}
Michael Hernandezd7459522016-12-12 14:40:07 -08008057
Quinn Tran82de8022017-06-13 20:47:17 -07008058struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8059 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008060{
8061 int rsp_id = 0;
8062 int req_id = 0;
8063 int i;
8064 struct qla_hw_data *ha = vha->hw;
8065 uint16_t qpair_id = 0;
8066 struct qla_qpair *qpair = NULL;
8067 struct qla_msix_entry *msix;
8068
8069 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8070 ql_log(ql_log_warn, vha, 0x00181,
8071 "FW/Driver is not multi-queue capable.\n");
8072 return NULL;
8073 }
8074
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008075 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008076 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8077 if (qpair == NULL) {
8078 ql_log(ql_log_warn, vha, 0x0182,
8079 "Failed to allocate memory for queue pair.\n");
8080 return NULL;
8081 }
8082 memset(qpair, 0, sizeof(struct qla_qpair));
8083
8084 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008085 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008086 qpair->qp_lock_ptr = &qpair->qp_lock;
8087 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008088 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008089
8090 /* Assign available que pair id */
8091 mutex_lock(&ha->mq_lock);
8092 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008093 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008094 mutex_unlock(&ha->mq_lock);
8095 ql_log(ql_log_warn, vha, 0x0183,
8096 "No resources to create additional q pair.\n");
8097 goto fail_qid_map;
8098 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008099 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008100 set_bit(qpair_id, ha->qpair_qid_map);
8101 ha->queue_pair_map[qpair_id] = qpair;
8102 qpair->id = qpair_id;
8103 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008104 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008105 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008106 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008107 qpair->chip_reset = ha->base_qpair->chip_reset;
8108 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8109 qpair->enable_explicit_conf =
8110 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008111
8112 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008113 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008114 if (msix->in_use)
8115 continue;
8116 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008117 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008118 "Vector %x selected for qpair\n", msix->vector);
8119 break;
8120 }
8121 if (!qpair->msix) {
8122 ql_log(ql_log_warn, vha, 0x0184,
8123 "Out of MSI-X vectors!.\n");
8124 goto fail_msix;
8125 }
8126
8127 qpair->msix->in_use = 1;
8128 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008129 qpair->pdev = ha->pdev;
8130 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8131 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008132
8133 mutex_unlock(&ha->mq_lock);
8134
8135 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008136 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008137 if (!rsp_id) {
8138 ql_log(ql_log_warn, vha, 0x0185,
8139 "Failed to create response queue.\n");
8140 goto fail_rsp;
8141 }
8142
8143 qpair->rsp = ha->rsp_q_map[rsp_id];
8144
8145 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008146 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8147 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008148 if (!req_id) {
8149 ql_log(ql_log_warn, vha, 0x0186,
8150 "Failed to create request queue.\n");
8151 goto fail_req;
8152 }
8153
8154 qpair->req = ha->req_q_map[req_id];
8155 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008156 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008157 /* init qpair to this cpu. Will adjust at run time. */
8158 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008159
8160 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8161 if (ha->fw_attributes & BIT_4)
8162 qpair->difdix_supported = 1;
8163 }
8164
8165 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8166 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008167 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008168 "Failed to create srb mempool for qpair %d\n",
8169 qpair->id);
8170 goto fail_mempool;
8171 }
8172
8173 /* Mark as online */
8174 qpair->online = 1;
8175
8176 if (!vha->flags.qpairs_available)
8177 vha->flags.qpairs_available = 1;
8178
8179 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8180 "Request/Response queue pair created, id %d\n",
8181 qpair->id);
8182 ql_dbg(ql_dbg_init, vha, 0x0187,
8183 "Request/Response queue pair created, id %d\n",
8184 qpair->id);
8185 }
8186 return qpair;
8187
8188fail_mempool:
8189fail_req:
8190 qla25xx_delete_rsp_que(vha, qpair->rsp);
8191fail_rsp:
8192 mutex_lock(&ha->mq_lock);
8193 qpair->msix->in_use = 0;
8194 list_del(&qpair->qp_list_elem);
8195 if (list_empty(&vha->qp_list))
8196 vha->flags.qpairs_available = 0;
8197fail_msix:
8198 ha->queue_pair_map[qpair_id] = NULL;
8199 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008200 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008201 mutex_unlock(&ha->mq_lock);
8202fail_qid_map:
8203 kfree(qpair);
8204 return NULL;
8205}
8206
8207int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8208{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008209 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008210 struct qla_hw_data *ha = qpair->hw;
8211
Sawan Chandakd65237c2017-06-13 20:47:19 -07008212 if (!vha->flags.qpairs_req_created && !vha->flags.qpairs_rsp_created)
8213 goto fail;
8214
Michael Hernandezd7459522016-12-12 14:40:07 -08008215 qpair->delete_in_progress = 1;
8216 while (atomic_read(&qpair->ref_count))
8217 msleep(500);
8218
8219 ret = qla25xx_delete_req_que(vha, qpair->req);
8220 if (ret != QLA_SUCCESS)
8221 goto fail;
8222 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8223 if (ret != QLA_SUCCESS)
8224 goto fail;
8225
8226 mutex_lock(&ha->mq_lock);
8227 ha->queue_pair_map[qpair->id] = NULL;
8228 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008229 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008230 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008231 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008232 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008233 vha->flags.qpairs_req_created = 0;
8234 vha->flags.qpairs_rsp_created = 0;
8235 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008236 mempool_destroy(qpair->srb_mempool);
8237 kfree(qpair);
8238 mutex_unlock(&ha->mq_lock);
8239
8240 return QLA_SUCCESS;
8241fail:
8242 return ret;
8243}