blob: aaa8c07b2d8d4d4ab9708786e154ab39b9cef4ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
Armen Baloyanbd21eaf2014-04-11 16:54:24 -04003 * Copyright (c) 2003-2014 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
Anirban Chakraborty73208df2008-12-09 16:45:39 -08008#include "qla_gbl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Andrew Vasquez0107109e2005-07-06 10:31:37 -070012#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "qla_devtbl.h"
15
David Miller4e08df32007-04-16 12:37:43 -070016#ifdef CONFIG_SPARC
17#include <asm/prom.h>
David Miller4e08df32007-04-16 12:37:43 -070018#endif
19
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040020#include <target/target_core_base.h>
21#include "qla_target.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24* QLogic ISP2x00 Hardware Support Function Prototypes.
25*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int qla2x00_setup_chip(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int qla2x00_fw_ready(scsi_qla_host_t *);
29static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int qla2x00_configure_loop(scsi_qla_host_t *);
31static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int qla2x00_configure_fabric(scsi_qla_host_t *);
Quinn Tran726b8542017-01-19 22:28:00 -080033static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Harihara Kadayam4d4df192008-04-03 13:13:26 -070036static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
37static int qla84xx_init_chip(scsi_qla_host_t *);
Anirban Chakraborty73208df2008-12-09 16:45:39 -080038static int qla25xx_init_queues(struct qla_hw_data *);
Duane Grigsbya5d42f42017-06-21 13:48:41 -070039static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
Quinn Tran726b8542017-01-19 22:28:00 -080040static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41 struct event_arg *);
Duane Grigsbya5d42f42017-06-21 13:48:41 -070042static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
43 struct event_arg *);
Quinn Trana4239942017-12-28 12:33:26 -080044static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
Harihara Kadayam4d4df192008-04-03 13:13:26 -070045
Andrew Vasquezac280b62009-08-20 11:06:05 -070046/* SRB Extensions ---------------------------------------------------------- */
47
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080048void
Kees Cook8e5f4ba2017-09-03 13:23:32 -070049qla2x00_sp_timeout(struct timer_list *t)
Andrew Vasquezac280b62009-08-20 11:06:05 -070050{
Kees Cook8e5f4ba2017-09-03 13:23:32 -070051 srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
Madhuranath Iyengar49163922010-05-04 15:01:28 -070052 struct srb_iocb *iocb;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080053 scsi_qla_host_t *vha = sp->vha;
Andrew Vasquezac280b62009-08-20 11:06:05 -070054 struct req_que *req;
55 unsigned long flags;
56
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080057 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
58 req = vha->hw->req_q_map[0];
Andrew Vasquezac280b62009-08-20 11:06:05 -070059 req->outstanding_cmds[sp->handle] = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080060 iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar49163922010-05-04 15:01:28 -070061 iocb->timeout(sp);
Giridhar Malavali045d6ea2017-12-28 12:33:21 -080062 if (sp->type != SRB_ELS_DCMD)
63 sp->free(sp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080064 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -070065}
66
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080067void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080068qla2x00_sp_free(void *ptr)
Andrew Vasquezac280b62009-08-20 11:06:05 -070069{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080070 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080071 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -070072
Chad Dupuis4d97cc52010-10-15 11:27:41 -070073 del_timer(&iocb->timer);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080074 qla2x00_rel_sp(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -070075}
76
Andrew Vasquezac280b62009-08-20 11:06:05 -070077/* Asynchronous Login/Logout Routines -------------------------------------- */
78
Saurav Kashyapa9b6f722012-08-22 14:21:01 -040079unsigned long
Andrew Vasquez5b914902010-05-28 15:08:30 -070080qla2x00_get_async_timeout(struct scsi_qla_host *vha)
81{
82 unsigned long tmo;
83 struct qla_hw_data *ha = vha->hw;
84
85 /* Firmware should use switch negotiated r_a_tov for timeout. */
86 tmo = ha->r_a_tov / 10 * 2;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040087 if (IS_QLAFX00(ha)) {
88 tmo = FX00_DEF_RATOV * 2;
89 } else if (!IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez5b914902010-05-28 15:08:30 -070090 /*
91 * Except for earlier ISPs where the timeout is seeded from the
92 * initialization control block.
93 */
94 tmo = ha->login_timeout;
95 }
96 return tmo;
97}
Andrew Vasquezac280b62009-08-20 11:06:05 -070098
Quinn Tran726b8542017-01-19 22:28:00 -080099void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800100qla2x00_async_iocb_timeout(void *data)
Andrew Vasquezac280b62009-08-20 11:06:05 -0700101{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800102 srb_t *sp = data;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700103 fc_port_t *fcport = sp->fcport;
Quinn Tran726b8542017-01-19 22:28:00 -0800104 struct srb_iocb *lio = &sp->u.iocb_cmd;
105 struct event_arg ea;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700106
Quinn Tran5c25d452017-12-28 12:33:09 -0800107 if (fcport) {
108 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
109 "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
110 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700111
Quinn Tran6d6749272017-12-28 12:33:41 -0800112 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran5c25d452017-12-28 12:33:09 -0800113 } else {
114 pr_info("Async-%s timeout - hdl=%x.\n",
115 sp->name, sp->handle);
116 }
Quinn Tran726b8542017-01-19 22:28:00 -0800117
118 switch (sp->type) {
119 case SRB_LOGIN_CMD:
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700120 /* Retry as needed. */
121 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
122 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
123 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800124 memset(&ea, 0, sizeof(ea));
125 ea.event = FCME_PLOGI_DONE;
126 ea.fcport = sp->fcport;
127 ea.data[0] = lio->u.logio.data[0];
128 ea.data[1] = lio->u.logio.data[1];
129 ea.sp = sp;
130 qla24xx_handle_plogi_done_event(fcport->vha, &ea);
131 break;
132 case SRB_LOGOUT_CMD:
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400133 qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800134 break;
135 case SRB_CT_PTHRU_CMD:
136 case SRB_MB_IOCB:
137 case SRB_NACK_PLOGI:
138 case SRB_NACK_PRLI:
139 case SRB_NACK_LOGO:
Quinn Tran28531922017-12-28 12:33:10 -0800140 case SRB_CTRL_VP:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800141 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800142 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700143 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700144}
145
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700146static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800147qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700148{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800149 srb_t *sp = ptr;
150 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800151 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800152 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700153
Quinn Tran83548fe2017-06-02 09:12:01 -0700154 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800155 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800156
Quinn Tran6d6749272017-12-28 12:33:41 -0800157 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
158
Quinn Tran726b8542017-01-19 22:28:00 -0800159 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
160 memset(&ea, 0, sizeof(ea));
161 ea.event = FCME_PLOGI_DONE;
162 ea.fcport = sp->fcport;
163 ea.data[0] = lio->u.logio.data[0];
164 ea.data[1] = lio->u.logio.data[1];
165 ea.iop[0] = lio->u.logio.iop[0];
166 ea.iop[1] = lio->u.logio.iop[1];
167 ea.sp = sp;
168 qla2x00_fcport_event_handler(vha, &ea);
169 }
170
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800171 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700172}
173
Andrew Vasquezac280b62009-08-20 11:06:05 -0700174int
175qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
176 uint16_t *data)
177{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700178 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700179 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800180 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700181
Quinn Tran726b8542017-01-19 22:28:00 -0800182 if (!vha->flags.online)
183 goto done;
184
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800185 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700186 if (!sp)
187 goto done;
188
Quinn Tran726b8542017-01-19 22:28:00 -0800189 fcport->flags |= FCF_ASYNC_SENT;
190 fcport->logout_completed = 0;
191
Quinn Trana4239942017-12-28 12:33:26 -0800192 fcport->disc_state = DSC_LOGIN_PEND;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800193 sp->type = SRB_LOGIN_CMD;
194 sp->name = "login";
Quinn Trana4239942017-12-28 12:33:26 -0800195 sp->gen1 = fcport->rscn_gen;
196 sp->gen2 = fcport->login_gen;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800197 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
198
199 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700200 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800201 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700202 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700203
204 if (fcport->fc4f_nvme)
205 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
206
Andrew Vasquezac280b62009-08-20 11:06:05 -0700207 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700208 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700209 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500210 if (rval != QLA_SUCCESS) {
Chad Dupuis080c9512016-01-27 12:03:37 -0500211 fcport->flags |= FCF_LOGIN_NEEDED;
212 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700213 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500214 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700215
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700216 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800217 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
218 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800219 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
220 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700221 return rval;
222
223done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800224 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800225 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800226done:
Andrew Vasquezac280b62009-08-20 11:06:05 -0700227 return rval;
228}
229
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700230static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800231qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700232{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800233 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800234 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700235
Quinn Tran6d6749272017-12-28 12:33:41 -0800236 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800237 if (!test_bit(UNLOADING, &sp->vha->dpc_flags))
238 qla2x00_post_async_logout_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800239 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800240 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700241}
242
Andrew Vasquezac280b62009-08-20 11:06:05 -0700243int
244qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
245{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700246 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700247 struct srb_iocb *lio;
Quinn Tran3dbec592017-12-28 12:33:40 -0800248 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700249
Quinn Tran3dbec592017-12-28 12:33:40 -0800250 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
251 return rval;
252
Quinn Tran726b8542017-01-19 22:28:00 -0800253 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800254 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700255 if (!sp)
256 goto done;
257
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800258 sp->type = SRB_LOGOUT_CMD;
259 sp->name = "logout";
260 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
261
262 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700263 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800264 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700265 rval = qla2x00_start_sp(sp);
266 if (rval != QLA_SUCCESS)
267 goto done_free_sp;
268
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700269 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800270 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800271 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800272 fcport->d_id.b.area, fcport->d_id.b.al_pa,
273 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700274 return rval;
275
276done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800277 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700278done:
Quinn Tran726b8542017-01-19 22:28:00 -0800279 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700280 return rval;
281}
Quinn Tran11aea162017-12-28 12:33:20 -0800282
283void
284qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
285 uint16_t *data)
286{
287 /* Don't re-login in target mode */
288 if (!fcport->tgt_session)
289 qla2x00_mark_device_lost(vha, fcport, 1, 0);
290 qlt_logo_completion_handler(fcport, data[0]);
291}
292
293static void
294qla2x00_async_prlo_sp_done(void *s, int res)
295{
296 srb_t *sp = (srb_t *)s;
297 struct srb_iocb *lio = &sp->u.iocb_cmd;
298 struct scsi_qla_host *vha = sp->vha;
299
300 if (!test_bit(UNLOADING, &vha->dpc_flags))
301 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
302 lio->u.logio.data);
303 sp->free(sp);
304}
305
306int
307qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
308{
309 srb_t *sp;
310 struct srb_iocb *lio;
311 int rval;
312
313 rval = QLA_FUNCTION_FAILED;
314 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
315 if (!sp)
316 goto done;
317
318 sp->type = SRB_PRLO_CMD;
319 sp->name = "prlo";
320 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
321
322 lio = &sp->u.iocb_cmd;
323 lio->timeout = qla2x00_async_iocb_timeout;
324 sp->done = qla2x00_async_prlo_sp_done;
325 rval = qla2x00_start_sp(sp);
326 if (rval != QLA_SUCCESS)
327 goto done_free_sp;
328
329 ql_dbg(ql_dbg_disc, vha, 0x2070,
330 "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
331 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
332 fcport->d_id.b.area, fcport->d_id.b.al_pa);
333 return rval;
334
335done_free_sp:
336 sp->free(sp);
337done:
338 return rval;
339}
340
Quinn Tranf13515a2017-12-28 12:33:15 -0800341static
342void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
343{
Quinn Tran0616e962017-12-28 12:33:34 -0800344 struct fc_port *fcport = ea->fcport;
345
346 ql_dbg(ql_dbg_disc, vha, 0x20d2,
347 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
348 __func__, fcport->port_name, fcport->disc_state,
349 fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
350 fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
351
352 if (ea->data[0] != MBS_COMMAND_COMPLETE) {
Quinn Trana4239942017-12-28 12:33:26 -0800353 ql_dbg(ql_dbg_disc, vha, 0x2066,
354 "%s %8phC: adisc fail: post delete\n",
355 __func__, ea->fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800356 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana4239942017-12-28 12:33:26 -0800357 return;
358 }
Quinn Trana4239942017-12-28 12:33:26 -0800359
360 if (ea->fcport->disc_state == DSC_DELETE_PEND)
361 return;
362
363 if (ea->sp->gen2 != ea->fcport->login_gen) {
364 /* target side must have changed it. */
365 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran0616e962017-12-28 12:33:34 -0800366 "%s %8phC generation changed\n",
367 __func__, ea->fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800368 return;
369 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
370 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
371 __func__, __LINE__, ea->fcport->port_name);
372 qla24xx_post_gidpn_work(vha, ea->fcport);
373 return;
374 }
375
376 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800377}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700378
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700379static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800380qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700381{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800382 srb_t *sp = ptr;
383 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800384 struct event_arg ea;
Quinn Tran0616e962017-12-28 12:33:34 -0800385 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700386
Quinn Tranf13515a2017-12-28 12:33:15 -0800387 ql_dbg(ql_dbg_disc, vha, 0x2066,
388 "Async done-%s res %x %8phC\n",
389 sp->name, res, sp->fcport->port_name);
390
391 memset(&ea, 0, sizeof(ea));
392 ea.event = FCME_ADISC_DONE;
393 ea.rc = res;
Quinn Tran0616e962017-12-28 12:33:34 -0800394 ea.data[0] = lio->u.logio.data[0];
395 ea.data[1] = lio->u.logio.data[1];
396 ea.iop[0] = lio->u.logio.iop[0];
397 ea.iop[1] = lio->u.logio.iop[1];
Quinn Tranf13515a2017-12-28 12:33:15 -0800398 ea.fcport = sp->fcport;
399 ea.sp = sp;
400
401 qla2x00_fcport_event_handler(vha, &ea);
402
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800403 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700404}
405
406int
407qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
408 uint16_t *data)
409{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700410 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700411 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700412 int rval;
413
414 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800415 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800416 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700417 if (!sp)
418 goto done;
419
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800420 sp->type = SRB_ADISC_CMD;
421 sp->name = "adisc";
422 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
423
424 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700425 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800426 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700427 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700428 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700429 rval = qla2x00_start_sp(sp);
430 if (rval != QLA_SUCCESS)
431 goto done_free_sp;
432
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700433 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800434 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
435 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700436 return rval;
437
438done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800439 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700440done:
Quinn Tran726b8542017-01-19 22:28:00 -0800441 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tranf13515a2017-12-28 12:33:15 -0800442 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700443 return rval;
444}
445
Quinn Tran726b8542017-01-19 22:28:00 -0800446static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
447 struct event_arg *ea)
448{
449 fc_port_t *fcport, *conflict_fcport;
450 struct get_name_list_extended *e;
451 u16 i, n, found = 0, loop_id;
452 port_id_t id;
453 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800454 u16 data[2];
455 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800456
457 fcport = ea->fcport;
Quinn Tranf352eeb2017-12-28 12:33:35 -0800458 ql_dbg(ql_dbg_disc, vha, 0xffff,
459 "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
460 __func__, fcport->port_name, fcport->disc_state,
461 fcport->fw_login_state, ea->rc,
462 fcport->login_gen, fcport->last_login_gen,
463 fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800464
Quinn Trana4239942017-12-28 12:33:26 -0800465 if (fcport->disc_state == DSC_DELETE_PEND)
466 return;
467
Quinn Tran726b8542017-01-19 22:28:00 -0800468 if (ea->rc) { /* rval */
469 if (fcport->login_retry == 0) {
470 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700471 ql_dbg(ql_dbg_disc, vha, 0x20de,
472 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
473 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800474 }
475 return;
476 }
477
478 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700479 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800480 "%s %8phC rscn gen changed rscn %d|%d \n",
481 __func__, fcport->port_name,
482 fcport->last_rscn_gen, fcport->rscn_gen);
483 qla24xx_post_gidpn_work(vha, fcport);
484 return;
485 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700486 ql_dbg(ql_dbg_disc, vha, 0x20e0,
Quinn Tranf352eeb2017-12-28 12:33:35 -0800487 "%s %8phC login gen changed\n",
488 __func__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800489 return;
490 }
491
492 n = ea->data[0] / sizeof(struct get_name_list_extended);
493
Quinn Tran83548fe2017-06-02 09:12:01 -0700494 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800495 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
496 __func__, __LINE__, fcport->port_name, n,
497 fcport->d_id.b.domain, fcport->d_id.b.area,
498 fcport->d_id.b.al_pa, fcport->loop_id);
499
500 for (i = 0; i < n; i++) {
501 e = &vha->gnl.l[i];
502 wwn = wwn_to_u64(e->port_name);
503
504 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
505 continue;
506
507 found = 1;
508 id.b.domain = e->port_id[2];
509 id.b.area = e->port_id[1];
510 id.b.al_pa = e->port_id[0];
511 id.b.rsvd_1 = 0;
512
513 loop_id = le16_to_cpu(e->nport_handle);
514 loop_id = (loop_id & 0x7fff);
515
Quinn Tran83548fe2017-06-02 09:12:01 -0700516 ql_dbg(ql_dbg_disc, vha, 0x20e2,
517 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
518 __func__, fcport->port_name,
519 e->current_login_state, fcport->fw_login_state,
520 id.b.domain, id.b.area, id.b.al_pa,
521 fcport->d_id.b.domain, fcport->d_id.b.area,
522 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800523
524 if ((id.b24 != fcport->d_id.b24) ||
525 ((fcport->loop_id != FC_NO_LOOP_ID) &&
526 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700527 ql_dbg(ql_dbg_disc, vha, 0x20e3,
528 "%s %d %8phC post del sess\n",
529 __func__, __LINE__, fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800530 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800531 return;
532 }
533
534 fcport->loop_id = loop_id;
535
536 wwn = wwn_to_u64(fcport->port_name);
537 qlt_find_sess_invalidate_other(vha, wwn,
538 id, loop_id, &conflict_fcport);
539
540 if (conflict_fcport) {
541 /*
542 * Another share fcport share the same loop_id &
543 * nport id. Conflict fcport needs to finish
544 * cleanup before this fcport can proceed to login.
545 */
546 conflict_fcport->conflict = fcport;
547 fcport->login_pause = 1;
548 }
549
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700550 if (fcport->fc4f_nvme)
551 current_login_state = e->current_login_state >> 4;
552 else
553 current_login_state = e->current_login_state & 0xf;
554
555 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800556 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700557 ql_dbg(ql_dbg_disc, vha, 0x20e4,
558 "%s %d %8phC post gpdb\n",
559 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800560
561 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
562 fcport->port_type = FCT_INITIATOR;
563 else
564 fcport->port_type = FCT_TARGET;
565
566 data[0] = data[1] = 0;
567 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800568 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800569 case DSC_LS_PORT_UNAVAIL:
570 default:
571 if (fcport->loop_id == FC_NO_LOOP_ID) {
572 qla2x00_find_new_loop_id(vha, fcport);
573 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
574 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700575 ql_dbg(ql_dbg_disc, vha, 0x20e5,
576 "%s %d %8phC\n",
577 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800578 qla24xx_fcport_handle_login(vha, fcport);
579 break;
580 }
581 }
582
583 if (!found) {
584 /* fw has no record of this port */
Quinn Tran040036b2017-12-28 12:33:38 -0800585 for (i = 0; i < n; i++) {
586 e = &vha->gnl.l[i];
587 id.b.domain = e->port_id[0];
588 id.b.area = e->port_id[1];
589 id.b.al_pa = e->port_id[2];
590 id.b.rsvd_1 = 0;
591 loop_id = le16_to_cpu(e->nport_handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800592
Quinn Tran040036b2017-12-28 12:33:38 -0800593 if (fcport->d_id.b24 == id.b24) {
594 conflict_fcport =
595 qla2x00_find_fcport_by_wwpn(vha,
596 e->port_name, 0);
597 ql_dbg(ql_dbg_disc, vha, 0x20e6,
598 "%s %d %8phC post del sess\n",
599 __func__, __LINE__,
600 conflict_fcport->port_name);
601 qlt_schedule_sess_for_deletion
Quinn Tran94cff6e2017-12-28 12:33:42 -0800602 (conflict_fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800603 }
Quinn Tran040036b2017-12-28 12:33:38 -0800604
605 /* FW already picked this loop id for another fcport */
606 if (fcport->loop_id == loop_id)
607 fcport->loop_id = FC_NO_LOOP_ID;
Quinn Tran726b8542017-01-19 22:28:00 -0800608 }
609 qla24xx_fcport_handle_login(vha, fcport);
610 }
611} /* gnl_event */
612
613static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800614qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800615{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800616 struct srb *sp = s;
617 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800618 unsigned long flags;
619 struct fc_port *fcport = NULL, *tf;
620 u16 i, n = 0, loop_id;
621 struct event_arg ea;
622 struct get_name_list_extended *e;
623 u64 wwn;
624 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800625 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800626
Quinn Tran83548fe2017-06-02 09:12:01 -0700627 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800628 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
629 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
630 sp->u.iocb_cmd.u.mbx.in_mb[2]);
631
632 memset(&ea, 0, sizeof(ea));
633 ea.sp = sp;
634 ea.rc = res;
635 ea.event = FCME_GNL_DONE;
636
637 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
638 sizeof(struct get_name_list_extended)) {
639 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
640 sizeof(struct get_name_list_extended);
641 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
642 }
643
644 for (i = 0; i < n; i++) {
645 e = &vha->gnl.l[i];
646 loop_id = le16_to_cpu(e->nport_handle);
647 /* mask out reserve bit */
648 loop_id = (loop_id & 0x7fff);
649 set_bit(loop_id, vha->hw->loop_id_map);
650 wwn = wwn_to_u64(e->port_name);
651
Quinn Tran83548fe2017-06-02 09:12:01 -0700652 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800653 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
654 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
655 e->port_id[0], e->current_login_state, e->last_login_state,
656 (loop_id & 0x7fff));
657 }
658
659 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
660 vha->gnl.sent = 0;
661
662 INIT_LIST_HEAD(&h);
663 fcport = tf = NULL;
664 if (!list_empty(&vha->gnl.fcports))
665 list_splice_init(&vha->gnl.fcports, &h);
666
667 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
668 list_del_init(&fcport->gnl_entry);
Quinn Tran6d6749272017-12-28 12:33:41 -0800669 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800670 ea.fcport = fcport;
671
672 qla2x00_fcport_event_handler(vha, &ea);
673 }
674
Quinn Trana4239942017-12-28 12:33:26 -0800675 /* create new fcport if fw has knowledge of new sessions */
676 for (i = 0; i < n; i++) {
677 port_id_t id;
678 u64 wwnn;
679
680 e = &vha->gnl.l[i];
681 wwn = wwn_to_u64(e->port_name);
682
683 found = false;
684 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
685 if (!memcmp((u8 *)&wwn, fcport->port_name,
686 WWN_SIZE)) {
687 found = true;
688 break;
689 }
690 }
691
Quinn Trancf055fb2017-12-28 12:33:33 -0800692 id.b.domain = e->port_id[2];
Quinn Trana4239942017-12-28 12:33:26 -0800693 id.b.area = e->port_id[1];
Quinn Trancf055fb2017-12-28 12:33:33 -0800694 id.b.al_pa = e->port_id[0];
Quinn Trana4239942017-12-28 12:33:26 -0800695 id.b.rsvd_1 = 0;
696
697 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
698 ql_dbg(ql_dbg_disc, vha, 0x2065,
Quinn Trancf055fb2017-12-28 12:33:33 -0800699 "%s %d %8phC %06x post new sess\n",
700 __func__, __LINE__, (u8 *)&wwn, id.b24);
Quinn Trana4239942017-12-28 12:33:26 -0800701 wwnn = wwn_to_u64(e->node_name);
702 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
703 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
704 }
705 }
706
Quinn Tran726b8542017-01-19 22:28:00 -0800707 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
708
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800709 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800710}
711
712int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
713{
714 srb_t *sp;
715 struct srb_iocb *mbx;
716 int rval = QLA_FUNCTION_FAILED;
717 unsigned long flags;
718 u16 *mb;
719
Quinn Tran3dbec592017-12-28 12:33:40 -0800720 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
721 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800722
Quinn Tran83548fe2017-06-02 09:12:01 -0700723 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800724 "Async-gnlist WWPN %8phC \n", fcport->port_name);
725
726 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800727 fcport->disc_state = DSC_GNL;
728 fcport->last_rscn_gen = fcport->rscn_gen;
729 fcport->last_login_gen = fcport->login_gen;
730
731 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
732 if (vha->gnl.sent) {
733 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tran3dbec592017-12-28 12:33:40 -0800734 return QLA_SUCCESS;
Quinn Tran726b8542017-01-19 22:28:00 -0800735 }
736 vha->gnl.sent = 1;
737 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
738
739 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
740 if (!sp)
741 goto done;
Quinn Tran3dbec592017-12-28 12:33:40 -0800742
743 fcport->flags |= FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800744 sp->type = SRB_MB_IOCB;
745 sp->name = "gnlist";
746 sp->gen1 = fcport->rscn_gen;
747 sp->gen2 = fcport->login_gen;
748
749 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
750
751 mb = sp->u.iocb_cmd.u.mbx.out_mb;
752 mb[0] = MBC_PORT_NODE_NAME_LIST;
753 mb[1] = BIT_2 | BIT_3;
754 mb[2] = MSW(vha->gnl.ldma);
755 mb[3] = LSW(vha->gnl.ldma);
756 mb[6] = MSW(MSD(vha->gnl.ldma));
757 mb[7] = LSW(MSD(vha->gnl.ldma));
758 mb[8] = vha->gnl.size;
759 mb[9] = vha->vp_idx;
760
761 mbx = &sp->u.iocb_cmd;
762 mbx->timeout = qla2x00_async_iocb_timeout;
763
764 sp->done = qla24xx_async_gnl_sp_done;
765
766 rval = qla2x00_start_sp(sp);
767 if (rval != QLA_SUCCESS)
768 goto done_free_sp;
769
Quinn Tran83548fe2017-06-02 09:12:01 -0700770 ql_dbg(ql_dbg_disc, vha, 0x20da,
771 "Async-%s - OUT WWPN %8phC hndl %x\n",
772 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800773
774 return rval;
775
776done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800777 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800778 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800779done:
Quinn Tran726b8542017-01-19 22:28:00 -0800780 return rval;
781}
782
783int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
784{
785 struct qla_work_evt *e;
786
787 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
788 if (!e)
789 return QLA_FUNCTION_FAILED;
790
791 e->u.fcport.fcport = fcport;
Quinn Tran6d6749272017-12-28 12:33:41 -0800792 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800793 return qla2x00_post_work(vha, e);
794}
795
796static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800797void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800798{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800799 struct srb *sp = s;
800 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800801 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800802 fc_port_t *fcport = sp->fcport;
803 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800804 struct event_arg ea;
805
Quinn Tran83548fe2017-06-02 09:12:01 -0700806 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800807 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
808 sp->name, res, fcport->port_name, mb[1], mb[2]);
809
Quinn Tran6d6749272017-12-28 12:33:41 -0800810 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800811
Quinn Tran726b8542017-01-19 22:28:00 -0800812 memset(&ea, 0, sizeof(ea));
813 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800814 ea.fcport = fcport;
815 ea.sp = sp;
816
817 qla2x00_fcport_event_handler(vha, &ea);
818
819 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
820 sp->u.iocb_cmd.u.mbx.in_dma);
821
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800822 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800823}
824
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700825static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
826{
827 struct qla_work_evt *e;
828
829 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
830 if (!e)
831 return QLA_FUNCTION_FAILED;
832
833 e->u.fcport.fcport = fcport;
834
835 return qla2x00_post_work(vha, e);
836}
837
838static void
839qla2x00_async_prli_sp_done(void *ptr, int res)
840{
841 srb_t *sp = ptr;
842 struct scsi_qla_host *vha = sp->vha;
843 struct srb_iocb *lio = &sp->u.iocb_cmd;
844 struct event_arg ea;
845
846 ql_dbg(ql_dbg_disc, vha, 0x2129,
847 "%s %8phC res %d \n", __func__,
848 sp->fcport->port_name, res);
849
850 sp->fcport->flags &= ~FCF_ASYNC_SENT;
851
852 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
853 memset(&ea, 0, sizeof(ea));
854 ea.event = FCME_PRLI_DONE;
855 ea.fcport = sp->fcport;
856 ea.data[0] = lio->u.logio.data[0];
857 ea.data[1] = lio->u.logio.data[1];
858 ea.iop[0] = lio->u.logio.iop[0];
859 ea.iop[1] = lio->u.logio.iop[1];
860 ea.sp = sp;
861
862 qla2x00_fcport_event_handler(vha, &ea);
863 }
864
865 sp->free(sp);
866}
867
868int
869qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
870{
871 srb_t *sp;
872 struct srb_iocb *lio;
873 int rval = QLA_FUNCTION_FAILED;
874
875 if (!vha->flags.online)
876 return rval;
877
878 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
879 fcport->fw_login_state == DSC_LS_PLOGI_COMP ||
880 fcport->fw_login_state == DSC_LS_PRLI_PEND)
881 return rval;
882
883 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
884 if (!sp)
885 return rval;
886
887 fcport->flags |= FCF_ASYNC_SENT;
888 fcport->logout_completed = 0;
889
890 sp->type = SRB_PRLI_CMD;
891 sp->name = "prli";
892 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
893
894 lio = &sp->u.iocb_cmd;
895 lio->timeout = qla2x00_async_iocb_timeout;
896 sp->done = qla2x00_async_prli_sp_done;
897 lio->u.logio.flags = 0;
898
899 if (fcport->fc4f_nvme)
900 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
901
902 rval = qla2x00_start_sp(sp);
903 if (rval != QLA_SUCCESS) {
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700904 fcport->flags |= FCF_LOGIN_NEEDED;
905 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
906 goto done_free_sp;
907 }
908
909 ql_dbg(ql_dbg_disc, vha, 0x211b,
910 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
911 fcport->port_name, sp->handle, fcport->loop_id,
912 fcport->d_id.b24, fcport->login_retry);
913
914 return rval;
915
916done_free_sp:
917 sp->free(sp);
918 fcport->flags &= ~FCF_ASYNC_SENT;
919 return rval;
920}
921
Quinn Trana07fc0a2017-08-23 15:05:21 -0700922int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800923{
924 struct qla_work_evt *e;
925
926 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
927 if (!e)
928 return QLA_FUNCTION_FAILED;
929
930 e->u.fcport.fcport = fcport;
931 e->u.fcport.opt = opt;
Quinn Tran6d6749272017-12-28 12:33:41 -0800932 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800933 return qla2x00_post_work(vha, e);
934}
935
936int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
937{
938 srb_t *sp;
939 struct srb_iocb *mbx;
940 int rval = QLA_FUNCTION_FAILED;
941 u16 *mb;
942 dma_addr_t pd_dma;
943 struct port_database_24xx *pd;
944 struct qla_hw_data *ha = vha->hw;
945
Quinn Tran3dbec592017-12-28 12:33:40 -0800946 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
947 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800948
Quinn Tran726b8542017-01-19 22:28:00 -0800949 fcport->disc_state = DSC_GPDB;
950
951 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
952 if (!sp)
953 goto done;
954
Quinn Tran3dbec592017-12-28 12:33:40 -0800955 fcport->flags |= FCF_ASYNC_SENT;
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700956 sp->type = SRB_MB_IOCB;
957 sp->name = "gpdb";
958 sp->gen1 = fcport->rscn_gen;
959 sp->gen2 = fcport->login_gen;
960 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
961
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200962 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800963 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700964 ql_log(ql_log_warn, vha, 0xd043,
965 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800966 goto done_free_sp;
967 }
Quinn Tran726b8542017-01-19 22:28:00 -0800968
Quinn Tran726b8542017-01-19 22:28:00 -0800969 mb = sp->u.iocb_cmd.u.mbx.out_mb;
970 mb[0] = MBC_GET_PORT_DATABASE;
971 mb[1] = fcport->loop_id;
972 mb[2] = MSW(pd_dma);
973 mb[3] = LSW(pd_dma);
974 mb[6] = MSW(MSD(pd_dma));
975 mb[7] = LSW(MSD(pd_dma));
976 mb[9] = vha->vp_idx;
977 mb[10] = opt;
978
979 mbx = &sp->u.iocb_cmd;
980 mbx->timeout = qla2x00_async_iocb_timeout;
981 mbx->u.mbx.in = (void *)pd;
982 mbx->u.mbx.in_dma = pd_dma;
983
984 sp->done = qla24xx_async_gpdb_sp_done;
985
986 rval = qla2x00_start_sp(sp);
987 if (rval != QLA_SUCCESS)
988 goto done_free_sp;
989
Quinn Tran83548fe2017-06-02 09:12:01 -0700990 ql_dbg(ql_dbg_disc, vha, 0x20dc,
991 "Async-%s %8phC hndl %x opt %x\n",
992 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800993
994 return rval;
995
996done_free_sp:
997 if (pd)
998 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
999
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001000 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -08001001 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -08001002done:
Quinn Tran726b8542017-01-19 22:28:00 -08001003 qla24xx_post_gpdb_work(vha, fcport, opt);
1004 return rval;
1005}
1006
1007static
Quinn Trana4239942017-12-28 12:33:26 -08001008void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001009{
Quinn Tran726b8542017-01-19 22:28:00 -08001010 unsigned long flags;
1011
Quinn Tran726b8542017-01-19 22:28:00 -08001012 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001013 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001014 ea->fcport->deleted = 0;
1015 ea->fcport->logout_on_delete = 1;
1016
1017 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1018 vha->fcport_count++;
1019 ea->fcport->login_succ = 1;
1020
1021 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1022 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001023 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001024 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001025 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001026 vha->fcport_count);
1027
Quinn Trana4239942017-12-28 12:33:26 -08001028 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001029 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001030 if (ea->fcport->id_changed) {
1031 ea->fcport->id_changed = 0;
1032 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1033 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1034 __func__, __LINE__, ea->fcport->port_name,
1035 vha->fcport_count);
1036 qla24xx_post_gfpnid_work(vha, ea->fcport);
1037 } else {
1038 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1039 "%s %d %8phC post gpsc fcp_cnt %d\n",
1040 __func__, __LINE__, ea->fcport->port_name,
1041 vha->fcport_count);
1042 qla24xx_post_gpsc_work(vha, ea->fcport);
1043 }
Quinn Tran726b8542017-01-19 22:28:00 -08001044 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001045 } else if (ea->fcport->login_succ) {
1046 /*
1047 * We have an existing session. A late RSCN delivery
1048 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001049 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001050 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001051 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1052 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001053 __func__, __LINE__, ea->fcport->port_name);
1054 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001055 }
1056 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001057}
Quinn Tran726b8542017-01-19 22:28:00 -08001058
Quinn Trana4239942017-12-28 12:33:26 -08001059static
1060void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1061{
Quinn Trana4239942017-12-28 12:33:26 -08001062 fc_port_t *fcport = ea->fcport;
1063 struct port_database_24xx *pd;
1064 struct srb *sp = ea->sp;
1065
1066 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1067
1068 fcport->flags &= ~FCF_ASYNC_SENT;
1069
1070 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001071 "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1072 fcport->disc_state, pd->current_login_state, ea->rc);
Quinn Trana4239942017-12-28 12:33:26 -08001073
1074 if (fcport->disc_state == DSC_DELETE_PEND)
1075 return;
1076
1077 switch (pd->current_login_state) {
1078 case PDS_PRLI_COMPLETE:
1079 __qla24xx_parse_gpdb(vha, fcport, pd);
1080 break;
1081 case PDS_PLOGI_PENDING:
1082 case PDS_PLOGI_COMPLETE:
1083 case PDS_PRLI_PENDING:
1084 case PDS_PRLI2_PENDING:
1085 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1086 __func__, __LINE__, fcport->port_name);
1087 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1088 return;
1089 case PDS_LOGO_PENDING:
1090 case PDS_PORT_UNAVAILABLE:
1091 default:
1092 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1093 __func__, __LINE__, fcport->port_name);
1094 qlt_schedule_sess_for_deletion_lock(fcport);
1095 return;
1096 }
1097 __qla24xx_handle_gpdb_event(vha, ea);
1098} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001099
1100static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1101{
1102 u8 login = 0;
Quinn Tran040036b2017-12-28 12:33:38 -08001103 int rc;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001104
1105 if (qla_tgt_mode_enabled(vha))
1106 return;
1107
1108 if (qla_dual_mode_enabled(vha)) {
1109 if (N2N_TOPO(vha->hw)) {
1110 u64 mywwn, wwn;
1111
1112 mywwn = wwn_to_u64(vha->port_name);
1113 wwn = wwn_to_u64(fcport->port_name);
1114 if (mywwn > wwn)
1115 login = 1;
1116 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1117 && time_after_eq(jiffies,
1118 fcport->plogi_nack_done_deadline))
1119 login = 1;
1120 } else {
1121 login = 1;
1122 }
1123 } else {
1124 /* initiator mode */
1125 login = 1;
1126 }
1127
1128 if (login) {
Quinn Tran040036b2017-12-28 12:33:38 -08001129 if (fcport->loop_id == FC_NO_LOOP_ID) {
1130 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1131 rc = qla2x00_find_new_loop_id(vha, fcport);
1132 if (rc) {
1133 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1134 "%s %d %8phC post del sess - out of loopid\n",
1135 __func__, __LINE__, fcport->port_name);
1136 fcport->scan_state = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001137 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran040036b2017-12-28 12:33:38 -08001138 return;
1139 }
1140 }
Quinn Tran9cd883f2017-12-28 12:33:24 -08001141 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1142 "%s %d %8phC post login\n",
1143 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001144 qla2x00_post_async_login_work(vha, fcport, NULL);
1145 }
1146}
1147
Quinn Tran726b8542017-01-19 22:28:00 -08001148int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1149{
Quinn Tranf13515a2017-12-28 12:33:15 -08001150 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001151 u64 wwn;
1152
1153 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001154 "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d retry %d lid %d scan %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001155 __func__, fcport->port_name, fcport->disc_state,
1156 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1157 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001158 fcport->login_gen, fcport->login_retry,
Quinn Trana4239942017-12-28 12:33:26 -08001159 fcport->loop_id, fcport->scan_state);
1160
Quinn Tran726b8542017-01-19 22:28:00 -08001161 if (fcport->login_retry == 0)
1162 return 0;
1163
1164 if (fcport->scan_state != QLA_FCPORT_FOUND)
1165 return 0;
1166
Quinn Tran726b8542017-01-19 22:28:00 -08001167 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001168 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1169 return 0;
1170
Quinn Tran5b334692017-03-15 09:48:48 -07001171 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001172 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1173 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001174 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001175 }
Quinn Tran5b334692017-03-15 09:48:48 -07001176 }
1177
Quinn Tran726b8542017-01-19 22:28:00 -08001178 /* for pure Target Mode. Login will not be initiated */
1179 if (vha->host->active_mode == MODE_TARGET)
1180 return 0;
1181
1182 if (fcport->flags & FCF_ASYNC_SENT) {
1183 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1184 return 0;
1185 }
1186
Quinn Trana4239942017-12-28 12:33:26 -08001187 fcport->login_retry--;
1188
Quinn Tran726b8542017-01-19 22:28:00 -08001189 switch (fcport->disc_state) {
1190 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001191 wwn = wwn_to_u64(fcport->node_name);
1192 if (wwn == 0) {
1193 ql_dbg(ql_dbg_disc, vha, 0xffff,
1194 "%s %d %8phC post GNNID\n",
1195 __func__, __LINE__, fcport->port_name);
1196 qla24xx_post_gnnid_work(vha, fcport);
1197 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001198 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1199 "%s %d %8phC post gnl\n",
1200 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001201 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001202 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001203 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001204 }
1205 break;
1206
1207 case DSC_GNL:
1208 if (fcport->login_pause) {
1209 fcport->last_rscn_gen = fcport->rscn_gen;
1210 fcport->last_login_gen = fcport->login_gen;
1211 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1212 break;
1213 }
1214
Quinn Tran9cd883f2017-12-28 12:33:24 -08001215 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001216 break;
1217
1218 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001219 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1220 "%s %d %8phC post gidpn\n",
1221 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001222 if (N2N_TOPO(vha->hw))
1223 qla_chk_n2n_b4_login(vha, fcport);
1224 else
1225 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001226 break;
1227
1228 case DSC_LOGIN_COMPLETE:
1229 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001230 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001231 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001232 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001233 data[0] = data[1] = 0;
1234 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001235 break;
1236
1237 default:
1238 break;
1239 }
1240
1241 return 0;
1242}
1243
1244static
1245void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1246{
1247 fcport->rscn_gen++;
1248
Quinn Tran83548fe2017-06-02 09:12:01 -07001249 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1250 "%s %8phC DS %d LS %d\n",
1251 __func__, fcport->port_name, fcport->disc_state,
1252 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001253
1254 if (fcport->flags & FCF_ASYNC_SENT)
1255 return;
1256
1257 switch (fcport->disc_state) {
1258 case DSC_DELETED:
1259 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001260 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001261 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001262 default:
1263 break;
1264 }
1265}
1266
1267int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001268 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001269{
1270 struct qla_work_evt *e;
1271 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1272 if (!e)
1273 return QLA_FUNCTION_FAILED;
1274
1275 e->u.new_sess.id = *id;
1276 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001277 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001278 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001279 if (node_name)
1280 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001281
1282 return qla2x00_post_work(vha, e);
1283}
1284
1285static
Quinn Tran726b8542017-01-19 22:28:00 -08001286void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1287 struct event_arg *ea)
1288{
1289 fc_port_t *fcport = ea->fcport;
1290
Quinn Tran83548fe2017-06-02 09:12:01 -07001291 ql_dbg(ql_dbg_disc, vha, 0x2102,
1292 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1293 __func__, fcport->port_name, fcport->disc_state,
1294 fcport->fw_login_state, fcport->login_pause,
1295 fcport->deleted, fcport->conflict,
1296 fcport->last_rscn_gen, fcport->rscn_gen,
1297 fcport->last_login_gen, fcport->login_gen,
1298 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001299
1300 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001301 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1302 return;
1303
Quinn Tran5b334692017-03-15 09:48:48 -07001304 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001305 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1306 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001307 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001308 }
Quinn Tran5b334692017-03-15 09:48:48 -07001309 }
1310
Quinn Tran726b8542017-01-19 22:28:00 -08001311 if (fcport->flags & FCF_ASYNC_SENT) {
1312 fcport->login_retry++;
1313 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1314 return;
1315 }
1316
1317 if (fcport->disc_state == DSC_DELETE_PEND) {
1318 fcport->login_retry++;
1319 return;
1320 }
1321
1322 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001323 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001324 __func__, __LINE__, fcport->port_name);
1325
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001326 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001327 return;
1328 }
1329
1330 qla24xx_fcport_handle_login(vha, fcport);
1331}
1332
Quinn Tran41dc5292017-01-19 22:28:03 -08001333void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001334{
Quinn Tranf352eeb2017-12-28 12:33:35 -08001335 fc_port_t *f, *tf;
Quinn Tran41dc5292017-01-19 22:28:03 -08001336 uint32_t id = 0, mask, rid;
Quinn Tranf352eeb2017-12-28 12:33:35 -08001337 unsigned long flags;
Quinn Tran726b8542017-01-19 22:28:00 -08001338
1339 switch (ea->event) {
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001340 case FCME_RSCN:
1341 case FCME_GIDPN_DONE:
1342 case FCME_GPSC_DONE:
1343 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001344 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001345 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1346 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1347 return;
1348 break;
1349 default:
1350 break;
1351 }
1352
1353 switch (ea->event) {
1354 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001355 if (test_bit(UNLOADING, &vha->dpc_flags))
1356 return;
1357
1358 qla24xx_handle_relogin_event(vha, ea);
1359 break;
1360 case FCME_RSCN:
1361 if (test_bit(UNLOADING, &vha->dpc_flags))
1362 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001363 switch (ea->id.b.rsvd_1) {
1364 case RSCN_PORT_ADDR:
Quinn Tranf352eeb2017-12-28 12:33:35 -08001365 spin_lock_irqsave(&vha->work_lock, flags);
1366 if (vha->scan.scan_flags == 0) {
1367 ql_dbg(ql_dbg_disc, vha, 0xffff,
1368 "%s: schedule\n", __func__);
1369 vha->scan.scan_flags |= SF_QUEUED;
1370 schedule_delayed_work(&vha->scan.scan_work, 5);
Quinn Tran726b8542017-01-19 22:28:00 -08001371 }
Quinn Tranf352eeb2017-12-28 12:33:35 -08001372 spin_unlock_irqrestore(&vha->work_lock, flags);
1373
Quinn Tran41dc5292017-01-19 22:28:03 -08001374 break;
1375 case RSCN_AREA_ADDR:
1376 case RSCN_DOM_ADDR:
1377 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1378 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001379 ql_dbg(ql_dbg_async, vha, 0x5044,
1380 "RSCN: Area 0x%06x was affected\n",
1381 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001382 } else {
1383 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001384 ql_dbg(ql_dbg_async, vha, 0x507a,
1385 "RSCN: Domain 0x%06x was affected\n",
1386 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001387 }
1388
1389 rid = ea->id.b24 & mask;
1390 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1391 list) {
1392 id = f->d_id.b24 & mask;
1393 if (rid == id) {
1394 ea->fcport = f;
1395 qla24xx_handle_rscn_event(f, ea);
1396 }
1397 }
1398 break;
1399 case RSCN_FAB_ADDR:
1400 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001401 ql_log(ql_log_warn, vha, 0xd045,
1402 "RSCN: Fabric was affected. Addr format %d\n",
1403 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001404 qla2x00_mark_all_devices_lost(vha, 1);
1405 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1406 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001407 }
1408 break;
1409 case FCME_GIDPN_DONE:
1410 qla24xx_handle_gidpn_event(vha, ea);
1411 break;
1412 case FCME_GNL_DONE:
1413 qla24xx_handle_gnl_done_event(vha, ea);
1414 break;
1415 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001416 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001417 break;
1418 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1419 qla24xx_handle_plogi_done_event(vha, ea);
1420 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001421 case FCME_PRLI_DONE:
1422 qla24xx_handle_prli_done_event(vha, ea);
1423 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001424 case FCME_GPDB_DONE:
1425 qla24xx_handle_gpdb_event(vha, ea);
1426 break;
1427 case FCME_GPNID_DONE:
1428 qla24xx_handle_gpnid_event(vha, ea);
1429 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001430 case FCME_GFFID_DONE:
1431 qla24xx_handle_gffid_event(vha, ea);
1432 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001433 case FCME_ADISC_DONE:
1434 qla24xx_handle_adisc_event(vha, ea);
1435 break;
Quinn Trana4239942017-12-28 12:33:26 -08001436 case FCME_GNNID_DONE:
1437 qla24xx_handle_gnnid_event(vha, ea);
1438 break;
1439 case FCME_GFPNID_DONE:
1440 qla24xx_handle_gfpnid_event(vha, ea);
1441 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001442 default:
1443 BUG_ON(1);
1444 break;
1445 }
1446}
1447
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001448static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001449qla2x00_tmf_iocb_timeout(void *data)
1450{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001451 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001452 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1453
1454 tmf->u.tmf.comp_status = CS_TIMEOUT;
1455 complete(&tmf->u.tmf.comp);
1456}
1457
1458static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001459qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001460{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001461 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001462 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001463
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001464 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001465}
1466
1467int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001468qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001469 uint32_t tag)
1470{
1471 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001472 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001473 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001474 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001475
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001476 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001477 if (!sp)
1478 goto done;
1479
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001480 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001481 sp->type = SRB_TM_CMD;
1482 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001483 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1484 tm_iocb->u.tmf.flags = flags;
1485 tm_iocb->u.tmf.lun = lun;
1486 tm_iocb->u.tmf.data = tag;
1487 sp->done = qla2x00_tmf_sp_done;
1488 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1489 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001490
1491 rval = qla2x00_start_sp(sp);
1492 if (rval != QLA_SUCCESS)
1493 goto done_free_sp;
1494
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001495 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001496 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1497 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1498 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001499
1500 wait_for_completion(&tm_iocb->u.tmf.comp);
1501
1502 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1503 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1504
1505 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1506 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1507 "TM IOCB failed (%x).\n", rval);
1508 }
1509
1510 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1511 flags = tm_iocb->u.tmf.flags;
1512 lun = (uint16_t)tm_iocb->u.tmf.lun;
1513
1514 /* Issue Marker IOCB */
1515 qla2x00_marker(vha, vha->hw->req_q_map[0],
1516 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1517 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1518 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001519
1520done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001521 sp->free(sp);
Quinn Tran6d6749272017-12-28 12:33:41 -08001522 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001523done:
1524 return rval;
1525}
1526
Armen Baloyan4440e462014-02-26 04:15:18 -05001527static void
1528qla24xx_abort_iocb_timeout(void *data)
1529{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001530 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001531 struct srb_iocb *abt = &sp->u.iocb_cmd;
1532
1533 abt->u.abt.comp_status = CS_TIMEOUT;
1534 complete(&abt->u.abt.comp);
1535}
1536
1537static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001538qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001539{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001540 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001541 struct srb_iocb *abt = &sp->u.iocb_cmd;
1542
1543 complete(&abt->u.abt.comp);
1544}
1545
Quinn Tran15f30a52017-03-15 09:48:52 -07001546int
Armen Baloyan4440e462014-02-26 04:15:18 -05001547qla24xx_async_abort_cmd(srb_t *cmd_sp)
1548{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001549 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001550 fc_port_t *fcport = cmd_sp->fcport;
1551 struct srb_iocb *abt_iocb;
1552 srb_t *sp;
1553 int rval = QLA_FUNCTION_FAILED;
1554
1555 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1556 if (!sp)
1557 goto done;
1558
1559 abt_iocb = &sp->u.iocb_cmd;
1560 sp->type = SRB_ABT_CMD;
1561 sp->name = "abort";
1562 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1563 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1564 sp->done = qla24xx_abort_sp_done;
1565 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1566 init_completion(&abt_iocb->u.abt.comp);
1567
1568 rval = qla2x00_start_sp(sp);
1569 if (rval != QLA_SUCCESS)
1570 goto done_free_sp;
1571
1572 ql_dbg(ql_dbg_async, vha, 0x507c,
1573 "Abort command issued - hdl=%x, target_id=%x\n",
1574 cmd_sp->handle, fcport->tgt_id);
1575
1576 wait_for_completion(&abt_iocb->u.abt.comp);
1577
1578 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1579 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1580
1581done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001582 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001583done:
1584 return rval;
1585}
1586
1587int
1588qla24xx_async_abort_command(srb_t *sp)
1589{
1590 unsigned long flags = 0;
1591
1592 uint32_t handle;
1593 fc_port_t *fcport = sp->fcport;
1594 struct scsi_qla_host *vha = fcport->vha;
1595 struct qla_hw_data *ha = vha->hw;
1596 struct req_que *req = vha->req;
1597
1598 spin_lock_irqsave(&ha->hardware_lock, flags);
1599 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1600 if (req->outstanding_cmds[handle] == sp)
1601 break;
1602 }
1603 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1604 if (handle == req->num_outstanding_cmds) {
1605 /* Command not found. */
1606 return QLA_FUNCTION_FAILED;
1607 }
1608 if (sp->type == SRB_FXIOCB_DCMD)
1609 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1610 FXDISC_ABORT_IOCTL);
1611
1612 return qla24xx_async_abort_cmd(sp);
1613}
1614
Quinn Tran726b8542017-01-19 22:28:00 -08001615static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001616qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1617{
1618 switch (ea->data[0]) {
1619 case MBS_COMMAND_COMPLETE:
1620 ql_dbg(ql_dbg_disc, vha, 0x2118,
1621 "%s %d %8phC post gpdb\n",
1622 __func__, __LINE__, ea->fcport->port_name);
1623
1624 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1625 ea->fcport->logout_on_delete = 1;
1626 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1627 break;
1628 default:
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001629 if (ea->fcport->n2n_flag) {
1630 ql_dbg(ql_dbg_disc, vha, 0x2118,
1631 "%s %d %8phC post fc4 prli\n",
1632 __func__, __LINE__, ea->fcport->port_name);
1633 ea->fcport->fc4f_nvme = 0;
1634 ea->fcport->n2n_flag = 0;
1635 qla24xx_post_prli_work(vha, ea->fcport);
1636 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001637 ql_dbg(ql_dbg_disc, vha, 0x2119,
1638 "%s %d %8phC unhandle event of %x\n",
1639 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1640 break;
1641 }
1642}
1643
1644static void
Quinn Tran726b8542017-01-19 22:28:00 -08001645qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001646{
Quinn Tran726b8542017-01-19 22:28:00 -08001647 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001648 u16 lid;
1649 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001650 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001651 struct fc_port *fcport = ea->fcport;
1652
Quinn Tranf352eeb2017-12-28 12:33:35 -08001653 ql_dbg(ql_dbg_disc, vha, 0xffff,
1654 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1655 __func__, fcport->port_name, fcport->disc_state,
1656 fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1657 ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1658 ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1659
Quinn Trana4239942017-12-28 12:33:26 -08001660 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1661 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1662 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1663 "%s %d %8phC Remote is trying to login\n",
1664 __func__, __LINE__, fcport->port_name);
1665 return;
1666 }
1667
1668 if (fcport->disc_state == DSC_DELETE_PEND)
1669 return;
1670
1671 if (ea->sp->gen2 != fcport->login_gen) {
1672 /* target side must have changed it. */
1673 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001674 "%s %8phC generation changed\n",
1675 __func__, fcport->port_name);
1676 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Trana4239942017-12-28 12:33:26 -08001677 return;
1678 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1679 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1680 __func__, __LINE__, fcport->port_name);
1681 qla24xx_post_gidpn_work(vha, fcport);
1682 return;
1683 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001684
Quinn Tran726b8542017-01-19 22:28:00 -08001685 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001686 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001687 /*
1688 * Driver must validate login state - If PRLI not complete,
1689 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1690 * requests.
1691 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001692 if (ea->fcport->fc4f_nvme) {
1693 ql_dbg(ql_dbg_disc, vha, 0x2117,
1694 "%s %d %8phC post prli\n",
1695 __func__, __LINE__, ea->fcport->port_name);
1696 qla24xx_post_prli_work(vha, ea->fcport);
1697 } else {
1698 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001699 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1700 __func__, __LINE__, ea->fcport->port_name,
1701 ea->fcport->loop_id, ea->fcport->d_id.b24);
1702
1703 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001704 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana084fd62017-12-04 14:45:00 -08001705 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001706 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1707 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001708 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001709 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1710 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1711
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001712 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1713 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001714 break;
1715 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001716 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001717 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1718
1719 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1720 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1721 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001722 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1723 else
Quinn Tran726b8542017-01-19 22:28:00 -08001724 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001725 break;
1726 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001727 /* data[1] = IO PARAM 1 = nport ID */
1728 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1729 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1730 cid.b.al_pa = ea->iop[1] & 0xff;
1731 cid.b.rsvd_1 = 0;
1732
Quinn Tran83548fe2017-06-02 09:12:01 -07001733 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1734 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1735 __func__, __LINE__, ea->fcport->port_name,
1736 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001737
1738 if (IS_SW_RESV_ADDR(cid)) {
1739 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1740 ea->fcport->loop_id = FC_NO_LOOP_ID;
1741 } else {
1742 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001743 }
Quinn Tran726b8542017-01-19 22:28:00 -08001744 qla24xx_post_gnl_work(vha, ea->fcport);
1745 break;
1746 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001747 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1748 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1749 __func__, __LINE__, ea->fcport->port_name,
1750 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1751 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001752
Quinn Trana084fd62017-12-04 14:45:00 -08001753 lid = ea->iop[1] & 0xffff;
1754 qlt_find_sess_invalidate_other(vha,
1755 wwn_to_u64(ea->fcport->port_name),
1756 ea->fcport->d_id, lid, &conflict_fcport);
1757
1758 if (conflict_fcport) {
1759 /*
1760 * Another fcport share the same loop_id/nport id.
1761 * Conflict fcport needs to finish cleanup before this
1762 * fcport can proceed to login.
1763 */
1764 conflict_fcport->conflict = ea->fcport;
1765 ea->fcport->login_pause = 1;
1766
1767 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1768 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1769 __func__, __LINE__, ea->fcport->port_name,
1770 ea->fcport->d_id.b24, lid);
1771 qla2x00_clear_loop_id(ea->fcport);
1772 qla24xx_post_gidpn_work(vha, ea->fcport);
1773 } else {
1774 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1775 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1776 __func__, __LINE__, ea->fcport->port_name,
1777 ea->fcport->d_id.b24, lid);
1778
1779 qla2x00_clear_loop_id(ea->fcport);
1780 set_bit(lid, vha->hw->loop_id_map);
1781 ea->fcport->loop_id = lid;
1782 ea->fcport->keep_nport_handle = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001783 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana084fd62017-12-04 14:45:00 -08001784 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001785 break;
1786 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001787 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001788}
1789
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001790void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001791qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1792 uint16_t *data)
1793{
Quinn Tran726b8542017-01-19 22:28:00 -08001794 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001795 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001796 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001797 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001798}
1799
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001800void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001801qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1802 uint16_t *data)
1803{
1804 if (data[0] == MBS_COMMAND_COMPLETE) {
1805 qla2x00_update_fcport(vha, fcport);
1806
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001807 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001808 }
1809
1810 /* Retry login. */
1811 fcport->flags &= ~FCF_ASYNC_SENT;
1812 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1813 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1814 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001815 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001816
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001817 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820/****************************************************************************/
1821/* QLogic ISP2x00 Hardware Support Functions. */
1822/****************************************************************************/
1823
Saurav Kashyapfa492632012-11-21 02:40:29 -05001824static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001825qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1826{
1827 int rval = QLA_SUCCESS;
1828 struct qla_hw_data *ha = vha->hw;
1829 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001830 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001831
1832 qla83xx_idc_lock(vha, 0);
1833
1834 /* SV: TODO: Assign initialization timeout from
1835 * flash-info / other param
1836 */
1837 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1838 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1839
1840 /* Set our fcoe function presence */
1841 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1842 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1843 "Error while setting DRV-Presence.\n");
1844 rval = QLA_FUNCTION_FAILED;
1845 goto exit;
1846 }
1847
1848 /* Decide the reset ownership */
1849 qla83xx_reset_ownership(vha);
1850
1851 /*
1852 * On first protocol driver load:
1853 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1854 * register.
1855 * Others: Check compatibility with current IDC Major version.
1856 */
1857 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1858 if (ha->flags.nic_core_reset_owner) {
1859 /* Set IDC Major version */
1860 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1861 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1862
1863 /* Clearing IDC-Lock-Recovery register */
1864 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1865 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1866 /*
1867 * Clear further IDC participation if we are not compatible with
1868 * the current IDC Major Version.
1869 */
1870 ql_log(ql_log_warn, vha, 0xb07d,
1871 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1872 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1873 __qla83xx_clear_drv_presence(vha);
1874 rval = QLA_FUNCTION_FAILED;
1875 goto exit;
1876 }
1877 /* Each function sets its supported Minor version. */
1878 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1879 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1880 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1881
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001882 if (ha->flags.nic_core_reset_owner) {
1883 memset(config, 0, sizeof(config));
1884 if (!qla81xx_get_port_config(vha, config))
1885 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1886 QLA8XXX_DEV_READY);
1887 }
1888
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001889 rval = qla83xx_idc_state_handler(vha);
1890
1891exit:
1892 qla83xx_idc_unlock(vha, 0);
1893
1894 return rval;
1895}
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/*
1898* qla2x00_initialize_adapter
1899* Initialize board.
1900*
1901* Input:
1902* ha = adapter block pointer.
1903*
1904* Returns:
1905* 0 = success
1906*/
1907int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001908qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
1910 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001911 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001912 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001913
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001914 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1915 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001918 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001919 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001920 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001921 ha->flags.pci_channel_io_perm_failure = 0;
1922 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001923 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001924 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1925 atomic_set(&vha->loop_state, LOOP_DOWN);
1926 vha->device_flags = DFLG_NO_CABLE;
1927 vha->dpc_flags = 0;
1928 vha->flags.management_server_logged_in = 0;
1929 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 ha->isp_abort_cnt = 0;
1931 ha->beacon_blink_led = 0;
1932
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001933 set_bit(0, ha->req_qid_map);
1934 set_bit(0, ha->rsp_qid_map);
1935
Chad Dupuiscfb09192011-11-18 09:03:07 -08001936 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001937 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001938 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001940 ql_log(ql_log_warn, vha, 0x0044,
1941 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return (rval);
1943 }
1944
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001945 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001947 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001948 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001949 ql_log(ql_log_fatal, vha, 0x004f,
1950 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001951 return rval;
1952 }
1953
1954 if (IS_QLA8044(ha)) {
1955 qla8044_read_reset_template(vha);
1956
1957 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1958 * If DONRESET_BIT0 is set, drivers should not set dev_state
1959 * to NEED_RESET. But if NEED_RESET is set, drivers should
1960 * should honor the reset. */
1961 if (ql2xdontresethba == 1)
1962 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001963 }
1964
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001965 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001966 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001967 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001968
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001969 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001971 if (ha->flags.disable_serdes) {
1972 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001973 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001974 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001975 return QLA_FUNCTION_FAILED;
1976 }
1977
Chad Dupuiscfb09192011-11-18 09:03:07 -08001978 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001979 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001981 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1982 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001983 if (rval)
1984 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001985 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001986 if (rval)
1987 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001989
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001990 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001991 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001992 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001993 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001994 "Unable to configure ISP84XX.\n");
1995 return QLA_FUNCTION_FAILED;
1996 }
1997 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001998
Quinn Tranead03852017-01-19 22:28:01 -08001999 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002000 rval = qla2x00_init_rings(vha);
2001
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002002 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002004 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07002005 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002006 rval = qla84xx_init_chip(vha);
2007 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002008 ql_log(ql_log_warn, vha, 0x00d4,
2009 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07002010 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002011 }
2012 }
2013
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002014 /* Load the NIC Core f/w if we are the first protocol driver. */
2015 if (IS_QLA8031(ha)) {
2016 rval = qla83xx_nic_core_fw_load(vha);
2017 if (rval)
2018 ql_log(ql_log_warn, vha, 0x0124,
2019 "Error in initializing NIC Core f/w.\n");
2020 }
2021
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002022 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2023 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002024
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002025 if (IS_P3P_TYPE(ha))
2026 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2027 else
2028 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return (rval);
2031}
2032
2033/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002034 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 * @ha: HA context
2036 *
2037 * Returns 0 on success.
2038 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002039int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002040qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002042 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002043 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002044 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002045 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002048 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002051 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2053
Andrew Vasquez737faec2008-10-24 15:13:45 -07002054 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002056 /* Get PCI bus information. */
2057 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002058 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002059 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2060
2061 return QLA_SUCCESS;
2062}
2063
2064/**
2065 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2066 * @ha: HA context
2067 *
2068 * Returns 0 on success.
2069 */
2070int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002071qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002072{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002073 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002074 unsigned long flags = 0;
2075 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002076 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002077 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002078
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002079 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002080 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002081
2082 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002083 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002084
2085 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2086 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002087 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002088
2089 /*
2090 * If this is a 2300 card and not 2312, reset the
2091 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2092 * the 2310 also reports itself as a 2300 so we need to get the
2093 * fb revision level -- a 6 indicates it really is a 2300 and
2094 * not a 2310.
2095 */
2096 if (IS_QLA2300(ha)) {
2097 spin_lock_irqsave(&ha->hardware_lock, flags);
2098
2099 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002100 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002101 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002102 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002103 break;
2104
2105 udelay(10);
2106 }
2107
2108 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002109 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2110 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002111
2112 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002113 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002114
2115 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002116 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002117
2118 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002119 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2120 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002121
2122 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002123 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002124 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002125 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002126 break;
2127
2128 udelay(10);
2129 }
2130
2131 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2132 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002133
2134 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2135
Andrew Vasquez737faec2008-10-24 15:13:45 -07002136 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002137
2138 /* Get PCI bus information. */
2139 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002140 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002141 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2142
2143 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002147 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2148 * @ha: HA context
2149 *
2150 * Returns 0 on success.
2151 */
2152int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002153qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002154{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002155 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002156 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002157 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002158 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002159
2160 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002161 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002162
2163 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002164 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002165 w &= ~PCI_COMMAND_INTX_DISABLE;
2166 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2167
2168 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2169
2170 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002171 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2172 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002173
2174 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002175 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002176 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002177
Andrew Vasquez737faec2008-10-24 15:13:45 -07002178 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002179
Auke Kok44c10132007-06-08 15:46:36 -07002180 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002181
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002182 /* Get PCI bus information. */
2183 spin_lock_irqsave(&ha->hardware_lock, flags);
2184 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2185 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2186
2187 return QLA_SUCCESS;
2188}
2189
2190/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002191 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2192 * @ha: HA context
2193 *
2194 * Returns 0 on success.
2195 */
2196int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002197qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002198{
2199 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002200 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002201
2202 pci_set_master(ha->pdev);
2203 pci_try_set_mwi(ha->pdev);
2204
2205 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2206 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2207 w &= ~PCI_COMMAND_INTX_DISABLE;
2208 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2209
2210 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002211 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002212 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002213
Andrew Vasquez737faec2008-10-24 15:13:45 -07002214 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002215
2216 ha->chip_revision = ha->pdev->revision;
2217
2218 return QLA_SUCCESS;
2219}
2220
2221/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 * qla2x00_isp_firmware() - Choose firmware image.
2223 * @ha: HA context
2224 *
2225 * Returns 0 on success.
2226 */
2227static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002228qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
2230 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002231 uint16_t loop_id, topo, sw_cap;
2232 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002233 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002236 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002239 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002242 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002243 if (rval == QLA_SUCCESS) {
2244 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002245 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002246 &area, &domain, &topo, &sw_cap);
2247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002250 if (rval)
2251 ql_dbg(ql_dbg_init, vha, 0x007a,
2252 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 return (rval);
2255}
2256
2257/**
2258 * qla2x00_reset_chip() - Reset ISP chip.
2259 * @ha: HA context
2260 *
2261 * Returns 0 on success.
2262 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002263void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002264qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002267 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002268 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 uint16_t cmd;
2271
Andrew Vasquez85880802009-12-15 21:29:46 -08002272 if (unlikely(pci_channel_offline(ha->pdev)))
2273 return;
2274
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002275 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 spin_lock_irqsave(&ha->hardware_lock, flags);
2278
2279 /* Turn off master enable */
2280 cmd = 0;
2281 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2282 cmd &= ~PCI_COMMAND_MASTER;
2283 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2284
2285 if (!IS_QLA2100(ha)) {
2286 /* Pause RISC. */
2287 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2288 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2289 for (cnt = 0; cnt < 30000; cnt++) {
2290 if ((RD_REG_WORD(&reg->hccr) &
2291 HCCR_RISC_PAUSE) != 0)
2292 break;
2293 udelay(100);
2294 }
2295 } else {
2296 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2297 udelay(10);
2298 }
2299
2300 /* Select FPM registers. */
2301 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2302 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2303
2304 /* FPM Soft Reset. */
2305 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2306 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2307
2308 /* Toggle Fpm Reset. */
2309 if (!IS_QLA2200(ha)) {
2310 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2311 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2312 }
2313
2314 /* Select frame buffer registers. */
2315 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2316 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2317
2318 /* Reset frame buffer FIFOs. */
2319 if (IS_QLA2200(ha)) {
2320 WRT_FB_CMD_REG(ha, reg, 0xa000);
2321 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2322 } else {
2323 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2324
2325 /* Read back fb_cmd until zero or 3 seconds max */
2326 for (cnt = 0; cnt < 3000; cnt++) {
2327 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2328 break;
2329 udelay(100);
2330 }
2331 }
2332
2333 /* Select RISC module registers. */
2334 WRT_REG_WORD(&reg->ctrl_status, 0);
2335 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2336
2337 /* Reset RISC processor. */
2338 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2339 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2340
2341 /* Release RISC processor. */
2342 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2343 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2344 }
2345
2346 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2347 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2348
2349 /* Reset ISP chip. */
2350 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2351
2352 /* Wait for RISC to recover from reset. */
2353 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2354 /*
2355 * It is necessary to for a delay here since the card doesn't
2356 * respond to PCI reads during a reset. On some architectures
2357 * this will result in an MCA.
2358 */
2359 udelay(20);
2360 for (cnt = 30000; cnt; cnt--) {
2361 if ((RD_REG_WORD(&reg->ctrl_status) &
2362 CSR_ISP_SOFT_RESET) == 0)
2363 break;
2364 udelay(100);
2365 }
2366 } else
2367 udelay(10);
2368
2369 /* Reset RISC processor. */
2370 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2371
2372 WRT_REG_WORD(&reg->semaphore, 0);
2373
2374 /* Release RISC processor. */
2375 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2376 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2377
2378 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2379 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002380 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 udelay(100);
2384 }
2385 } else
2386 udelay(100);
2387
2388 /* Turn on master enable */
2389 cmd |= PCI_COMMAND_MASTER;
2390 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2391
2392 /* Disable RISC pause on FPM parity error. */
2393 if (!IS_QLA2100(ha)) {
2394 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2395 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2396 }
2397
2398 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2399}
2400
2401/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002402 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2403 *
2404 * Returns 0 on success.
2405 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002406static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002407qla81xx_reset_mpi(scsi_qla_host_t *vha)
2408{
2409 uint16_t mb[4] = {0x1010, 0, 1, 0};
2410
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002411 if (!IS_QLA81XX(vha->hw))
2412 return QLA_SUCCESS;
2413
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002414 return qla81xx_write_mpi_register(vha, mb);
2415}
2416
2417/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002418 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002419 * @ha: HA context
2420 *
2421 * Returns 0 on success.
2422 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002423static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002424qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002425{
2426 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002427 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002428 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002429 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002430 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002431 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002432 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002433
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002434 spin_lock_irqsave(&ha->hardware_lock, flags);
2435
2436 /* Reset RISC. */
2437 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2438 for (cnt = 0; cnt < 30000; cnt++) {
2439 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2440 break;
2441
2442 udelay(10);
2443 }
2444
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002445 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2446 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2447
2448 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2449 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2450 RD_REG_DWORD(&reg->hccr),
2451 RD_REG_DWORD(&reg->ctrl_status),
2452 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2453
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002454 WRT_REG_DWORD(&reg->ctrl_status,
2455 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002456 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002457
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002458 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002459
Andrew Vasquez88c26662005-07-08 17:59:26 -07002460 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002461 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002462 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2463 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002464 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002465 if (cnt)
2466 udelay(5);
2467 else
2468 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002469 }
2470
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002471 if (rval == QLA_SUCCESS)
2472 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2473
2474 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2475 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2476 RD_REG_DWORD(&reg->hccr),
2477 RD_REG_DWORD(&reg->mailbox0));
2478
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002479 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002480 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002481 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002482 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002483 if ((RD_REG_DWORD(&reg->ctrl_status) &
2484 CSRX_ISP_SOFT_RESET) == 0)
2485 break;
2486
2487 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002488 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002489 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2490 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2491
2492 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2493 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2494 RD_REG_DWORD(&reg->hccr),
2495 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002496
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002497 /* If required, do an MPI FW reset now */
2498 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2499 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2500 if (++abts_cnt < 5) {
2501 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2502 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2503 } else {
2504 /*
2505 * We exhausted the ISP abort retries. We have to
2506 * set the board offline.
2507 */
2508 abts_cnt = 0;
2509 vha->flags.online = 0;
2510 }
2511 }
2512 }
2513
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002514 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2515 RD_REG_DWORD(&reg->hccr);
2516
2517 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2518 RD_REG_DWORD(&reg->hccr);
2519
2520 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2521 RD_REG_DWORD(&reg->hccr);
2522
Bart Van Assche52c82822015-07-09 07:23:26 -07002523 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002524 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002525 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002526 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002527 if (cnt)
2528 udelay(5);
2529 else
2530 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002531 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002532 if (rval == QLA_SUCCESS)
2533 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2534
2535 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2536 "Host Risc 0x%x, mailbox0 0x%x\n",
2537 RD_REG_DWORD(&reg->hccr),
2538 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002539
2540 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002541
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002542 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2543 "Driver in %s mode\n",
2544 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2545
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002546 if (IS_NOPOLLING_TYPE(ha))
2547 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002548
2549 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002550}
2551
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002552static void
2553qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2554{
2555 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2556
2557 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2558 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2559
2560}
2561
2562static void
2563qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2564{
2565 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2566
2567 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2568 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2569}
2570
2571static void
2572qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2573{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002574 uint32_t wd32 = 0;
2575 uint delta_msec = 100;
2576 uint elapsed_msec = 0;
2577 uint timeout_msec;
2578 ulong n;
2579
Joe Carnucciocc790762015-08-04 13:37:53 -04002580 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2581 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002582 return;
2583
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002584 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2585 udelay(100);
2586
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002587attempt:
2588 timeout_msec = TIMEOUT_SEMAPHORE;
2589 n = timeout_msec / delta_msec;
2590 while (n--) {
2591 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2592 qla25xx_read_risc_sema_reg(vha, &wd32);
2593 if (wd32 & RISC_SEMAPHORE)
2594 break;
2595 msleep(delta_msec);
2596 elapsed_msec += delta_msec;
2597 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2598 goto force;
2599 }
2600
2601 if (!(wd32 & RISC_SEMAPHORE))
2602 goto force;
2603
2604 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2605 goto acquired;
2606
2607 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2608 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2609 n = timeout_msec / delta_msec;
2610 while (n--) {
2611 qla25xx_read_risc_sema_reg(vha, &wd32);
2612 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2613 break;
2614 msleep(delta_msec);
2615 elapsed_msec += delta_msec;
2616 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2617 goto force;
2618 }
2619
2620 if (wd32 & RISC_SEMAPHORE_FORCE)
2621 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2622
2623 goto attempt;
2624
2625force:
2626 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2627
2628acquired:
2629 return;
2630}
2631
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002632/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002633 * qla24xx_reset_chip() - Reset ISP24xx chip.
2634 * @ha: HA context
2635 *
2636 * Returns 0 on success.
2637 */
2638void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002639qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002640{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002641 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002642
2643 if (pci_channel_offline(ha->pdev) &&
2644 ha->flags.pci_channel_io_perm_failure) {
2645 return;
2646 }
2647
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002648 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002649
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002650 qla25xx_manipulate_risc_semaphore(vha);
2651
Andrew Vasquez88c26662005-07-08 17:59:26 -07002652 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002653 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002654}
2655
2656/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 * qla2x00_chip_diag() - Test chip for proper operation.
2658 * @ha: HA context
2659 *
2660 * Returns 0 on success.
2661 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002662int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002663qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002666 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002667 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 unsigned long flags = 0;
2669 uint16_t data;
2670 uint32_t cnt;
2671 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002672 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 /* Assume a failed state */
2675 rval = QLA_FUNCTION_FAILED;
2676
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002677 ql_dbg(ql_dbg_init, vha, 0x007b,
2678 "Testing device at %lx.\n", (u_long)&reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 spin_lock_irqsave(&ha->hardware_lock, flags);
2681
2682 /* Reset ISP chip. */
2683 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2684
2685 /*
2686 * We need to have a delay here since the card will not respond while
2687 * in reset causing an MCA on some architectures.
2688 */
2689 udelay(20);
2690 data = qla2x00_debounce_register(&reg->ctrl_status);
2691 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2692 udelay(5);
2693 data = RD_REG_WORD(&reg->ctrl_status);
2694 barrier();
2695 }
2696
2697 if (!cnt)
2698 goto chip_diag_failed;
2699
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002700 ql_dbg(ql_dbg_init, vha, 0x007c,
2701 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 /* Reset RISC processor. */
2704 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2705 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2706
2707 /* Workaround for QLA2312 PCI parity error */
2708 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2709 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2710 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2711 udelay(5);
2712 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002713 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
2715 } else
2716 udelay(10);
2717
2718 if (!cnt)
2719 goto chip_diag_failed;
2720
2721 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002722 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2725 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2726 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2727 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2728 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2729 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002730 ql_log(ql_log_warn, vha, 0x0062,
2731 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2732 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 goto chip_diag_failed;
2735 }
2736 ha->product_id[0] = mb[1];
2737 ha->product_id[1] = mb[2];
2738 ha->product_id[2] = mb[3];
2739 ha->product_id[3] = mb[4];
2740
2741 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002742 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2744 else
2745 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002746 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 if (IS_QLA2200(ha) &&
2749 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2750 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002751 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002753 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 ha->fw_transfer_size = 128;
2755 }
2756
2757 /* Wrap Incoming Mailboxes Test. */
2758 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2759
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002760 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002761 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002762 if (rval)
2763 ql_log(ql_log_warn, vha, 0x0080,
2764 "Failed mailbox send register test.\n");
2765 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* Flag a successful rval */
2767 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 spin_lock_irqsave(&ha->hardware_lock, flags);
2769
2770chip_diag_failed:
2771 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002772 ql_log(ql_log_info, vha, 0x0081,
2773 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2776
2777 return (rval);
2778}
2779
2780/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002781 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2782 * @ha: HA context
2783 *
2784 * Returns 0 on success.
2785 */
2786int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002787qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002788{
2789 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002790 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002791 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002792
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002793 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002794 return QLA_SUCCESS;
2795
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002796 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002797
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002798 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002799 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002800 ql_log(ql_log_warn, vha, 0x0082,
2801 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002802 } else {
2803 /* Flag a successful rval */
2804 rval = QLA_SUCCESS;
2805 }
2806
2807 return rval;
2808}
2809
Quinn Tranad0a0b02017-12-28 12:33:14 -08002810static void
2811qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002812{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002813 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002814 dma_addr_t tc_dma;
2815 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002816 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002817
Quinn Tranad0a0b02017-12-28 12:33:14 -08002818 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002819 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002820 "%s: Offload Mem is already allocated.\n",
2821 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002822 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002823 }
2824
Quinn Tranad0a0b02017-12-28 12:33:14 -08002825 if (IS_FWI2_CAPABLE(ha)) {
2826 /* Allocate memory for Fibre Channel Event Buffer. */
2827 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2828 !IS_QLA27XX(ha))
2829 goto try_eft;
2830
2831 if (ha->fce)
2832 dma_free_coherent(&ha->pdev->dev,
2833 FCE_SIZE, ha->fce, ha->fce_dma);
2834
2835 /* Allocate memory for Fibre Channel Event Buffer. */
2836 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2837 GFP_KERNEL);
2838 if (!tc) {
2839 ql_log(ql_log_warn, vha, 0x00be,
2840 "Unable to allocate (%d KB) for FCE.\n",
2841 FCE_SIZE / 1024);
2842 goto try_eft;
2843 }
2844
2845 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2846 ha->fce_mb, &ha->fce_bufs);
2847 if (rval) {
2848 ql_log(ql_log_warn, vha, 0x00bf,
2849 "Unable to initialize FCE (%d).\n", rval);
2850 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2851 tc_dma);
2852 ha->flags.fce_enabled = 0;
2853 goto try_eft;
2854 }
2855 ql_dbg(ql_dbg_init, vha, 0x00c0,
2856 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2857
2858 ha->flags.fce_enabled = 1;
2859 ha->fce_dma = tc_dma;
2860 ha->fce = tc;
2861
2862try_eft:
2863 if (ha->eft)
2864 dma_free_coherent(&ha->pdev->dev,
2865 EFT_SIZE, ha->eft, ha->eft_dma);
2866
2867 /* Allocate memory for Extended Trace Buffer. */
2868 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2869 GFP_KERNEL);
2870 if (!tc) {
2871 ql_log(ql_log_warn, vha, 0x00c1,
2872 "Unable to allocate (%d KB) for EFT.\n",
2873 EFT_SIZE / 1024);
2874 goto eft_err;
2875 }
2876
2877 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2878 if (rval) {
2879 ql_log(ql_log_warn, vha, 0x00c2,
2880 "Unable to initialize EFT (%d).\n", rval);
2881 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2882 tc_dma);
2883 goto eft_err;
2884 }
2885 ql_dbg(ql_dbg_init, vha, 0x00c3,
2886 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2887
2888 ha->eft_dma = tc_dma;
2889 ha->eft = tc;
2890 }
2891
2892eft_err:
2893 return;
2894}
2895
2896void
2897qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2898{
2899 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2900 eft_size, fce_size, mq_size;
2901 struct qla_hw_data *ha = vha->hw;
2902 struct req_que *req = ha->req_q_map[0];
2903 struct rsp_que *rsp = ha->rsp_q_map[0];
2904 struct qla2xxx_fw_dump *fw_dump;
2905
Chad Dupuisf73cb692014-02-26 04:15:06 -05002906 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2907 req_q_size = rsp_q_size = 0;
2908
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002909 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2910 fixed_size = sizeof(struct qla2100_fw_dump);
2911 } else if (IS_QLA23XX(ha)) {
2912 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2913 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2914 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002915 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002916 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002917 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2918 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002919 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2920 else if (IS_QLA25XX(ha))
2921 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2922 else
2923 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002924
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002925 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2926 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002927 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002928 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002929 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002930 /*
2931 * Allocate maximum buffer size for all queues.
2932 * Resizing must be done at end-of-dump processing.
2933 */
2934 mq_size += ha->max_req_queues *
2935 (req->length * sizeof(request_t));
2936 mq_size += ha->max_rsp_queues *
2937 (rsp->length * sizeof(response_t));
2938 }
Arun Easi00876ae2013-03-25 02:21:37 -04002939 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002940 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002941 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002942 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2943 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002944 goto try_eft;
2945
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002946 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002947try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002948 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002949 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002950 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002951 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002952
Chad Dupuisf73cb692014-02-26 04:15:06 -05002953 if (IS_QLA27XX(ha)) {
2954 if (!ha->fw_dump_template) {
2955 ql_log(ql_log_warn, vha, 0x00ba,
2956 "Failed missing fwdump template\n");
2957 return;
2958 }
2959 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2960 ql_dbg(ql_dbg_init, vha, 0x00fa,
2961 "-> allocating fwdump (%x bytes)...\n", dump_size);
2962 goto allocate;
2963 }
2964
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002965 req_q_size = req->length * sizeof(request_t);
2966 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002967 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002968 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002969 ha->chain_offset = dump_size;
2970 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002971
Quinn Tranb945e772017-06-13 20:47:29 -07002972 if (ha->exchoffld_buf)
2973 dump_size += sizeof(struct qla2xxx_offld_chain) +
2974 ha->exchoffld_size;
2975 if (ha->exlogin_buf)
2976 dump_size += sizeof(struct qla2xxx_offld_chain) +
2977 ha->exlogin_size;
2978
Chad Dupuisf73cb692014-02-26 04:15:06 -05002979allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08002980 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
2981 fw_dump = vmalloc(dump_size);
2982 if (!fw_dump) {
2983 ql_log(ql_log_warn, vha, 0x00c4,
2984 "Unable to allocate (%d KB) for firmware dump.\n",
2985 dump_size / 1024);
2986 } else {
2987 if (ha->fw_dump)
2988 vfree(ha->fw_dump);
2989 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002990
Quinn Tranad0a0b02017-12-28 12:33:14 -08002991 ha->fw_dump_len = dump_size;
2992 ql_dbg(ql_dbg_init, vha, 0x00c5,
2993 "Allocated (%d KB) for firmware dump.\n",
2994 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002995
Quinn Tranad0a0b02017-12-28 12:33:14 -08002996 if (IS_QLA27XX(ha))
2997 return;
2998
2999 ha->fw_dump->signature[0] = 'Q';
3000 ha->fw_dump->signature[1] = 'L';
3001 ha->fw_dump->signature[2] = 'G';
3002 ha->fw_dump->signature[3] = 'C';
3003 ha->fw_dump->version = htonl(1);
3004
3005 ha->fw_dump->fixed_size = htonl(fixed_size);
3006 ha->fw_dump->mem_size = htonl(mem_size);
3007 ha->fw_dump->req_q_size = htonl(req_q_size);
3008 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3009
3010 ha->fw_dump->eft_size = htonl(eft_size);
3011 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3012 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3013
3014 ha->fw_dump->header_size =
3015 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003016 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003017 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003018}
3019
Andrew Vasquez18e75552009-06-03 09:55:30 -07003020static int
3021qla81xx_mpi_sync(scsi_qla_host_t *vha)
3022{
3023#define MPS_MASK 0xe0
3024 int rval;
3025 uint16_t dc;
3026 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003027
3028 if (!IS_QLA81XX(vha->hw))
3029 return QLA_SUCCESS;
3030
3031 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3032 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003033 ql_log(ql_log_warn, vha, 0x0105,
3034 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003035 goto done;
3036 }
3037
3038 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3039 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3040 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003041 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003042 goto done_release;
3043 }
3044
3045 dc &= MPS_MASK;
3046 if (dc == (dw & MPS_MASK))
3047 goto done_release;
3048
3049 dw &= ~MPS_MASK;
3050 dw |= dc;
3051 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3052 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003053 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003054 }
3055
3056done_release:
3057 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3058 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003059 ql_log(ql_log_warn, vha, 0x006d,
3060 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003061 }
3062
3063done:
3064 return rval;
3065}
3066
Chad Dupuis8d93f552013-01-30 03:34:37 -05003067int
3068qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3069{
3070 /* Don't try to reallocate the array */
3071 if (req->outstanding_cmds)
3072 return QLA_SUCCESS;
3073
Michael Hernandezd7459522016-12-12 14:40:07 -08003074 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003075 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3076 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003077 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3078 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003079 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003080 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003081 }
3082
3083 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3084 req->num_outstanding_cmds, GFP_KERNEL);
3085
3086 if (!req->outstanding_cmds) {
3087 /*
3088 * Try to allocate a minimal size just so we can get through
3089 * initialization.
3090 */
3091 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3092 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3093 req->num_outstanding_cmds, GFP_KERNEL);
3094
3095 if (!req->outstanding_cmds) {
3096 ql_log(ql_log_fatal, NULL, 0x0126,
3097 "Failed to allocate memory for "
3098 "outstanding_cmds for req_que %p.\n", req);
3099 req->num_outstanding_cmds = 0;
3100 return QLA_FUNCTION_FAILED;
3101 }
3102 }
3103
3104 return QLA_SUCCESS;
3105}
3106
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003107#define PRINT_FIELD(_field, _flag, _str) { \
3108 if (a0->_field & _flag) {\
3109 if (p) {\
3110 strcat(ptr, "|");\
3111 ptr++;\
3112 leftover--;\
3113 } \
3114 len = snprintf(ptr, leftover, "%s", _str); \
3115 p = 1;\
3116 leftover -= len;\
3117 ptr += len; \
3118 } \
3119}
3120
3121static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3122{
3123#define STR_LEN 64
3124 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3125 u8 str[STR_LEN], *ptr, p;
3126 int leftover, len;
3127
3128 memset(str, 0, STR_LEN);
3129 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3130 ql_dbg(ql_dbg_init, vha, 0x015a,
3131 "SFP MFG Name: %s\n", str);
3132
3133 memset(str, 0, STR_LEN);
3134 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3135 ql_dbg(ql_dbg_init, vha, 0x015c,
3136 "SFP Part Name: %s\n", str);
3137
3138 /* media */
3139 memset(str, 0, STR_LEN);
3140 ptr = str;
3141 leftover = STR_LEN;
3142 p = len = 0;
3143 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3144 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3145 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3146 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3147 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3148 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3149 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3150 ql_dbg(ql_dbg_init, vha, 0x0160,
3151 "SFP Media: %s\n", str);
3152
3153 /* link length */
3154 memset(str, 0, STR_LEN);
3155 ptr = str;
3156 leftover = STR_LEN;
3157 p = len = 0;
3158 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3159 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3160 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3161 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3162 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3163 ql_dbg(ql_dbg_init, vha, 0x0196,
3164 "SFP Link Length: %s\n", str);
3165
3166 memset(str, 0, STR_LEN);
3167 ptr = str;
3168 leftover = STR_LEN;
3169 p = len = 0;
3170 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3171 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3172 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3173 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3174 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3175 ql_dbg(ql_dbg_init, vha, 0x016e,
3176 "SFP FC Link Tech: %s\n", str);
3177
3178 if (a0->length_km)
3179 ql_dbg(ql_dbg_init, vha, 0x016f,
3180 "SFP Distant: %d km\n", a0->length_km);
3181 if (a0->length_100m)
3182 ql_dbg(ql_dbg_init, vha, 0x0170,
3183 "SFP Distant: %d m\n", a0->length_100m*100);
3184 if (a0->length_50um_10m)
3185 ql_dbg(ql_dbg_init, vha, 0x0189,
3186 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3187 if (a0->length_62um_10m)
3188 ql_dbg(ql_dbg_init, vha, 0x018a,
3189 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3190 if (a0->length_om4_10m)
3191 ql_dbg(ql_dbg_init, vha, 0x0194,
3192 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3193 if (a0->length_om3_10m)
3194 ql_dbg(ql_dbg_init, vha, 0x0195,
3195 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3196}
3197
3198
3199/*
3200 * Return Code:
3201 * QLA_SUCCESS: no action
3202 * QLA_INTERFACE_ERROR: SFP is not there.
3203 * QLA_FUNCTION_FAILED: detected New SFP
3204 */
3205int
3206qla24xx_detect_sfp(scsi_qla_host_t *vha)
3207{
3208 int rc = QLA_SUCCESS;
3209 struct sff_8247_a0 *a;
3210 struct qla_hw_data *ha = vha->hw;
3211
3212 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3213 goto out;
3214
3215 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3216 if (rc)
3217 goto out;
3218
3219 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3220 qla2xxx_print_sfp_info(vha);
3221
3222 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3223 /* long range */
3224 ha->flags.detected_lr_sfp = 1;
3225
3226 if (a->length_km > 5 || a->length_100m > 50)
3227 ha->long_range_distance = LR_DISTANCE_10K;
3228 else
3229 ha->long_range_distance = LR_DISTANCE_5K;
3230
3231 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3232 ql_dbg(ql_dbg_async, vha, 0x507b,
3233 "Detected Long Range SFP.\n");
3234 } else {
3235 /* short range */
3236 ha->flags.detected_lr_sfp = 0;
3237 if (ha->flags.using_lr_setting)
3238 ql_dbg(ql_dbg_async, vha, 0x5084,
3239 "Detected Short Range SFP.\n");
3240 }
3241
3242 if (!vha->flags.init_done)
3243 rc = QLA_SUCCESS;
3244out:
3245 return rc;
3246}
3247
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003248/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 * qla2x00_setup_chip() - Load and start RISC firmware.
3250 * @ha: HA context
3251 *
3252 * Returns 0 on success.
3253 */
3254static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003255qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003257 int rval;
3258 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003259 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003260 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3261 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003262 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003263
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003264 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003265 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003266 if (rval == QLA_SUCCESS) {
3267 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003268 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003269 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003270 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003271 }
3272
Andrew Vasquez3db06522008-01-31 12:33:49 -08003273 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3274 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3275 spin_lock_irqsave(&ha->hardware_lock, flags);
3276 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3277 RD_REG_WORD(&reg->hccr);
3278 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Andrew Vasquez18e75552009-06-03 09:55:30 -07003281 qla81xx_mpi_sync(vha);
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003284 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003285 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003286 ql_dbg(ql_dbg_init, vha, 0x00c9,
3287 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003289 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (rval == QLA_SUCCESS) {
3291 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003292 ql_dbg(ql_dbg_init, vha, 0x00ca,
3293 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003295 if (ql2xexlogins)
3296 ha->flags.exlogins_enabled = 1;
3297
Quinn Tran99e1b682017-06-02 09:12:03 -07003298 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003299 ha->flags.exchoffld_enabled = 1;
3300
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003301 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003303 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003304 qla24xx_detect_sfp(vha);
3305
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003306 rval = qla2x00_set_exlogins_buffer(vha);
3307 if (rval != QLA_SUCCESS)
3308 goto failed;
3309
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003310 rval = qla2x00_set_exchoffld_buffer(vha);
3311 if (rval != QLA_SUCCESS)
3312 goto failed;
3313
Giridhar Malavalia9083012010-04-12 17:59:55 -07003314enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003315 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003316 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003317 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003318 else
3319 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003320 if (rval != QLA_SUCCESS)
3321 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003322 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003323 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003324 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003325 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003326 if ((!ha->max_npiv_vports) ||
3327 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003328 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003329 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003330 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003331 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003332 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003333
Chad Dupuis8d93f552013-01-30 03:34:37 -05003334 /*
3335 * Allocate the array of outstanding commands
3336 * now that we know the firmware resources.
3337 */
3338 rval = qla2x00_alloc_outstanding_cmds(ha,
3339 vha->req);
3340 if (rval != QLA_SUCCESS)
3341 goto failed;
3342
Quinn Tranad0a0b02017-12-28 12:33:14 -08003343 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3344 qla2x00_alloc_offload_mem(vha);
3345
3346 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003347 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003348
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003349 } else {
3350 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003353 ql_log(ql_log_fatal, vha, 0x00cd,
3354 "ISP Firmware failed checksum.\n");
3355 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003357 } else
3358 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Andrew Vasquez3db06522008-01-31 12:33:49 -08003360 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3361 /* Enable proper parity. */
3362 spin_lock_irqsave(&ha->hardware_lock, flags);
3363 if (IS_QLA2300(ha))
3364 /* SRAM parity */
3365 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3366 else
3367 /* SRAM, Instruction RAM and GP RAM parity */
3368 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3369 RD_REG_WORD(&reg->hccr);
3370 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3371 }
3372
Chad Dupuisf3982d82014-09-25 05:16:57 -04003373 if (IS_QLA27XX(ha))
3374 ha->flags.fac_supported = 1;
3375 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003376 uint32_t size;
3377
3378 rval = qla81xx_fac_get_sector_size(vha, &size);
3379 if (rval == QLA_SUCCESS) {
3380 ha->flags.fac_supported = 1;
3381 ha->fdt_block_size = size << 2;
3382 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003383 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003384 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3385 ha->fw_major_version, ha->fw_minor_version,
3386 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003387
Chad Dupuisf73cb692014-02-26 04:15:06 -05003388 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003389 ha->flags.fac_supported = 0;
3390 rval = QLA_SUCCESS;
3391 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003392 }
3393 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003394failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003396 ql_log(ql_log_fatal, vha, 0x00cf,
3397 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
3399
3400 return (rval);
3401}
3402
3403/**
3404 * qla2x00_init_response_q_entries() - Initializes response queue entries.
3405 * @ha: HA context
3406 *
3407 * Beginning of request ring has initialization control block already built
3408 * by nvram config routine.
3409 *
3410 * Returns 0 on success.
3411 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003412void
3413qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
3415 uint16_t cnt;
3416 response_t *pkt;
3417
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003418 rsp->ring_ptr = rsp->ring;
3419 rsp->ring_index = 0;
3420 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003421 pkt = rsp->ring_ptr;
3422 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 pkt->signature = RESPONSE_PROCESSED;
3424 pkt++;
3425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428/**
3429 * qla2x00_update_fw_options() - Read and process firmware options.
3430 * @ha: HA context
3431 *
3432 * Returns 0 on success.
3433 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003434void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003435qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436{
3437 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003438 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003441 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3444 return;
3445
3446 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003447 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3448 "Serial link options.\n");
3449 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3450 (uint8_t *)&ha->fw_seriallink_options,
3451 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3454 if (ha->fw_seriallink_options[3] & BIT_2) {
3455 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3456
3457 /* 1G settings */
3458 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3459 emphasis = (ha->fw_seriallink_options[2] &
3460 (BIT_4 | BIT_3)) >> 3;
3461 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003462 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 rx_sens = (ha->fw_seriallink_options[0] &
3464 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3465 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3466 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3467 if (rx_sens == 0x0)
3468 rx_sens = 0x3;
3469 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3470 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3471 ha->fw_options[10] |= BIT_5 |
3472 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3473 (tx_sens & (BIT_1 | BIT_0));
3474
3475 /* 2G settings */
3476 swing = (ha->fw_seriallink_options[2] &
3477 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3478 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3479 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003480 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 rx_sens = (ha->fw_seriallink_options[1] &
3482 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3483 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3484 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3485 if (rx_sens == 0x0)
3486 rx_sens = 0x3;
3487 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3488 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3489 ha->fw_options[11] |= BIT_5 |
3490 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3491 (tx_sens & (BIT_1 | BIT_0));
3492 }
3493
3494 /* FCP2 options. */
3495 /* Return command IOCBs without waiting for an ABTS to complete. */
3496 ha->fw_options[3] |= BIT_13;
3497
3498 /* LED scheme. */
3499 if (ha->flags.enable_led_scheme)
3500 ha->fw_options[2] |= BIT_12;
3501
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003502 /* Detect ISP6312. */
3503 if (IS_QLA6312(ha))
3504 ha->fw_options[2] |= BIT_13;
3505
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003506 /* Set Retry FLOGI in case of P2P connection */
3507 if (ha->operating_mode == P2P) {
3508 ha->fw_options[2] |= BIT_3;
3509 ql_dbg(ql_dbg_disc, vha, 0x2100,
3510 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3511 __func__, ha->fw_options[2]);
3512 }
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003515 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003518void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003519qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003520{
3521 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003522 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003523
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003524 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003525 return;
3526
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003527 /* Hold status IOCBs until ABTS response received. */
3528 if (ql2xfwholdabts)
3529 ha->fw_options[3] |= BIT_12;
3530
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003531 /* Set Retry FLOGI in case of P2P connection */
3532 if (ha->operating_mode == P2P) {
3533 ha->fw_options[2] |= BIT_3;
3534 ql_dbg(ql_dbg_disc, vha, 0x2101,
3535 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3536 __func__, ha->fw_options[2]);
3537 }
3538
Quinn Tran41dc5292017-01-19 22:28:03 -08003539 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003540 if (ql2xmvasynctoatio &&
3541 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003542 if (qla_tgt_mode_enabled(vha) ||
3543 qla_dual_mode_enabled(vha))
3544 ha->fw_options[2] |= BIT_11;
3545 else
3546 ha->fw_options[2] &= ~BIT_11;
3547 }
3548
Quinn Tranf7e761f2017-06-02 09:12:02 -07003549 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3550 /*
3551 * Tell FW to track each exchange to prevent
3552 * driver from using stale exchange.
3553 */
3554 if (qla_tgt_mode_enabled(vha) ||
3555 qla_dual_mode_enabled(vha))
3556 ha->fw_options[2] |= BIT_4;
3557 else
3558 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003559
3560 /* Reserve 1/2 of emergency exchanges for ELS.*/
3561 if (qla2xuseresexchforels)
3562 ha->fw_options[2] |= BIT_8;
3563 else
3564 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003565 }
3566
Quinn Tran83548fe2017-06-02 09:12:01 -07003567 ql_dbg(ql_dbg_init, vha, 0x00e8,
3568 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3569 __func__, ha->fw_options[1], ha->fw_options[2],
3570 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003571
3572 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3573 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003574
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003575 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003576 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003577 return;
3578
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003579 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003580 le16_to_cpu(ha->fw_seriallink_options24[1]),
3581 le16_to_cpu(ha->fw_seriallink_options24[2]),
3582 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003583 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003584 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003585 "Unable to update Serial Link options (%x).\n", rval);
3586 }
3587}
3588
3589void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003590qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003591{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003592 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003593 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003594 struct req_que *req = ha->req_q_map[0];
3595 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003596
3597 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003598 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3599 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003600 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3601 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3602 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3603 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3604 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3605 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003606
3607 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3608 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3609 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3610 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3611 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3612}
3613
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003614void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003615qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003616{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003617 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003618 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003619 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3620 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003621 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003622 uint16_t rid = 0;
3623 struct req_que *req = ha->req_q_map[0];
3624 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003625
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003626 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003627 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003628 icb->request_q_outpointer = cpu_to_le16(0);
3629 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003630 icb->request_q_length = cpu_to_le16(req->length);
3631 icb->response_q_length = cpu_to_le16(rsp->length);
3632 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3633 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3634 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3635 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003636
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003637 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003638 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003639 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3640 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3641 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3642
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003643 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003644 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003645
Chad Dupuisf73cb692014-02-26 04:15:06 -05003646 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003647 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3648 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003649 if (ha->flags.msix_enabled) {
3650 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003651 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003652 "Registering vector 0x%x for base que.\n",
3653 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003654 icb->msix = cpu_to_le16(msix->entry);
3655 }
3656 /* Use alternate PCI bus number */
3657 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003658 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003659 /* Use alternate PCI devfn */
3660 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003661 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003662
Anirban Chakraborty31557542009-12-02 10:36:55 -08003663 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003664 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3665 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003666 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003667 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003668 ql_dbg(ql_dbg_init, vha, 0x00fe,
3669 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003670 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003671 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003672 }
Bart Van Asschead950362015-07-09 07:24:08 -07003673 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003674
3675 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3676 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3677 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3678 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3679 } else {
3680 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3681 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3682 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3683 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3684 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003685 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003686
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003687 /* PCI posting */
3688 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003689}
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691/**
3692 * qla2x00_init_rings() - Initializes firmware.
3693 * @ha: HA context
3694 *
3695 * Beginning of request ring has initialization control block already built
3696 * by nvram config routine.
3697 *
3698 * Returns 0 on success.
3699 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003700int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003701qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
3703 int rval;
3704 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003705 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003706 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003707 struct req_que *req;
3708 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003709 struct mid_init_cb_24xx *mid_init_cb =
3710 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712 spin_lock_irqsave(&ha->hardware_lock, flags);
3713
3714 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003715 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003716 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003717 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003718 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003719 req->out_ptr = (void *)(req->ring + req->length);
3720 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003721 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003722 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003724 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003725
3726 /* Initialize firmware. */
3727 req->ring_ptr = req->ring;
3728 req->ring_index = 0;
3729 req->cnt = req->length;
3730 }
3731
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003732 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003733 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003734 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003735 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003736 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3737 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003738 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003739 if (IS_QLAFX00(ha))
3740 qlafx00_init_response_q_entries(rsp);
3741 else
3742 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003745 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3746 ha->tgt.atio_ring_index = 0;
3747 /* Initialize ATIO queue entries */
3748 qlt_init_atio_q_entries(vha);
3749
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003750 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3753
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003754 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3755
3756 if (IS_QLAFX00(ha)) {
3757 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3758 goto next_check;
3759 }
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003762 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003764 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003765 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003766 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003767 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003768 }
3769
Andrew Vasquez24a08132009-03-24 09:08:16 -07003770 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003771 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003772 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003773 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003774 ha->flags.dport_enabled =
3775 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3776 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3777 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3778 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003779 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003780 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003781 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003782 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003783 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003784
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003785 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003786next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003788 ql_log(ql_log_fatal, vha, 0x00d2,
3789 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003791 ql_dbg(ql_dbg_init, vha, 0x00d3,
3792 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003793 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 }
3795
3796 return (rval);
3797}
3798
3799/**
3800 * qla2x00_fw_ready() - Waits for firmware ready.
3801 * @ha: HA context
3802 *
3803 * Returns 0 on success.
3804 */
3805static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003806qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003809 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 uint16_t min_wait; /* Minimum wait time if loop is down */
3811 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003812 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003813 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003815 if (IS_QLAFX00(vha->hw))
3816 return qlafx00_fw_ready(vha);
3817
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 rval = QLA_SUCCESS;
3819
Chad Dupuis334614912015-04-09 14:59:57 -04003820 /* Time to wait for loop down */
3821 if (IS_P3P_TYPE(ha))
3822 min_wait = 30;
3823 else
3824 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 /*
3827 * Firmware should take at most one RATOV to login, plus 5 seconds for
3828 * our own processing.
3829 */
3830 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3831 wait_time = min_wait;
3832 }
3833
3834 /* Min wait time if loop down */
3835 mtime = jiffies + (min_wait * HZ);
3836
3837 /* wait time before firmware ready */
3838 wtime = jiffies + (wait_time * HZ);
3839
3840 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003841 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003842 ql_log(ql_log_info, vha, 0x801e,
3843 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
3845 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003846 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003847 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003849 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003850 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003852 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003853 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3854 "fw_state=%x 84xx=%x.\n", state[0],
3855 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003856 if ((state[2] & FSTATE_LOGGED_IN) &&
3857 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003858 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3859 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003860
3861 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003862 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003863 if (rval != QLA_SUCCESS) {
3864 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003865 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003866 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003867 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003868 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003869
3870 /* Add time taken to initialize. */
3871 cs84xx_time = jiffies - cs84xx_time;
3872 wtime += cs84xx_time;
3873 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003874 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003875 "Increasing wait time by %ld. "
3876 "New time %ld.\n", cs84xx_time,
3877 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003878 }
3879 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003880 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3881 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003883 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 &ha->login_timeout, &ha->r_a_tov);
3885
3886 rval = QLA_SUCCESS;
3887 break;
3888 }
3889
3890 rval = QLA_FUNCTION_FAILED;
3891
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003892 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003893 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003895 * other than Wait for Login.
3896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003898 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 "Cable is unplugged...\n");
3900
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003901 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 break;
3903 }
3904 }
3905 } else {
3906 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003907 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003908 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 break;
3910 }
3911
3912 if (time_after_eq(jiffies, wtime))
3913 break;
3914
3915 /* Delay for a while */
3916 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 } while (1);
3918
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003919 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003920 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3921 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Chad Dupuiscfb09192011-11-18 09:03:07 -08003923 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003924 ql_log(ql_log_warn, vha, 0x803b,
3925 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
3927
3928 return (rval);
3929}
3930
3931/*
3932* qla2x00_configure_hba
3933* Setup adapter context.
3934*
3935* Input:
3936* ha = adapter state pointer.
3937*
3938* Returns:
3939* 0 = success
3940*
3941* Context:
3942* Kernel context.
3943*/
3944static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003945qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
3947 int rval;
3948 uint16_t loop_id;
3949 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003950 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 uint8_t al_pa;
3952 uint8_t area;
3953 uint8_t domain;
3954 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003955 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003956 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003957 port_id_t id;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08003958 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003961 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003962 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003964 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003965 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003966 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003967 ql_dbg(ql_dbg_disc, vha, 0x2008,
3968 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003969 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003970 ql_log(ql_log_warn, vha, 0x2009,
3971 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003972 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3973 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3974 ql_log(ql_log_warn, vha, 0x1151,
3975 "Doing link init.\n");
3976 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3977 return rval;
3978 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003979 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 return (rval);
3982 }
3983
3984 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003985 ql_log(ql_log_info, vha, 0x200a,
3986 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 return (QLA_FUNCTION_FAILED);
3988 }
3989
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003990 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 /* initialize */
3993 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3994 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003995 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 switch (topo) {
3998 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003999 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 ha->current_topology = ISP_CFG_NL;
4001 strcpy(connect_type, "(Loop)");
4002 break;
4003
4004 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004005 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004006 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 ha->current_topology = ISP_CFG_FL;
4008 strcpy(connect_type, "(FL_Port)");
4009 break;
4010
4011 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004012 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 ha->operating_mode = P2P;
4014 ha->current_topology = ISP_CFG_N;
4015 strcpy(connect_type, "(N_Port-to-N_Port)");
4016 break;
4017
4018 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004019 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004020 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 ha->operating_mode = P2P;
4022 ha->current_topology = ISP_CFG_F;
4023 strcpy(connect_type, "(F_Port)");
4024 break;
4025
4026 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004027 ql_dbg(ql_dbg_disc, vha, 0x200f,
4028 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 ha->current_topology = ISP_CFG_NL;
4030 strcpy(connect_type, "(Loop)");
4031 break;
4032 }
4033
4034 /* Save Host port and loop ID. */
4035 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004036 id.b.domain = domain;
4037 id.b.area = area;
4038 id.b.al_pa = al_pa;
4039 id.b.rsvd_1 = 0;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004040 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran482c9dc2017-03-15 09:48:54 -07004041 qlt_update_host_map(vha, id);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004042 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004043
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004044 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004045 ql_log(ql_log_info, vha, 0x2010,
4046 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004047 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 return(rval);
4050}
4051
Giridhar Malavalia9083012010-04-12 17:59:55 -07004052inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004053qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4054 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004055{
4056 char *st, *en;
4057 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004058 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004059 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004060 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004061
4062 if (memcmp(model, BINZERO, len) != 0) {
4063 strncpy(ha->model_number, model, len);
4064 st = en = ha->model_number;
4065 en += len - 1;
4066 while (en > st) {
4067 if (*en != 0x20 && *en != 0x00)
4068 break;
4069 *en-- = '\0';
4070 }
4071
4072 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004073 if (use_tbl &&
4074 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004075 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004076 strncpy(ha->model_desc,
4077 qla2x00_model_name[index * 2 + 1],
4078 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004079 } else {
4080 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004081 if (use_tbl &&
4082 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004083 index < QLA_MODEL_NAMES) {
4084 strcpy(ha->model_number,
4085 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004086 strncpy(ha->model_desc,
4087 qla2x00_model_name[index * 2 + 1],
4088 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004089 } else {
4090 strcpy(ha->model_number, def);
4091 }
4092 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004093 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004094 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004095 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004096}
4097
David Miller4e08df32007-04-16 12:37:43 -07004098/* On sparc systems, obtain port and node WWN from firmware
4099 * properties.
4100 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004101static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004102{
4103#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004104 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004105 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004106 struct device_node *dp = pci_device_to_OF_node(pdev);
4107 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004108 int len;
4109
4110 val = of_get_property(dp, "port-wwn", &len);
4111 if (val && len >= WWN_SIZE)
4112 memcpy(nv->port_name, val, WWN_SIZE);
4113
4114 val = of_get_property(dp, "node-wwn", &len);
4115 if (val && len >= WWN_SIZE)
4116 memcpy(nv->node_name, val, WWN_SIZE);
4117#endif
4118}
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120/*
4121* NVRAM configuration for ISP 2xxx
4122*
4123* Input:
4124* ha = adapter block pointer.
4125*
4126* Output:
4127* initialization control block in response_ring
4128* host adapters parameters in host adapter block
4129*
4130* Returns:
4131* 0 = success.
4132*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004133int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004134qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135{
David Miller4e08df32007-04-16 12:37:43 -07004136 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004137 uint8_t chksum = 0;
4138 uint16_t cnt;
4139 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004140 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004141 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004142 nvram_t *nv = ha->nvram;
4143 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004144 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
David Miller4e08df32007-04-16 12:37:43 -07004146 rval = QLA_SUCCESS;
4147
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004149 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 ha->nvram_base = 0;
4151 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4152 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4153 ha->nvram_base = 0x80;
4154
4155 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004156 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004157 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4158 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004160 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4161 "Contents of NVRAM.\n");
4162 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4163 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165 /* Bad NVRAM data, set defaults parameters. */
4166 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4167 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4168 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004169 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004170 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004171 "detected: checksum=0x%x id=%c version=0x%x.\n",
4172 chksum, nv->id[0], nv->nvram_version);
4173 ql_log(ql_log_warn, vha, 0x0065,
4174 "Falling back to "
4175 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004176
4177 /*
4178 * Set default initialization control block.
4179 */
4180 memset(nv, 0, ha->nvram_size);
4181 nv->parameter_block_version = ICB_VERSION;
4182
4183 if (IS_QLA23XX(ha)) {
4184 nv->firmware_options[0] = BIT_2 | BIT_1;
4185 nv->firmware_options[1] = BIT_7 | BIT_5;
4186 nv->add_firmware_options[0] = BIT_5;
4187 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004188 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004189 nv->special_options[1] = BIT_7;
4190 } else if (IS_QLA2200(ha)) {
4191 nv->firmware_options[0] = BIT_2 | BIT_1;
4192 nv->firmware_options[1] = BIT_7 | BIT_5;
4193 nv->add_firmware_options[0] = BIT_5;
4194 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004195 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004196 } else if (IS_QLA2100(ha)) {
4197 nv->firmware_options[0] = BIT_3 | BIT_1;
4198 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004199 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004200 }
4201
Bart Van Asschead950362015-07-09 07:24:08 -07004202 nv->max_iocb_allocation = cpu_to_le16(256);
4203 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004204 nv->retry_count = 8;
4205 nv->retry_delay = 1;
4206
4207 nv->port_name[0] = 33;
4208 nv->port_name[3] = 224;
4209 nv->port_name[4] = 139;
4210
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004211 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004212
4213 nv->login_timeout = 4;
4214
4215 /*
4216 * Set default host adapter parameters
4217 */
4218 nv->host_p[1] = BIT_2;
4219 nv->reset_delay = 5;
4220 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004221 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004222 nv->link_down_timeout = 60;
4223
4224 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
4226
4227#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4228 /*
4229 * The SN2 does not provide BIOS emulation which means you can't change
4230 * potentially bogus BIOS settings. Force the use of default settings
4231 * for link rate and frame size. Hope that the rest of the settings
4232 * are valid.
4233 */
4234 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004235 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 if (IS_QLA23XX(ha))
4237 nv->special_options[1] = BIT_7;
4238 }
4239#endif
4240
4241 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004242 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 /*
4245 * Setup driver NVRAM options.
4246 */
4247 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4248 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4249 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4250 nv->firmware_options[1] &= ~BIT_4;
4251
4252 if (IS_QLA23XX(ha)) {
4253 nv->firmware_options[0] |= BIT_2;
4254 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004255 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004256 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 if (IS_QLA2300(ha)) {
4259 if (ha->fb_rev == FPM_2310) {
4260 strcpy(ha->model_number, "QLA2310");
4261 } else {
4262 strcpy(ha->model_number, "QLA2300");
4263 }
4264 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004265 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004266 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 }
4268 } else if (IS_QLA2200(ha)) {
4269 nv->firmware_options[0] |= BIT_2;
4270 /*
4271 * 'Point-to-point preferred, else loop' is not a safe
4272 * connection mode setting.
4273 */
4274 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4275 (BIT_5 | BIT_4)) {
4276 /* Force 'loop preferred, else point-to-point'. */
4277 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4278 nv->add_firmware_options[0] |= BIT_5;
4279 }
4280 strcpy(ha->model_number, "QLA22xx");
4281 } else /*if (IS_QLA2100(ha))*/ {
4282 strcpy(ha->model_number, "QLA2100");
4283 }
4284
4285 /*
4286 * Copy over NVRAM RISC parameter block to initialization control block.
4287 */
4288 dptr1 = (uint8_t *)icb;
4289 dptr2 = (uint8_t *)&nv->parameter_block_version;
4290 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4291 while (cnt--)
4292 *dptr1++ = *dptr2++;
4293
4294 /* Copy 2nd half. */
4295 dptr1 = (uint8_t *)icb->add_firmware_options;
4296 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4297 while (cnt--)
4298 *dptr1++ = *dptr2++;
4299
Andrew Vasquez5341e862006-05-17 15:09:16 -07004300 /* Use alternate WWN? */
4301 if (nv->host_p[1] & BIT_7) {
4302 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4303 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4304 }
4305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 /* Prepare nodename */
4307 if ((icb->firmware_options[1] & BIT_6) == 0) {
4308 /*
4309 * Firmware will apply the following mask if the nodename was
4310 * not provided.
4311 */
4312 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4313 icb->node_name[0] &= 0xF0;
4314 }
4315
4316 /*
4317 * Set host adapter parameters.
4318 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004319
4320 /*
4321 * BIT_7 in the host-parameters section allows for modification to
4322 * internal driver logging.
4323 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004324 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004325 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4327 /* Always load RISC code on non ISP2[12]00 chips. */
4328 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4329 ha->flags.disable_risc_code_load = 0;
4330 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4331 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4332 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004333 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004334 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 ha->operating_mode =
4337 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4338
4339 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4340 sizeof(ha->fw_seriallink_options));
4341
4342 /* save HBA serial number */
4343 ha->serial0 = icb->port_name[5];
4344 ha->serial1 = icb->port_name[6];
4345 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004346 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4347 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Bart Van Asschead950362015-07-09 07:24:08 -07004349 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 ha->retry_count = nv->retry_count;
4352
4353 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004354 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 nv->login_timeout = ql2xlogintimeout;
4356 if (nv->login_timeout < 4)
4357 nv->login_timeout = 4;
4358 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004360 /* Set minimum RATOV to 100 tenths of a second. */
4361 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 ha->loop_reset_delay = nv->reset_delay;
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 /* Link Down Timeout = 0:
4366 *
4367 * When Port Down timer expires we will start returning
4368 * I/O's to OS with "DID_NO_CONNECT".
4369 *
4370 * Link Down Timeout != 0:
4371 *
4372 * The driver waits for the link to come up after link down
4373 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004374 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (nv->link_down_timeout == 0) {
4376 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004377 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 } else {
4379 ha->link_down_timeout = nv->link_down_timeout;
4380 ha->loop_down_abort_time =
4381 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 /*
4385 * Need enough time to try and get the port back.
4386 */
4387 ha->port_down_retry_count = nv->port_down_retry_count;
4388 if (qlport_down_retry)
4389 ha->port_down_retry_count = qlport_down_retry;
4390 /* Set login_retry_count */
4391 ha->login_retry_count = nv->retry_count;
4392 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4393 ha->port_down_retry_count > 3)
4394 ha->login_retry_count = ha->port_down_retry_count;
4395 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4396 ha->login_retry_count = ha->port_down_retry_count;
4397 if (ql2xloginretrycount)
4398 ha->login_retry_count = ql2xloginretrycount;
4399
Bart Van Asschead950362015-07-09 07:24:08 -07004400 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 icb->command_resource_count = 0;
4402 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004403 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4406 /* Enable RIO */
4407 icb->firmware_options[0] &= ~BIT_3;
4408 icb->add_firmware_options[0] &=
4409 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4410 icb->add_firmware_options[0] |= BIT_2;
4411 icb->response_accumulation_timer = 3;
4412 icb->interrupt_delay_timer = 5;
4413
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004414 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004416 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004417 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004418 ha->zio_mode = icb->add_firmware_options[0] &
4419 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4420 ha->zio_timer = icb->interrupt_delay_timer ?
4421 icb->interrupt_delay_timer: 2;
4422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 icb->add_firmware_options[0] &=
4424 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004425 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004426 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004427 ha->zio_mode = QLA_ZIO_MODE_6;
4428
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004429 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004430 "ZIO mode %d enabled; timer delay (%d us).\n",
4431 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004433 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4434 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004435 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
4437 }
4438
David Miller4e08df32007-04-16 12:37:43 -07004439 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004440 ql_log(ql_log_warn, vha, 0x0069,
4441 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004442 }
4443 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004446static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004447qla2x00_rport_del(void *data)
4448{
4449 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004450 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004451 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004452
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004453 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004454 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004455 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004456 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004457 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004458 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4459 "%s %8phN. rport %p roles %x\n",
4460 __func__, fcport->port_name, rport,
4461 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004462
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004463 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004464 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004465}
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467/**
4468 * qla2x00_alloc_fcport() - Allocate a generic fcport.
4469 * @ha: HA context
4470 * @flags: allocation flags
4471 *
4472 * Returns a pointer to the allocated fcport, or NULL, if none available.
4473 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004474fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004475qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
4477 fc_port_t *fcport;
4478
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004479 fcport = kzalloc(sizeof(fc_port_t), flags);
4480 if (!fcport)
4481 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004484 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 fcport->port_type = FCT_UNKNOWN;
4486 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004487 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004488 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Quinn Tran726b8542017-01-19 22:28:00 -08004490 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4491 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004492 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004493 fcport->disc_state = DSC_DELETED;
4494 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4495 fcport->deleted = QLA_SESS_DELETED;
4496 fcport->login_retry = vha->hw->login_retry_count;
4497 fcport->login_retry = 5;
4498 fcport->logout_on_delete = 1;
4499
4500 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004501 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004502 "Failed to allocate ct_sns request.\n");
4503 kfree(fcport);
4504 fcport = NULL;
4505 }
4506 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4507 INIT_LIST_HEAD(&fcport->gnl_entry);
4508 INIT_LIST_HEAD(&fcport->list);
4509
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004510 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511}
4512
Quinn Tran726b8542017-01-19 22:28:00 -08004513void
4514qla2x00_free_fcport(fc_port_t *fcport)
4515{
4516 if (fcport->ct_desc.ct_sns) {
4517 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4518 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4519 fcport->ct_desc.ct_sns_dma);
4520
4521 fcport->ct_desc.ct_sns = NULL;
4522 }
4523 kfree(fcport);
4524}
4525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526/*
4527 * qla2x00_configure_loop
4528 * Updates Fibre Channel Device Database with what is actually on loop.
4529 *
4530 * Input:
4531 * ha = adapter block pointer.
4532 *
4533 * Returns:
4534 * 0 = success.
4535 * 1 = error.
4536 * 2 = database was full and device was not configured.
4537 */
4538static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004539qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
4541 int rval;
4542 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004543 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 rval = QLA_SUCCESS;
4545
4546 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004547 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4548 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004550 ql_dbg(ql_dbg_disc, vha, 0x2013,
4551 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 return (rval);
4553 }
4554 }
4555
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004556 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004557 ql_dbg(ql_dbg_disc, vha, 0x2014,
4558 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 /*
4561 * If we have both an RSCN and PORT UPDATE pending then handle them
4562 * both at the same time.
4563 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004564 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4565 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Michael Hernandez3064ff32009-12-15 21:29:44 -08004567 qla2x00_get_data_rate(vha);
4568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 /* Determine what we need to do */
4570 if (ha->current_topology == ISP_CFG_FL &&
4571 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 set_bit(RSCN_UPDATE, &flags);
4574
4575 } else if (ha->current_topology == ISP_CFG_F &&
4576 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 set_bit(RSCN_UPDATE, &flags);
4579 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4580
Andrew Vasquez21333b42006-05-17 15:09:56 -07004581 } else if (ha->current_topology == ISP_CFG_N) {
4582 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004583 if (ha->flags.rida_fmt2) {
4584 /* With Rida Format 2, the login is already triggered.
4585 * We know who is on the other side of the wire.
4586 * No need to login to do login to find out or drop into
4587 * qla2x00_configure_local_loop().
4588 */
4589 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4590 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4591 } else {
4592 if (qla_tgt_mode_enabled(vha)) {
4593 /* allow the other side to start the login */
4594 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4595 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4596 }
4597 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004598 } else if (ha->current_topology == ISP_CFG_NL) {
4599 clear_bit(RSCN_UPDATE, &flags);
4600 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004601 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 set_bit(RSCN_UPDATE, &flags);
4604 set_bit(LOCAL_LOOP_UPDATE, &flags);
4605 }
4606
4607 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004608 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4609 ql_dbg(ql_dbg_disc, vha, 0x2015,
4610 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004612 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004613 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
4615
4616 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004617 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004618 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004619 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004621 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004622 else
4623 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625
4626 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004627 if (atomic_read(&vha->loop_down_timer) ||
4628 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 rval = QLA_FUNCTION_FAILED;
4630 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004631 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004632 ql_dbg(ql_dbg_disc, vha, 0x2069,
4633 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004634 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004635
4636 /*
4637 * Process any ATIO queue entries that came in
4638 * while we weren't online.
4639 */
Quinn Tranead03852017-01-19 22:28:01 -08004640 if (qla_tgt_mode_enabled(vha) ||
4641 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004642 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4643 spin_lock_irqsave(&ha->tgt.atio_lock,
4644 flags);
4645 qlt_24xx_process_atio_queue(vha, 0);
4646 spin_unlock_irqrestore(
4647 &ha->tgt.atio_lock, flags);
4648 } else {
4649 spin_lock_irqsave(&ha->hardware_lock,
4650 flags);
4651 qlt_24xx_process_atio_queue(vha, 1);
4652 spin_unlock_irqrestore(
4653 &ha->hardware_lock, flags);
4654 }
4655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 }
4657 }
4658
4659 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004660 ql_dbg(ql_dbg_disc, vha, 0x206a,
4661 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004663 ql_dbg(ql_dbg_disc, vha, 0x206b,
4664 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004667 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004668 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004670 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004671 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004672 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675
4676 return (rval);
4677}
4678
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004679/*
4680 * N2N Login
4681 * Updates Fibre Channel Device Database with local loop devices.
4682 *
4683 * Input:
4684 * ha = adapter block pointer.
4685 *
4686 * Returns:
4687 */
4688static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4689 fc_port_t *fcport)
4690{
4691 struct qla_hw_data *ha = vha->hw;
4692 int res = QLA_SUCCESS, rval;
4693 int greater_wwpn = 0;
4694 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004696 if (ha->current_topology != ISP_CFG_N)
4697 return res;
4698
4699 if (wwn_to_u64(vha->port_name) >
4700 wwn_to_u64(vha->n2n_port_name)) {
4701 ql_dbg(ql_dbg_disc, vha, 0x2002,
4702 "HBA WWPN is greater %llx > target %llx\n",
4703 wwn_to_u64(vha->port_name),
4704 wwn_to_u64(vha->n2n_port_name));
4705 greater_wwpn = 1;
4706 fcport->d_id.b24 = vha->n2n_id;
4707 }
4708
4709 fcport->loop_id = vha->loop_id;
4710 fcport->fc4f_nvme = 0;
4711 fcport->query = 1;
4712
4713 ql_dbg(ql_dbg_disc, vha, 0x4001,
4714 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4715 fcport->d_id.b24, vha->loop_id);
4716
4717 /* Fill in member data. */
4718 if (!greater_wwpn) {
4719 rval = qla2x00_get_port_database(vha, fcport, 0);
4720 ql_dbg(ql_dbg_disc, vha, 0x1051,
4721 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4722 fcport->current_login_state, fcport->last_login_state,
4723 fcport->d_id.b24, fcport->loop_id, rval);
4724
4725 if (((fcport->current_login_state & 0xf) == 0x4) ||
4726 ((fcport->current_login_state & 0xf) == 0x6))
4727 logged_in = 1;
4728 }
4729
4730 if (logged_in || greater_wwpn) {
4731 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4732 qla_nvme_register_hba(vha);
4733
4734 /* Set connected N_Port d_id */
4735 if (vha->flags.nvme_enabled)
4736 fcport->fc4f_nvme = 1;
4737
4738 fcport->scan_state = QLA_FCPORT_FOUND;
4739 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4740 fcport->disc_state = DSC_GNL;
4741 fcport->n2n_flag = 1;
4742 fcport->flags = 3;
4743 vha->hw->flags.gpsc_supported = 0;
4744
4745 if (greater_wwpn) {
4746 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4747 "%s %d PLOGI ELS %8phC\n",
4748 __func__, __LINE__, fcport->port_name);
4749
4750 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4751 fcport, fcport->d_id);
4752 }
4753
4754 if (res != QLA_SUCCESS) {
4755 ql_log(ql_log_info, vha, 0xd04d,
4756 "PLOGI Failed: portid=%06x - retrying\n",
4757 fcport->d_id.b24);
4758 res = QLA_SUCCESS;
4759 } else {
4760 /* State 0x6 means FCP PRLI complete */
4761 if ((fcport->current_login_state & 0xf) == 0x6) {
4762 ql_dbg(ql_dbg_disc, vha, 0x2118,
4763 "%s %d %8phC post GPDB work\n",
4764 __func__, __LINE__, fcport->port_name);
4765 fcport->chip_reset =
4766 vha->hw->base_qpair->chip_reset;
4767 qla24xx_post_gpdb_work(vha, fcport, 0);
4768 } else {
4769 ql_dbg(ql_dbg_disc, vha, 0x2118,
4770 "%s %d %8phC post NVMe PRLI\n",
4771 __func__, __LINE__, fcport->port_name);
4772 qla24xx_post_prli_work(vha, fcport);
4773 }
4774 }
4775 } else {
4776 /* Wait for next database change */
4777 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4778 }
4779
4780 return res;
4781}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
4783/*
4784 * qla2x00_configure_local_loop
4785 * Updates Fibre Channel Device Database with local loop devices.
4786 *
4787 * Input:
4788 * ha = adapter block pointer.
4789 *
4790 * Returns:
4791 * 0 = success.
4792 */
4793static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004794qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
4796 int rval, rval2;
4797 int found_devs;
4798 int found;
4799 fc_port_t *fcport, *new_fcport;
4800
4801 uint16_t index;
4802 uint16_t entries;
4803 char *id_iter;
4804 uint16_t loop_id;
4805 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004806 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004807 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
4809 found_devs = 0;
4810 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004811 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004814 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004815 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 &entries);
4817 if (rval != QLA_SUCCESS)
4818 goto cleanup_allocation;
4819
Quinn Tran83548fe2017-06-02 09:12:01 -07004820 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004821 "Entries in ID list (%d).\n", entries);
4822 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4823 (uint8_t *)ha->gid_list,
4824 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Quinn Tran9cd883f2017-12-28 12:33:24 -08004826 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4827 fcport->scan_state = QLA_FCPORT_SCAN;
4828 }
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004831 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004833 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004834 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 rval = QLA_MEMORY_ALLOC_FAILED;
4836 goto cleanup_allocation;
4837 }
4838 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4839
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004840 /* Inititae N2N login. */
4841 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4842 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4843 if (rval != QLA_SUCCESS)
4844 goto cleanup_allocation;
4845 return QLA_SUCCESS;
4846 }
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 /* Add devices to port list. */
4849 id_iter = (char *)ha->gid_list;
4850 for (index = 0; index < entries; index++) {
4851 domain = ((struct gid_list_info *)id_iter)->domain;
4852 area = ((struct gid_list_info *)id_iter)->area;
4853 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004854 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 loop_id = (uint16_t)
4856 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004857 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 loop_id = le16_to_cpu(
4859 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004860 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
4862 /* Bypass reserved domain fields. */
4863 if ((domain & 0xf0) == 0xf0)
4864 continue;
4865
4866 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004867 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004868 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 continue;
4870
4871 /* Bypass invalid local loop ID. */
4872 if (loop_id > LAST_LOCAL_LOOP_ID)
4873 continue;
4874
Quinn Tran41dc5292017-01-19 22:28:03 -08004875 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 /* Fill in member data. */
4878 new_fcport->d_id.b.domain = domain;
4879 new_fcport->d_id.b.area = area;
4880 new_fcport->d_id.b.al_pa = al_pa;
4881 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004882 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004883
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004884 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004886 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004887 "Failed to retrieve fcport information "
4888 "-- get_port_database=%x, loop_id=0x%04x.\n",
4889 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004890 /* Skip retry if N2N */
4891 if (ha->current_topology != ISP_CFG_N) {
4892 ql_dbg(ql_dbg_disc, vha, 0x2105,
4893 "Scheduling resync.\n");
4894 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4895 continue;
4896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 }
4898
Quinn Tran41dc5292017-01-19 22:28:03 -08004899 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 /* Check for matching device in port list. */
4901 found = 0;
4902 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004903 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (memcmp(new_fcport->port_name, fcport->port_name,
4905 WWN_SIZE))
4906 continue;
4907
Shyam Sundarddb9b122009-03-24 09:08:10 -07004908 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 fcport->loop_id = new_fcport->loop_id;
4910 fcport->port_type = new_fcport->port_type;
4911 fcport->d_id.b24 = new_fcport->d_id.b24;
4912 memcpy(fcport->node_name, new_fcport->node_name,
4913 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004914 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 found++;
4916 break;
4917 }
4918
4919 if (!found) {
4920 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004921 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 /* Allocate a new replacement fcport. */
4924 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004925
4926 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4927
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004928 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004931 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004932 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 rval = QLA_MEMORY_ALLOC_FAILED;
4934 goto cleanup_allocation;
4935 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004936 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4938 }
4939
Quinn Tran41dc5292017-01-19 22:28:03 -08004940 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4941
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004942 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004943 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 found_devs++;
4946 }
4947
Quinn Tran9cd883f2017-12-28 12:33:24 -08004948 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4949 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4950 break;
4951
4952 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4953 if ((qla_dual_mode_enabled(vha) ||
4954 qla_ini_mode_enabled(vha)) &&
4955 atomic_read(&fcport->state) == FCS_ONLINE) {
4956 qla2x00_mark_device_lost(vha, fcport,
4957 ql2xplogiabsentdevice, 0);
4958 if (fcport->loop_id != FC_NO_LOOP_ID &&
4959 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4960 fcport->port_type != FCT_INITIATOR &&
4961 fcport->port_type != FCT_BROADCAST) {
4962 ql_dbg(ql_dbg_disc, vha, 0x20f0,
4963 "%s %d %8phC post del sess\n",
4964 __func__, __LINE__,
4965 fcport->port_name);
4966
4967 qlt_schedule_sess_for_deletion_lock
4968 (fcport);
4969 continue;
4970 }
4971 }
4972 }
4973
4974 if (fcport->scan_state == QLA_FCPORT_FOUND)
4975 qla24xx_fcport_handle_login(vha, fcport);
4976 }
4977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004979 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004982 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004983 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 }
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 return (rval);
4987}
4988
4989static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004990qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004991{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004992 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004993 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004994 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004995
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004996 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004997 return;
4998
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07004999 if (atomic_read(&fcport->state) != FCS_ONLINE)
5000 return;
5001
Andrew Vasquez39bd9622007-09-20 14:07:34 -07005002 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5003 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005004 return;
5005
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005006 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07005007 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005008 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005009 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005010 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5011 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005012 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005013 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005014 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05005015 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005016 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005017 }
5018}
5019
Quinn Tran726b8542017-01-19 22:28:00 -08005020/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005021static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005022qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005023{
5024 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005025 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005026 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005027
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005028 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5029 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005030 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5031 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5032 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005033 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005034 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005035 ql_log(ql_log_warn, vha, 0x2006,
5036 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005037 return;
5038 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005039
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005040 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005041 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005042 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005043
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005044 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005045
5046 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005047 if (fcport->port_type == FCT_INITIATOR)
5048 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5049 if (fcport->port_type == FCT_TARGET)
5050 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005051
Quinn Tran83548fe2017-06-02 09:12:01 -07005052 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5053 "%s %8phN. rport %p is %s mode\n",
5054 __func__, fcport->port_name, rport,
5055 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005056
Andrew Vasquez77d74142005-07-08 18:00:36 -07005057 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
5060/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005061 * qla2x00_update_fcport
5062 * Updates device on list.
5063 *
5064 * Input:
5065 * ha = adapter block pointer.
5066 * fcport = port structure pointer.
5067 *
5068 * Return:
5069 * 0 - Success
5070 * BIT_0 - error
5071 *
5072 * Context:
5073 * Kernel context.
5074 */
5075void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005076qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005077{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005078 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005079
Quinn Tran726b8542017-01-19 22:28:00 -08005080 if (IS_SW_RESV_ADDR(fcport->d_id))
5081 return;
5082
Quinn Tran83548fe2017-06-02 09:12:01 -07005083 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005084 __func__, fcport->port_name);
5085
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005086 if (IS_QLAFX00(vha->hw)) {
5087 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005088 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005089 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005090 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005091 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005092 fcport->disc_state = DSC_LOGIN_COMPLETE;
5093 fcport->deleted = 0;
5094 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005095
Duane Grigsbye84067d2017-06-21 13:48:43 -07005096 if (fcport->fc4f_nvme) {
5097 qla_nvme_register_remote(vha, fcport);
5098 return;
5099 }
5100
Joe Carnuccio1f93da522012-11-21 02:40:38 -05005101 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005102 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005103 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005104
5105reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005106 switch (vha->host->active_mode) {
5107 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005108 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005109 break;
5110 case MODE_TARGET:
5111 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5112 !vha->vha_tgt.qla_tgt->tgt_stopped)
5113 qlt_fc_port_added(vha, fcport);
5114 break;
5115 case MODE_DUAL:
5116 qla2x00_reg_remote_port(vha, fcport);
5117 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5118 !vha->vha_tgt.qla_tgt->tgt_stopped)
5119 qlt_fc_port_added(vha, fcport);
5120 break;
5121 default:
5122 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005123 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005124}
5125
5126/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 * qla2x00_configure_fabric
5128 * Setup SNS devices with loop ID's.
5129 *
5130 * Input:
5131 * ha = adapter block pointer.
5132 *
5133 * Returns:
5134 * 0 = success.
5135 * BIT_0 = error
5136 */
5137static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005138qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139{
Arun Easib3b02e62012-02-09 11:15:39 -08005140 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005141 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005143 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005145 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005146 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
5148 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005149 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005150 loop_id = NPH_F_PORT;
5151 else
5152 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005153 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005155 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005156 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005158 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 return (QLA_SUCCESS);
5160 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005161 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Quinn Tran41dc5292017-01-19 22:28:03 -08005163
5164 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5165 rval = qla2x00_send_change_request(vha, 0x3, 0);
5166 if (rval != QLA_SUCCESS)
5167 ql_log(ql_log_warn, vha, 0x121,
5168 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5169 rval);
5170 }
5171
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005174 qla2x00_mgmt_svr_login(vha);
5175
Andrew Vasquezcca53352005-08-26 19:08:30 -07005176 /* FDMI support. */
5177 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005178 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5179 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005182 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005183 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5184 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005185 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5186 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5187 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5188 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005189 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005190 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005191 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005192 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5193 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005195 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005196 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005197 if (test_bit(LOOP_RESYNC_NEEDED,
5198 &vha->dpc_flags))
5199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005201 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005203 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005204 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005205 if (test_bit(LOOP_RESYNC_NEEDED,
5206 &vha->dpc_flags))
5207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005209 if (vha->flags.nvme_enabled) {
5210 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5211 ql_dbg(ql_dbg_disc, vha, 0x2049,
5212 "Register NVME FC Type Features failed.\n");
5213 }
5214 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005215 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005217 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005218 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005219 if (test_bit(LOOP_RESYNC_NEEDED,
5220 &vha->dpc_flags))
5221 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005222 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005224 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005225 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005226 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
5229 }
5230
Joe Carnuccio827210b2013-02-08 01:57:57 -05005231
Alexei Potashnikdf673272015-07-14 16:00:46 -04005232 /* Mark the time right before querying FW for connected ports.
5233 * This process is long, asynchronous and by the time it's done,
5234 * collected information might not be accurate anymore. E.g.
5235 * disconnected port might have re-connected and a brand new
5236 * session has been created. In this case session's generation
5237 * will be newer than discovery_gen. */
5238 qlt_do_generation_tick(vha, &discovery_gen);
5239
Quinn Trana4239942017-12-28 12:33:26 -08005240 if (USE_ASYNC_SCAN(ha)) {
5241 rval = QLA_SUCCESS;
5242 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5243 if (rval)
5244 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5245 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005246 list_for_each_entry(fcport, &vha->vp_fcports, list)
5247 fcport->scan_state = QLA_FCPORT_SCAN;
5248
Quinn Trana4239942017-12-28 12:33:26 -08005249 rval = qla2x00_find_all_fabric_devs(vha);
5250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 if (rval != QLA_SUCCESS)
5252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 } while (0);
5254
Duane Grigsbye84067d2017-06-21 13:48:43 -07005255 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5256 qla_nvme_register_hba(vha);
5257
Quinn Tran726b8542017-01-19 22:28:00 -08005258 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005259 ql_dbg(ql_dbg_disc, vha, 0x2068,
5260 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
5262 return (rval);
5263}
5264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265/*
5266 * qla2x00_find_all_fabric_devs
5267 *
5268 * Input:
5269 * ha = adapter block pointer.
5270 * dev = database device entry pointer.
5271 *
5272 * Returns:
5273 * 0 = success.
5274 *
5275 * Context:
5276 * Kernel context.
5277 */
5278static int
Quinn Tran726b8542017-01-19 22:28:00 -08005279qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280{
5281 int rval;
5282 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005283 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 int found;
5285
5286 sw_info_t *swl;
5287 int swl_idx;
5288 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005289 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005290 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005291 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005292 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
5294 rval = QLA_SUCCESS;
5295
5296 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005297 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005298 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005299 GFP_KERNEL);
5300 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005301 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005303 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005304 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005306 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005307 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005309 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5310 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005311 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005313 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5314 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005315 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005317 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5318 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005319 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5320 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005321 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5322 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005324
5325 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005326 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005327 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005328 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5329 return rval;
5330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 }
5332 swl_idx = 0;
5333
5334 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005335 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005337 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005338 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 return (QLA_MEMORY_ALLOC_FAILED);
5340 }
5341 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 /* Set start port ID scan at adapter ID. */
5343 first_dev = 1;
5344 last_dev = 0;
5345
5346 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005347 loop_id = ha->min_external_loopid;
5348 for (; loop_id <= ha->max_loop_id; loop_id++) {
5349 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 continue;
5351
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005352 if (ha->current_topology == ISP_CFG_FL &&
5353 (atomic_read(&vha->loop_down_timer) ||
5354 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005355 atomic_set(&vha->loop_down_timer, 0);
5356 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5357 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 if (swl != NULL) {
5362 if (last_dev) {
5363 wrap.b24 = new_fcport->d_id.b24;
5364 } else {
5365 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5366 memcpy(new_fcport->node_name,
5367 swl[swl_idx].node_name, WWN_SIZE);
5368 memcpy(new_fcport->port_name,
5369 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005370 memcpy(new_fcport->fabric_port_name,
5371 swl[swl_idx].fabric_port_name, WWN_SIZE);
5372 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005373 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005375 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005376 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005377 if (vha->flags.nvme_enabled &&
5378 swl[swl_idx].fc4f_nvme) {
5379 new_fcport->fc4f_nvme =
5380 swl[swl_idx].fc4f_nvme;
5381 ql_log(ql_log_info, vha, 0x2131,
5382 "FOUND: NVME port %8phC as FC Type 28h\n",
5383 new_fcport->port_name);
5384 }
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5387 last_dev = 1;
5388 }
5389 swl_idx++;
5390 }
5391 } else {
5392 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005393 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005395 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005396 "SNS scan failed -- assuming "
5397 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 rval = QLA_SUCCESS;
5399 break;
5400 }
5401 }
5402
5403 /* If wrap on switch device list, exit. */
5404 if (first_dev) {
5405 wrap.b24 = new_fcport->d_id.b24;
5406 first_dev = 0;
5407 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005408 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005409 "Device wrap (%02x%02x%02x).\n",
5410 new_fcport->d_id.b.domain,
5411 new_fcport->d_id.b.area,
5412 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 break;
5414 }
5415
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005416 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005417 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 continue;
5419
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005420 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005421 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5422 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005423
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005424 /* Bypass if same domain and area of adapter. */
5425 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005426 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005427 ISP_CFG_FL)
5428 continue;
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 /* Bypass reserved domain fields. */
5431 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5432 continue;
5433
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005434 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005435 if (ql2xgffidenable &&
5436 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5437 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005438 continue;
5439
Quinn Tran726b8542017-01-19 22:28:00 -08005440 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5441
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 /* Locate matching device in database. */
5443 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005444 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 if (memcmp(new_fcport->port_name, fcport->port_name,
5446 WWN_SIZE))
5447 continue;
5448
Joe Carnuccio827210b2013-02-08 01:57:57 -05005449 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 found++;
5452
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005453 /* Update port state. */
5454 memcpy(fcport->fabric_port_name,
5455 new_fcport->fabric_port_name, WWN_SIZE);
5456 fcport->fp_speed = new_fcport->fp_speed;
5457
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005459 * If address the same and state FCS_ONLINE
5460 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 */
5462 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005463 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005464 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 break;
5466 }
5467
5468 /*
5469 * If device was not a fabric device before.
5470 */
5471 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5472 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005473 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 fcport->flags |= (FCF_FABRIC_DEVICE |
5475 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 break;
5477 }
5478
5479 /*
5480 * Port ID changed or device was marked to be updated;
5481 * Log it out if still logged in and mark it for
5482 * relogin later.
5483 */
Quinn Tran726b8542017-01-19 22:28:00 -08005484 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005485 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5486 "port changed FC ID, %8phC"
5487 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5488 fcport->port_name,
5489 fcport->d_id.b.domain,
5490 fcport->d_id.b.area,
5491 fcport->d_id.b.al_pa,
5492 fcport->loop_id,
5493 new_fcport->d_id.b.domain,
5494 new_fcport->d_id.b.area,
5495 new_fcport->d_id.b.al_pa);
5496 fcport->d_id.b24 = new_fcport->d_id.b24;
5497 break;
5498 }
5499
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 fcport->d_id.b24 = new_fcport->d_id.b24;
5501 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 break;
5503 }
5504
Quinn Tran726b8542017-01-19 22:28:00 -08005505 if (found) {
5506 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005510 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005511 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5512
5513 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
5516 /* Allocate a new replacement fcport. */
5517 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005518 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005520 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005521 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 return (QLA_MEMORY_ALLOC_FAILED);
5523 }
5524 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5525 new_fcport->d_id.b24 = nxt_d_id.b24;
5526 }
5527
Quinn Tran726b8542017-01-19 22:28:00 -08005528 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Quinn Tran726b8542017-01-19 22:28:00 -08005530 /*
5531 * Logout all previous fabric dev marked lost, except FCP2 devices.
5532 */
5533 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5534 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5535 break;
5536
5537 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5538 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5539 continue;
5540
5541 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5542 if ((qla_dual_mode_enabled(vha) ||
5543 qla_ini_mode_enabled(vha)) &&
5544 atomic_read(&fcport->state) == FCS_ONLINE) {
5545 qla2x00_mark_device_lost(vha, fcport,
5546 ql2xplogiabsentdevice, 0);
5547 if (fcport->loop_id != FC_NO_LOOP_ID &&
5548 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5549 fcport->port_type != FCT_INITIATOR &&
5550 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005551 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005552 "%s %d %8phC post del sess\n",
5553 __func__, __LINE__,
5554 fcport->port_name);
5555
5556 qlt_schedule_sess_for_deletion_lock
5557 (fcport);
5558 continue;
5559 }
5560 }
5561 }
5562
5563 if (fcport->scan_state == QLA_FCPORT_FOUND)
5564 qla24xx_fcport_handle_login(vha, fcport);
5565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return (rval);
5567}
5568
5569/*
5570 * qla2x00_find_new_loop_id
5571 * Scan through our port list and find a new usable loop ID.
5572 *
5573 * Input:
5574 * ha: adapter state pointer.
5575 * dev: port structure pointer.
5576 *
5577 * Returns:
5578 * qla2x00 local function return status code.
5579 *
5580 * Context:
5581 * Kernel context.
5582 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005583int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005584qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585{
5586 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005587 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005588 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
5590 rval = QLA_SUCCESS;
5591
Chad Dupuis5f16b332012-08-22 14:21:00 -04005592 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Chad Dupuis5f16b332012-08-22 14:21:00 -04005594 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5595 LOOPID_MAP_SIZE);
5596 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5597 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5598 dev->loop_id = FC_NO_LOOP_ID;
5599 rval = QLA_FUNCTION_FAILED;
5600 } else
5601 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Chad Dupuis5f16b332012-08-22 14:21:00 -04005603 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Chad Dupuis5f16b332012-08-22 14:21:00 -04005605 if (rval == QLA_SUCCESS)
5606 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5607 "Assigning new loopid=%x, portid=%x.\n",
5608 dev->loop_id, dev->d_id.b24);
5609 else
5610 ql_log(ql_log_warn, dev->vha, 0x2087,
5611 "No loop_id's available, portid=%x.\n",
5612 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
5614 return (rval);
5615}
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618/*
5619 * qla2x00_fabric_login
5620 * Issue fabric login command.
5621 *
5622 * Input:
5623 * ha = adapter block pointer.
5624 * device = pointer to FC device type structure.
5625 *
5626 * Returns:
5627 * 0 - Login successfully
5628 * 1 - Login failed
5629 * 2 - Initiator device
5630 * 3 - Fatal error
5631 */
5632int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005633qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 uint16_t *next_loopid)
5635{
5636 int rval;
5637 int retry;
5638 uint16_t tmp_loopid;
5639 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005640 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 retry = 0;
5643 tmp_loopid = 0;
5644
5645 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005646 ql_dbg(ql_dbg_disc, vha, 0x2000,
5647 "Trying Fabric Login w/loop id 0x%04x for port "
5648 "%02x%02x%02x.\n",
5649 fcport->loop_id, fcport->d_id.b.domain,
5650 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
5652 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005653 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 fcport->d_id.b.domain, fcport->d_id.b.area,
5655 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005656 if (rval != QLA_SUCCESS) {
5657 return rval;
5658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 if (mb[0] == MBS_PORT_ID_USED) {
5660 /*
5661 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005662 * recommends the driver perform an implicit login with
5663 * the specified ID again. The ID we just used is save
5664 * here so we return with an ID that can be tried by
5665 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 */
5667 retry++;
5668 tmp_loopid = fcport->loop_id;
5669 fcport->loop_id = mb[1];
5670
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005671 ql_dbg(ql_dbg_disc, vha, 0x2001,
5672 "Fabric Login: port in use - next loop "
5673 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005675 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
5677 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5678 /*
5679 * Login succeeded.
5680 */
5681 if (retry) {
5682 /* A retry occurred before. */
5683 *next_loopid = tmp_loopid;
5684 } else {
5685 /*
5686 * No retry occurred before. Just increment the
5687 * ID value for next login.
5688 */
5689 *next_loopid = (fcport->loop_id + 1);
5690 }
5691
5692 if (mb[1] & BIT_0) {
5693 fcport->port_type = FCT_INITIATOR;
5694 } else {
5695 fcport->port_type = FCT_TARGET;
5696 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005697 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 }
5699 }
5700
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005701 if (mb[10] & BIT_0)
5702 fcport->supported_classes |= FC_COS_CLASS2;
5703 if (mb[10] & BIT_1)
5704 fcport->supported_classes |= FC_COS_CLASS3;
5705
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005706 if (IS_FWI2_CAPABLE(ha)) {
5707 if (mb[10] & BIT_7)
5708 fcport->flags |=
5709 FCF_CONF_COMP_SUPPORTED;
5710 }
5711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 rval = QLA_SUCCESS;
5713 break;
5714 } else if (mb[0] == MBS_LOOP_ID_USED) {
5715 /*
5716 * Loop ID already used, try next loop ID.
5717 */
5718 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005719 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 if (rval != QLA_SUCCESS) {
5721 /* Ran out of loop IDs to use */
5722 break;
5723 }
5724 } else if (mb[0] == MBS_COMMAND_ERROR) {
5725 /*
5726 * Firmware possibly timed out during login. If NO
5727 * retries are left to do then the device is declared
5728 * dead.
5729 */
5730 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005731 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005732 fcport->d_id.b.domain, fcport->d_id.b.area,
5733 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005734 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 rval = 1;
5737 break;
5738 } else {
5739 /*
5740 * unrecoverable / not handled error
5741 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005742 ql_dbg(ql_dbg_disc, vha, 0x2002,
5743 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5744 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5745 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5746 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005749 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005750 fcport->d_id.b.domain, fcport->d_id.b.area,
5751 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005752 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005753 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755 rval = 3;
5756 break;
5757 }
5758 }
5759
5760 return (rval);
5761}
5762
5763/*
5764 * qla2x00_local_device_login
5765 * Issue local device login command.
5766 *
5767 * Input:
5768 * ha = adapter block pointer.
5769 * loop_id = loop id of device to login to.
5770 *
5771 * Returns (Where's the #define!!!!):
5772 * 0 - Login successfully
5773 * 1 - Login failed
5774 * 3 - Fatal error
5775 */
5776int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005777qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
5779 int rval;
5780 uint16_t mb[MAILBOX_REGISTER_COUNT];
5781
5782 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005783 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 if (rval == QLA_SUCCESS) {
5785 /* Interrogate mailbox registers for any errors */
5786 if (mb[0] == MBS_COMMAND_ERROR)
5787 rval = 1;
5788 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5789 /* device not in PCB table */
5790 rval = 3;
5791 }
5792
5793 return (rval);
5794}
5795
5796/*
5797 * qla2x00_loop_resync
5798 * Resync with fibre channel devices.
5799 *
5800 * Input:
5801 * ha = adapter block pointer.
5802 *
5803 * Returns:
5804 * 0 = success
5805 */
5806int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005807qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005809 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005811 struct req_que *req;
5812 struct rsp_que *rsp;
5813
Michael Hernandezd7459522016-12-12 14:40:07 -08005814 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005815 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005817 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5818 if (vha->flags.online) {
5819 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5821 wait_time = 256;
5822 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005823 if (!IS_QLAFX00(vha->hw)) {
5824 /*
5825 * Issue a marker after FW becomes
5826 * ready.
5827 */
5828 qla2x00_marker(vha, req, rsp, 0, 0,
5829 MK_SYNC_ALL);
5830 vha->marker_needed = 0;
5831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
5833 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005834 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005836 if (IS_QLAFX00(vha->hw))
5837 qlafx00_configure_devices(vha);
5838 else
5839 qla2x00_configure_loop(vha);
5840
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005842 } while (!atomic_read(&vha->loop_down_timer) &&
5843 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5844 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5845 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 }
5848
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005849 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005852 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005853 ql_dbg(ql_dbg_disc, vha, 0x206c,
5854 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
5856 return (rval);
5857}
5858
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005859/*
5860* qla2x00_perform_loop_resync
5861* Description: This function will set the appropriate flags and call
5862* qla2x00_loop_resync. If successful loop will be resynced
5863* Arguments : scsi_qla_host_t pointer
5864* returm : Success or Failure
5865*/
5866
5867int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5868{
5869 int32_t rval = 0;
5870
5871 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5872 /*Configure the flags so that resync happens properly*/
5873 atomic_set(&ha->loop_down_timer, 0);
5874 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5875 atomic_set(&ha->loop_state, LOOP_UP);
5876 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5877 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5878 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5879
5880 rval = qla2x00_loop_resync(ha);
5881 } else
5882 atomic_set(&ha->loop_state, LOOP_DEAD);
5883
5884 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5885 }
5886
5887 return rval;
5888}
5889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005891qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005892{
5893 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005894 struct scsi_qla_host *vha;
5895 struct qla_hw_data *ha = base_vha->hw;
5896 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005897
Arun Easifeafb7b2010-09-03 14:57:00 -07005898 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005899 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005900 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5901 atomic_inc(&vha->vref_count);
5902 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005903 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005904 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5905 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005906 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005907
Arun Easifeafb7b2010-09-03 14:57:00 -07005908 spin_lock_irqsave(&ha->vport_slock, flags);
5909 }
5910 }
5911 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005912 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005913 }
5914 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005915}
5916
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005917/* Assumes idc_lock always held on entry */
5918void
5919qla83xx_reset_ownership(scsi_qla_host_t *vha)
5920{
5921 struct qla_hw_data *ha = vha->hw;
5922 uint32_t drv_presence, drv_presence_mask;
5923 uint32_t dev_part_info1, dev_part_info2, class_type;
5924 uint32_t class_type_mask = 0x3;
5925 uint16_t fcoe_other_function = 0xffff, i;
5926
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005927 if (IS_QLA8044(ha)) {
5928 drv_presence = qla8044_rd_direct(vha,
5929 QLA8044_CRB_DRV_ACTIVE_INDEX);
5930 dev_part_info1 = qla8044_rd_direct(vha,
5931 QLA8044_CRB_DEV_PART_INFO_INDEX);
5932 dev_part_info2 = qla8044_rd_direct(vha,
5933 QLA8044_CRB_DEV_PART_INFO2);
5934 } else {
5935 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5936 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5937 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5938 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005939 for (i = 0; i < 8; i++) {
5940 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5941 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5942 (i != ha->portnum)) {
5943 fcoe_other_function = i;
5944 break;
5945 }
5946 }
5947 if (fcoe_other_function == 0xffff) {
5948 for (i = 0; i < 8; i++) {
5949 class_type = ((dev_part_info2 >> (i * 4)) &
5950 class_type_mask);
5951 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5952 ((i + 8) != ha->portnum)) {
5953 fcoe_other_function = i + 8;
5954 break;
5955 }
5956 }
5957 }
5958 /*
5959 * Prepare drv-presence mask based on fcoe functions present.
5960 * However consider only valid physical fcoe function numbers (0-15).
5961 */
5962 drv_presence_mask = ~((1 << (ha->portnum)) |
5963 ((fcoe_other_function == 0xffff) ?
5964 0 : (1 << (fcoe_other_function))));
5965
5966 /* We are the reset owner iff:
5967 * - No other protocol drivers present.
5968 * - This is the lowest among fcoe functions. */
5969 if (!(drv_presence & drv_presence_mask) &&
5970 (ha->portnum < fcoe_other_function)) {
5971 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5972 "This host is Reset owner.\n");
5973 ha->flags.nic_core_reset_owner = 1;
5974 }
5975}
5976
Saurav Kashyapfa492632012-11-21 02:40:29 -05005977static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005978__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5979{
5980 int rval = QLA_SUCCESS;
5981 struct qla_hw_data *ha = vha->hw;
5982 uint32_t drv_ack;
5983
5984 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5985 if (rval == QLA_SUCCESS) {
5986 drv_ack |= (1 << ha->portnum);
5987 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5988 }
5989
5990 return rval;
5991}
5992
Saurav Kashyapfa492632012-11-21 02:40:29 -05005993static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005994__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5995{
5996 int rval = QLA_SUCCESS;
5997 struct qla_hw_data *ha = vha->hw;
5998 uint32_t drv_ack;
5999
6000 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6001 if (rval == QLA_SUCCESS) {
6002 drv_ack &= ~(1 << ha->portnum);
6003 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6004 }
6005
6006 return rval;
6007}
6008
Saurav Kashyapfa492632012-11-21 02:40:29 -05006009static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006010qla83xx_dev_state_to_string(uint32_t dev_state)
6011{
6012 switch (dev_state) {
6013 case QLA8XXX_DEV_COLD:
6014 return "COLD/RE-INIT";
6015 case QLA8XXX_DEV_INITIALIZING:
6016 return "INITIALIZING";
6017 case QLA8XXX_DEV_READY:
6018 return "READY";
6019 case QLA8XXX_DEV_NEED_RESET:
6020 return "NEED RESET";
6021 case QLA8XXX_DEV_NEED_QUIESCENT:
6022 return "NEED QUIESCENT";
6023 case QLA8XXX_DEV_FAILED:
6024 return "FAILED";
6025 case QLA8XXX_DEV_QUIESCENT:
6026 return "QUIESCENT";
6027 default:
6028 return "Unknown";
6029 }
6030}
6031
6032/* Assumes idc-lock always held on entry */
6033void
6034qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6035{
6036 struct qla_hw_data *ha = vha->hw;
6037 uint32_t idc_audit_reg = 0, duration_secs = 0;
6038
6039 switch (audit_type) {
6040 case IDC_AUDIT_TIMESTAMP:
6041 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6042 idc_audit_reg = (ha->portnum) |
6043 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6044 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6045 break;
6046
6047 case IDC_AUDIT_COMPLETION:
6048 duration_secs = ((jiffies_to_msecs(jiffies) -
6049 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6050 idc_audit_reg = (ha->portnum) |
6051 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6052 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6053 break;
6054
6055 default:
6056 ql_log(ql_log_warn, vha, 0xb078,
6057 "Invalid audit type specified.\n");
6058 break;
6059 }
6060}
6061
6062/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006063static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006064qla83xx_initiating_reset(scsi_qla_host_t *vha)
6065{
6066 struct qla_hw_data *ha = vha->hw;
6067 uint32_t idc_control, dev_state;
6068
6069 __qla83xx_get_idc_control(vha, &idc_control);
6070 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6071 ql_log(ql_log_info, vha, 0xb080,
6072 "NIC Core reset has been disabled. idc-control=0x%x\n",
6073 idc_control);
6074 return QLA_FUNCTION_FAILED;
6075 }
6076
6077 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6078 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6079 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6080 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6081 QLA8XXX_DEV_NEED_RESET);
6082 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6083 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6084 } else {
6085 const char *state = qla83xx_dev_state_to_string(dev_state);
6086 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6087
6088 /* SV: XXX: Is timeout required here? */
6089 /* Wait for IDC state change READY -> NEED_RESET */
6090 while (dev_state == QLA8XXX_DEV_READY) {
6091 qla83xx_idc_unlock(vha, 0);
6092 msleep(200);
6093 qla83xx_idc_lock(vha, 0);
6094 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6095 }
6096 }
6097
6098 /* Send IDC ack by writing to drv-ack register */
6099 __qla83xx_set_drv_ack(vha);
6100
6101 return QLA_SUCCESS;
6102}
6103
6104int
6105__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6106{
6107 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6108}
6109
6110int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006111__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6112{
6113 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6114}
6115
Saurav Kashyapfa492632012-11-21 02:40:29 -05006116static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006117qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6118{
6119 uint32_t drv_presence = 0;
6120 struct qla_hw_data *ha = vha->hw;
6121
6122 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6123 if (drv_presence & (1 << ha->portnum))
6124 return QLA_SUCCESS;
6125 else
6126 return QLA_TEST_FAILED;
6127}
6128
6129int
6130qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6131{
6132 int rval = QLA_SUCCESS;
6133 struct qla_hw_data *ha = vha->hw;
6134
6135 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6136 "Entered %s().\n", __func__);
6137
6138 if (vha->device_flags & DFLG_DEV_FAILED) {
6139 ql_log(ql_log_warn, vha, 0xb059,
6140 "Device in unrecoverable FAILED state.\n");
6141 return QLA_FUNCTION_FAILED;
6142 }
6143
6144 qla83xx_idc_lock(vha, 0);
6145
6146 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6147 ql_log(ql_log_warn, vha, 0xb05a,
6148 "Function=0x%x has been removed from IDC participation.\n",
6149 ha->portnum);
6150 rval = QLA_FUNCTION_FAILED;
6151 goto exit;
6152 }
6153
6154 qla83xx_reset_ownership(vha);
6155
6156 rval = qla83xx_initiating_reset(vha);
6157
6158 /*
6159 * Perform reset if we are the reset-owner,
6160 * else wait till IDC state changes to READY/FAILED.
6161 */
6162 if (rval == QLA_SUCCESS) {
6163 rval = qla83xx_idc_state_handler(vha);
6164
6165 if (rval == QLA_SUCCESS)
6166 ha->flags.nic_core_hung = 0;
6167 __qla83xx_clear_drv_ack(vha);
6168 }
6169
6170exit:
6171 qla83xx_idc_unlock(vha, 0);
6172
6173 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6174
6175 return rval;
6176}
6177
Saurav Kashyap81178772012-08-22 14:21:04 -04006178int
6179qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6180{
6181 struct qla_hw_data *ha = vha->hw;
6182 int rval = QLA_FUNCTION_FAILED;
6183
6184 if (!IS_MCTP_CAPABLE(ha)) {
6185 /* This message can be removed from the final version */
6186 ql_log(ql_log_info, vha, 0x506d,
6187 "This board is not MCTP capable\n");
6188 return rval;
6189 }
6190
6191 if (!ha->mctp_dump) {
6192 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6193 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6194
6195 if (!ha->mctp_dump) {
6196 ql_log(ql_log_warn, vha, 0x506e,
6197 "Failed to allocate memory for mctp dump\n");
6198 return rval;
6199 }
6200 }
6201
6202#define MCTP_DUMP_STR_ADDR 0x00000000
6203 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6204 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6205 if (rval != QLA_SUCCESS) {
6206 ql_log(ql_log_warn, vha, 0x506f,
6207 "Failed to capture mctp dump\n");
6208 } else {
6209 ql_log(ql_log_info, vha, 0x5070,
6210 "Mctp dump capture for host (%ld/%p).\n",
6211 vha->host_no, ha->mctp_dump);
6212 ha->mctp_dumped = 1;
6213 }
6214
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006215 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006216 ha->flags.nic_core_reset_hdlr_active = 1;
6217 rval = qla83xx_restart_nic_firmware(vha);
6218 if (rval)
6219 /* NIC Core reset failed. */
6220 ql_log(ql_log_warn, vha, 0x5071,
6221 "Failed to restart nic firmware\n");
6222 else
6223 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6224 "Restarted NIC firmware successfully.\n");
6225 ha->flags.nic_core_reset_hdlr_active = 0;
6226 }
6227
6228 return rval;
6229
6230}
6231
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006232/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006233* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006234* Description: This function will block the new I/Os
6235* Its not aborting any I/Os as context
6236* is not destroyed during quiescence
6237* Arguments: scsi_qla_host_t
6238* return : void
6239*/
6240void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006241qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006242{
6243 struct qla_hw_data *ha = vha->hw;
6244 struct scsi_qla_host *vp;
6245
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006246 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6247 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006248
6249 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6250 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6251 atomic_set(&vha->loop_state, LOOP_DOWN);
6252 qla2x00_mark_all_devices_lost(vha, 0);
6253 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006254 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006255 } else {
6256 if (!atomic_read(&vha->loop_down_timer))
6257 atomic_set(&vha->loop_down_timer,
6258 LOOP_DOWN_TIME);
6259 }
6260 /* Wait for pending cmds to complete */
6261 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6262}
6263
Giridhar Malavalia9083012010-04-12 17:59:55 -07006264void
6265qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6266{
6267 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006268 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006269 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006270 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006271 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006272
Saurav Kashyape46ef002011-02-23 15:27:16 -08006273 /* For ISP82XX, driver waits for completion of the commands.
6274 * online flag should be set.
6275 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006276 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006277 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006278 ha->flags.chip_reset_done = 0;
6279 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006280 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006281
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006282 ql_log(ql_log_info, vha, 0x00af,
6283 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006284
Saurav Kashyape46ef002011-02-23 15:27:16 -08006285 /* For ISP82XX, reset_chip is just disabling interrupts.
6286 * Driver waits for the completion of the commands.
6287 * the interrupts need to be enabled.
6288 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006289 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006290 ha->isp_ops->reset_chip(vha);
6291
Quinn Tran9cd883f2017-12-28 12:33:24 -08006292 SAVE_TOPO(ha);
6293 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006294 ha->flags.n2n_ae = 0;
6295 ha->flags.lip_ae = 0;
6296 ha->current_topology = 0;
6297 ha->flags.fw_started = 0;
6298 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006299 ha->base_qpair->chip_reset++;
6300 for (i = 0; i < ha->max_qpairs; i++) {
6301 if (ha->queue_pair_map[i])
6302 ha->queue_pair_map[i]->chip_reset =
6303 ha->base_qpair->chip_reset;
6304 }
Quinn Tran726b8542017-01-19 22:28:00 -08006305
Giridhar Malavalia9083012010-04-12 17:59:55 -07006306 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6307 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6308 atomic_set(&vha->loop_state, LOOP_DOWN);
6309 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006310
6311 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006312 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006313 atomic_inc(&vp->vref_count);
6314 spin_unlock_irqrestore(&ha->vport_slock, flags);
6315
Giridhar Malavalia9083012010-04-12 17:59:55 -07006316 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006317
6318 spin_lock_irqsave(&ha->vport_slock, flags);
6319 atomic_dec(&vp->vref_count);
6320 }
6321 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006322 } else {
6323 if (!atomic_read(&vha->loop_down_timer))
6324 atomic_set(&vha->loop_down_timer,
6325 LOOP_DOWN_TIME);
6326 }
6327
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006328 /* Clear all async request states across all VPs. */
6329 list_for_each_entry(fcport, &vha->vp_fcports, list)
6330 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6331 spin_lock_irqsave(&ha->vport_slock, flags);
6332 list_for_each_entry(vp, &ha->vp_list, list) {
6333 atomic_inc(&vp->vref_count);
6334 spin_unlock_irqrestore(&ha->vport_slock, flags);
6335
6336 list_for_each_entry(fcport, &vp->vp_fcports, list)
6337 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6338
6339 spin_lock_irqsave(&ha->vport_slock, flags);
6340 atomic_dec(&vp->vref_count);
6341 }
6342 spin_unlock_irqrestore(&ha->vport_slock, flags);
6343
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006344 if (!ha->flags.eeh_busy) {
6345 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006346 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006347 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006348 ql_log(ql_log_info, vha, 0x00b4,
6349 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006350
Saurav Kashyape46ef002011-02-23 15:27:16 -08006351 /* Done waiting for pending commands.
6352 * Reset the online flag.
6353 */
6354 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006357 /* Requeue all commands in outstanding command list. */
6358 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6359 }
Arun Easib6a029e2014-09-25 06:14:52 -04006360 /* memory barrier */
6361 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362}
6363
6364/*
6365* qla2x00_abort_isp
6366* Resets ISP and aborts all outstanding commands.
6367*
6368* Input:
6369* ha = adapter block pointer.
6370*
6371* Returns:
6372* 0 = success
6373*/
6374int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006375qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006377 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006379 struct qla_hw_data *ha = vha->hw;
6380 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006381 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006382 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006384 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006385 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
Santosh Vernekara61712972012-08-22 14:21:13 -04006387 if (IS_QLA8031(ha)) {
6388 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6389 "Clearing fcoe driver presence.\n");
6390 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6391 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6392 "Error while clearing DRV-Presence.\n");
6393 }
6394
Andrew Vasquez85880802009-12-15 21:29:46 -08006395 if (unlikely(pci_channel_offline(ha->pdev) &&
6396 ha->flags.pci_channel_io_perm_failure)) {
6397 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6398 status = 0;
6399 return status;
6400 }
6401
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006402 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006403
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006404 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006406 if (!qla2x00_restart_isp(vha)) {
6407 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006409 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 /*
6411 * Issue marker command only when we are going
6412 * to start the I/O .
6413 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006414 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 }
6416
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006417 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006419 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006421 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006422 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006423
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006424 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6425 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006426 if (ha->fce) {
6427 ha->flags.fce_enabled = 1;
6428 memset(ha->fce, 0,
6429 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006430 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006431 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6432 &ha->fce_bufs);
6433 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006434 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006435 "Unable to reinitialize FCE "
6436 "(%d).\n", rval);
6437 ha->flags.fce_enabled = 0;
6438 }
6439 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006440
6441 if (ha->eft) {
6442 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006443 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006444 ha->eft_dma, EFT_NUM_BUFFERS);
6445 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006446 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006447 "Unable to reinitialize EFT "
6448 "(%d).\n", rval);
6449 }
6450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006452 vha->flags.online = 1;
6453 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006455 ql_log(ql_log_fatal, vha, 0x8035,
6456 "ISP error recover failed - "
6457 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006458 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 * The next call disables the board
6460 * completely.
6461 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006462 ha->isp_ops->reset_adapter(vha);
6463 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006465 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 status = 0;
6467 } else { /* schedule another ISP abort */
6468 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006469 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6470 "ISP abort - retry remaining %d.\n",
6471 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 status = 1;
6473 }
6474 } else {
6475 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006476 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6477 "ISP error recovery - retrying (%d) "
6478 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006479 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 status = 1;
6481 }
6482 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006483
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 }
6485
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006486 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006487 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006488 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006489 spin_lock_irqsave(&ha->vport_slock, flags);
6490 list_for_each_entry(vp, &ha->vp_list, list) {
6491 if (vp->vp_idx) {
6492 atomic_inc(&vp->vref_count);
6493 spin_unlock_irqrestore(&ha->vport_slock, flags);
6494
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006495 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006496
6497 spin_lock_irqsave(&ha->vport_slock, flags);
6498 atomic_dec(&vp->vref_count);
6499 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006500 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006501 spin_unlock_irqrestore(&ha->vport_slock, flags);
6502
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006503 if (IS_QLA8031(ha)) {
6504 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6505 "Setting back fcoe driver presence.\n");
6506 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6507 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6508 "Error while setting DRV-Presence.\n");
6509 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006510 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006511 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6512 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 }
6514
6515 return(status);
6516}
6517
6518/*
6519* qla2x00_restart_isp
6520* restarts the ISP after a reset
6521*
6522* Input:
6523* ha = adapter block pointer.
6524*
6525* Returns:
6526* 0 = success
6527*/
6528static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006529qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006531 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006532 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006533 struct req_que *req = ha->req_q_map[0];
6534 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
6536 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006537 if (qla2x00_isp_firmware(vha)) {
6538 vha->flags.online = 0;
6539 status = ha->isp_ops->chip_diag(vha);
6540 if (!status)
6541 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 }
6543
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006544 if (!status && !(status = qla2x00_init_rings(vha))) {
6545 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006546 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006547
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006548 /* Initialize the queues in use */
6549 qla25xx_init_queues(ha);
6550
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006551 status = qla2x00_fw_ready(vha);
6552 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006553 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006554 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006555 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 }
6557
6558 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006559 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 }
6562 return (status);
6563}
6564
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006565static int
6566qla25xx_init_queues(struct qla_hw_data *ha)
6567{
6568 struct rsp_que *rsp = NULL;
6569 struct req_que *req = NULL;
6570 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6571 int ret = -1;
6572 int i;
6573
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006574 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006575 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006576 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006577 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006578 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006579 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006580 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6581 "%s Rsp que: %d init failed.\n",
6582 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006583 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006584 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6585 "%s Rsp que: %d inited.\n",
6586 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006587 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006588 }
6589 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006590 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006591 if (req && test_bit(i, ha->req_qid_map)) {
6592 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006593 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006594 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006595 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006596 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6597 "%s Req que: %d init failed.\n",
6598 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006599 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006600 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6601 "%s Req que: %d inited.\n",
6602 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006603 }
6604 }
6605 return ret;
6606}
6607
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608/*
6609* qla2x00_reset_adapter
6610* Reset adapter.
6611*
6612* Input:
6613* ha = adapter block pointer.
6614*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006615void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006616qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617{
6618 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006619 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006620 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006622 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006623 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 spin_lock_irqsave(&ha->hardware_lock, flags);
6626 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6627 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6628 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6629 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6630 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6631}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006632
6633void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006634qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006635{
6636 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006637 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006638 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6639
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006640 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006641 return;
6642
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006643 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006644 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006645
6646 spin_lock_irqsave(&ha->hardware_lock, flags);
6647 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6648 RD_REG_DWORD(&reg->hccr);
6649 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6650 RD_REG_DWORD(&reg->hccr);
6651 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006652
6653 if (IS_NOPOLLING_TYPE(ha))
6654 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006655}
6656
David Miller4e08df32007-04-16 12:37:43 -07006657/* On sparc systems, obtain port and node WWN from firmware
6658 * properties.
6659 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006660static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6661 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006662{
6663#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006664 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006665 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006666 struct device_node *dp = pci_device_to_OF_node(pdev);
6667 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006668 int len;
6669
6670 val = of_get_property(dp, "port-wwn", &len);
6671 if (val && len >= WWN_SIZE)
6672 memcpy(nv->port_name, val, WWN_SIZE);
6673
6674 val = of_get_property(dp, "node-wwn", &len);
6675 if (val && len >= WWN_SIZE)
6676 memcpy(nv->node_name, val, WWN_SIZE);
6677#endif
6678}
6679
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006680int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006681qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006682{
David Miller4e08df32007-04-16 12:37:43 -07006683 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006684 struct init_cb_24xx *icb;
6685 struct nvram_24xx *nv;
6686 uint32_t *dptr;
6687 uint8_t *dptr1, *dptr2;
6688 uint32_t chksum;
6689 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006690 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006691
David Miller4e08df32007-04-16 12:37:43 -07006692 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006693 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006694 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006695
6696 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006697 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006698 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6699 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6700 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006701 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006702 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6703 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006704
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006705 ha->nvram_size = sizeof(struct nvram_24xx);
6706 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006707
Seokmann Ju281afe12007-07-26 13:43:34 -07006708 /* Get VPD data into cache */
6709 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006710 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006711 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6712
6713 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006714 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006715 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006716 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006717 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6718 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006719
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006720 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6721 "Contents of NVRAM\n");
6722 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6723 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006724
6725 /* Bad NVRAM data, set defaults parameters. */
6726 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6727 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006728 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006729 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006730 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006731 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006732 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6733 ql_log(ql_log_warn, vha, 0x006c,
6734 "Falling back to functioning (yet invalid -- WWPN) "
6735 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006736
6737 /*
6738 * Set default initialization control block.
6739 */
6740 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006741 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6742 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006743 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006744 nv->execution_throttle = cpu_to_le16(0xFFFF);
6745 nv->exchange_count = cpu_to_le16(0);
6746 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006747 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006748 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006749 nv->port_name[2] = 0x00;
6750 nv->port_name[3] = 0xe0;
6751 nv->port_name[4] = 0x8b;
6752 nv->port_name[5] = 0x1c;
6753 nv->port_name[6] = 0x55;
6754 nv->port_name[7] = 0x86;
6755 nv->node_name[0] = 0x20;
6756 nv->node_name[1] = 0x00;
6757 nv->node_name[2] = 0x00;
6758 nv->node_name[3] = 0xe0;
6759 nv->node_name[4] = 0x8b;
6760 nv->node_name[5] = 0x1c;
6761 nv->node_name[6] = 0x55;
6762 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006763 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006764 nv->login_retry_count = cpu_to_le16(8);
6765 nv->interrupt_delay_timer = cpu_to_le16(0);
6766 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006767 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006768 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6769 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6770 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6771 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6772 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6773 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006774 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006775 nv->max_luns_per_target = cpu_to_le16(128);
6776 nv->port_down_retry_count = cpu_to_le16(30);
6777 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006778
6779 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006780 }
6781
Quinn Tran726b8542017-01-19 22:28:00 -08006782 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006783 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006784 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006785 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006786 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006787 }
6788
6789 qlt_24xx_config_nvram_stage1(vha, nv);
6790
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006791 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006792 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006793
6794 /* Copy 1st segment. */
6795 dptr1 = (uint8_t *)icb;
6796 dptr2 = (uint8_t *)&nv->version;
6797 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6798 while (cnt--)
6799 *dptr1++ = *dptr2++;
6800
6801 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006802 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006803
6804 /* Copy 2nd segment. */
6805 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6806 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6807 cnt = (uint8_t *)&icb->reserved_3 -
6808 (uint8_t *)&icb->interrupt_delay_timer;
6809 while (cnt--)
6810 *dptr1++ = *dptr2++;
6811
6812 /*
6813 * Setup driver NVRAM options.
6814 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006815 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006816 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006817
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006818 qlt_24xx_config_nvram_stage2(vha, icb);
6819
Bart Van Asschead950362015-07-09 07:24:08 -07006820 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006821 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006822 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6823 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6824 }
6825
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006826 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006827 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006828 /*
6829 * Firmware will apply the following mask if the nodename was
6830 * not provided.
6831 */
6832 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6833 icb->node_name[0] &= 0xF0;
6834 }
6835
6836 /* Set host adapter parameters. */
6837 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006838 ha->flags.enable_lip_reset = 0;
6839 ha->flags.enable_lip_full_login =
6840 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6841 ha->flags.enable_target_reset =
6842 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006843 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006844 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006845
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006846 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6847 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006848
6849 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6850 sizeof(ha->fw_seriallink_options24));
6851
6852 /* save HBA serial number */
6853 ha->serial0 = icb->port_name[5];
6854 ha->serial1 = icb->port_name[6];
6855 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006856 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6857 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006858
Bart Van Asschead950362015-07-09 07:24:08 -07006859 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006860
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006861 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6862
6863 /* Set minimum login_timeout to 4 seconds. */
6864 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6865 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6866 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006867 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006868 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006869
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006870 /* Set minimum RATOV to 100 tenths of a second. */
6871 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006872
6873 ha->loop_reset_delay = nv->reset_delay;
6874
6875 /* Link Down Timeout = 0:
6876 *
6877 * When Port Down timer expires we will start returning
6878 * I/O's to OS with "DID_NO_CONNECT".
6879 *
6880 * Link Down Timeout != 0:
6881 *
6882 * The driver waits for the link to come up after link down
6883 * before returning I/Os to OS with "DID_NO_CONNECT".
6884 */
6885 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6886 ha->loop_down_abort_time =
6887 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6888 } else {
6889 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6890 ha->loop_down_abort_time =
6891 (LOOP_DOWN_TIME - ha->link_down_timeout);
6892 }
6893
6894 /* Need enough time to try and get the port back. */
6895 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6896 if (qlport_down_retry)
6897 ha->port_down_retry_count = qlport_down_retry;
6898
6899 /* Set login_retry_count */
6900 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6901 if (ha->port_down_retry_count ==
6902 le16_to_cpu(nv->port_down_retry_count) &&
6903 ha->port_down_retry_count > 3)
6904 ha->login_retry_count = ha->port_down_retry_count;
6905 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6906 ha->login_retry_count = ha->port_down_retry_count;
6907 if (ql2xloginretrycount)
6908 ha->login_retry_count = ql2xloginretrycount;
6909
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006910 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006911 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006912 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6913 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6914 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6915 le16_to_cpu(icb->interrupt_delay_timer): 2;
6916 }
Bart Van Asschead950362015-07-09 07:24:08 -07006917 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006918 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006919 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006920 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006921 ha->zio_mode = QLA_ZIO_MODE_6;
6922
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006923 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006924 "ZIO mode %d enabled; timer delay (%d us).\n",
6925 ha->zio_mode, ha->zio_timer * 100);
6926
6927 icb->firmware_options_2 |= cpu_to_le32(
6928 (uint32_t)ha->zio_mode);
6929 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006930 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006931 }
6932
David Miller4e08df32007-04-16 12:37:43 -07006933 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006934 ql_log(ql_log_warn, vha, 0x0070,
6935 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006936 }
6937 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006938}
6939
Sawan Chandak4243c112016-01-27 12:03:31 -05006940uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6941{
6942 struct qla27xx_image_status pri_image_status, sec_image_status;
6943 uint8_t valid_pri_image, valid_sec_image;
6944 uint32_t *wptr;
6945 uint32_t cnt, chksum, size;
6946 struct qla_hw_data *ha = vha->hw;
6947
6948 valid_pri_image = valid_sec_image = 1;
6949 ha->active_image = 0;
6950 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6951
6952 if (!ha->flt_region_img_status_pri) {
6953 valid_pri_image = 0;
6954 goto check_sec_image;
6955 }
6956
6957 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6958 ha->flt_region_img_status_pri, size);
6959
6960 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6961 ql_dbg(ql_dbg_init, vha, 0x018b,
6962 "Primary image signature (0x%x) not valid\n",
6963 pri_image_status.signature);
6964 valid_pri_image = 0;
6965 goto check_sec_image;
6966 }
6967
6968 wptr = (uint32_t *)(&pri_image_status);
6969 cnt = size;
6970
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006971 for (chksum = 0; cnt--; wptr++)
6972 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006973
Sawan Chandak4243c112016-01-27 12:03:31 -05006974 if (chksum) {
6975 ql_dbg(ql_dbg_init, vha, 0x018c,
6976 "Checksum validation failed for primary image (0x%x)\n",
6977 chksum);
6978 valid_pri_image = 0;
6979 }
6980
6981check_sec_image:
6982 if (!ha->flt_region_img_status_sec) {
6983 valid_sec_image = 0;
6984 goto check_valid_image;
6985 }
6986
6987 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6988 ha->flt_region_img_status_sec, size);
6989
6990 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6991 ql_dbg(ql_dbg_init, vha, 0x018d,
6992 "Secondary image signature(0x%x) not valid\n",
6993 sec_image_status.signature);
6994 valid_sec_image = 0;
6995 goto check_valid_image;
6996 }
6997
6998 wptr = (uint32_t *)(&sec_image_status);
6999 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007000 for (chksum = 0; cnt--; wptr++)
7001 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05007002 if (chksum) {
7003 ql_dbg(ql_dbg_init, vha, 0x018e,
7004 "Checksum validation failed for secondary image (0x%x)\n",
7005 chksum);
7006 valid_sec_image = 0;
7007 }
7008
7009check_valid_image:
7010 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7011 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7012 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7013 if (!ha->active_image ||
7014 pri_image_status.generation_number <
7015 sec_image_status.generation_number)
7016 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7017 }
7018
7019 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7020 ha->active_image == 0 ? "default bootld and fw" :
7021 ha->active_image == 1 ? "primary" :
7022 ha->active_image == 2 ? "secondary" :
7023 "Invalid");
7024
7025 return ha->active_image;
7026}
7027
Adrian Bunk413975a2006-06-30 02:33:06 -07007028static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007029qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7030 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007031{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007032 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007033 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007034 uint32_t *dcode, dlen;
7035 uint32_t risc_addr;
7036 uint32_t risc_size;
7037 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007038 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007039 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007040
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007041 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007042 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007043
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007044 rval = QLA_SUCCESS;
7045
7046 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007047 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007048 *srisc_addr = 0;
7049
Sawan Chandak4243c112016-01-27 12:03:31 -05007050 if (IS_QLA27XX(ha) &&
7051 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7052 faddr = ha->flt_region_fw_sec;
7053
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007054 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007055 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007056 for (i = 0; i < 4; i++)
7057 dcode[i] = be32_to_cpu(dcode[i]);
7058 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7059 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7060 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7061 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007062 ql_log(ql_log_fatal, vha, 0x008c,
7063 "Unable to verify the integrity of flash firmware "
7064 "image.\n");
7065 ql_log(ql_log_fatal, vha, 0x008d,
7066 "Firmware data: %08x %08x %08x %08x.\n",
7067 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007068
7069 return QLA_FUNCTION_FAILED;
7070 }
7071
7072 while (segments && rval == QLA_SUCCESS) {
7073 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007074 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007075
7076 risc_addr = be32_to_cpu(dcode[2]);
7077 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7078 risc_size = be32_to_cpu(dcode[3]);
7079
7080 fragment = 0;
7081 while (risc_size > 0 && rval == QLA_SUCCESS) {
7082 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7083 if (dlen > risc_size)
7084 dlen = risc_size;
7085
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007086 ql_dbg(ql_dbg_init, vha, 0x008e,
7087 "Loading risc segment@ risc addr %x "
7088 "number of dwords 0x%x offset 0x%x.\n",
7089 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007090
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007091 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007092 for (i = 0; i < dlen; i++)
7093 dcode[i] = swab32(dcode[i]);
7094
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007095 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007096 dlen);
7097 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007098 ql_log(ql_log_fatal, vha, 0x008f,
7099 "Failed to load segment %d of firmware.\n",
7100 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007101 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007102 }
7103
7104 faddr += dlen;
7105 risc_addr += dlen;
7106 risc_size -= dlen;
7107 fragment++;
7108 }
7109
7110 /* Next segment. */
7111 segments--;
7112 }
7113
Chad Dupuisf73cb692014-02-26 04:15:06 -05007114 if (!IS_QLA27XX(ha))
7115 return rval;
7116
7117 if (ha->fw_dump_template)
7118 vfree(ha->fw_dump_template);
7119 ha->fw_dump_template = NULL;
7120 ha->fw_dump_template_len = 0;
7121
7122 ql_dbg(ql_dbg_init, vha, 0x0161,
7123 "Loading fwdump template from %x\n", faddr);
7124 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7125 risc_size = be32_to_cpu(dcode[2]);
7126 ql_dbg(ql_dbg_init, vha, 0x0162,
7127 "-> array size %x dwords\n", risc_size);
7128 if (risc_size == 0 || risc_size == ~0)
7129 goto default_template;
7130
7131 dlen = (risc_size - 8) * sizeof(*dcode);
7132 ql_dbg(ql_dbg_init, vha, 0x0163,
7133 "-> template allocating %x bytes...\n", dlen);
7134 ha->fw_dump_template = vmalloc(dlen);
7135 if (!ha->fw_dump_template) {
7136 ql_log(ql_log_warn, vha, 0x0164,
7137 "Failed fwdump template allocate %x bytes.\n", risc_size);
7138 goto default_template;
7139 }
7140
7141 faddr += 7;
7142 risc_size -= 8;
7143 dcode = ha->fw_dump_template;
7144 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7145 for (i = 0; i < risc_size; i++)
7146 dcode[i] = le32_to_cpu(dcode[i]);
7147
7148 if (!qla27xx_fwdt_template_valid(dcode)) {
7149 ql_log(ql_log_warn, vha, 0x0165,
7150 "Failed fwdump template validate\n");
7151 goto default_template;
7152 }
7153
7154 dlen = qla27xx_fwdt_template_size(dcode);
7155 ql_dbg(ql_dbg_init, vha, 0x0166,
7156 "-> template size %x bytes\n", dlen);
7157 if (dlen > risc_size * sizeof(*dcode)) {
7158 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007159 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007160 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007161 goto default_template;
7162 }
7163 ha->fw_dump_template_len = dlen;
7164 return rval;
7165
7166default_template:
7167 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7168 if (ha->fw_dump_template)
7169 vfree(ha->fw_dump_template);
7170 ha->fw_dump_template = NULL;
7171 ha->fw_dump_template_len = 0;
7172
7173 dlen = qla27xx_fwdt_template_default_size();
7174 ql_dbg(ql_dbg_init, vha, 0x0169,
7175 "-> template allocating %x bytes...\n", dlen);
7176 ha->fw_dump_template = vmalloc(dlen);
7177 if (!ha->fw_dump_template) {
7178 ql_log(ql_log_warn, vha, 0x016a,
7179 "Failed fwdump template allocate %x bytes.\n", risc_size);
7180 goto failed_template;
7181 }
7182
7183 dcode = ha->fw_dump_template;
7184 risc_size = dlen / sizeof(*dcode);
7185 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7186 for (i = 0; i < risc_size; i++)
7187 dcode[i] = be32_to_cpu(dcode[i]);
7188
7189 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7190 ql_log(ql_log_warn, vha, 0x016b,
7191 "Failed fwdump template validate\n");
7192 goto failed_template;
7193 }
7194
7195 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7196 ql_dbg(ql_dbg_init, vha, 0x016c,
7197 "-> template size %x bytes\n", dlen);
7198 ha->fw_dump_template_len = dlen;
7199 return rval;
7200
7201failed_template:
7202 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7203 if (ha->fw_dump_template)
7204 vfree(ha->fw_dump_template);
7205 ha->fw_dump_template = NULL;
7206 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007207 return rval;
7208}
7209
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007210#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007211
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007212int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007213qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007214{
7215 int rval;
7216 int i, fragment;
7217 uint16_t *wcode, *fwcode;
7218 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7219 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007220 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007221 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007222
7223 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007224 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007225 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007226 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007227 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007228 ql_log(ql_log_info, vha, 0x0084,
7229 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007230 return QLA_FUNCTION_FAILED;
7231 }
7232
7233 rval = QLA_SUCCESS;
7234
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007235 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007236 *srisc_addr = 0;
7237 fwcode = (uint16_t *)blob->fw->data;
7238 fwclen = 0;
7239
7240 /* Validate firmware image by checking version. */
7241 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007242 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007243 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007244 blob->fw->size);
7245 goto fail_fw_integrity;
7246 }
7247 for (i = 0; i < 4; i++)
7248 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7249 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7250 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7251 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007252 ql_log(ql_log_fatal, vha, 0x0086,
7253 "Unable to verify integrity of firmware image.\n");
7254 ql_log(ql_log_fatal, vha, 0x0087,
7255 "Firmware data: %04x %04x %04x %04x.\n",
7256 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007257 goto fail_fw_integrity;
7258 }
7259
7260 seg = blob->segs;
7261 while (*seg && rval == QLA_SUCCESS) {
7262 risc_addr = *seg;
7263 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7264 risc_size = be16_to_cpu(fwcode[3]);
7265
7266 /* Validate firmware image size. */
7267 fwclen += risc_size * sizeof(uint16_t);
7268 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007269 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007270 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007271 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007272 goto fail_fw_integrity;
7273 }
7274
7275 fragment = 0;
7276 while (risc_size > 0 && rval == QLA_SUCCESS) {
7277 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7278 if (wlen > risc_size)
7279 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007280 ql_dbg(ql_dbg_init, vha, 0x0089,
7281 "Loading risc segment@ risc addr %x number of "
7282 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007283
7284 for (i = 0; i < wlen; i++)
7285 wcode[i] = swab16(fwcode[i]);
7286
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007287 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007288 wlen);
7289 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007290 ql_log(ql_log_fatal, vha, 0x008a,
7291 "Failed to load segment %d of firmware.\n",
7292 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007293 break;
7294 }
7295
7296 fwcode += wlen;
7297 risc_addr += wlen;
7298 risc_size -= wlen;
7299 fragment++;
7300 }
7301
7302 /* Next segment. */
7303 seg++;
7304 }
7305 return rval;
7306
7307fail_fw_integrity:
7308 return QLA_FUNCTION_FAILED;
7309}
7310
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007311static int
7312qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007313{
7314 int rval;
7315 int segments, fragment;
7316 uint32_t *dcode, dlen;
7317 uint32_t risc_addr;
7318 uint32_t risc_size;
7319 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007320 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007321 const uint32_t *fwcode;
7322 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007323 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007324 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007325
Andrew Vasquez54333832005-11-09 15:49:04 -08007326 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007327 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007328 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007329 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007330 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007331 ql_log(ql_log_warn, vha, 0x0091,
7332 "Firmware images can be retrieved from: "
7333 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007334
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007335 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007336 }
7337
Chad Dupuiscfb09192011-11-18 09:03:07 -08007338 ql_dbg(ql_dbg_init, vha, 0x0092,
7339 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007340
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007341 rval = QLA_SUCCESS;
7342
7343 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007344 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007345 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007346 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007347 fwclen = 0;
7348
7349 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007350 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007351 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007352 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007353 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007354 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007355 }
7356 for (i = 0; i < 4; i++)
7357 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7358 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7359 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7360 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7361 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007362 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007363 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007364 blob->fw->size);
7365 ql_log(ql_log_fatal, vha, 0x0095,
7366 "Firmware data: %08x %08x %08x %08x.\n",
7367 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007368 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007369 }
7370
7371 while (segments && rval == QLA_SUCCESS) {
7372 risc_addr = be32_to_cpu(fwcode[2]);
7373 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7374 risc_size = be32_to_cpu(fwcode[3]);
7375
7376 /* Validate firmware image size. */
7377 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007378 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007379 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007380 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007381 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007382 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007383 }
7384
7385 fragment = 0;
7386 while (risc_size > 0 && rval == QLA_SUCCESS) {
7387 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7388 if (dlen > risc_size)
7389 dlen = risc_size;
7390
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007391 ql_dbg(ql_dbg_init, vha, 0x0097,
7392 "Loading risc segment@ risc addr %x "
7393 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007394
7395 for (i = 0; i < dlen; i++)
7396 dcode[i] = swab32(fwcode[i]);
7397
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007398 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007399 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007400 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007401 ql_log(ql_log_fatal, vha, 0x0098,
7402 "Failed to load segment %d of firmware.\n",
7403 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007404 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007405 }
7406
7407 fwcode += dlen;
7408 risc_addr += dlen;
7409 risc_size -= dlen;
7410 fragment++;
7411 }
7412
7413 /* Next segment. */
7414 segments--;
7415 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007416
7417 if (!IS_QLA27XX(ha))
7418 return rval;
7419
7420 if (ha->fw_dump_template)
7421 vfree(ha->fw_dump_template);
7422 ha->fw_dump_template = NULL;
7423 ha->fw_dump_template_len = 0;
7424
7425 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007426 "Loading fwdump template from %x\n",
7427 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007428 risc_size = be32_to_cpu(fwcode[2]);
7429 ql_dbg(ql_dbg_init, vha, 0x172,
7430 "-> array size %x dwords\n", risc_size);
7431 if (risc_size == 0 || risc_size == ~0)
7432 goto default_template;
7433
7434 dlen = (risc_size - 8) * sizeof(*fwcode);
7435 ql_dbg(ql_dbg_init, vha, 0x0173,
7436 "-> template allocating %x bytes...\n", dlen);
7437 ha->fw_dump_template = vmalloc(dlen);
7438 if (!ha->fw_dump_template) {
7439 ql_log(ql_log_warn, vha, 0x0174,
7440 "Failed fwdump template allocate %x bytes.\n", risc_size);
7441 goto default_template;
7442 }
7443
7444 fwcode += 7;
7445 risc_size -= 8;
7446 dcode = ha->fw_dump_template;
7447 for (i = 0; i < risc_size; i++)
7448 dcode[i] = le32_to_cpu(fwcode[i]);
7449
7450 if (!qla27xx_fwdt_template_valid(dcode)) {
7451 ql_log(ql_log_warn, vha, 0x0175,
7452 "Failed fwdump template validate\n");
7453 goto default_template;
7454 }
7455
7456 dlen = qla27xx_fwdt_template_size(dcode);
7457 ql_dbg(ql_dbg_init, vha, 0x0176,
7458 "-> template size %x bytes\n", dlen);
7459 if (dlen > risc_size * sizeof(*fwcode)) {
7460 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007461 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007462 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007463 goto default_template;
7464 }
7465 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007466 return rval;
7467
Chad Dupuisf73cb692014-02-26 04:15:06 -05007468default_template:
7469 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7470 if (ha->fw_dump_template)
7471 vfree(ha->fw_dump_template);
7472 ha->fw_dump_template = NULL;
7473 ha->fw_dump_template_len = 0;
7474
7475 dlen = qla27xx_fwdt_template_default_size();
7476 ql_dbg(ql_dbg_init, vha, 0x0179,
7477 "-> template allocating %x bytes...\n", dlen);
7478 ha->fw_dump_template = vmalloc(dlen);
7479 if (!ha->fw_dump_template) {
7480 ql_log(ql_log_warn, vha, 0x017a,
7481 "Failed fwdump template allocate %x bytes.\n", risc_size);
7482 goto failed_template;
7483 }
7484
7485 dcode = ha->fw_dump_template;
7486 risc_size = dlen / sizeof(*fwcode);
7487 fwcode = qla27xx_fwdt_template_default();
7488 for (i = 0; i < risc_size; i++)
7489 dcode[i] = be32_to_cpu(fwcode[i]);
7490
7491 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7492 ql_log(ql_log_warn, vha, 0x017b,
7493 "Failed fwdump template validate\n");
7494 goto failed_template;
7495 }
7496
7497 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7498 ql_dbg(ql_dbg_init, vha, 0x017c,
7499 "-> template size %x bytes\n", dlen);
7500 ha->fw_dump_template_len = dlen;
7501 return rval;
7502
7503failed_template:
7504 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7505 if (ha->fw_dump_template)
7506 vfree(ha->fw_dump_template);
7507 ha->fw_dump_template = NULL;
7508 ha->fw_dump_template_len = 0;
7509 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007510}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007511
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007512int
7513qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7514{
7515 int rval;
7516
Andrew Vasqueze337d902009-04-06 22:33:49 -07007517 if (ql2xfwloadbin == 1)
7518 return qla81xx_load_risc(vha, srisc_addr);
7519
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007520 /*
7521 * FW Load priority:
7522 * 1) Firmware via request-firmware interface (.bin file).
7523 * 2) Firmware residing in flash.
7524 */
7525 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7526 if (rval == QLA_SUCCESS)
7527 return rval;
7528
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007529 return qla24xx_load_risc_flash(vha, srisc_addr,
7530 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007531}
7532
7533int
7534qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7535{
7536 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007537 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007538
Andrew Vasqueze337d902009-04-06 22:33:49 -07007539 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007540 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007541
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007542 /*
7543 * FW Load priority:
7544 * 1) Firmware residing in flash.
7545 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007546 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007547 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007548 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007549 if (rval == QLA_SUCCESS)
7550 return rval;
7551
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007552try_blob_fw:
7553 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7554 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7555 return rval;
7556
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007557 ql_log(ql_log_info, vha, 0x0099,
7558 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007559 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7560 if (rval != QLA_SUCCESS)
7561 return rval;
7562
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007563 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007564 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007565 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007566}
7567
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007568void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007569qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007570{
7571 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007572 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007573
Andrew Vasquez85880802009-12-15 21:29:46 -08007574 if (ha->flags.pci_channel_io_perm_failure)
7575 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007576 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007577 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007578 if (!ha->fw_major_version)
7579 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007580 if (!ha->flags.fw_started)
7581 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007582
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007583 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007584 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007585 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007586 ha->isp_ops->reset_chip(vha);
7587 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007588 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007589 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007590 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007591 ql_log(ql_log_info, vha, 0x8015,
7592 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007593 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007594 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007595
Quinn Tran4b60c822017-06-13 20:47:21 -07007596 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007597 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007598}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007599
7600int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007601qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007602{
7603 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007604 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007605 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007606 struct qla_hw_data *ha = vha->hw;
7607 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007608 struct req_que *req;
7609 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007610
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007611 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007612 return -EINVAL;
7613
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007614 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007615 if (vha->qpair)
7616 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007617 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007618 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007619 rsp = req->rsp;
7620
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007621 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007622 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007623 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007624 }
7625
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007626 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007627
7628 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007629 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7630 BIT_1);
7631 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7632 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7633 ql_dbg(ql_dbg_init, vha, 0x0120,
7634 "Failed SNS login: loop_id=%x, rval2=%d\n",
7635 NPH_SNS, rval2);
7636 else
7637 ql_dbg(ql_dbg_init, vha, 0x0103,
7638 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7639 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7640 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007641 return (QLA_FUNCTION_FAILED);
7642 }
7643
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007644 atomic_set(&vha->loop_down_timer, 0);
7645 atomic_set(&vha->loop_state, LOOP_UP);
7646 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7647 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7648 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007649
7650 return rval;
7651}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007652
7653/* 84XX Support **************************************************************/
7654
7655static LIST_HEAD(qla_cs84xx_list);
7656static DEFINE_MUTEX(qla_cs84xx_mutex);
7657
7658static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007659qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007660{
7661 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007662 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007663
7664 mutex_lock(&qla_cs84xx_mutex);
7665
7666 /* Find any shared 84xx chip. */
7667 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7668 if (cs84xx->bus == ha->pdev->bus) {
7669 kref_get(&cs84xx->kref);
7670 goto done;
7671 }
7672 }
7673
7674 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7675 if (!cs84xx)
7676 goto done;
7677
7678 kref_init(&cs84xx->kref);
7679 spin_lock_init(&cs84xx->access_lock);
7680 mutex_init(&cs84xx->fw_update_mutex);
7681 cs84xx->bus = ha->pdev->bus;
7682
7683 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7684done:
7685 mutex_unlock(&qla_cs84xx_mutex);
7686 return cs84xx;
7687}
7688
7689static void
7690__qla84xx_chip_release(struct kref *kref)
7691{
7692 struct qla_chip_state_84xx *cs84xx =
7693 container_of(kref, struct qla_chip_state_84xx, kref);
7694
7695 mutex_lock(&qla_cs84xx_mutex);
7696 list_del(&cs84xx->list);
7697 mutex_unlock(&qla_cs84xx_mutex);
7698 kfree(cs84xx);
7699}
7700
7701void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007702qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007703{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007704 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007705 if (ha->cs84xx)
7706 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7707}
7708
7709static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007710qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007711{
7712 int rval;
7713 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007714 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007715
7716 mutex_lock(&ha->cs84xx->fw_update_mutex);
7717
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007718 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007719
7720 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7721
7722 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7723 QLA_SUCCESS;
7724}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007725
7726/* 81XX Support **************************************************************/
7727
7728int
7729qla81xx_nvram_config(scsi_qla_host_t *vha)
7730{
7731 int rval;
7732 struct init_cb_81xx *icb;
7733 struct nvram_81xx *nv;
7734 uint32_t *dptr;
7735 uint8_t *dptr1, *dptr2;
7736 uint32_t chksum;
7737 uint16_t cnt;
7738 struct qla_hw_data *ha = vha->hw;
7739
7740 rval = QLA_SUCCESS;
7741 icb = (struct init_cb_81xx *)ha->init_cb;
7742 nv = ha->nvram;
7743
7744 /* Determine NVRAM starting address. */
7745 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007746 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007747 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7748 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007749
7750 /* Get VPD data into cache */
7751 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007752 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7753 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007754
7755 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007756 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007757 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007758 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007759 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7760 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007761
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007762 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7763 "Contents of NVRAM:\n");
7764 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7765 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007766
7767 /* Bad NVRAM data, set defaults parameters. */
7768 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7769 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007770 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007771 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007772 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007773 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007774 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007775 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007776 ql_log(ql_log_info, vha, 0x0074,
7777 "Falling back to functioning (yet invalid -- WWPN) "
7778 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007779
7780 /*
7781 * Set default initialization control block.
7782 */
7783 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007784 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7785 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007786 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007787 nv->execution_throttle = cpu_to_le16(0xFFFF);
7788 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007789 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007790 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007791 nv->port_name[2] = 0x00;
7792 nv->port_name[3] = 0xe0;
7793 nv->port_name[4] = 0x8b;
7794 nv->port_name[5] = 0x1c;
7795 nv->port_name[6] = 0x55;
7796 nv->port_name[7] = 0x86;
7797 nv->node_name[0] = 0x20;
7798 nv->node_name[1] = 0x00;
7799 nv->node_name[2] = 0x00;
7800 nv->node_name[3] = 0xe0;
7801 nv->node_name[4] = 0x8b;
7802 nv->node_name[5] = 0x1c;
7803 nv->node_name[6] = 0x55;
7804 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007805 nv->login_retry_count = cpu_to_le16(8);
7806 nv->interrupt_delay_timer = cpu_to_le16(0);
7807 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007808 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007809 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7810 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7811 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7812 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7813 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7814 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007815 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007816 nv->max_luns_per_target = cpu_to_le16(128);
7817 nv->port_down_retry_count = cpu_to_le16(30);
7818 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007819 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007820 nv->enode_mac[1] = 0xC0;
7821 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007822 nv->enode_mac[3] = 0x04;
7823 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007824 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007825
7826 rval = 1;
7827 }
7828
Arun Easi9e522cd2012-08-22 14:21:31 -04007829 if (IS_T10_PI_CAPABLE(ha))
7830 nv->frame_payload_size &= ~7;
7831
Arun Easiaa230bc2013-01-30 03:34:39 -05007832 qlt_81xx_config_nvram_stage1(vha, nv);
7833
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007834 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007835 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007836
7837 /* Copy 1st segment. */
7838 dptr1 = (uint8_t *)icb;
7839 dptr2 = (uint8_t *)&nv->version;
7840 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7841 while (cnt--)
7842 *dptr1++ = *dptr2++;
7843
7844 icb->login_retry_count = nv->login_retry_count;
7845
7846 /* Copy 2nd segment. */
7847 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7848 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7849 cnt = (uint8_t *)&icb->reserved_5 -
7850 (uint8_t *)&icb->interrupt_delay_timer;
7851 while (cnt--)
7852 *dptr1++ = *dptr2++;
7853
7854 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7855 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7856 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007857 icb->enode_mac[0] = 0x00;
7858 icb->enode_mac[1] = 0xC0;
7859 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007860 icb->enode_mac[3] = 0x04;
7861 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007862 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007863 }
7864
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007865 /* Use extended-initialization control block. */
7866 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7867
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007868 /*
7869 * Setup driver NVRAM options.
7870 */
7871 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007872 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007873
Arun Easiaa230bc2013-01-30 03:34:39 -05007874 qlt_81xx_config_nvram_stage2(vha, icb);
7875
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007876 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007877 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007878 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7879 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7880 }
7881
7882 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007883 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007884 /*
7885 * Firmware will apply the following mask if the nodename was
7886 * not provided.
7887 */
7888 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7889 icb->node_name[0] &= 0xF0;
7890 }
7891
7892 /* Set host adapter parameters. */
7893 ha->flags.disable_risc_code_load = 0;
7894 ha->flags.enable_lip_reset = 0;
7895 ha->flags.enable_lip_full_login =
7896 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7897 ha->flags.enable_target_reset =
7898 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7899 ha->flags.enable_led_scheme = 0;
7900 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7901
7902 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7903 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7904
7905 /* save HBA serial number */
7906 ha->serial0 = icb->port_name[5];
7907 ha->serial1 = icb->port_name[6];
7908 ha->serial2 = icb->port_name[7];
7909 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7910 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7911
Bart Van Asschead950362015-07-09 07:24:08 -07007912 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007913
7914 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7915
7916 /* Set minimum login_timeout to 4 seconds. */
7917 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7918 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7919 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007920 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007921 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007922
7923 /* Set minimum RATOV to 100 tenths of a second. */
7924 ha->r_a_tov = 100;
7925
7926 ha->loop_reset_delay = nv->reset_delay;
7927
7928 /* Link Down Timeout = 0:
7929 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007930 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007931 * I/O's to OS with "DID_NO_CONNECT".
7932 *
7933 * Link Down Timeout != 0:
7934 *
7935 * The driver waits for the link to come up after link down
7936 * before returning I/Os to OS with "DID_NO_CONNECT".
7937 */
7938 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7939 ha->loop_down_abort_time =
7940 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7941 } else {
7942 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7943 ha->loop_down_abort_time =
7944 (LOOP_DOWN_TIME - ha->link_down_timeout);
7945 }
7946
7947 /* Need enough time to try and get the port back. */
7948 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7949 if (qlport_down_retry)
7950 ha->port_down_retry_count = qlport_down_retry;
7951
7952 /* Set login_retry_count */
7953 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7954 if (ha->port_down_retry_count ==
7955 le16_to_cpu(nv->port_down_retry_count) &&
7956 ha->port_down_retry_count > 3)
7957 ha->login_retry_count = ha->port_down_retry_count;
7958 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7959 ha->login_retry_count = ha->port_down_retry_count;
7960 if (ql2xloginretrycount)
7961 ha->login_retry_count = ql2xloginretrycount;
7962
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007963 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007964 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007965 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007966
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007967 /* Enable ZIO. */
7968 if (!vha->flags.init_done) {
7969 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7970 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7971 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7972 le16_to_cpu(icb->interrupt_delay_timer): 2;
7973 }
Bart Van Asschead950362015-07-09 07:24:08 -07007974 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007975 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7976 vha->flags.process_response_queue = 0;
7977 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7978 ha->zio_mode = QLA_ZIO_MODE_6;
7979
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007980 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007981 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007982 ha->zio_mode,
7983 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007984
7985 icb->firmware_options_2 |= cpu_to_le32(
7986 (uint32_t)ha->zio_mode);
7987 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7988 vha->flags.process_response_queue = 1;
7989 }
7990
Quinn Tran41dc5292017-01-19 22:28:03 -08007991 /* enable RIDA Format2 */
7992 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7993 icb->firmware_options_3 |= BIT_0;
7994
Duane Grigsbyedd05de2017-10-13 09:34:06 -07007995 if (IS_QLA27XX(ha)) {
7996 icb->firmware_options_3 |= BIT_8;
7997 ql_dbg(ql_log_info, vha, 0x0075,
7998 "Enabling direct connection.\n");
7999 }
8000
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008001 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008002 ql_log(ql_log_warn, vha, 0x0076,
8003 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008004 }
8005 return (rval);
8006}
8007
Giridhar Malavalia9083012010-04-12 17:59:55 -07008008int
8009qla82xx_restart_isp(scsi_qla_host_t *vha)
8010{
8011 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008012 struct qla_hw_data *ha = vha->hw;
8013 struct req_que *req = ha->req_q_map[0];
8014 struct rsp_que *rsp = ha->rsp_q_map[0];
8015 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008016 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008017
8018 status = qla2x00_init_rings(vha);
8019 if (!status) {
8020 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8021 ha->flags.chip_reset_done = 1;
8022
8023 status = qla2x00_fw_ready(vha);
8024 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008025 /* Issue a marker after FW becomes ready. */
8026 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008027 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008028 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008029 }
8030
8031 /* if no cable then assume it's good */
8032 if ((vha->device_flags & DFLG_NO_CABLE))
8033 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008034 }
8035
8036 if (!status) {
8037 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8038
8039 if (!atomic_read(&vha->loop_down_timer)) {
8040 /*
8041 * Issue marker command only when we are going
8042 * to start the I/O .
8043 */
8044 vha->marker_needed = 1;
8045 }
8046
Giridhar Malavalia9083012010-04-12 17:59:55 -07008047 ha->isp_ops->enable_intrs(ha);
8048
8049 ha->isp_abort_cnt = 0;
8050 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8051
Saurav Kashyap53296782011-05-10 11:30:06 -07008052 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008053 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008054
Giridhar Malavalia9083012010-04-12 17:59:55 -07008055 if (ha->fce) {
8056 ha->flags.fce_enabled = 1;
8057 memset(ha->fce, 0,
8058 fce_calc_size(ha->fce_bufs));
8059 rval = qla2x00_enable_fce_trace(vha,
8060 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8061 &ha->fce_bufs);
8062 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008063 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008064 "Unable to reinitialize FCE (%d).\n",
8065 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008066 ha->flags.fce_enabled = 0;
8067 }
8068 }
8069
8070 if (ha->eft) {
8071 memset(ha->eft, 0, EFT_SIZE);
8072 rval = qla2x00_enable_eft_trace(vha,
8073 ha->eft_dma, EFT_NUM_BUFFERS);
8074 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008075 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008076 "Unable to reinitialize EFT (%d).\n",
8077 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008078 }
8079 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008080 }
8081
8082 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008083 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008084 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008085
8086 spin_lock_irqsave(&ha->vport_slock, flags);
8087 list_for_each_entry(vp, &ha->vp_list, list) {
8088 if (vp->vp_idx) {
8089 atomic_inc(&vp->vref_count);
8090 spin_unlock_irqrestore(&ha->vport_slock, flags);
8091
Giridhar Malavalia9083012010-04-12 17:59:55 -07008092 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008093
8094 spin_lock_irqsave(&ha->vport_slock, flags);
8095 atomic_dec(&vp->vref_count);
8096 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008097 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008098 spin_unlock_irqrestore(&ha->vport_slock, flags);
8099
Giridhar Malavalia9083012010-04-12 17:59:55 -07008100 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008101 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008102 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008103 }
8104
8105 return status;
8106}
8107
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008108void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008109qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008110{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008111 struct qla_hw_data *ha = vha->hw;
8112
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008113 /* Hold status IOCBs until ABTS response received. */
8114 if (ql2xfwholdabts)
8115 ha->fw_options[3] |= BIT_12;
8116
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008117 /* Set Retry FLOGI in case of P2P connection */
8118 if (ha->operating_mode == P2P) {
8119 ha->fw_options[2] |= BIT_3;
8120 ql_dbg(ql_dbg_disc, vha, 0x2103,
8121 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8122 __func__, ha->fw_options[2]);
8123 }
8124
Quinn Tran41dc5292017-01-19 22:28:03 -08008125 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8126 if (ql2xmvasynctoatio) {
8127 if (qla_tgt_mode_enabled(vha) ||
8128 qla_dual_mode_enabled(vha))
8129 ha->fw_options[2] |= BIT_11;
8130 else
8131 ha->fw_options[2] &= ~BIT_11;
8132 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008133
Quinn Tranf7e761f2017-06-02 09:12:02 -07008134 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008135 qla_dual_mode_enabled(vha)) {
8136 /* FW auto send SCSI status during */
8137 ha->fw_options[1] |= BIT_8;
8138 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8139
8140 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008141 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008142 } else {
8143 ha->fw_options[1] &= ~BIT_8;
8144 ha->fw_options[10] &= 0x00ff;
8145
Quinn Tranf7e761f2017-06-02 09:12:02 -07008146 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008147 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008148
Quinn Tran41dc5292017-01-19 22:28:03 -08008149 if (ql2xetsenable) {
8150 /* Enable ETS Burst. */
8151 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8152 ha->fw_options[2] |= BIT_9;
8153 }
8154
Quinn Tran83548fe2017-06-02 09:12:01 -07008155 ql_dbg(ql_dbg_init, vha, 0x00e9,
8156 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8157 __func__, ha->fw_options[1], ha->fw_options[2],
8158 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008159
Andrew Vasquezae97c912010-02-18 10:07:28 -08008160 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008161}
Sarang Radke09ff7012010-03-19 17:03:59 -07008162
8163/*
8164 * qla24xx_get_fcp_prio
8165 * Gets the fcp cmd priority value for the logged in port.
8166 * Looks for a match of the port descriptors within
8167 * each of the fcp prio config entries. If a match is found,
8168 * the tag (priority) value is returned.
8169 *
8170 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008171 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008172 * fcport = port structure pointer.
8173 *
8174 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008175 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008176 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008177 *
8178 * Context:
8179 * Kernel context
8180 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008181static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008182qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8183{
8184 int i, entries;
8185 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008186 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008187 uint32_t pid1, pid2;
8188 uint64_t wwn1, wwn2;
8189 struct qla_fcp_prio_entry *pri_entry;
8190 struct qla_hw_data *ha = vha->hw;
8191
8192 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008193 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008194
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008195 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008196 entries = ha->fcp_prio_cfg->num_entries;
8197 pri_entry = &ha->fcp_prio_cfg->entry[0];
8198
8199 for (i = 0; i < entries; i++) {
8200 pid_match = wwn_match = 0;
8201
8202 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8203 pri_entry++;
8204 continue;
8205 }
8206
8207 /* check source pid for a match */
8208 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8209 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8210 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8211 if (pid1 == INVALID_PORT_ID)
8212 pid_match++;
8213 else if (pid1 == pid2)
8214 pid_match++;
8215 }
8216
8217 /* check destination pid for a match */
8218 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8219 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8220 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8221 if (pid1 == INVALID_PORT_ID)
8222 pid_match++;
8223 else if (pid1 == pid2)
8224 pid_match++;
8225 }
8226
8227 /* check source WWN for a match */
8228 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8229 wwn1 = wwn_to_u64(vha->port_name);
8230 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8231 if (wwn2 == (uint64_t)-1)
8232 wwn_match++;
8233 else if (wwn1 == wwn2)
8234 wwn_match++;
8235 }
8236
8237 /* check destination WWN for a match */
8238 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8239 wwn1 = wwn_to_u64(fcport->port_name);
8240 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8241 if (wwn2 == (uint64_t)-1)
8242 wwn_match++;
8243 else if (wwn1 == wwn2)
8244 wwn_match++;
8245 }
8246
8247 if (pid_match == 2 || wwn_match == 2) {
8248 /* Found a matching entry */
8249 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8250 priority = pri_entry->tag;
8251 break;
8252 }
8253
8254 pri_entry++;
8255 }
8256
8257 return priority;
8258}
8259
8260/*
8261 * qla24xx_update_fcport_fcp_prio
8262 * Activates fcp priority for the logged in fc port
8263 *
8264 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008265 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008266 * fcp = port structure pointer.
8267 *
8268 * Return:
8269 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8270 *
8271 * Context:
8272 * Kernel context.
8273 */
8274int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008275qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008276{
8277 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008278 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008279 uint16_t mb[5];
8280
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008281 if (fcport->port_type != FCT_TARGET ||
8282 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008283 return QLA_FUNCTION_FAILED;
8284
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008285 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008286 if (priority < 0)
8287 return QLA_FUNCTION_FAILED;
8288
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008289 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008290 fcport->fcp_prio = priority & 0xf;
8291 return QLA_SUCCESS;
8292 }
8293
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008294 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008295 if (ret == QLA_SUCCESS) {
8296 if (fcport->fcp_prio != priority)
8297 ql_dbg(ql_dbg_user, vha, 0x709e,
8298 "Updated FCP_CMND priority - value=%d loop_id=%d "
8299 "port_id=%02x%02x%02x.\n", priority,
8300 fcport->loop_id, fcport->d_id.b.domain,
8301 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008302 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008303 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008304 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008305 "Unable to update FCP_CMND priority - ret=0x%x for "
8306 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8307 fcport->d_id.b.domain, fcport->d_id.b.area,
8308 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008309 return ret;
8310}
8311
8312/*
8313 * qla24xx_update_all_fcp_prio
8314 * Activates fcp priority for all the logged in ports
8315 *
8316 * Input:
8317 * ha = adapter block pointer.
8318 *
8319 * Return:
8320 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8321 *
8322 * Context:
8323 * Kernel context.
8324 */
8325int
8326qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8327{
8328 int ret;
8329 fc_port_t *fcport;
8330
8331 ret = QLA_FUNCTION_FAILED;
8332 /* We need to set priority for all logged in ports */
8333 list_for_each_entry(fcport, &vha->vp_fcports, list)
8334 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8335
8336 return ret;
8337}
Michael Hernandezd7459522016-12-12 14:40:07 -08008338
Quinn Tran82de8022017-06-13 20:47:17 -07008339struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8340 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008341{
8342 int rsp_id = 0;
8343 int req_id = 0;
8344 int i;
8345 struct qla_hw_data *ha = vha->hw;
8346 uint16_t qpair_id = 0;
8347 struct qla_qpair *qpair = NULL;
8348 struct qla_msix_entry *msix;
8349
8350 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8351 ql_log(ql_log_warn, vha, 0x00181,
8352 "FW/Driver is not multi-queue capable.\n");
8353 return NULL;
8354 }
8355
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008356 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008357 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8358 if (qpair == NULL) {
8359 ql_log(ql_log_warn, vha, 0x0182,
8360 "Failed to allocate memory for queue pair.\n");
8361 return NULL;
8362 }
8363 memset(qpair, 0, sizeof(struct qla_qpair));
8364
8365 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008366 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008367 qpair->qp_lock_ptr = &qpair->qp_lock;
8368 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008369 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008370
8371 /* Assign available que pair id */
8372 mutex_lock(&ha->mq_lock);
8373 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008374 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008375 mutex_unlock(&ha->mq_lock);
8376 ql_log(ql_log_warn, vha, 0x0183,
8377 "No resources to create additional q pair.\n");
8378 goto fail_qid_map;
8379 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008380 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008381 set_bit(qpair_id, ha->qpair_qid_map);
8382 ha->queue_pair_map[qpair_id] = qpair;
8383 qpair->id = qpair_id;
8384 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008385 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008386 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008387 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008388 qpair->chip_reset = ha->base_qpair->chip_reset;
8389 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8390 qpair->enable_explicit_conf =
8391 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008392
8393 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008394 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008395 if (msix->in_use)
8396 continue;
8397 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008398 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008399 "Vector %x selected for qpair\n", msix->vector);
8400 break;
8401 }
8402 if (!qpair->msix) {
8403 ql_log(ql_log_warn, vha, 0x0184,
8404 "Out of MSI-X vectors!.\n");
8405 goto fail_msix;
8406 }
8407
8408 qpair->msix->in_use = 1;
8409 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008410 qpair->pdev = ha->pdev;
8411 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8412 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008413
8414 mutex_unlock(&ha->mq_lock);
8415
8416 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008417 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008418 if (!rsp_id) {
8419 ql_log(ql_log_warn, vha, 0x0185,
8420 "Failed to create response queue.\n");
8421 goto fail_rsp;
8422 }
8423
8424 qpair->rsp = ha->rsp_q_map[rsp_id];
8425
8426 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008427 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8428 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008429 if (!req_id) {
8430 ql_log(ql_log_warn, vha, 0x0186,
8431 "Failed to create request queue.\n");
8432 goto fail_req;
8433 }
8434
8435 qpair->req = ha->req_q_map[req_id];
8436 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008437 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008438 /* init qpair to this cpu. Will adjust at run time. */
8439 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008440
8441 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8442 if (ha->fw_attributes & BIT_4)
8443 qpair->difdix_supported = 1;
8444 }
8445
8446 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8447 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008448 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008449 "Failed to create srb mempool for qpair %d\n",
8450 qpair->id);
8451 goto fail_mempool;
8452 }
8453
8454 /* Mark as online */
8455 qpair->online = 1;
8456
8457 if (!vha->flags.qpairs_available)
8458 vha->flags.qpairs_available = 1;
8459
8460 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8461 "Request/Response queue pair created, id %d\n",
8462 qpair->id);
8463 ql_dbg(ql_dbg_init, vha, 0x0187,
8464 "Request/Response queue pair created, id %d\n",
8465 qpair->id);
8466 }
8467 return qpair;
8468
8469fail_mempool:
8470fail_req:
8471 qla25xx_delete_rsp_que(vha, qpair->rsp);
8472fail_rsp:
8473 mutex_lock(&ha->mq_lock);
8474 qpair->msix->in_use = 0;
8475 list_del(&qpair->qp_list_elem);
8476 if (list_empty(&vha->qp_list))
8477 vha->flags.qpairs_available = 0;
8478fail_msix:
8479 ha->queue_pair_map[qpair_id] = NULL;
8480 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008481 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008482 mutex_unlock(&ha->mq_lock);
8483fail_qid_map:
8484 kfree(qpair);
8485 return NULL;
8486}
8487
8488int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8489{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008490 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008491 struct qla_hw_data *ha = qpair->hw;
8492
8493 qpair->delete_in_progress = 1;
8494 while (atomic_read(&qpair->ref_count))
8495 msleep(500);
8496
8497 ret = qla25xx_delete_req_que(vha, qpair->req);
8498 if (ret != QLA_SUCCESS)
8499 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008500
Michael Hernandezd7459522016-12-12 14:40:07 -08008501 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8502 if (ret != QLA_SUCCESS)
8503 goto fail;
8504
8505 mutex_lock(&ha->mq_lock);
8506 ha->queue_pair_map[qpair->id] = NULL;
8507 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008508 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008509 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008510 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008511 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008512 vha->flags.qpairs_req_created = 0;
8513 vha->flags.qpairs_rsp_created = 0;
8514 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008515 mempool_destroy(qpair->srb_mempool);
8516 kfree(qpair);
8517 mutex_unlock(&ha->mq_lock);
8518
8519 return QLA_SUCCESS;
8520fail:
8521 return ret;
8522}