blob: 7f8bfa0454d2d8b8bc0b8ecb5975195a86fc135f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
Armen Baloyanbd21eaf2014-04-11 16:54:24 -04003 * Copyright (c) 2003-2014 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
Anirban Chakraborty73208df2008-12-09 16:45:39 -08008#include "qla_gbl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Andrew Vasquez0107109e2005-07-06 10:31:37 -070012#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include "qla_devtbl.h"
15
David Miller4e08df32007-04-16 12:37:43 -070016#ifdef CONFIG_SPARC
17#include <asm/prom.h>
David Miller4e08df32007-04-16 12:37:43 -070018#endif
19
Nicholas Bellinger2d70c102012-05-15 14:34:28 -040020#include <target/target_core_base.h>
21#include "qla_target.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24* QLogic ISP2x00 Hardware Support Function Prototypes.
25*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int qla2x00_isp_firmware(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int qla2x00_setup_chip(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int qla2x00_fw_ready(scsi_qla_host_t *);
29static int qla2x00_configure_hba(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static int qla2x00_configure_loop(scsi_qla_host_t *);
31static int qla2x00_configure_local_loop(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int qla2x00_configure_fabric(scsi_qla_host_t *);
Quinn Tran726b8542017-01-19 22:28:00 -080033static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int qla2x00_restart_isp(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Harihara Kadayam4d4df192008-04-03 13:13:26 -070036static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
37static int qla84xx_init_chip(scsi_qla_host_t *);
Anirban Chakraborty73208df2008-12-09 16:45:39 -080038static int qla25xx_init_queues(struct qla_hw_data *);
Duane Grigsbya5d42f42017-06-21 13:48:41 -070039static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
Quinn Tran726b8542017-01-19 22:28:00 -080040static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41 struct event_arg *);
Duane Grigsbya5d42f42017-06-21 13:48:41 -070042static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
43 struct event_arg *);
Quinn Trana4239942017-12-28 12:33:26 -080044static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
Harihara Kadayam4d4df192008-04-03 13:13:26 -070045
Andrew Vasquezac280b62009-08-20 11:06:05 -070046/* SRB Extensions ---------------------------------------------------------- */
47
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080048void
Kees Cook8e5f4ba2017-09-03 13:23:32 -070049qla2x00_sp_timeout(struct timer_list *t)
Andrew Vasquezac280b62009-08-20 11:06:05 -070050{
Kees Cook8e5f4ba2017-09-03 13:23:32 -070051 srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
Madhuranath Iyengar49163922010-05-04 15:01:28 -070052 struct srb_iocb *iocb;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080053 scsi_qla_host_t *vha = sp->vha;
Andrew Vasquezac280b62009-08-20 11:06:05 -070054 struct req_que *req;
55 unsigned long flags;
56
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080057 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
58 req = vha->hw->req_q_map[0];
Andrew Vasquezac280b62009-08-20 11:06:05 -070059 req->outstanding_cmds[sp->handle] = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080060 iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar49163922010-05-04 15:01:28 -070061 iocb->timeout(sp);
Giridhar Malavali045d6ea2017-12-28 12:33:21 -080062 if (sp->type != SRB_ELS_DCMD)
63 sp->free(sp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080064 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -070065}
66
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080067void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080068qla2x00_sp_free(void *ptr)
Andrew Vasquezac280b62009-08-20 11:06:05 -070069{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080070 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080071 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -070072
Chad Dupuis4d97cc52010-10-15 11:27:41 -070073 del_timer(&iocb->timer);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080074 qla2x00_rel_sp(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -070075}
76
Andrew Vasquezac280b62009-08-20 11:06:05 -070077/* Asynchronous Login/Logout Routines -------------------------------------- */
78
Saurav Kashyapa9b6f722012-08-22 14:21:01 -040079unsigned long
Andrew Vasquez5b914902010-05-28 15:08:30 -070080qla2x00_get_async_timeout(struct scsi_qla_host *vha)
81{
82 unsigned long tmo;
83 struct qla_hw_data *ha = vha->hw;
84
85 /* Firmware should use switch negotiated r_a_tov for timeout. */
86 tmo = ha->r_a_tov / 10 * 2;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040087 if (IS_QLAFX00(ha)) {
88 tmo = FX00_DEF_RATOV * 2;
89 } else if (!IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez5b914902010-05-28 15:08:30 -070090 /*
91 * Except for earlier ISPs where the timeout is seeded from the
92 * initialization control block.
93 */
94 tmo = ha->login_timeout;
95 }
96 return tmo;
97}
Andrew Vasquezac280b62009-08-20 11:06:05 -070098
Quinn Tran726b8542017-01-19 22:28:00 -080099void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800100qla2x00_async_iocb_timeout(void *data)
Andrew Vasquezac280b62009-08-20 11:06:05 -0700101{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800102 srb_t *sp = data;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700103 fc_port_t *fcport = sp->fcport;
Quinn Tran726b8542017-01-19 22:28:00 -0800104 struct srb_iocb *lio = &sp->u.iocb_cmd;
105 struct event_arg ea;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700106
Quinn Tran5c25d452017-12-28 12:33:09 -0800107 if (fcport) {
108 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
109 "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
110 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700111
Quinn Tran6d6749272017-12-28 12:33:41 -0800112 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran5c25d452017-12-28 12:33:09 -0800113 } else {
114 pr_info("Async-%s timeout - hdl=%x.\n",
115 sp->name, sp->handle);
116 }
Quinn Tran726b8542017-01-19 22:28:00 -0800117
118 switch (sp->type) {
119 case SRB_LOGIN_CMD:
himanshu.madhani@cavium.com7ac0c332018-01-15 20:46:48 -0800120 if (!fcport)
121 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700122 /* Retry as needed. */
123 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
124 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
125 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800126 memset(&ea, 0, sizeof(ea));
127 ea.event = FCME_PLOGI_DONE;
128 ea.fcport = sp->fcport;
129 ea.data[0] = lio->u.logio.data[0];
130 ea.data[1] = lio->u.logio.data[1];
131 ea.sp = sp;
132 qla24xx_handle_plogi_done_event(fcport->vha, &ea);
133 break;
134 case SRB_LOGOUT_CMD:
himanshu.madhani@cavium.com7ac0c332018-01-15 20:46:48 -0800135 if (!fcport)
136 break;
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400137 qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800138 break;
139 case SRB_CT_PTHRU_CMD:
140 case SRB_MB_IOCB:
141 case SRB_NACK_PLOGI:
142 case SRB_NACK_PRLI:
143 case SRB_NACK_LOGO:
Quinn Tran28531922017-12-28 12:33:10 -0800144 case SRB_CTRL_VP:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800145 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800146 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700147 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700148}
149
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700150static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800151qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700152{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800153 srb_t *sp = ptr;
154 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800155 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800156 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700157
Quinn Tran83548fe2017-06-02 09:12:01 -0700158 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800159 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800160
Quinn Tran6d6749272017-12-28 12:33:41 -0800161 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
162
Quinn Tran726b8542017-01-19 22:28:00 -0800163 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
164 memset(&ea, 0, sizeof(ea));
165 ea.event = FCME_PLOGI_DONE;
166 ea.fcport = sp->fcport;
167 ea.data[0] = lio->u.logio.data[0];
168 ea.data[1] = lio->u.logio.data[1];
169 ea.iop[0] = lio->u.logio.iop[0];
170 ea.iop[1] = lio->u.logio.iop[1];
171 ea.sp = sp;
172 qla2x00_fcport_event_handler(vha, &ea);
173 }
174
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800175 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700176}
177
Andrew Vasquezac280b62009-08-20 11:06:05 -0700178int
179qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
180 uint16_t *data)
181{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700182 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700183 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800184 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700185
Quinn Tran726b8542017-01-19 22:28:00 -0800186 if (!vha->flags.online)
187 goto done;
188
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800189 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700190 if (!sp)
191 goto done;
192
Quinn Tran726b8542017-01-19 22:28:00 -0800193 fcport->flags |= FCF_ASYNC_SENT;
194 fcport->logout_completed = 0;
195
Quinn Trana4239942017-12-28 12:33:26 -0800196 fcport->disc_state = DSC_LOGIN_PEND;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800197 sp->type = SRB_LOGIN_CMD;
198 sp->name = "login";
Quinn Trana4239942017-12-28 12:33:26 -0800199 sp->gen1 = fcport->rscn_gen;
200 sp->gen2 = fcport->login_gen;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800201 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
202
203 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700204 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800205 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700206 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700207
208 if (fcport->fc4f_nvme)
209 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
210
Andrew Vasquezac280b62009-08-20 11:06:05 -0700211 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700212 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700213 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500214 if (rval != QLA_SUCCESS) {
Chad Dupuis080c9512016-01-27 12:03:37 -0500215 fcport->flags |= FCF_LOGIN_NEEDED;
216 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700217 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500218 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700219
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700220 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800221 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
222 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800223 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
224 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700225 return rval;
226
227done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800228 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800229 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800230done:
Andrew Vasquezac280b62009-08-20 11:06:05 -0700231 return rval;
232}
233
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700234static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800235qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700236{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800237 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800238 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700239
Quinn Tran6d6749272017-12-28 12:33:41 -0800240 sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800241 if (!test_bit(UNLOADING, &sp->vha->dpc_flags))
242 qla2x00_post_async_logout_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800243 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800244 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700245}
246
Andrew Vasquezac280b62009-08-20 11:06:05 -0700247int
248qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
249{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700250 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700251 struct srb_iocb *lio;
Quinn Tran3dbec592017-12-28 12:33:40 -0800252 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700253
Quinn Tran3dbec592017-12-28 12:33:40 -0800254 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
255 return rval;
256
Quinn Tran726b8542017-01-19 22:28:00 -0800257 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800258 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700259 if (!sp)
260 goto done;
261
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800262 sp->type = SRB_LOGOUT_CMD;
263 sp->name = "logout";
264 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
265
266 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700267 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800268 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700269 rval = qla2x00_start_sp(sp);
270 if (rval != QLA_SUCCESS)
271 goto done_free_sp;
272
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700273 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800274 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800275 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800276 fcport->d_id.b.area, fcport->d_id.b.al_pa,
277 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700278 return rval;
279
280done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800281 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700282done:
Quinn Tran726b8542017-01-19 22:28:00 -0800283 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700284 return rval;
285}
Quinn Tran11aea162017-12-28 12:33:20 -0800286
287void
288qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
289 uint16_t *data)
290{
291 /* Don't re-login in target mode */
292 if (!fcport->tgt_session)
293 qla2x00_mark_device_lost(vha, fcport, 1, 0);
294 qlt_logo_completion_handler(fcport, data[0]);
295}
296
297static void
298qla2x00_async_prlo_sp_done(void *s, int res)
299{
300 srb_t *sp = (srb_t *)s;
301 struct srb_iocb *lio = &sp->u.iocb_cmd;
302 struct scsi_qla_host *vha = sp->vha;
303
304 if (!test_bit(UNLOADING, &vha->dpc_flags))
305 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
306 lio->u.logio.data);
307 sp->free(sp);
308}
309
310int
311qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
312{
313 srb_t *sp;
314 struct srb_iocb *lio;
315 int rval;
316
317 rval = QLA_FUNCTION_FAILED;
318 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
319 if (!sp)
320 goto done;
321
322 sp->type = SRB_PRLO_CMD;
323 sp->name = "prlo";
324 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
325
326 lio = &sp->u.iocb_cmd;
327 lio->timeout = qla2x00_async_iocb_timeout;
328 sp->done = qla2x00_async_prlo_sp_done;
329 rval = qla2x00_start_sp(sp);
330 if (rval != QLA_SUCCESS)
331 goto done_free_sp;
332
333 ql_dbg(ql_dbg_disc, vha, 0x2070,
334 "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
335 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
336 fcport->d_id.b.area, fcport->d_id.b.al_pa);
337 return rval;
338
339done_free_sp:
340 sp->free(sp);
341done:
342 return rval;
343}
344
Quinn Tranf13515a2017-12-28 12:33:15 -0800345static
346void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
347{
Quinn Tran0616e962017-12-28 12:33:34 -0800348 struct fc_port *fcport = ea->fcport;
349
350 ql_dbg(ql_dbg_disc, vha, 0x20d2,
351 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
352 __func__, fcport->port_name, fcport->disc_state,
353 fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
354 fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
355
356 if (ea->data[0] != MBS_COMMAND_COMPLETE) {
Quinn Trana4239942017-12-28 12:33:26 -0800357 ql_dbg(ql_dbg_disc, vha, 0x2066,
358 "%s %8phC: adisc fail: post delete\n",
359 __func__, ea->fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800360 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana4239942017-12-28 12:33:26 -0800361 return;
362 }
Quinn Trana4239942017-12-28 12:33:26 -0800363
364 if (ea->fcport->disc_state == DSC_DELETE_PEND)
365 return;
366
367 if (ea->sp->gen2 != ea->fcport->login_gen) {
368 /* target side must have changed it. */
369 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran0616e962017-12-28 12:33:34 -0800370 "%s %8phC generation changed\n",
371 __func__, ea->fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800372 return;
373 } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
374 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
375 __func__, __LINE__, ea->fcport->port_name);
376 qla24xx_post_gidpn_work(vha, ea->fcport);
377 return;
378 }
379
380 __qla24xx_handle_gpdb_event(vha, ea);
Quinn Tranf13515a2017-12-28 12:33:15 -0800381}
Andrew Vasquezac280b62009-08-20 11:06:05 -0700382
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700383static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800384qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700385{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800386 srb_t *sp = ptr;
387 struct scsi_qla_host *vha = sp->vha;
Quinn Tranf13515a2017-12-28 12:33:15 -0800388 struct event_arg ea;
Quinn Tran0616e962017-12-28 12:33:34 -0800389 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700390
Quinn Tranf13515a2017-12-28 12:33:15 -0800391 ql_dbg(ql_dbg_disc, vha, 0x2066,
392 "Async done-%s res %x %8phC\n",
393 sp->name, res, sp->fcport->port_name);
394
395 memset(&ea, 0, sizeof(ea));
396 ea.event = FCME_ADISC_DONE;
397 ea.rc = res;
Quinn Tran0616e962017-12-28 12:33:34 -0800398 ea.data[0] = lio->u.logio.data[0];
399 ea.data[1] = lio->u.logio.data[1];
400 ea.iop[0] = lio->u.logio.iop[0];
401 ea.iop[1] = lio->u.logio.iop[1];
Quinn Tranf13515a2017-12-28 12:33:15 -0800402 ea.fcport = sp->fcport;
403 ea.sp = sp;
404
405 qla2x00_fcport_event_handler(vha, &ea);
406
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800407 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700408}
409
410int
411qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
412 uint16_t *data)
413{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700414 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700415 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700416 int rval;
417
418 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800419 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800420 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700421 if (!sp)
422 goto done;
423
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800424 sp->type = SRB_ADISC_CMD;
425 sp->name = "adisc";
426 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
427
428 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700429 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800430 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700431 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700432 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700433 rval = qla2x00_start_sp(sp);
434 if (rval != QLA_SUCCESS)
435 goto done_free_sp;
436
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700437 ql_dbg(ql_dbg_disc, vha, 0x206f,
Quinn Tranf13515a2017-12-28 12:33:15 -0800438 "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
439 sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700440 return rval;
441
442done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800443 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700444done:
Quinn Tran726b8542017-01-19 22:28:00 -0800445 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tranf13515a2017-12-28 12:33:15 -0800446 qla2x00_post_async_adisc_work(vha, fcport, data);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700447 return rval;
448}
449
Quinn Tran726b8542017-01-19 22:28:00 -0800450static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
451 struct event_arg *ea)
452{
453 fc_port_t *fcport, *conflict_fcport;
454 struct get_name_list_extended *e;
455 u16 i, n, found = 0, loop_id;
456 port_id_t id;
457 u64 wwn;
Quinn Trana4239942017-12-28 12:33:26 -0800458 u16 data[2];
459 u8 current_login_state;
Quinn Tran726b8542017-01-19 22:28:00 -0800460
461 fcport = ea->fcport;
Quinn Tranf352eeb2017-12-28 12:33:35 -0800462 ql_dbg(ql_dbg_disc, vha, 0xffff,
463 "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
464 __func__, fcport->port_name, fcport->disc_state,
465 fcport->fw_login_state, ea->rc,
466 fcport->login_gen, fcport->last_login_gen,
467 fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800468
Quinn Trana4239942017-12-28 12:33:26 -0800469 if (fcport->disc_state == DSC_DELETE_PEND)
470 return;
471
Quinn Tran726b8542017-01-19 22:28:00 -0800472 if (ea->rc) { /* rval */
473 if (fcport->login_retry == 0) {
474 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700475 ql_dbg(ql_dbg_disc, vha, 0x20de,
476 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
477 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800478 }
479 return;
480 }
481
482 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700483 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800484 "%s %8phC rscn gen changed rscn %d|%d \n",
485 __func__, fcport->port_name,
486 fcport->last_rscn_gen, fcport->rscn_gen);
487 qla24xx_post_gidpn_work(vha, fcport);
488 return;
489 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700490 ql_dbg(ql_dbg_disc, vha, 0x20e0,
Quinn Tranf352eeb2017-12-28 12:33:35 -0800491 "%s %8phC login gen changed\n",
492 __func__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800493 return;
494 }
495
496 n = ea->data[0] / sizeof(struct get_name_list_extended);
497
Quinn Tran83548fe2017-06-02 09:12:01 -0700498 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800499 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
500 __func__, __LINE__, fcport->port_name, n,
501 fcport->d_id.b.domain, fcport->d_id.b.area,
502 fcport->d_id.b.al_pa, fcport->loop_id);
503
504 for (i = 0; i < n; i++) {
505 e = &vha->gnl.l[i];
506 wwn = wwn_to_u64(e->port_name);
507
508 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
509 continue;
510
511 found = 1;
512 id.b.domain = e->port_id[2];
513 id.b.area = e->port_id[1];
514 id.b.al_pa = e->port_id[0];
515 id.b.rsvd_1 = 0;
516
517 loop_id = le16_to_cpu(e->nport_handle);
518 loop_id = (loop_id & 0x7fff);
519
Quinn Tran83548fe2017-06-02 09:12:01 -0700520 ql_dbg(ql_dbg_disc, vha, 0x20e2,
521 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
522 __func__, fcport->port_name,
523 e->current_login_state, fcport->fw_login_state,
524 id.b.domain, id.b.area, id.b.al_pa,
525 fcport->d_id.b.domain, fcport->d_id.b.area,
526 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800527
528 if ((id.b24 != fcport->d_id.b24) ||
529 ((fcport->loop_id != FC_NO_LOOP_ID) &&
530 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700531 ql_dbg(ql_dbg_disc, vha, 0x20e3,
532 "%s %d %8phC post del sess\n",
533 __func__, __LINE__, fcport->port_name);
Quinn Tran94cff6e2017-12-28 12:33:42 -0800534 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800535 return;
536 }
537
538 fcport->loop_id = loop_id;
539
540 wwn = wwn_to_u64(fcport->port_name);
541 qlt_find_sess_invalidate_other(vha, wwn,
542 id, loop_id, &conflict_fcport);
543
544 if (conflict_fcport) {
545 /*
546 * Another share fcport share the same loop_id &
547 * nport id. Conflict fcport needs to finish
548 * cleanup before this fcport can proceed to login.
549 */
550 conflict_fcport->conflict = fcport;
551 fcport->login_pause = 1;
552 }
553
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700554 if (fcport->fc4f_nvme)
555 current_login_state = e->current_login_state >> 4;
556 else
557 current_login_state = e->current_login_state & 0xf;
558
559 switch (current_login_state) {
Quinn Tran726b8542017-01-19 22:28:00 -0800560 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700561 ql_dbg(ql_dbg_disc, vha, 0x20e4,
562 "%s %d %8phC post gpdb\n",
563 __func__, __LINE__, fcport->port_name);
Quinn Trana4239942017-12-28 12:33:26 -0800564
565 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
566 fcport->port_type = FCT_INITIATOR;
567 else
568 fcport->port_type = FCT_TARGET;
569
570 data[0] = data[1] = 0;
571 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -0800572 break;
Quinn Tran726b8542017-01-19 22:28:00 -0800573 case DSC_LS_PORT_UNAVAIL:
574 default:
575 if (fcport->loop_id == FC_NO_LOOP_ID) {
576 qla2x00_find_new_loop_id(vha, fcport);
577 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
578 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700579 ql_dbg(ql_dbg_disc, vha, 0x20e5,
580 "%s %d %8phC\n",
581 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800582 qla24xx_fcport_handle_login(vha, fcport);
583 break;
584 }
585 }
586
587 if (!found) {
588 /* fw has no record of this port */
Quinn Tran040036b2017-12-28 12:33:38 -0800589 for (i = 0; i < n; i++) {
590 e = &vha->gnl.l[i];
591 id.b.domain = e->port_id[0];
592 id.b.area = e->port_id[1];
593 id.b.al_pa = e->port_id[2];
594 id.b.rsvd_1 = 0;
595 loop_id = le16_to_cpu(e->nport_handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800596
Quinn Tran040036b2017-12-28 12:33:38 -0800597 if (fcport->d_id.b24 == id.b24) {
598 conflict_fcport =
599 qla2x00_find_fcport_by_wwpn(vha,
600 e->port_name, 0);
601 ql_dbg(ql_dbg_disc, vha, 0x20e6,
602 "%s %d %8phC post del sess\n",
603 __func__, __LINE__,
604 conflict_fcport->port_name);
605 qlt_schedule_sess_for_deletion
Quinn Tran94cff6e2017-12-28 12:33:42 -0800606 (conflict_fcport);
Quinn Tran726b8542017-01-19 22:28:00 -0800607 }
Quinn Tran040036b2017-12-28 12:33:38 -0800608
609 /* FW already picked this loop id for another fcport */
610 if (fcport->loop_id == loop_id)
611 fcport->loop_id = FC_NO_LOOP_ID;
Quinn Tran726b8542017-01-19 22:28:00 -0800612 }
613 qla24xx_fcport_handle_login(vha, fcport);
614 }
615} /* gnl_event */
616
617static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800618qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800619{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800620 struct srb *sp = s;
621 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800622 unsigned long flags;
623 struct fc_port *fcport = NULL, *tf;
624 u16 i, n = 0, loop_id;
625 struct event_arg ea;
626 struct get_name_list_extended *e;
627 u64 wwn;
628 struct list_head h;
Quinn Trana4239942017-12-28 12:33:26 -0800629 bool found = false;
Quinn Tran726b8542017-01-19 22:28:00 -0800630
Quinn Tran83548fe2017-06-02 09:12:01 -0700631 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800632 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
633 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
634 sp->u.iocb_cmd.u.mbx.in_mb[2]);
635
636 memset(&ea, 0, sizeof(ea));
637 ea.sp = sp;
638 ea.rc = res;
639 ea.event = FCME_GNL_DONE;
640
641 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
642 sizeof(struct get_name_list_extended)) {
643 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
644 sizeof(struct get_name_list_extended);
645 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
646 }
647
648 for (i = 0; i < n; i++) {
649 e = &vha->gnl.l[i];
650 loop_id = le16_to_cpu(e->nport_handle);
651 /* mask out reserve bit */
652 loop_id = (loop_id & 0x7fff);
653 set_bit(loop_id, vha->hw->loop_id_map);
654 wwn = wwn_to_u64(e->port_name);
655
Quinn Tran83548fe2017-06-02 09:12:01 -0700656 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800657 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
658 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
659 e->port_id[0], e->current_login_state, e->last_login_state,
660 (loop_id & 0x7fff));
661 }
662
663 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
664 vha->gnl.sent = 0;
665
666 INIT_LIST_HEAD(&h);
667 fcport = tf = NULL;
668 if (!list_empty(&vha->gnl.fcports))
669 list_splice_init(&vha->gnl.fcports, &h);
670
671 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
672 list_del_init(&fcport->gnl_entry);
Quinn Tran6d6749272017-12-28 12:33:41 -0800673 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800674 ea.fcport = fcport;
675
676 qla2x00_fcport_event_handler(vha, &ea);
677 }
678
Quinn Trana4239942017-12-28 12:33:26 -0800679 /* create new fcport if fw has knowledge of new sessions */
680 for (i = 0; i < n; i++) {
681 port_id_t id;
682 u64 wwnn;
683
684 e = &vha->gnl.l[i];
685 wwn = wwn_to_u64(e->port_name);
686
687 found = false;
688 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
689 if (!memcmp((u8 *)&wwn, fcport->port_name,
690 WWN_SIZE)) {
691 found = true;
692 break;
693 }
694 }
695
Quinn Trancf055fb2017-12-28 12:33:33 -0800696 id.b.domain = e->port_id[2];
Quinn Trana4239942017-12-28 12:33:26 -0800697 id.b.area = e->port_id[1];
Quinn Trancf055fb2017-12-28 12:33:33 -0800698 id.b.al_pa = e->port_id[0];
Quinn Trana4239942017-12-28 12:33:26 -0800699 id.b.rsvd_1 = 0;
700
701 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
702 ql_dbg(ql_dbg_disc, vha, 0x2065,
Quinn Trancf055fb2017-12-28 12:33:33 -0800703 "%s %d %8phC %06x post new sess\n",
704 __func__, __LINE__, (u8 *)&wwn, id.b24);
Quinn Trana4239942017-12-28 12:33:26 -0800705 wwnn = wwn_to_u64(e->node_name);
706 qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
707 (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
708 }
709 }
710
Quinn Tran726b8542017-01-19 22:28:00 -0800711 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
712
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800713 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800714}
715
716int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
717{
718 srb_t *sp;
719 struct srb_iocb *mbx;
720 int rval = QLA_FUNCTION_FAILED;
721 unsigned long flags;
722 u16 *mb;
723
Quinn Tran3dbec592017-12-28 12:33:40 -0800724 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
725 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800726
Quinn Tran83548fe2017-06-02 09:12:01 -0700727 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800728 "Async-gnlist WWPN %8phC \n", fcport->port_name);
729
730 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -0800731 fcport->disc_state = DSC_GNL;
732 fcport->last_rscn_gen = fcport->rscn_gen;
733 fcport->last_login_gen = fcport->login_gen;
734
735 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
736 if (vha->gnl.sent) {
737 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Tran3dbec592017-12-28 12:33:40 -0800738 return QLA_SUCCESS;
Quinn Tran726b8542017-01-19 22:28:00 -0800739 }
740 vha->gnl.sent = 1;
741 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
742
743 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
744 if (!sp)
745 goto done;
Quinn Tran3dbec592017-12-28 12:33:40 -0800746
747 fcport->flags |= FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800748 sp->type = SRB_MB_IOCB;
749 sp->name = "gnlist";
750 sp->gen1 = fcport->rscn_gen;
751 sp->gen2 = fcport->login_gen;
752
753 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
754
755 mb = sp->u.iocb_cmd.u.mbx.out_mb;
756 mb[0] = MBC_PORT_NODE_NAME_LIST;
757 mb[1] = BIT_2 | BIT_3;
758 mb[2] = MSW(vha->gnl.ldma);
759 mb[3] = LSW(vha->gnl.ldma);
760 mb[6] = MSW(MSD(vha->gnl.ldma));
761 mb[7] = LSW(MSD(vha->gnl.ldma));
762 mb[8] = vha->gnl.size;
763 mb[9] = vha->vp_idx;
764
765 mbx = &sp->u.iocb_cmd;
766 mbx->timeout = qla2x00_async_iocb_timeout;
767
768 sp->done = qla24xx_async_gnl_sp_done;
769
770 rval = qla2x00_start_sp(sp);
771 if (rval != QLA_SUCCESS)
772 goto done_free_sp;
773
Quinn Tran83548fe2017-06-02 09:12:01 -0700774 ql_dbg(ql_dbg_disc, vha, 0x20da,
775 "Async-%s - OUT WWPN %8phC hndl %x\n",
776 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800777
778 return rval;
779
780done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800781 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800782 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -0800783done:
Quinn Tran726b8542017-01-19 22:28:00 -0800784 return rval;
785}
786
787int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
788{
789 struct qla_work_evt *e;
790
791 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
792 if (!e)
793 return QLA_FUNCTION_FAILED;
794
795 e->u.fcport.fcport = fcport;
Quinn Tran6d6749272017-12-28 12:33:41 -0800796 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800797 return qla2x00_post_work(vha, e);
798}
799
800static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800801void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800802{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800803 struct srb *sp = s;
804 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800805 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800806 fc_port_t *fcport = sp->fcport;
807 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
Quinn Tran726b8542017-01-19 22:28:00 -0800808 struct event_arg ea;
809
Quinn Tran83548fe2017-06-02 09:12:01 -0700810 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800811 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
812 sp->name, res, fcport->port_name, mb[1], mb[2]);
813
Quinn Tran6d6749272017-12-28 12:33:41 -0800814 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
Quinn Tran726b8542017-01-19 22:28:00 -0800815
Quinn Tran726b8542017-01-19 22:28:00 -0800816 memset(&ea, 0, sizeof(ea));
817 ea.event = FCME_GPDB_DONE;
Quinn Tran726b8542017-01-19 22:28:00 -0800818 ea.fcport = fcport;
819 ea.sp = sp;
820
821 qla2x00_fcport_event_handler(vha, &ea);
822
823 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
824 sp->u.iocb_cmd.u.mbx.in_dma);
825
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800826 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800827}
828
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700829static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
830{
831 struct qla_work_evt *e;
832
833 e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
834 if (!e)
835 return QLA_FUNCTION_FAILED;
836
837 e->u.fcport.fcport = fcport;
838
839 return qla2x00_post_work(vha, e);
840}
841
842static void
843qla2x00_async_prli_sp_done(void *ptr, int res)
844{
845 srb_t *sp = ptr;
846 struct scsi_qla_host *vha = sp->vha;
847 struct srb_iocb *lio = &sp->u.iocb_cmd;
848 struct event_arg ea;
849
850 ql_dbg(ql_dbg_disc, vha, 0x2129,
851 "%s %8phC res %d \n", __func__,
852 sp->fcport->port_name, res);
853
854 sp->fcport->flags &= ~FCF_ASYNC_SENT;
855
856 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
857 memset(&ea, 0, sizeof(ea));
858 ea.event = FCME_PRLI_DONE;
859 ea.fcport = sp->fcport;
860 ea.data[0] = lio->u.logio.data[0];
861 ea.data[1] = lio->u.logio.data[1];
862 ea.iop[0] = lio->u.logio.iop[0];
863 ea.iop[1] = lio->u.logio.iop[1];
864 ea.sp = sp;
865
866 qla2x00_fcport_event_handler(vha, &ea);
867 }
868
869 sp->free(sp);
870}
871
872int
873qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
874{
875 srb_t *sp;
876 struct srb_iocb *lio;
877 int rval = QLA_FUNCTION_FAILED;
878
879 if (!vha->flags.online)
880 return rval;
881
882 if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
883 fcport->fw_login_state == DSC_LS_PLOGI_COMP ||
884 fcport->fw_login_state == DSC_LS_PRLI_PEND)
885 return rval;
886
887 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
888 if (!sp)
889 return rval;
890
891 fcport->flags |= FCF_ASYNC_SENT;
892 fcport->logout_completed = 0;
893
894 sp->type = SRB_PRLI_CMD;
895 sp->name = "prli";
896 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
897
898 lio = &sp->u.iocb_cmd;
899 lio->timeout = qla2x00_async_iocb_timeout;
900 sp->done = qla2x00_async_prli_sp_done;
901 lio->u.logio.flags = 0;
902
903 if (fcport->fc4f_nvme)
904 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
905
906 rval = qla2x00_start_sp(sp);
907 if (rval != QLA_SUCCESS) {
Duane Grigsbya5d42f42017-06-21 13:48:41 -0700908 fcport->flags |= FCF_LOGIN_NEEDED;
909 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
910 goto done_free_sp;
911 }
912
913 ql_dbg(ql_dbg_disc, vha, 0x211b,
914 "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d.\n",
915 fcport->port_name, sp->handle, fcport->loop_id,
916 fcport->d_id.b24, fcport->login_retry);
917
918 return rval;
919
920done_free_sp:
921 sp->free(sp);
922 fcport->flags &= ~FCF_ASYNC_SENT;
923 return rval;
924}
925
Quinn Trana07fc0a2017-08-23 15:05:21 -0700926int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
Quinn Tran726b8542017-01-19 22:28:00 -0800927{
928 struct qla_work_evt *e;
929
930 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
931 if (!e)
932 return QLA_FUNCTION_FAILED;
933
934 e->u.fcport.fcport = fcport;
935 e->u.fcport.opt = opt;
Quinn Tran6d6749272017-12-28 12:33:41 -0800936 fcport->flags |= FCF_ASYNC_ACTIVE;
Quinn Tran726b8542017-01-19 22:28:00 -0800937 return qla2x00_post_work(vha, e);
938}
939
940int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
941{
942 srb_t *sp;
943 struct srb_iocb *mbx;
944 int rval = QLA_FUNCTION_FAILED;
945 u16 *mb;
946 dma_addr_t pd_dma;
947 struct port_database_24xx *pd;
948 struct qla_hw_data *ha = vha->hw;
949
Quinn Tran3dbec592017-12-28 12:33:40 -0800950 if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
951 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -0800952
Quinn Tran726b8542017-01-19 22:28:00 -0800953 fcport->disc_state = DSC_GPDB;
954
955 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
956 if (!sp)
957 goto done;
958
Quinn Tran3dbec592017-12-28 12:33:40 -0800959 fcport->flags |= FCF_ASYNC_SENT;
Joe Carnuccioe0824e62017-08-23 15:05:08 -0700960 sp->type = SRB_MB_IOCB;
961 sp->name = "gpdb";
962 sp->gen1 = fcport->rscn_gen;
963 sp->gen2 = fcport->login_gen;
964 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
965
Thomas Meyer08eb7f42017-09-21 08:15:26 +0200966 pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
Quinn Tran726b8542017-01-19 22:28:00 -0800967 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700968 ql_log(ql_log_warn, vha, 0xd043,
969 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800970 goto done_free_sp;
971 }
Quinn Tran726b8542017-01-19 22:28:00 -0800972
Quinn Tran726b8542017-01-19 22:28:00 -0800973 mb = sp->u.iocb_cmd.u.mbx.out_mb;
974 mb[0] = MBC_GET_PORT_DATABASE;
975 mb[1] = fcport->loop_id;
976 mb[2] = MSW(pd_dma);
977 mb[3] = LSW(pd_dma);
978 mb[6] = MSW(MSD(pd_dma));
979 mb[7] = LSW(MSD(pd_dma));
980 mb[9] = vha->vp_idx;
981 mb[10] = opt;
982
983 mbx = &sp->u.iocb_cmd;
984 mbx->timeout = qla2x00_async_iocb_timeout;
985 mbx->u.mbx.in = (void *)pd;
986 mbx->u.mbx.in_dma = pd_dma;
987
988 sp->done = qla24xx_async_gpdb_sp_done;
989
990 rval = qla2x00_start_sp(sp);
991 if (rval != QLA_SUCCESS)
992 goto done_free_sp;
993
Quinn Tran83548fe2017-06-02 09:12:01 -0700994 ql_dbg(ql_dbg_disc, vha, 0x20dc,
995 "Async-%s %8phC hndl %x opt %x\n",
996 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800997
998 return rval;
999
1000done_free_sp:
1001 if (pd)
1002 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1003
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001004 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -08001005 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran3dbec592017-12-28 12:33:40 -08001006done:
Quinn Tran726b8542017-01-19 22:28:00 -08001007 qla24xx_post_gpdb_work(vha, fcport, opt);
1008 return rval;
1009}
1010
1011static
Quinn Trana4239942017-12-28 12:33:26 -08001012void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001013{
Quinn Tran726b8542017-01-19 22:28:00 -08001014 unsigned long flags;
1015
Quinn Tran726b8542017-01-19 22:28:00 -08001016 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Tranf13515a2017-12-28 12:33:15 -08001017 ea->fcport->login_gen++;
Quinn Tran726b8542017-01-19 22:28:00 -08001018 ea->fcport->deleted = 0;
1019 ea->fcport->logout_on_delete = 1;
1020
1021 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1022 vha->fcport_count++;
1023 ea->fcport->login_succ = 1;
1024
1025 if (!IS_IIDMA_CAPABLE(vha->hw) ||
1026 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001027 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -08001028 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
Quinn Trana4239942017-12-28 12:33:26 -08001029 __func__, __LINE__, ea->fcport->port_name,
Quinn Tran726b8542017-01-19 22:28:00 -08001030 vha->fcport_count);
1031
Quinn Trana4239942017-12-28 12:33:26 -08001032 qla24xx_post_upd_fcport_work(vha, ea->fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001033 } else {
Quinn Trana4239942017-12-28 12:33:26 -08001034 if (ea->fcport->id_changed) {
1035 ea->fcport->id_changed = 0;
1036 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1037 "%s %d %8phC post gfpnid fcp_cnt %d\n",
1038 __func__, __LINE__, ea->fcport->port_name,
1039 vha->fcport_count);
1040 qla24xx_post_gfpnid_work(vha, ea->fcport);
1041 } else {
1042 ql_dbg(ql_dbg_disc, vha, 0x20d7,
1043 "%s %d %8phC post gpsc fcp_cnt %d\n",
1044 __func__, __LINE__, ea->fcport->port_name,
1045 vha->fcport_count);
1046 qla24xx_post_gpsc_work(vha, ea->fcport);
1047 }
Quinn Tran726b8542017-01-19 22:28:00 -08001048 }
Quinn Tran414d9ff2017-12-04 14:45:03 -08001049 } else if (ea->fcport->login_succ) {
1050 /*
1051 * We have an existing session. A late RSCN delivery
1052 * must have triggered the session to be re-validate.
Quinn Trana4239942017-12-28 12:33:26 -08001053 * Session is still valid.
Quinn Tran414d9ff2017-12-04 14:45:03 -08001054 */
Quinn Tran5ef696a2017-12-04 14:45:05 -08001055 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1056 "%s %d %8phC session revalidate success\n",
Quinn Trana4239942017-12-28 12:33:26 -08001057 __func__, __LINE__, ea->fcport->port_name);
himanshu.madhani@cavium.com8a7eac22018-01-15 20:46:50 -08001058 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
Quinn Tran726b8542017-01-19 22:28:00 -08001059 }
1060 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Quinn Trana4239942017-12-28 12:33:26 -08001061}
Quinn Tran726b8542017-01-19 22:28:00 -08001062
Quinn Trana4239942017-12-28 12:33:26 -08001063static
1064void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1065{
Quinn Trana4239942017-12-28 12:33:26 -08001066 fc_port_t *fcport = ea->fcport;
1067 struct port_database_24xx *pd;
1068 struct srb *sp = ea->sp;
1069
1070 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1071
1072 fcport->flags &= ~FCF_ASYNC_SENT;
1073
1074 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001075 "%s %8phC DS %d LS %d rc %d\n", __func__, fcport->port_name,
1076 fcport->disc_state, pd->current_login_state, ea->rc);
Quinn Trana4239942017-12-28 12:33:26 -08001077
1078 if (fcport->disc_state == DSC_DELETE_PEND)
1079 return;
1080
1081 switch (pd->current_login_state) {
1082 case PDS_PRLI_COMPLETE:
1083 __qla24xx_parse_gpdb(vha, fcport, pd);
1084 break;
1085 case PDS_PLOGI_PENDING:
1086 case PDS_PLOGI_COMPLETE:
1087 case PDS_PRLI_PENDING:
1088 case PDS_PRLI2_PENDING:
1089 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC relogin needed\n",
1090 __func__, __LINE__, fcport->port_name);
1091 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1092 return;
1093 case PDS_LOGO_PENDING:
1094 case PDS_PORT_UNAVAILABLE:
1095 default:
1096 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1097 __func__, __LINE__, fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08001098 qlt_schedule_sess_for_deletion(fcport);
Quinn Trana4239942017-12-28 12:33:26 -08001099 return;
1100 }
1101 __qla24xx_handle_gpdb_event(vha, ea);
1102} /* gpdb event */
Quinn Tran9cd883f2017-12-28 12:33:24 -08001103
1104static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1105{
1106 u8 login = 0;
Quinn Tran040036b2017-12-28 12:33:38 -08001107 int rc;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001108
1109 if (qla_tgt_mode_enabled(vha))
1110 return;
1111
1112 if (qla_dual_mode_enabled(vha)) {
1113 if (N2N_TOPO(vha->hw)) {
1114 u64 mywwn, wwn;
1115
1116 mywwn = wwn_to_u64(vha->port_name);
1117 wwn = wwn_to_u64(fcport->port_name);
1118 if (mywwn > wwn)
1119 login = 1;
1120 else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1121 && time_after_eq(jiffies,
1122 fcport->plogi_nack_done_deadline))
1123 login = 1;
1124 } else {
1125 login = 1;
1126 }
1127 } else {
1128 /* initiator mode */
1129 login = 1;
1130 }
1131
1132 if (login) {
Quinn Tran040036b2017-12-28 12:33:38 -08001133 if (fcport->loop_id == FC_NO_LOOP_ID) {
1134 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1135 rc = qla2x00_find_new_loop_id(vha, fcport);
1136 if (rc) {
1137 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1138 "%s %d %8phC post del sess - out of loopid\n",
1139 __func__, __LINE__, fcport->port_name);
1140 fcport->scan_state = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001141 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran040036b2017-12-28 12:33:38 -08001142 return;
1143 }
1144 }
Quinn Tran9cd883f2017-12-28 12:33:24 -08001145 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1146 "%s %d %8phC post login\n",
1147 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001148 qla2x00_post_async_login_work(vha, fcport, NULL);
1149 }
1150}
1151
Quinn Tran726b8542017-01-19 22:28:00 -08001152int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1153{
Quinn Tranf13515a2017-12-28 12:33:15 -08001154 u16 data[2];
Quinn Trana4239942017-12-28 12:33:26 -08001155 u64 wwn;
1156
1157 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001158 "%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 -08001159 __func__, fcport->port_name, fcport->disc_state,
1160 fcport->fw_login_state, fcport->login_pause, fcport->flags,
1161 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001162 fcport->login_gen, fcport->login_retry,
Quinn Trana4239942017-12-28 12:33:26 -08001163 fcport->loop_id, fcport->scan_state);
1164
Quinn Tran726b8542017-01-19 22:28:00 -08001165 if (fcport->login_retry == 0)
1166 return 0;
1167
1168 if (fcport->scan_state != QLA_FCPORT_FOUND)
1169 return 0;
1170
Quinn Tran726b8542017-01-19 22:28:00 -08001171 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001172 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1173 return 0;
1174
Quinn Tran5b334692017-03-15 09:48:48 -07001175 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001176 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1177 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001178 return 0;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001179 }
Quinn Tran5b334692017-03-15 09:48:48 -07001180 }
1181
Quinn Tran726b8542017-01-19 22:28:00 -08001182 /* for pure Target Mode. Login will not be initiated */
1183 if (vha->host->active_mode == MODE_TARGET)
1184 return 0;
1185
1186 if (fcport->flags & FCF_ASYNC_SENT) {
1187 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1188 return 0;
1189 }
1190
Quinn Trana4239942017-12-28 12:33:26 -08001191 fcport->login_retry--;
1192
Quinn Tran726b8542017-01-19 22:28:00 -08001193 switch (fcport->disc_state) {
1194 case DSC_DELETED:
Quinn Trana4239942017-12-28 12:33:26 -08001195 wwn = wwn_to_u64(fcport->node_name);
1196 if (wwn == 0) {
1197 ql_dbg(ql_dbg_disc, vha, 0xffff,
1198 "%s %d %8phC post GNNID\n",
1199 __func__, __LINE__, fcport->port_name);
1200 qla24xx_post_gnnid_work(vha, fcport);
1201 } else if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001202 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1203 "%s %d %8phC post gnl\n",
1204 __func__, __LINE__, fcport->port_name);
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001205 qla24xx_post_gnl_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001206 } else {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001207 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001208 }
1209 break;
1210
1211 case DSC_GNL:
1212 if (fcport->login_pause) {
1213 fcport->last_rscn_gen = fcport->rscn_gen;
1214 fcport->last_login_gen = fcport->login_gen;
1215 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1216 break;
1217 }
1218
Quinn Tran9cd883f2017-12-28 12:33:24 -08001219 qla_chk_n2n_b4_login(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001220 break;
1221
1222 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001223 ql_dbg(ql_dbg_disc, vha, 0x20d0,
1224 "%s %d %8phC post gidpn\n",
1225 __func__, __LINE__, fcport->port_name);
Quinn Tran9cd883f2017-12-28 12:33:24 -08001226 if (N2N_TOPO(vha->hw))
1227 qla_chk_n2n_b4_login(vha, fcport);
1228 else
1229 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001230 break;
1231
1232 case DSC_LOGIN_COMPLETE:
1233 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -07001234 ql_dbg(ql_dbg_disc, vha, 0x20d1,
Quinn Tranf13515a2017-12-28 12:33:15 -08001235 "%s %d %8phC post adisc\n",
Quinn Tran83548fe2017-06-02 09:12:01 -07001236 __func__, __LINE__, fcport->port_name);
Quinn Tranf13515a2017-12-28 12:33:15 -08001237 data[0] = data[1] = 0;
1238 qla2x00_post_async_adisc_work(vha, fcport, data);
Quinn Tran726b8542017-01-19 22:28:00 -08001239 break;
1240
1241 default:
1242 break;
1243 }
1244
1245 return 0;
1246}
1247
1248static
1249void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
1250{
1251 fcport->rscn_gen++;
1252
Quinn Tran83548fe2017-06-02 09:12:01 -07001253 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
1254 "%s %8phC DS %d LS %d\n",
1255 __func__, fcport->port_name, fcport->disc_state,
1256 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -08001257
1258 if (fcport->flags & FCF_ASYNC_SENT)
1259 return;
1260
1261 switch (fcport->disc_state) {
1262 case DSC_DELETED:
1263 case DSC_LOGIN_COMPLETE:
Quinn Tran5ef696a2017-12-04 14:45:05 -08001264 qla24xx_post_gpnid_work(fcport->vha, &ea->id);
Quinn Tran726b8542017-01-19 22:28:00 -08001265 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001266 default:
1267 break;
1268 }
1269}
1270
1271int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
Quinn Trana4239942017-12-28 12:33:26 -08001272 u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
Quinn Tran726b8542017-01-19 22:28:00 -08001273{
1274 struct qla_work_evt *e;
1275 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1276 if (!e)
1277 return QLA_FUNCTION_FAILED;
1278
1279 e->u.new_sess.id = *id;
1280 e->u.new_sess.pla = pla;
Quinn Trana4239942017-12-28 12:33:26 -08001281 e->u.new_sess.fc4_type = fc4_type;
Quinn Tran726b8542017-01-19 22:28:00 -08001282 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
Quinn Trana4239942017-12-28 12:33:26 -08001283 if (node_name)
1284 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
Quinn Tran726b8542017-01-19 22:28:00 -08001285
1286 return qla2x00_post_work(vha, e);
1287}
1288
1289static
Quinn Tran726b8542017-01-19 22:28:00 -08001290void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1291 struct event_arg *ea)
1292{
1293 fc_port_t *fcport = ea->fcport;
1294
Quinn Tran83548fe2017-06-02 09:12:01 -07001295 ql_dbg(ql_dbg_disc, vha, 0x2102,
1296 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1297 __func__, fcport->port_name, fcport->disc_state,
1298 fcport->fw_login_state, fcport->login_pause,
1299 fcport->deleted, fcport->conflict,
1300 fcport->last_rscn_gen, fcport->rscn_gen,
1301 fcport->last_login_gen, fcport->login_gen,
1302 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001303
1304 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001305 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1306 return;
1307
Quinn Tran5b334692017-03-15 09:48:48 -07001308 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
Quinn Tran9cd883f2017-12-28 12:33:24 -08001309 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1310 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Tran5b334692017-03-15 09:48:48 -07001311 return;
Quinn Tran9cd883f2017-12-28 12:33:24 -08001312 }
Quinn Tran5b334692017-03-15 09:48:48 -07001313 }
1314
Quinn Tran726b8542017-01-19 22:28:00 -08001315 if (fcport->flags & FCF_ASYNC_SENT) {
1316 fcport->login_retry++;
1317 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1318 return;
1319 }
1320
1321 if (fcport->disc_state == DSC_DELETE_PEND) {
1322 fcport->login_retry++;
1323 return;
1324 }
1325
1326 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001327 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001328 __func__, __LINE__, fcport->port_name);
1329
Giridhar Malavali5d3300a2017-12-04 14:45:13 -08001330 qla24xx_post_gidpn_work(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08001331 return;
1332 }
1333
1334 qla24xx_fcport_handle_login(vha, fcport);
1335}
1336
Quinn Tran41dc5292017-01-19 22:28:03 -08001337void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001338{
Quinn Tranf352eeb2017-12-28 12:33:35 -08001339 fc_port_t *f, *tf;
Quinn Tran41dc5292017-01-19 22:28:03 -08001340 uint32_t id = 0, mask, rid;
Quinn Tranf352eeb2017-12-28 12:33:35 -08001341 unsigned long flags;
Quinn Tran726b8542017-01-19 22:28:00 -08001342
1343 switch (ea->event) {
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001344 case FCME_RSCN:
1345 case FCME_GIDPN_DONE:
1346 case FCME_GPSC_DONE:
1347 case FCME_GPNID_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001348 case FCME_GNNID_DONE:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001349 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1350 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1351 return;
1352 break;
1353 default:
1354 break;
1355 }
1356
1357 switch (ea->event) {
1358 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001359 if (test_bit(UNLOADING, &vha->dpc_flags))
1360 return;
1361
1362 qla24xx_handle_relogin_event(vha, ea);
1363 break;
1364 case FCME_RSCN:
1365 if (test_bit(UNLOADING, &vha->dpc_flags))
1366 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001367 switch (ea->id.b.rsvd_1) {
1368 case RSCN_PORT_ADDR:
Quinn Tranf352eeb2017-12-28 12:33:35 -08001369 spin_lock_irqsave(&vha->work_lock, flags);
1370 if (vha->scan.scan_flags == 0) {
1371 ql_dbg(ql_dbg_disc, vha, 0xffff,
1372 "%s: schedule\n", __func__);
1373 vha->scan.scan_flags |= SF_QUEUED;
1374 schedule_delayed_work(&vha->scan.scan_work, 5);
Quinn Tran726b8542017-01-19 22:28:00 -08001375 }
Quinn Tranf352eeb2017-12-28 12:33:35 -08001376 spin_unlock_irqrestore(&vha->work_lock, flags);
1377
Quinn Tran41dc5292017-01-19 22:28:03 -08001378 break;
1379 case RSCN_AREA_ADDR:
1380 case RSCN_DOM_ADDR:
1381 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1382 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001383 ql_dbg(ql_dbg_async, vha, 0x5044,
1384 "RSCN: Area 0x%06x was affected\n",
1385 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001386 } else {
1387 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001388 ql_dbg(ql_dbg_async, vha, 0x507a,
1389 "RSCN: Domain 0x%06x was affected\n",
1390 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001391 }
1392
1393 rid = ea->id.b24 & mask;
1394 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1395 list) {
1396 id = f->d_id.b24 & mask;
1397 if (rid == id) {
1398 ea->fcport = f;
1399 qla24xx_handle_rscn_event(f, ea);
1400 }
1401 }
1402 break;
1403 case RSCN_FAB_ADDR:
1404 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001405 ql_log(ql_log_warn, vha, 0xd045,
1406 "RSCN: Fabric was affected. Addr format %d\n",
1407 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001408 qla2x00_mark_all_devices_lost(vha, 1);
1409 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1410 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001411 }
1412 break;
1413 case FCME_GIDPN_DONE:
1414 qla24xx_handle_gidpn_event(vha, ea);
1415 break;
1416 case FCME_GNL_DONE:
1417 qla24xx_handle_gnl_done_event(vha, ea);
1418 break;
1419 case FCME_GPSC_DONE:
Quinn Trana4239942017-12-28 12:33:26 -08001420 qla24xx_handle_gpsc_event(vha, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001421 break;
1422 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1423 qla24xx_handle_plogi_done_event(vha, ea);
1424 break;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001425 case FCME_PRLI_DONE:
1426 qla24xx_handle_prli_done_event(vha, ea);
1427 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001428 case FCME_GPDB_DONE:
1429 qla24xx_handle_gpdb_event(vha, ea);
1430 break;
1431 case FCME_GPNID_DONE:
1432 qla24xx_handle_gpnid_event(vha, ea);
1433 break;
Duane Grigsbyd3bae932017-06-21 13:48:44 -07001434 case FCME_GFFID_DONE:
1435 qla24xx_handle_gffid_event(vha, ea);
1436 break;
Quinn Tranf13515a2017-12-28 12:33:15 -08001437 case FCME_ADISC_DONE:
1438 qla24xx_handle_adisc_event(vha, ea);
1439 break;
Quinn Trana4239942017-12-28 12:33:26 -08001440 case FCME_GNNID_DONE:
1441 qla24xx_handle_gnnid_event(vha, ea);
1442 break;
1443 case FCME_GFPNID_DONE:
1444 qla24xx_handle_gfpnid_event(vha, ea);
1445 break;
Quinn Tran726b8542017-01-19 22:28:00 -08001446 default:
1447 BUG_ON(1);
1448 break;
1449 }
1450}
1451
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001452static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001453qla2x00_tmf_iocb_timeout(void *data)
1454{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001455 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001456 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1457
1458 tmf->u.tmf.comp_status = CS_TIMEOUT;
1459 complete(&tmf->u.tmf.comp);
1460}
1461
1462static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001463qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001464{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001465 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001466 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001467
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001468 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001469}
1470
1471int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001472qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001473 uint32_t tag)
1474{
1475 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001476 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001477 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001478 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001479
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001480 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001481 if (!sp)
1482 goto done;
1483
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001484 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001485 sp->type = SRB_TM_CMD;
1486 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001487 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1488 tm_iocb->u.tmf.flags = flags;
1489 tm_iocb->u.tmf.lun = lun;
1490 tm_iocb->u.tmf.data = tag;
1491 sp->done = qla2x00_tmf_sp_done;
1492 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1493 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001494
1495 rval = qla2x00_start_sp(sp);
1496 if (rval != QLA_SUCCESS)
1497 goto done_free_sp;
1498
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001499 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001500 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1501 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1502 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001503
1504 wait_for_completion(&tm_iocb->u.tmf.comp);
1505
1506 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1507 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1508
1509 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1510 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1511 "TM IOCB failed (%x).\n", rval);
1512 }
1513
1514 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1515 flags = tm_iocb->u.tmf.flags;
1516 lun = (uint16_t)tm_iocb->u.tmf.lun;
1517
1518 /* Issue Marker IOCB */
1519 qla2x00_marker(vha, vha->hw->req_q_map[0],
1520 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1521 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1522 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001523
1524done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001525 sp->free(sp);
Quinn Tran6d6749272017-12-28 12:33:41 -08001526 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001527done:
1528 return rval;
1529}
1530
Armen Baloyan4440e462014-02-26 04:15:18 -05001531static void
1532qla24xx_abort_iocb_timeout(void *data)
1533{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001534 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001535 struct srb_iocb *abt = &sp->u.iocb_cmd;
1536
1537 abt->u.abt.comp_status = CS_TIMEOUT;
1538 complete(&abt->u.abt.comp);
1539}
1540
1541static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001542qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001543{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001544 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001545 struct srb_iocb *abt = &sp->u.iocb_cmd;
1546
1547 complete(&abt->u.abt.comp);
1548}
1549
Quinn Tran15f30a52017-03-15 09:48:52 -07001550int
Armen Baloyan4440e462014-02-26 04:15:18 -05001551qla24xx_async_abort_cmd(srb_t *cmd_sp)
1552{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001553 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001554 fc_port_t *fcport = cmd_sp->fcport;
1555 struct srb_iocb *abt_iocb;
1556 srb_t *sp;
1557 int rval = QLA_FUNCTION_FAILED;
1558
1559 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1560 if (!sp)
1561 goto done;
1562
1563 abt_iocb = &sp->u.iocb_cmd;
1564 sp->type = SRB_ABT_CMD;
1565 sp->name = "abort";
1566 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1567 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
Darren Trappb027a5a2018-01-15 20:46:51 -08001568
1569 if (vha->flags.qpairs_available && cmd_sp->qpair)
1570 abt_iocb->u.abt.req_que_no =
1571 cpu_to_le16(cmd_sp->qpair->req->id);
1572 else
1573 abt_iocb->u.abt.req_que_no = cpu_to_le16(vha->req->id);
1574
Armen Baloyan4440e462014-02-26 04:15:18 -05001575 sp->done = qla24xx_abort_sp_done;
1576 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1577 init_completion(&abt_iocb->u.abt.comp);
1578
1579 rval = qla2x00_start_sp(sp);
1580 if (rval != QLA_SUCCESS)
1581 goto done_free_sp;
1582
1583 ql_dbg(ql_dbg_async, vha, 0x507c,
1584 "Abort command issued - hdl=%x, target_id=%x\n",
1585 cmd_sp->handle, fcport->tgt_id);
1586
1587 wait_for_completion(&abt_iocb->u.abt.comp);
1588
1589 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1590 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1591
1592done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001593 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001594done:
1595 return rval;
1596}
1597
1598int
1599qla24xx_async_abort_command(srb_t *sp)
1600{
1601 unsigned long flags = 0;
1602
1603 uint32_t handle;
1604 fc_port_t *fcport = sp->fcport;
1605 struct scsi_qla_host *vha = fcport->vha;
1606 struct qla_hw_data *ha = vha->hw;
1607 struct req_que *req = vha->req;
1608
Darren Trappb027a5a2018-01-15 20:46:51 -08001609 if (vha->flags.qpairs_available && sp->qpair)
1610 req = sp->qpair->req;
1611
Armen Baloyan4440e462014-02-26 04:15:18 -05001612 spin_lock_irqsave(&ha->hardware_lock, flags);
1613 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1614 if (req->outstanding_cmds[handle] == sp)
1615 break;
1616 }
1617 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1618 if (handle == req->num_outstanding_cmds) {
1619 /* Command not found. */
1620 return QLA_FUNCTION_FAILED;
1621 }
1622 if (sp->type == SRB_FXIOCB_DCMD)
1623 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1624 FXDISC_ABORT_IOCTL);
1625
1626 return qla24xx_async_abort_cmd(sp);
1627}
1628
Quinn Tran726b8542017-01-19 22:28:00 -08001629static void
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001630qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1631{
1632 switch (ea->data[0]) {
1633 case MBS_COMMAND_COMPLETE:
1634 ql_dbg(ql_dbg_disc, vha, 0x2118,
1635 "%s %d %8phC post gpdb\n",
1636 __func__, __LINE__, ea->fcport->port_name);
1637
1638 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1639 ea->fcport->logout_on_delete = 1;
1640 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1641 break;
1642 default:
Duane Grigsbyedd05de2017-10-13 09:34:06 -07001643 if (ea->fcport->n2n_flag) {
1644 ql_dbg(ql_dbg_disc, vha, 0x2118,
1645 "%s %d %8phC post fc4 prli\n",
1646 __func__, __LINE__, ea->fcport->port_name);
1647 ea->fcport->fc4f_nvme = 0;
1648 ea->fcport->n2n_flag = 0;
1649 qla24xx_post_prli_work(vha, ea->fcport);
1650 }
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001651 ql_dbg(ql_dbg_disc, vha, 0x2119,
1652 "%s %d %8phC unhandle event of %x\n",
1653 __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1654 break;
1655 }
1656}
1657
1658static void
Quinn Tran726b8542017-01-19 22:28:00 -08001659qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001660{
Quinn Tran726b8542017-01-19 22:28:00 -08001661 port_id_t cid; /* conflict Nport id */
Quinn Trana084fd62017-12-04 14:45:00 -08001662 u16 lid;
1663 struct fc_port *conflict_fcport;
Quinn Tran82abdca2017-12-28 12:33:22 -08001664 unsigned long flags;
Quinn Trana4239942017-12-28 12:33:26 -08001665 struct fc_port *fcport = ea->fcport;
1666
Quinn Tranf352eeb2017-12-28 12:33:35 -08001667 ql_dbg(ql_dbg_disc, vha, 0xffff,
1668 "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1669 __func__, fcport->port_name, fcport->disc_state,
1670 fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1671 ea->sp->gen2, fcport->rscn_gen|ea->sp->gen1,
1672 ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1673
Quinn Trana4239942017-12-28 12:33:26 -08001674 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1675 (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1676 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1677 "%s %d %8phC Remote is trying to login\n",
1678 __func__, __LINE__, fcport->port_name);
1679 return;
1680 }
1681
1682 if (fcport->disc_state == DSC_DELETE_PEND)
1683 return;
1684
1685 if (ea->sp->gen2 != fcport->login_gen) {
1686 /* target side must have changed it. */
1687 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tranf352eeb2017-12-28 12:33:35 -08001688 "%s %8phC generation changed\n",
1689 __func__, fcport->port_name);
1690 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Quinn Trana4239942017-12-28 12:33:26 -08001691 return;
1692 } else if (ea->sp->gen1 != fcport->rscn_gen) {
1693 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
1694 __func__, __LINE__, fcport->port_name);
1695 qla24xx_post_gidpn_work(vha, fcport);
1696 return;
1697 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001698
Quinn Tran726b8542017-01-19 22:28:00 -08001699 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001700 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001701 /*
1702 * Driver must validate login state - If PRLI not complete,
1703 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1704 * requests.
1705 */
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001706 if (ea->fcport->fc4f_nvme) {
1707 ql_dbg(ql_dbg_disc, vha, 0x2117,
1708 "%s %d %8phC post prli\n",
1709 __func__, __LINE__, ea->fcport->port_name);
1710 qla24xx_post_prli_work(vha, ea->fcport);
1711 } else {
1712 ql_dbg(ql_dbg_disc, vha, 0x20ea,
Quinn Trana084fd62017-12-04 14:45:00 -08001713 "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1714 __func__, __LINE__, ea->fcport->port_name,
1715 ea->fcport->loop_id, ea->fcport->d_id.b24);
1716
1717 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
Quinn Tran82abdca2017-12-28 12:33:22 -08001718 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Quinn Trana084fd62017-12-04 14:45:00 -08001719 ea->fcport->loop_id = FC_NO_LOOP_ID;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001720 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1721 ea->fcport->logout_on_delete = 1;
Quinn Tran35158322017-08-30 10:16:50 -07001722 ea->fcport->send_els_logo = 0;
Quinn Tran82abdca2017-12-28 12:33:22 -08001723 ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1724 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1725
Duane Grigsbya5d42f42017-06-21 13:48:41 -07001726 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1727 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001728 break;
1729 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001730 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001731 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1732
1733 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1734 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1735 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001736 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1737 else
Quinn Tran726b8542017-01-19 22:28:00 -08001738 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001739 break;
1740 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001741 /* data[1] = IO PARAM 1 = nport ID */
1742 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1743 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1744 cid.b.al_pa = ea->iop[1] & 0xff;
1745 cid.b.rsvd_1 = 0;
1746
Quinn Tran83548fe2017-06-02 09:12:01 -07001747 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1748 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1749 __func__, __LINE__, ea->fcport->port_name,
1750 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001751
1752 if (IS_SW_RESV_ADDR(cid)) {
1753 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1754 ea->fcport->loop_id = FC_NO_LOOP_ID;
1755 } else {
1756 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001757 }
Quinn Tran726b8542017-01-19 22:28:00 -08001758 qla24xx_post_gnl_work(vha, ea->fcport);
1759 break;
1760 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001761 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1762 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1763 __func__, __LINE__, ea->fcport->port_name,
1764 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1765 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001766
Quinn Trana084fd62017-12-04 14:45:00 -08001767 lid = ea->iop[1] & 0xffff;
1768 qlt_find_sess_invalidate_other(vha,
1769 wwn_to_u64(ea->fcport->port_name),
1770 ea->fcport->d_id, lid, &conflict_fcport);
1771
1772 if (conflict_fcport) {
1773 /*
1774 * Another fcport share the same loop_id/nport id.
1775 * Conflict fcport needs to finish cleanup before this
1776 * fcport can proceed to login.
1777 */
1778 conflict_fcport->conflict = ea->fcport;
1779 ea->fcport->login_pause = 1;
1780
1781 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1782 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1783 __func__, __LINE__, ea->fcport->port_name,
1784 ea->fcport->d_id.b24, lid);
1785 qla2x00_clear_loop_id(ea->fcport);
1786 qla24xx_post_gidpn_work(vha, ea->fcport);
1787 } else {
1788 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1789 "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1790 __func__, __LINE__, ea->fcport->port_name,
1791 ea->fcport->d_id.b24, lid);
1792
1793 qla2x00_clear_loop_id(ea->fcport);
1794 set_bit(lid, vha->hw->loop_id_map);
1795 ea->fcport->loop_id = lid;
1796 ea->fcport->keep_nport_handle = 0;
Quinn Tran94cff6e2017-12-28 12:33:42 -08001797 qlt_schedule_sess_for_deletion(ea->fcport);
Quinn Trana084fd62017-12-04 14:45:00 -08001798 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001799 break;
1800 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001801 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001802}
1803
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001804void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001805qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1806 uint16_t *data)
1807{
Quinn Tran726b8542017-01-19 22:28:00 -08001808 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001809 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001810 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001811 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001812}
1813
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001814void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001815qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1816 uint16_t *data)
1817{
1818 if (data[0] == MBS_COMMAND_COMPLETE) {
1819 qla2x00_update_fcport(vha, fcport);
1820
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001821 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001822 }
1823
1824 /* Retry login. */
1825 fcport->flags &= ~FCF_ASYNC_SENT;
1826 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1827 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1828 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001829 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001830
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001831 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001832}
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834/****************************************************************************/
1835/* QLogic ISP2x00 Hardware Support Functions. */
1836/****************************************************************************/
1837
Saurav Kashyapfa492632012-11-21 02:40:29 -05001838static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001839qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1840{
1841 int rval = QLA_SUCCESS;
1842 struct qla_hw_data *ha = vha->hw;
1843 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001844 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001845
1846 qla83xx_idc_lock(vha, 0);
1847
1848 /* SV: TODO: Assign initialization timeout from
1849 * flash-info / other param
1850 */
1851 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1852 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1853
1854 /* Set our fcoe function presence */
1855 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1856 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1857 "Error while setting DRV-Presence.\n");
1858 rval = QLA_FUNCTION_FAILED;
1859 goto exit;
1860 }
1861
1862 /* Decide the reset ownership */
1863 qla83xx_reset_ownership(vha);
1864
1865 /*
1866 * On first protocol driver load:
1867 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1868 * register.
1869 * Others: Check compatibility with current IDC Major version.
1870 */
1871 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1872 if (ha->flags.nic_core_reset_owner) {
1873 /* Set IDC Major version */
1874 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1875 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1876
1877 /* Clearing IDC-Lock-Recovery register */
1878 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1879 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1880 /*
1881 * Clear further IDC participation if we are not compatible with
1882 * the current IDC Major Version.
1883 */
1884 ql_log(ql_log_warn, vha, 0xb07d,
1885 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1886 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1887 __qla83xx_clear_drv_presence(vha);
1888 rval = QLA_FUNCTION_FAILED;
1889 goto exit;
1890 }
1891 /* Each function sets its supported Minor version. */
1892 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1893 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1894 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1895
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001896 if (ha->flags.nic_core_reset_owner) {
1897 memset(config, 0, sizeof(config));
1898 if (!qla81xx_get_port_config(vha, config))
1899 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1900 QLA8XXX_DEV_READY);
1901 }
1902
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001903 rval = qla83xx_idc_state_handler(vha);
1904
1905exit:
1906 qla83xx_idc_unlock(vha, 0);
1907
1908 return rval;
1909}
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911/*
1912* qla2x00_initialize_adapter
1913* Initialize board.
1914*
1915* Input:
1916* ha = adapter block pointer.
1917*
1918* Returns:
1919* 0 = success
1920*/
1921int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001922qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001925 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001926 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001927
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001928 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1929 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001932 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001933 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001934 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001935 ha->flags.pci_channel_io_perm_failure = 0;
1936 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001937 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001938 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1939 atomic_set(&vha->loop_state, LOOP_DOWN);
1940 vha->device_flags = DFLG_NO_CABLE;
1941 vha->dpc_flags = 0;
1942 vha->flags.management_server_logged_in = 0;
1943 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 ha->isp_abort_cnt = 0;
1945 ha->beacon_blink_led = 0;
1946
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001947 set_bit(0, ha->req_qid_map);
1948 set_bit(0, ha->rsp_qid_map);
1949
Chad Dupuiscfb09192011-11-18 09:03:07 -08001950 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001951 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001952 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001954 ql_log(ql_log_warn, vha, 0x0044,
1955 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return (rval);
1957 }
1958
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001959 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001961 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001962 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001963 ql_log(ql_log_fatal, vha, 0x004f,
1964 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001965 return rval;
1966 }
1967
1968 if (IS_QLA8044(ha)) {
1969 qla8044_read_reset_template(vha);
1970
1971 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1972 * If DONRESET_BIT0 is set, drivers should not set dev_state
1973 * to NEED_RESET. But if NEED_RESET is set, drivers should
1974 * should honor the reset. */
1975 if (ql2xdontresethba == 1)
1976 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001977 }
1978
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001979 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001980 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001981 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001982
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001983 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001985 if (ha->flags.disable_serdes) {
1986 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001987 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001988 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001989 return QLA_FUNCTION_FAILED;
1990 }
1991
Chad Dupuiscfb09192011-11-18 09:03:07 -08001992 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001993 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001995 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1996 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001997 if (rval)
1998 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001999 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08002000 if (rval)
2001 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07002003
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002004 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002005 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002006 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002007 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07002008 "Unable to configure ISP84XX.\n");
2009 return QLA_FUNCTION_FAILED;
2010 }
2011 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002012
Quinn Tranead03852017-01-19 22:28:01 -08002013 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002014 rval = qla2x00_init_rings(vha);
2015
Lalit Chandivade2533cf62009-03-24 09:08:07 -07002016 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002018 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07002019 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002020 rval = qla84xx_init_chip(vha);
2021 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002022 ql_log(ql_log_warn, vha, 0x00d4,
2023 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07002024 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002025 }
2026 }
2027
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04002028 /* Load the NIC Core f/w if we are the first protocol driver. */
2029 if (IS_QLA8031(ha)) {
2030 rval = qla83xx_nic_core_fw_load(vha);
2031 if (rval)
2032 ql_log(ql_log_warn, vha, 0x0124,
2033 "Error in initializing NIC Core f/w.\n");
2034 }
2035
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05002036 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2037 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07002038
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04002039 if (IS_P3P_TYPE(ha))
2040 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2041 else
2042 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return (rval);
2045}
2046
2047/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002048 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002049 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 *
2051 * Returns 0 on success.
2052 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002053int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002054qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002056 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002057 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002058 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002059 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002062 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002065 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2067
Andrew Vasquez737faec2008-10-24 15:13:45 -07002068 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002070 /* Get PCI bus information. */
2071 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002072 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002073 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2074
2075 return QLA_SUCCESS;
2076}
2077
2078/**
2079 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002080 * @vha: HA context
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002081 *
2082 * Returns 0 on success.
2083 */
2084int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002085qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002086{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002087 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002088 unsigned long flags = 0;
2089 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002090 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002091 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002092
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002093 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002094 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002095
2096 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002097 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002098
2099 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2100 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07002101 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002102
2103 /*
2104 * If this is a 2300 card and not 2312, reset the
2105 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2106 * the 2310 also reports itself as a 2300 so we need to get the
2107 * fb revision level -- a 6 indicates it really is a 2300 and
2108 * not a 2310.
2109 */
2110 if (IS_QLA2300(ha)) {
2111 spin_lock_irqsave(&ha->hardware_lock, flags);
2112
2113 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002114 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002115 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002116 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002117 break;
2118
2119 udelay(10);
2120 }
2121
2122 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002123 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2124 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002125
2126 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002127 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002128
2129 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07002130 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002131
2132 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002133 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2134 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002135
2136 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07002137 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002138 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07002139 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002140 break;
2141
2142 udelay(10);
2143 }
2144
2145 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2146 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002147
2148 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2149
Andrew Vasquez737faec2008-10-24 15:13:45 -07002150 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002151
2152 /* Get PCI bus information. */
2153 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07002154 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002155 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2156
2157 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
2160/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002161 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002162 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002163 *
2164 * Returns 0 on success.
2165 */
2166int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002167qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002168{
Andrew Vasqueza157b102007-05-07 07:43:01 -07002169 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002170 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002171 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002172 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002173
2174 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07002175 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002176
2177 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07002178 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002179 w &= ~PCI_COMMAND_INTX_DISABLE;
2180 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2181
2182 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2183
2184 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07002185 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2186 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002187
2188 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002189 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002190 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002191
Andrew Vasquez737faec2008-10-24 15:13:45 -07002192 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002193
Auke Kok44c10132007-06-08 15:46:36 -07002194 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08002195
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002196 /* Get PCI bus information. */
2197 spin_lock_irqsave(&ha->hardware_lock, flags);
2198 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2199 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2200
2201 return QLA_SUCCESS;
2202}
2203
2204/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002205 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
Bart Van Assche2db62282018-01-23 16:33:51 -08002206 * @vha: HA context
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002207 *
2208 * Returns 0 on success.
2209 */
2210int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002211qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002212{
2213 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002214 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002215
2216 pci_set_master(ha->pdev);
2217 pci_try_set_mwi(ha->pdev);
2218
2219 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2220 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2221 w &= ~PCI_COMMAND_INTX_DISABLE;
2222 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2223
2224 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07002225 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04002226 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002227
Andrew Vasquez737faec2008-10-24 15:13:45 -07002228 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07002229
2230 ha->chip_revision = ha->pdev->revision;
2231
2232 return QLA_SUCCESS;
2233}
2234
2235/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * qla2x00_isp_firmware() - Choose firmware image.
Bart Van Assche2db62282018-01-23 16:33:51 -08002237 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 *
2239 * Returns 0 on success.
2240 */
2241static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002242qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002245 uint16_t loop_id, topo, sw_cap;
2246 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002247 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002250 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002253 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002256 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002257 if (rval == QLA_SUCCESS) {
2258 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002259 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07002260 &area, &domain, &topo, &sw_cap);
2261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002264 if (rval)
2265 ql_dbg(ql_dbg_init, vha, 0x007a,
2266 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
2268 return (rval);
2269}
2270
2271/**
2272 * qla2x00_reset_chip() - Reset ISP chip.
Bart Van Assche2db62282018-01-23 16:33:51 -08002273 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 *
2275 * Returns 0 on success.
2276 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002277void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002278qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
2280 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002281 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002282 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 uint16_t cmd;
2285
Andrew Vasquez85880802009-12-15 21:29:46 -08002286 if (unlikely(pci_channel_offline(ha->pdev)))
2287 return;
2288
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002289 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 spin_lock_irqsave(&ha->hardware_lock, flags);
2292
2293 /* Turn off master enable */
2294 cmd = 0;
2295 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2296 cmd &= ~PCI_COMMAND_MASTER;
2297 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2298
2299 if (!IS_QLA2100(ha)) {
2300 /* Pause RISC. */
2301 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2302 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2303 for (cnt = 0; cnt < 30000; cnt++) {
2304 if ((RD_REG_WORD(&reg->hccr) &
2305 HCCR_RISC_PAUSE) != 0)
2306 break;
2307 udelay(100);
2308 }
2309 } else {
2310 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2311 udelay(10);
2312 }
2313
2314 /* Select FPM registers. */
2315 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2316 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2317
2318 /* FPM Soft Reset. */
2319 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2320 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2321
2322 /* Toggle Fpm Reset. */
2323 if (!IS_QLA2200(ha)) {
2324 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2325 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2326 }
2327
2328 /* Select frame buffer registers. */
2329 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2330 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2331
2332 /* Reset frame buffer FIFOs. */
2333 if (IS_QLA2200(ha)) {
2334 WRT_FB_CMD_REG(ha, reg, 0xa000);
2335 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
2336 } else {
2337 WRT_FB_CMD_REG(ha, reg, 0x00fc);
2338
2339 /* Read back fb_cmd until zero or 3 seconds max */
2340 for (cnt = 0; cnt < 3000; cnt++) {
2341 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2342 break;
2343 udelay(100);
2344 }
2345 }
2346
2347 /* Select RISC module registers. */
2348 WRT_REG_WORD(&reg->ctrl_status, 0);
2349 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
2350
2351 /* Reset RISC processor. */
2352 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2353 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2354
2355 /* Release RISC processor. */
2356 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2357 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2358 }
2359
2360 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2361 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2362
2363 /* Reset ISP chip. */
2364 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2365
2366 /* Wait for RISC to recover from reset. */
2367 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2368 /*
2369 * It is necessary to for a delay here since the card doesn't
2370 * respond to PCI reads during a reset. On some architectures
2371 * this will result in an MCA.
2372 */
2373 udelay(20);
2374 for (cnt = 30000; cnt; cnt--) {
2375 if ((RD_REG_WORD(&reg->ctrl_status) &
2376 CSR_ISP_SOFT_RESET) == 0)
2377 break;
2378 udelay(100);
2379 }
2380 } else
2381 udelay(10);
2382
2383 /* Reset RISC processor. */
2384 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2385
2386 WRT_REG_WORD(&reg->semaphore, 0);
2387
2388 /* Release RISC processor. */
2389 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2390 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2391
2392 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2393 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07002394 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 udelay(100);
2398 }
2399 } else
2400 udelay(100);
2401
2402 /* Turn on master enable */
2403 cmd |= PCI_COMMAND_MASTER;
2404 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2405
2406 /* Disable RISC pause on FPM parity error. */
2407 if (!IS_QLA2100(ha)) {
2408 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2409 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
2410 }
2411
2412 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2413}
2414
2415/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002416 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
Bart Van Assche2db62282018-01-23 16:33:51 -08002417 * @vha: HA context
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002418 *
2419 * Returns 0 on success.
2420 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05002421static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002422qla81xx_reset_mpi(scsi_qla_host_t *vha)
2423{
2424 uint16_t mb[4] = {0x1010, 0, 1, 0};
2425
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002426 if (!IS_QLA81XX(vha->hw))
2427 return QLA_SUCCESS;
2428
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002429 return qla81xx_write_mpi_register(vha, mb);
2430}
2431
2432/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002433 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Bart Van Assche2db62282018-01-23 16:33:51 -08002434 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002435 *
2436 * Returns 0 on success.
2437 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002438static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002439qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002440{
2441 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002442 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002443 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002444 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002445 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002446 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002447 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002448
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002449 spin_lock_irqsave(&ha->hardware_lock, flags);
2450
2451 /* Reset RISC. */
2452 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2453 for (cnt = 0; cnt < 30000; cnt++) {
2454 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2455 break;
2456
2457 udelay(10);
2458 }
2459
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002460 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2461 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2462
2463 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2464 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2465 RD_REG_DWORD(&reg->hccr),
2466 RD_REG_DWORD(&reg->ctrl_status),
2467 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2468
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002469 WRT_REG_DWORD(&reg->ctrl_status,
2470 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002471 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002472
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002473 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002474
Andrew Vasquez88c26662005-07-08 17:59:26 -07002475 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002476 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002477 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2478 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002479 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002480 if (cnt)
2481 udelay(5);
2482 else
2483 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002484 }
2485
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002486 if (rval == QLA_SUCCESS)
2487 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2488
2489 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2490 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2491 RD_REG_DWORD(&reg->hccr),
2492 RD_REG_DWORD(&reg->mailbox0));
2493
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002494 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002495 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002496 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002497 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002498 if ((RD_REG_DWORD(&reg->ctrl_status) &
2499 CSRX_ISP_SOFT_RESET) == 0)
2500 break;
2501
2502 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002503 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002504 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2505 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2506
2507 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2508 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2509 RD_REG_DWORD(&reg->hccr),
2510 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002511
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002512 /* If required, do an MPI FW reset now */
2513 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2514 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2515 if (++abts_cnt < 5) {
2516 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2517 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2518 } else {
2519 /*
2520 * We exhausted the ISP abort retries. We have to
2521 * set the board offline.
2522 */
2523 abts_cnt = 0;
2524 vha->flags.online = 0;
2525 }
2526 }
2527 }
2528
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002529 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2530 RD_REG_DWORD(&reg->hccr);
2531
2532 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2533 RD_REG_DWORD(&reg->hccr);
2534
2535 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2536 RD_REG_DWORD(&reg->hccr);
2537
Bart Van Assche52c82822015-07-09 07:23:26 -07002538 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002539 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002540 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002541 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002542 if (cnt)
2543 udelay(5);
2544 else
2545 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002546 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002547 if (rval == QLA_SUCCESS)
2548 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2549
2550 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2551 "Host Risc 0x%x, mailbox0 0x%x\n",
2552 RD_REG_DWORD(&reg->hccr),
2553 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002554
2555 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002556
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002557 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2558 "Driver in %s mode\n",
2559 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2560
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002561 if (IS_NOPOLLING_TYPE(ha))
2562 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002563
2564 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002565}
2566
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002567static void
2568qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2569{
2570 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2571
2572 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2573 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2574
2575}
2576
2577static void
2578qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2579{
2580 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2581
2582 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2583 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2584}
2585
2586static void
2587qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2588{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002589 uint32_t wd32 = 0;
2590 uint delta_msec = 100;
2591 uint elapsed_msec = 0;
2592 uint timeout_msec;
2593 ulong n;
2594
Joe Carnucciocc790762015-08-04 13:37:53 -04002595 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2596 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002597 return;
2598
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002599 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2600 udelay(100);
2601
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002602attempt:
2603 timeout_msec = TIMEOUT_SEMAPHORE;
2604 n = timeout_msec / delta_msec;
2605 while (n--) {
2606 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2607 qla25xx_read_risc_sema_reg(vha, &wd32);
2608 if (wd32 & RISC_SEMAPHORE)
2609 break;
2610 msleep(delta_msec);
2611 elapsed_msec += delta_msec;
2612 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2613 goto force;
2614 }
2615
2616 if (!(wd32 & RISC_SEMAPHORE))
2617 goto force;
2618
2619 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2620 goto acquired;
2621
2622 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2623 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2624 n = timeout_msec / delta_msec;
2625 while (n--) {
2626 qla25xx_read_risc_sema_reg(vha, &wd32);
2627 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2628 break;
2629 msleep(delta_msec);
2630 elapsed_msec += delta_msec;
2631 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2632 goto force;
2633 }
2634
2635 if (wd32 & RISC_SEMAPHORE_FORCE)
2636 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2637
2638 goto attempt;
2639
2640force:
2641 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2642
2643acquired:
2644 return;
2645}
2646
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002647/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002648 * qla24xx_reset_chip() - Reset ISP24xx chip.
Bart Van Assche2db62282018-01-23 16:33:51 -08002649 * @vha: HA context
Andrew Vasquez88c26662005-07-08 17:59:26 -07002650 *
2651 * Returns 0 on success.
2652 */
2653void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002654qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002655{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002656 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002657
2658 if (pci_channel_offline(ha->pdev) &&
2659 ha->flags.pci_channel_io_perm_failure) {
2660 return;
2661 }
2662
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002663 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002664
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002665 qla25xx_manipulate_risc_semaphore(vha);
2666
Andrew Vasquez88c26662005-07-08 17:59:26 -07002667 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002668 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002669}
2670
2671/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 * qla2x00_chip_diag() - Test chip for proper operation.
Bart Van Assche2db62282018-01-23 16:33:51 -08002673 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 *
2675 * Returns 0 on success.
2676 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002677int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002678qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002681 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002682 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 unsigned long flags = 0;
2684 uint16_t data;
2685 uint32_t cnt;
2686 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002687 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 /* Assume a failed state */
2690 rval = QLA_FUNCTION_FAILED;
2691
Bart Van Asscheda4704d2018-01-23 16:33:47 -08002692 ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
2693 &reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695 spin_lock_irqsave(&ha->hardware_lock, flags);
2696
2697 /* Reset ISP chip. */
2698 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2699
2700 /*
2701 * We need to have a delay here since the card will not respond while
2702 * in reset causing an MCA on some architectures.
2703 */
2704 udelay(20);
2705 data = qla2x00_debounce_register(&reg->ctrl_status);
2706 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2707 udelay(5);
2708 data = RD_REG_WORD(&reg->ctrl_status);
2709 barrier();
2710 }
2711
2712 if (!cnt)
2713 goto chip_diag_failed;
2714
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002715 ql_dbg(ql_dbg_init, vha, 0x007c,
2716 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 /* Reset RISC processor. */
2719 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2720 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2721
2722 /* Workaround for QLA2312 PCI parity error */
2723 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2724 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2725 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2726 udelay(5);
2727 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002728 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
2730 } else
2731 udelay(10);
2732
2733 if (!cnt)
2734 goto chip_diag_failed;
2735
2736 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002737 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2740 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2741 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2742 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2743 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2744 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002745 ql_log(ql_log_warn, vha, 0x0062,
2746 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2747 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 goto chip_diag_failed;
2750 }
2751 ha->product_id[0] = mb[1];
2752 ha->product_id[1] = mb[2];
2753 ha->product_id[2] = mb[3];
2754 ha->product_id[3] = mb[4];
2755
2756 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002757 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2759 else
2760 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002761 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 if (IS_QLA2200(ha) &&
2764 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2765 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002766 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002768 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 ha->fw_transfer_size = 128;
2770 }
2771
2772 /* Wrap Incoming Mailboxes Test. */
2773 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2774
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002775 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002776 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002777 if (rval)
2778 ql_log(ql_log_warn, vha, 0x0080,
2779 "Failed mailbox send register test.\n");
2780 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 /* Flag a successful rval */
2782 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 spin_lock_irqsave(&ha->hardware_lock, flags);
2784
2785chip_diag_failed:
2786 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002787 ql_log(ql_log_info, vha, 0x0081,
2788 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2791
2792 return (rval);
2793}
2794
2795/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002796 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
Bart Van Assche2db62282018-01-23 16:33:51 -08002797 * @vha: HA context
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002798 *
2799 * Returns 0 on success.
2800 */
2801int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002802qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002803{
2804 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002805 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002806 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002807
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002808 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002809 return QLA_SUCCESS;
2810
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002811 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002812
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002813 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002814 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002815 ql_log(ql_log_warn, vha, 0x0082,
2816 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002817 } else {
2818 /* Flag a successful rval */
2819 rval = QLA_SUCCESS;
2820 }
2821
2822 return rval;
2823}
2824
Quinn Tranad0a0b02017-12-28 12:33:14 -08002825static void
2826qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002827{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002828 int rval;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002829 dma_addr_t tc_dma;
2830 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002831 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002832
Quinn Tranad0a0b02017-12-28 12:33:14 -08002833 if (ha->eft) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002834 ql_dbg(ql_dbg_init, vha, 0x00bd,
Quinn Tranad0a0b02017-12-28 12:33:14 -08002835 "%s: Offload Mem is already allocated.\n",
2836 __func__);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002837 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002838 }
2839
Quinn Tranad0a0b02017-12-28 12:33:14 -08002840 if (IS_FWI2_CAPABLE(ha)) {
2841 /* Allocate memory for Fibre Channel Event Buffer. */
2842 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2843 !IS_QLA27XX(ha))
2844 goto try_eft;
2845
2846 if (ha->fce)
2847 dma_free_coherent(&ha->pdev->dev,
2848 FCE_SIZE, ha->fce, ha->fce_dma);
2849
2850 /* Allocate memory for Fibre Channel Event Buffer. */
2851 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2852 GFP_KERNEL);
2853 if (!tc) {
2854 ql_log(ql_log_warn, vha, 0x00be,
2855 "Unable to allocate (%d KB) for FCE.\n",
2856 FCE_SIZE / 1024);
2857 goto try_eft;
2858 }
2859
2860 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
2861 ha->fce_mb, &ha->fce_bufs);
2862 if (rval) {
2863 ql_log(ql_log_warn, vha, 0x00bf,
2864 "Unable to initialize FCE (%d).\n", rval);
2865 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2866 tc_dma);
2867 ha->flags.fce_enabled = 0;
2868 goto try_eft;
2869 }
2870 ql_dbg(ql_dbg_init, vha, 0x00c0,
2871 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
2872
2873 ha->flags.fce_enabled = 1;
2874 ha->fce_dma = tc_dma;
2875 ha->fce = tc;
2876
2877try_eft:
2878 if (ha->eft)
2879 dma_free_coherent(&ha->pdev->dev,
2880 EFT_SIZE, ha->eft, ha->eft_dma);
2881
2882 /* Allocate memory for Extended Trace Buffer. */
2883 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2884 GFP_KERNEL);
2885 if (!tc) {
2886 ql_log(ql_log_warn, vha, 0x00c1,
2887 "Unable to allocate (%d KB) for EFT.\n",
2888 EFT_SIZE / 1024);
2889 goto eft_err;
2890 }
2891
2892 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
2893 if (rval) {
2894 ql_log(ql_log_warn, vha, 0x00c2,
2895 "Unable to initialize EFT (%d).\n", rval);
2896 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2897 tc_dma);
2898 goto eft_err;
2899 }
2900 ql_dbg(ql_dbg_init, vha, 0x00c3,
2901 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
2902
2903 ha->eft_dma = tc_dma;
2904 ha->eft = tc;
2905 }
2906
2907eft_err:
2908 return;
2909}
2910
2911void
2912qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
2913{
2914 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
2915 eft_size, fce_size, mq_size;
2916 struct qla_hw_data *ha = vha->hw;
2917 struct req_que *req = ha->req_q_map[0];
2918 struct rsp_que *rsp = ha->rsp_q_map[0];
2919 struct qla2xxx_fw_dump *fw_dump;
2920
Chad Dupuisf73cb692014-02-26 04:15:06 -05002921 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2922 req_q_size = rsp_q_size = 0;
2923
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002924 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2925 fixed_size = sizeof(struct qla2100_fw_dump);
2926 } else if (IS_QLA23XX(ha)) {
2927 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2928 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2929 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002930 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002931 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002932 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2933 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002934 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2935 else if (IS_QLA25XX(ha))
2936 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2937 else
2938 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002939
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002940 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2941 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002942 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002943 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002944 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002945 /*
2946 * Allocate maximum buffer size for all queues.
2947 * Resizing must be done at end-of-dump processing.
2948 */
2949 mq_size += ha->max_req_queues *
2950 (req->length * sizeof(request_t));
2951 mq_size += ha->max_rsp_queues *
2952 (rsp->length * sizeof(response_t));
2953 }
Arun Easi00876ae2013-03-25 02:21:37 -04002954 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002955 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002956 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002957 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2958 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002959 goto try_eft;
2960
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002961 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002962try_eft:
Chad Dupuiscfb09192011-11-18 09:03:07 -08002963 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002964 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002965 eft_size = EFT_SIZE;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002966 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002967
Chad Dupuisf73cb692014-02-26 04:15:06 -05002968 if (IS_QLA27XX(ha)) {
2969 if (!ha->fw_dump_template) {
2970 ql_log(ql_log_warn, vha, 0x00ba,
2971 "Failed missing fwdump template\n");
2972 return;
2973 }
2974 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2975 ql_dbg(ql_dbg_init, vha, 0x00fa,
2976 "-> allocating fwdump (%x bytes)...\n", dump_size);
2977 goto allocate;
2978 }
2979
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002980 req_q_size = req->length * sizeof(request_t);
2981 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002982 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002983 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002984 ha->chain_offset = dump_size;
2985 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002986
Quinn Tranb945e772017-06-13 20:47:29 -07002987 if (ha->exchoffld_buf)
2988 dump_size += sizeof(struct qla2xxx_offld_chain) +
2989 ha->exchoffld_size;
2990 if (ha->exlogin_buf)
2991 dump_size += sizeof(struct qla2xxx_offld_chain) +
2992 ha->exlogin_size;
2993
Chad Dupuisf73cb692014-02-26 04:15:06 -05002994allocate:
Quinn Tranad0a0b02017-12-28 12:33:14 -08002995 if (!ha->fw_dump_len || dump_size != ha->fw_dump_len) {
2996 fw_dump = vmalloc(dump_size);
2997 if (!fw_dump) {
2998 ql_log(ql_log_warn, vha, 0x00c4,
2999 "Unable to allocate (%d KB) for firmware dump.\n",
3000 dump_size / 1024);
3001 } else {
3002 if (ha->fw_dump)
3003 vfree(ha->fw_dump);
3004 ha->fw_dump = fw_dump;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003005
Quinn Tranad0a0b02017-12-28 12:33:14 -08003006 ha->fw_dump_len = dump_size;
3007 ql_dbg(ql_dbg_init, vha, 0x00c5,
3008 "Allocated (%d KB) for firmware dump.\n",
3009 dump_size / 1024);
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07003010
Quinn Tranad0a0b02017-12-28 12:33:14 -08003011 if (IS_QLA27XX(ha))
3012 return;
3013
3014 ha->fw_dump->signature[0] = 'Q';
3015 ha->fw_dump->signature[1] = 'L';
3016 ha->fw_dump->signature[2] = 'G';
3017 ha->fw_dump->signature[3] = 'C';
3018 ha->fw_dump->version = htonl(1);
3019
3020 ha->fw_dump->fixed_size = htonl(fixed_size);
3021 ha->fw_dump->mem_size = htonl(mem_size);
3022 ha->fw_dump->req_q_size = htonl(req_q_size);
3023 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3024
3025 ha->fw_dump->eft_size = htonl(eft_size);
3026 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
3027 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
3028
3029 ha->fw_dump->header_size =
3030 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003031 }
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07003032 }
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003033}
3034
Andrew Vasquez18e75552009-06-03 09:55:30 -07003035static int
3036qla81xx_mpi_sync(scsi_qla_host_t *vha)
3037{
3038#define MPS_MASK 0xe0
3039 int rval;
3040 uint16_t dc;
3041 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07003042
3043 if (!IS_QLA81XX(vha->hw))
3044 return QLA_SUCCESS;
3045
3046 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3047 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003048 ql_log(ql_log_warn, vha, 0x0105,
3049 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003050 goto done;
3051 }
3052
3053 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3054 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3055 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003056 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003057 goto done_release;
3058 }
3059
3060 dc &= MPS_MASK;
3061 if (dc == (dw & MPS_MASK))
3062 goto done_release;
3063
3064 dw &= ~MPS_MASK;
3065 dw |= dc;
3066 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3067 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003068 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003069 }
3070
3071done_release:
3072 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3073 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003074 ql_log(ql_log_warn, vha, 0x006d,
3075 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07003076 }
3077
3078done:
3079 return rval;
3080}
3081
Chad Dupuis8d93f552013-01-30 03:34:37 -05003082int
3083qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3084{
3085 /* Don't try to reallocate the array */
3086 if (req->outstanding_cmds)
3087 return QLA_SUCCESS;
3088
Michael Hernandezd7459522016-12-12 14:40:07 -08003089 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05003090 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3091 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05003092 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3093 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003094 else
Quinn Tran03e8c682015-12-17 14:56:59 -05003095 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003096 }
3097
3098 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3099 req->num_outstanding_cmds, GFP_KERNEL);
3100
3101 if (!req->outstanding_cmds) {
3102 /*
3103 * Try to allocate a minimal size just so we can get through
3104 * initialization.
3105 */
3106 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3107 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
3108 req->num_outstanding_cmds, GFP_KERNEL);
3109
3110 if (!req->outstanding_cmds) {
3111 ql_log(ql_log_fatal, NULL, 0x0126,
3112 "Failed to allocate memory for "
3113 "outstanding_cmds for req_que %p.\n", req);
3114 req->num_outstanding_cmds = 0;
3115 return QLA_FUNCTION_FAILED;
3116 }
3117 }
3118
3119 return QLA_SUCCESS;
3120}
3121
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003122#define PRINT_FIELD(_field, _flag, _str) { \
3123 if (a0->_field & _flag) {\
3124 if (p) {\
3125 strcat(ptr, "|");\
3126 ptr++;\
3127 leftover--;\
3128 } \
3129 len = snprintf(ptr, leftover, "%s", _str); \
3130 p = 1;\
3131 leftover -= len;\
3132 ptr += len; \
3133 } \
3134}
3135
3136static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3137{
3138#define STR_LEN 64
3139 struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3140 u8 str[STR_LEN], *ptr, p;
3141 int leftover, len;
3142
3143 memset(str, 0, STR_LEN);
3144 snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3145 ql_dbg(ql_dbg_init, vha, 0x015a,
3146 "SFP MFG Name: %s\n", str);
3147
3148 memset(str, 0, STR_LEN);
3149 snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3150 ql_dbg(ql_dbg_init, vha, 0x015c,
3151 "SFP Part Name: %s\n", str);
3152
3153 /* media */
3154 memset(str, 0, STR_LEN);
3155 ptr = str;
3156 leftover = STR_LEN;
3157 p = len = 0;
3158 PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3159 PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3160 PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3161 PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3162 PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3163 PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3164 PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3165 ql_dbg(ql_dbg_init, vha, 0x0160,
3166 "SFP Media: %s\n", str);
3167
3168 /* link length */
3169 memset(str, 0, STR_LEN);
3170 ptr = str;
3171 leftover = STR_LEN;
3172 p = len = 0;
3173 PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3174 PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3175 PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3176 PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3177 PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3178 ql_dbg(ql_dbg_init, vha, 0x0196,
3179 "SFP Link Length: %s\n", str);
3180
3181 memset(str, 0, STR_LEN);
3182 ptr = str;
3183 leftover = STR_LEN;
3184 p = len = 0;
3185 PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3186 PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3187 PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3188 PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3189 PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3190 ql_dbg(ql_dbg_init, vha, 0x016e,
3191 "SFP FC Link Tech: %s\n", str);
3192
3193 if (a0->length_km)
3194 ql_dbg(ql_dbg_init, vha, 0x016f,
3195 "SFP Distant: %d km\n", a0->length_km);
3196 if (a0->length_100m)
3197 ql_dbg(ql_dbg_init, vha, 0x0170,
3198 "SFP Distant: %d m\n", a0->length_100m*100);
3199 if (a0->length_50um_10m)
3200 ql_dbg(ql_dbg_init, vha, 0x0189,
3201 "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3202 if (a0->length_62um_10m)
3203 ql_dbg(ql_dbg_init, vha, 0x018a,
3204 "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3205 if (a0->length_om4_10m)
3206 ql_dbg(ql_dbg_init, vha, 0x0194,
3207 "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3208 if (a0->length_om3_10m)
3209 ql_dbg(ql_dbg_init, vha, 0x0195,
3210 "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3211}
3212
3213
3214/*
3215 * Return Code:
3216 * QLA_SUCCESS: no action
3217 * QLA_INTERFACE_ERROR: SFP is not there.
3218 * QLA_FUNCTION_FAILED: detected New SFP
3219 */
3220int
3221qla24xx_detect_sfp(scsi_qla_host_t *vha)
3222{
3223 int rc = QLA_SUCCESS;
3224 struct sff_8247_a0 *a;
3225 struct qla_hw_data *ha = vha->hw;
3226
3227 if (!AUTO_DETECT_SFP_SUPPORT(vha))
3228 goto out;
3229
3230 rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3231 if (rc)
3232 goto out;
3233
3234 a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3235 qla2xxx_print_sfp_info(vha);
3236
3237 if (a->fc_ll_cc7 & FC_LL_VL || a->fc_ll_cc7 & FC_LL_L) {
3238 /* long range */
3239 ha->flags.detected_lr_sfp = 1;
3240
3241 if (a->length_km > 5 || a->length_100m > 50)
3242 ha->long_range_distance = LR_DISTANCE_10K;
3243 else
3244 ha->long_range_distance = LR_DISTANCE_5K;
3245
3246 if (ha->flags.detected_lr_sfp != ha->flags.using_lr_setting)
3247 ql_dbg(ql_dbg_async, vha, 0x507b,
3248 "Detected Long Range SFP.\n");
3249 } else {
3250 /* short range */
3251 ha->flags.detected_lr_sfp = 0;
3252 if (ha->flags.using_lr_setting)
3253 ql_dbg(ql_dbg_async, vha, 0x5084,
3254 "Detected Short Range SFP.\n");
3255 }
3256
3257 if (!vha->flags.init_done)
3258 rc = QLA_SUCCESS;
3259out:
3260 return rc;
3261}
3262
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003263/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 * qla2x00_setup_chip() - Load and start RISC firmware.
Bart Van Assche2db62282018-01-23 16:33:51 -08003265 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 *
3267 * Returns 0 on success.
3268 */
3269static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003270qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003272 int rval;
3273 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003274 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003275 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3276 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003277 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08003278
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003279 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07003280 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003281 if (rval == QLA_SUCCESS) {
3282 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07003283 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05003284 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07003285 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07003286 }
3287
Andrew Vasquez3db06522008-01-31 12:33:49 -08003288 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3289 /* Disable SRAM, Instruction RAM and GP RAM parity. */
3290 spin_lock_irqsave(&ha->hardware_lock, flags);
3291 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3292 RD_REG_WORD(&reg->hccr);
3293 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Andrew Vasquez18e75552009-06-03 09:55:30 -07003296 qla81xx_mpi_sync(vha);
3297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003299 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003300 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003301 ql_dbg(ql_dbg_init, vha, 0x00c9,
3302 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003304 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (rval == QLA_SUCCESS) {
3306 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003307 ql_dbg(ql_dbg_init, vha, 0x00ca,
3308 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003310 if (ql2xexlogins)
3311 ha->flags.exlogins_enabled = 1;
3312
Quinn Tran99e1b682017-06-02 09:12:03 -07003313 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003314 ha->flags.exchoffld_enabled = 1;
3315
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003316 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003318 if (rval == QLA_SUCCESS) {
Quinn Trane4e3a2c2017-08-23 15:05:07 -07003319 qla24xx_detect_sfp(vha);
3320
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05003321 rval = qla2x00_set_exlogins_buffer(vha);
3322 if (rval != QLA_SUCCESS)
3323 goto failed;
3324
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05003325 rval = qla2x00_set_exchoffld_buffer(vha);
3326 if (rval != QLA_SUCCESS)
3327 goto failed;
3328
Giridhar Malavalia9083012010-04-12 17:59:55 -07003329enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07003330 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003331 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07003332 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003333 else
3334 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003335 if (rval != QLA_SUCCESS)
3336 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003337 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003338 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07003339 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003340 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003341 if ((!ha->max_npiv_vports) ||
3342 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003343 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003344 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08003345 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07003346 }
Quinn Tran03e8c682015-12-17 14:56:59 -05003347 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07003348
Chad Dupuis8d93f552013-01-30 03:34:37 -05003349 /*
3350 * Allocate the array of outstanding commands
3351 * now that we know the firmware resources.
3352 */
3353 rval = qla2x00_alloc_outstanding_cmds(ha,
3354 vha->req);
3355 if (rval != QLA_SUCCESS)
3356 goto failed;
3357
Quinn Tranad0a0b02017-12-28 12:33:14 -08003358 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
3359 qla2x00_alloc_offload_mem(vha);
3360
3361 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07003362 qla2x00_alloc_fw_dump(vha);
Quinn Tranad0a0b02017-12-28 12:33:14 -08003363
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04003364 } else {
3365 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 }
3367 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003368 ql_log(ql_log_fatal, vha, 0x00cd,
3369 "ISP Firmware failed checksum.\n");
3370 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04003372 } else
3373 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Andrew Vasquez3db06522008-01-31 12:33:49 -08003375 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3376 /* Enable proper parity. */
3377 spin_lock_irqsave(&ha->hardware_lock, flags);
3378 if (IS_QLA2300(ha))
3379 /* SRAM parity */
3380 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3381 else
3382 /* SRAM, Instruction RAM and GP RAM parity */
3383 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3384 RD_REG_WORD(&reg->hccr);
3385 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3386 }
3387
Chad Dupuisf3982d82014-09-25 05:16:57 -04003388 if (IS_QLA27XX(ha))
3389 ha->flags.fac_supported = 1;
3390 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003391 uint32_t size;
3392
3393 rval = qla81xx_fac_get_sector_size(vha, &size);
3394 if (rval == QLA_SUCCESS) {
3395 ha->flags.fac_supported = 1;
3396 ha->fdt_block_size = size << 2;
3397 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003398 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003399 "Unsupported FAC firmware (%d.%02d.%02d).\n",
3400 ha->fw_major_version, ha->fw_minor_version,
3401 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05003402
Chad Dupuisf73cb692014-02-26 04:15:06 -05003403 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003404 ha->flags.fac_supported = 0;
3405 rval = QLA_SUCCESS;
3406 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07003407 }
3408 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07003409failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003411 ql_log(ql_log_fatal, vha, 0x00cf,
3412 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
3414
3415 return (rval);
3416}
3417
3418/**
3419 * qla2x00_init_response_q_entries() - Initializes response queue entries.
Bart Van Assche2db62282018-01-23 16:33:51 -08003420 * @rsp: response queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 *
3422 * Beginning of request ring has initialization control block already built
3423 * by nvram config routine.
3424 *
3425 * Returns 0 on success.
3426 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003427void
3428qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 uint16_t cnt;
3431 response_t *pkt;
3432
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003433 rsp->ring_ptr = rsp->ring;
3434 rsp->ring_index = 0;
3435 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003436 pkt = rsp->ring_ptr;
3437 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 pkt->signature = RESPONSE_PROCESSED;
3439 pkt++;
3440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
3443/**
3444 * qla2x00_update_fw_options() - Read and process firmware options.
Bart Van Assche2db62282018-01-23 16:33:51 -08003445 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 *
3447 * Returns 0 on success.
3448 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003449void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003450qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451{
3452 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003453 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003456 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 if (IS_QLA2100(ha) || IS_QLA2200(ha))
3459 return;
3460
3461 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003462 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3463 "Serial link options.\n");
3464 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3465 (uint8_t *)&ha->fw_seriallink_options,
3466 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
3469 if (ha->fw_seriallink_options[3] & BIT_2) {
3470 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
3471
3472 /* 1G settings */
3473 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
3474 emphasis = (ha->fw_seriallink_options[2] &
3475 (BIT_4 | BIT_3)) >> 3;
3476 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003477 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 rx_sens = (ha->fw_seriallink_options[0] &
3479 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3480 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
3481 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3482 if (rx_sens == 0x0)
3483 rx_sens = 0x3;
3484 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
3485 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3486 ha->fw_options[10] |= BIT_5 |
3487 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3488 (tx_sens & (BIT_1 | BIT_0));
3489
3490 /* 2G settings */
3491 swing = (ha->fw_seriallink_options[2] &
3492 (BIT_7 | BIT_6 | BIT_5)) >> 5;
3493 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
3494 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003495 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 rx_sens = (ha->fw_seriallink_options[1] &
3497 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
3498 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
3499 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
3500 if (rx_sens == 0x0)
3501 rx_sens = 0x3;
3502 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
3503 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
3504 ha->fw_options[11] |= BIT_5 |
3505 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
3506 (tx_sens & (BIT_1 | BIT_0));
3507 }
3508
3509 /* FCP2 options. */
3510 /* Return command IOCBs without waiting for an ABTS to complete. */
3511 ha->fw_options[3] |= BIT_13;
3512
3513 /* LED scheme. */
3514 if (ha->flags.enable_led_scheme)
3515 ha->fw_options[2] |= BIT_12;
3516
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08003517 /* Detect ISP6312. */
3518 if (IS_QLA6312(ha))
3519 ha->fw_options[2] |= BIT_13;
3520
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003521 /* Set Retry FLOGI in case of P2P connection */
3522 if (ha->operating_mode == P2P) {
3523 ha->fw_options[2] |= BIT_3;
3524 ql_dbg(ql_dbg_disc, vha, 0x2100,
3525 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3526 __func__, ha->fw_options[2]);
3527 }
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003530 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003533void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003534qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003535{
3536 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003537 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003538
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04003539 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07003540 return;
3541
Himanshu Madhanif198caf2016-01-27 12:03:30 -05003542 /* Hold status IOCBs until ABTS response received. */
3543 if (ql2xfwholdabts)
3544 ha->fw_options[3] |= BIT_12;
3545
Giridhar Malavali088d09d2016-07-06 11:14:20 -04003546 /* Set Retry FLOGI in case of P2P connection */
3547 if (ha->operating_mode == P2P) {
3548 ha->fw_options[2] |= BIT_3;
3549 ql_dbg(ql_dbg_disc, vha, 0x2101,
3550 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
3551 __func__, ha->fw_options[2]);
3552 }
3553
Quinn Tran41dc5292017-01-19 22:28:03 -08003554 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07003555 if (ql2xmvasynctoatio &&
3556 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08003557 if (qla_tgt_mode_enabled(vha) ||
3558 qla_dual_mode_enabled(vha))
3559 ha->fw_options[2] |= BIT_11;
3560 else
3561 ha->fw_options[2] &= ~BIT_11;
3562 }
3563
Quinn Tranf7e761f2017-06-02 09:12:02 -07003564 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
3565 /*
3566 * Tell FW to track each exchange to prevent
3567 * driver from using stale exchange.
3568 */
3569 if (qla_tgt_mode_enabled(vha) ||
3570 qla_dual_mode_enabled(vha))
3571 ha->fw_options[2] |= BIT_4;
3572 else
3573 ha->fw_options[2] &= ~BIT_4;
Quinn Tran9ecf0b02017-12-28 12:33:19 -08003574
3575 /* Reserve 1/2 of emergency exchanges for ELS.*/
3576 if (qla2xuseresexchforels)
3577 ha->fw_options[2] |= BIT_8;
3578 else
3579 ha->fw_options[2] &= ~BIT_8;
Quinn Tranf7e761f2017-06-02 09:12:02 -07003580 }
3581
Quinn Tran83548fe2017-06-02 09:12:01 -07003582 ql_dbg(ql_dbg_init, vha, 0x00e8,
3583 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
3584 __func__, ha->fw_options[1], ha->fw_options[2],
3585 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07003586
3587 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
3588 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08003589
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003590 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003591 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003592 return;
3593
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003594 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003595 le16_to_cpu(ha->fw_seriallink_options24[1]),
3596 le16_to_cpu(ha->fw_seriallink_options24[2]),
3597 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003598 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003599 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003600 "Unable to update Serial Link options (%x).\n", rval);
3601 }
3602}
3603
3604void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003605qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003606{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003607 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003608 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003609 struct req_que *req = ha->req_q_map[0];
3610 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003611
3612 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003613 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3614 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003615 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3616 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3617 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3618 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3619 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3620 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003621
3622 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3623 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3624 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3625 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3626 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3627}
3628
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003629void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003630qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003631{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003632 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003633 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003634 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3635 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003636 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003637 uint16_t rid = 0;
3638 struct req_que *req = ha->req_q_map[0];
3639 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003640
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003641 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003642 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003643 icb->request_q_outpointer = cpu_to_le16(0);
3644 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003645 icb->request_q_length = cpu_to_le16(req->length);
3646 icb->response_q_length = cpu_to_le16(rsp->length);
3647 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3648 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3649 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3650 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003651
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003652 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003653 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003654 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3655 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3656 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3657
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003658 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003659 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003660
Chad Dupuisf73cb692014-02-26 04:15:06 -05003661 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003662 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3663 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003664 if (ha->flags.msix_enabled) {
3665 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003666 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003667 "Registering vector 0x%x for base que.\n",
3668 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003669 icb->msix = cpu_to_le16(msix->entry);
3670 }
3671 /* Use alternate PCI bus number */
3672 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003673 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003674 /* Use alternate PCI devfn */
3675 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003676 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003677
Anirban Chakraborty31557542009-12-02 10:36:55 -08003678 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003679 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3680 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003681 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003682 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003683 ql_dbg(ql_dbg_init, vha, 0x00fe,
3684 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003685 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003686 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003687 }
Bart Van Asschead950362015-07-09 07:24:08 -07003688 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003689
3690 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3691 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3692 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3693 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3694 } else {
3695 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3696 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3697 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3698 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3699 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003700 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003701
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003702 /* PCI posting */
3703 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706/**
3707 * qla2x00_init_rings() - Initializes firmware.
Bart Van Assche2db62282018-01-23 16:33:51 -08003708 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 *
3710 * Beginning of request ring has initialization control block already built
3711 * by nvram config routine.
3712 *
3713 * Returns 0 on success.
3714 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003715int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003716qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
3718 int rval;
3719 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003720 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003721 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003722 struct req_que *req;
3723 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003724 struct mid_init_cb_24xx *mid_init_cb =
3725 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
3727 spin_lock_irqsave(&ha->hardware_lock, flags);
3728
3729 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003730 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003731 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003732 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003733 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003734 req->out_ptr = (void *)(req->ring + req->length);
3735 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003736 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003737 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003739 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003740
3741 /* Initialize firmware. */
3742 req->ring_ptr = req->ring;
3743 req->ring_index = 0;
3744 req->cnt = req->length;
3745 }
3746
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003747 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003748 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003749 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003750 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003751 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3752 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003753 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003754 if (IS_QLAFX00(ha))
3755 qlafx00_init_response_q_entries(rsp);
3756 else
3757 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003760 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3761 ha->tgt.atio_ring_index = 0;
3762 /* Initialize ATIO queue entries */
3763 qlt_init_atio_q_entries(vha);
3764
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003765 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3768
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003769 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3770
3771 if (IS_QLAFX00(ha)) {
3772 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3773 goto next_check;
3774 }
3775
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003777 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003779 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003780 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003781 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003782 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003783 }
3784
Andrew Vasquez24a08132009-03-24 09:08:16 -07003785 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003786 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003787 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003788 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003789 ha->flags.dport_enabled =
3790 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3791 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3792 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3793 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003794 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003795 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003796 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003797 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003798 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003799
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003800 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003801next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003803 ql_log(ql_log_fatal, vha, 0x00d2,
3804 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003806 ql_dbg(ql_dbg_init, vha, 0x00d3,
3807 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003808 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810
3811 return (rval);
3812}
3813
3814/**
3815 * qla2x00_fw_ready() - Waits for firmware ready.
Bart Van Assche2db62282018-01-23 16:33:51 -08003816 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 *
3818 * Returns 0 on success.
3819 */
3820static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003821qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
3823 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003824 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 uint16_t min_wait; /* Minimum wait time if loop is down */
3826 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003827 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003828 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003830 if (IS_QLAFX00(vha->hw))
3831 return qlafx00_fw_ready(vha);
3832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 rval = QLA_SUCCESS;
3834
Chad Dupuis334614912015-04-09 14:59:57 -04003835 /* Time to wait for loop down */
3836 if (IS_P3P_TYPE(ha))
3837 min_wait = 30;
3838 else
3839 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
3841 /*
3842 * Firmware should take at most one RATOV to login, plus 5 seconds for
3843 * our own processing.
3844 */
3845 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3846 wait_time = min_wait;
3847 }
3848
3849 /* Min wait time if loop down */
3850 mtime = jiffies + (min_wait * HZ);
3851
3852 /* wait time before firmware ready */
3853 wtime = jiffies + (wait_time * HZ);
3854
3855 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003856 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003857 ql_log(ql_log_info, vha, 0x801e,
3858 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003861 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003862 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003864 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003865 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003867 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003868 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3869 "fw_state=%x 84xx=%x.\n", state[0],
3870 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003871 if ((state[2] & FSTATE_LOGGED_IN) &&
3872 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003873 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3874 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003875
3876 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003877 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003878 if (rval != QLA_SUCCESS) {
3879 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003880 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003881 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003882 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003883 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003884
3885 /* Add time taken to initialize. */
3886 cs84xx_time = jiffies - cs84xx_time;
3887 wtime += cs84xx_time;
3888 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003889 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003890 "Increasing wait time by %ld. "
3891 "New time %ld.\n", cs84xx_time,
3892 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003893 }
3894 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003895 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3896 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003898 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 &ha->login_timeout, &ha->r_a_tov);
3900
3901 rval = QLA_SUCCESS;
3902 break;
3903 }
3904
3905 rval = QLA_FUNCTION_FAILED;
3906
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003907 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003908 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003910 * other than Wait for Login.
3911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003913 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 "Cable is unplugged...\n");
3915
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003916 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 break;
3918 }
3919 }
3920 } else {
3921 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003922 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003923 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 break;
3925 }
3926
3927 if (time_after_eq(jiffies, wtime))
3928 break;
3929
3930 /* Delay for a while */
3931 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 } while (1);
3933
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003934 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003935 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3936 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Chad Dupuiscfb09192011-11-18 09:03:07 -08003938 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003939 ql_log(ql_log_warn, vha, 0x803b,
3940 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
3942
3943 return (rval);
3944}
3945
3946/*
3947* qla2x00_configure_hba
3948* Setup adapter context.
3949*
3950* Input:
3951* ha = adapter state pointer.
3952*
3953* Returns:
3954* 0 = success
3955*
3956* Context:
3957* Kernel context.
3958*/
3959static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003960qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 int rval;
3963 uint16_t loop_id;
3964 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003965 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 uint8_t al_pa;
3967 uint8_t area;
3968 uint8_t domain;
3969 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003970 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003971 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003972 port_id_t id;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08003973 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
3975 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003976 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003977 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003979 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003980 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003981 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003982 ql_dbg(ql_dbg_disc, vha, 0x2008,
3983 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003984 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003985 ql_log(ql_log_warn, vha, 0x2009,
3986 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003987 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3988 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3989 ql_log(ql_log_warn, vha, 0x1151,
3990 "Doing link init.\n");
3991 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3992 return rval;
3993 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003994 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 return (rval);
3997 }
3998
3999 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004000 ql_log(ql_log_info, vha, 0x200a,
4001 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return (QLA_FUNCTION_FAILED);
4003 }
4004
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004005 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 /* initialize */
4008 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4009 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004010 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
4012 switch (topo) {
4013 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004014 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 ha->current_topology = ISP_CFG_NL;
4016 strcpy(connect_type, "(Loop)");
4017 break;
4018
4019 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004020 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004021 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 ha->current_topology = ISP_CFG_FL;
4023 strcpy(connect_type, "(FL_Port)");
4024 break;
4025
4026 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004027 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 ha->operating_mode = P2P;
4029 ha->current_topology = ISP_CFG_N;
4030 strcpy(connect_type, "(N_Port-to-N_Port)");
4031 break;
4032
4033 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004034 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004035 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 ha->operating_mode = P2P;
4037 ha->current_topology = ISP_CFG_F;
4038 strcpy(connect_type, "(F_Port)");
4039 break;
4040
4041 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004042 ql_dbg(ql_dbg_disc, vha, 0x200f,
4043 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 ha->current_topology = ISP_CFG_NL;
4045 strcpy(connect_type, "(Loop)");
4046 break;
4047 }
4048
4049 /* Save Host port and loop ID. */
4050 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07004051 id.b.domain = domain;
4052 id.b.area = area;
4053 id.b.al_pa = al_pa;
4054 id.b.rsvd_1 = 0;
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004055 spin_lock_irqsave(&ha->hardware_lock, flags);
Quinn Tran482c9dc2017-03-15 09:48:54 -07004056 qlt_update_host_map(vha, id);
Quinn Tran9d1aa4e2017-12-28 12:33:27 -08004057 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004058
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004059 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004060 ql_log(ql_log_info, vha, 0x2010,
4061 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004062 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return(rval);
4065}
4066
Giridhar Malavalia9083012010-04-12 17:59:55 -07004067inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004068qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4069 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004070{
4071 char *st, *en;
4072 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004073 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07004074 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08004075 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004076
4077 if (memcmp(model, BINZERO, len) != 0) {
4078 strncpy(ha->model_number, model, len);
4079 st = en = ha->model_number;
4080 en += len - 1;
4081 while (en > st) {
4082 if (*en != 0x20 && *en != 0x00)
4083 break;
4084 *en-- = '\0';
4085 }
4086
4087 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004088 if (use_tbl &&
4089 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004090 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004091 strncpy(ha->model_desc,
4092 qla2x00_model_name[index * 2 + 1],
4093 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004094 } else {
4095 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07004096 if (use_tbl &&
4097 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004098 index < QLA_MODEL_NAMES) {
4099 strcpy(ha->model_number,
4100 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004101 strncpy(ha->model_desc,
4102 qla2x00_model_name[index * 2 + 1],
4103 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004104 } else {
4105 strcpy(ha->model_number, def);
4106 }
4107 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004108 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004109 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07004110 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004111}
4112
David Miller4e08df32007-04-16 12:37:43 -07004113/* On sparc systems, obtain port and node WWN from firmware
4114 * properties.
4115 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004116static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07004117{
4118#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004119 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07004120 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07004121 struct device_node *dp = pci_device_to_OF_node(pdev);
4122 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07004123 int len;
4124
4125 val = of_get_property(dp, "port-wwn", &len);
4126 if (val && len >= WWN_SIZE)
4127 memcpy(nv->port_name, val, WWN_SIZE);
4128
4129 val = of_get_property(dp, "node-wwn", &len);
4130 if (val && len >= WWN_SIZE)
4131 memcpy(nv->node_name, val, WWN_SIZE);
4132#endif
4133}
4134
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135/*
4136* NVRAM configuration for ISP 2xxx
4137*
4138* Input:
4139* ha = adapter block pointer.
4140*
4141* Output:
4142* initialization control block in response_ring
4143* host adapters parameters in host adapter block
4144*
4145* Returns:
4146* 0 = success.
4147*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004148int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004149qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
David Miller4e08df32007-04-16 12:37:43 -07004151 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004152 uint8_t chksum = 0;
4153 uint16_t cnt;
4154 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004155 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004156 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07004157 nvram_t *nv = ha->nvram;
4158 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07004159 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
David Miller4e08df32007-04-16 12:37:43 -07004161 rval = QLA_SUCCESS;
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004164 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 ha->nvram_base = 0;
4166 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4167 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
4168 ha->nvram_base = 0x80;
4169
4170 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004171 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004172 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4173 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004175 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4176 "Contents of NVRAM.\n");
4177 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4178 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 /* Bad NVRAM data, set defaults parameters. */
4181 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
4182 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
4183 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004184 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04004185 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004186 "detected: checksum=0x%x id=%c version=0x%x.\n",
4187 chksum, nv->id[0], nv->nvram_version);
4188 ql_log(ql_log_warn, vha, 0x0065,
4189 "Falling back to "
4190 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07004191
4192 /*
4193 * Set default initialization control block.
4194 */
4195 memset(nv, 0, ha->nvram_size);
4196 nv->parameter_block_version = ICB_VERSION;
4197
4198 if (IS_QLA23XX(ha)) {
4199 nv->firmware_options[0] = BIT_2 | BIT_1;
4200 nv->firmware_options[1] = BIT_7 | BIT_5;
4201 nv->add_firmware_options[0] = BIT_5;
4202 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004203 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07004204 nv->special_options[1] = BIT_7;
4205 } else if (IS_QLA2200(ha)) {
4206 nv->firmware_options[0] = BIT_2 | BIT_1;
4207 nv->firmware_options[1] = BIT_7 | BIT_5;
4208 nv->add_firmware_options[0] = BIT_5;
4209 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004210 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004211 } else if (IS_QLA2100(ha)) {
4212 nv->firmware_options[0] = BIT_3 | BIT_1;
4213 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04004214 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07004215 }
4216
Bart Van Asschead950362015-07-09 07:24:08 -07004217 nv->max_iocb_allocation = cpu_to_le16(256);
4218 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07004219 nv->retry_count = 8;
4220 nv->retry_delay = 1;
4221
4222 nv->port_name[0] = 33;
4223 nv->port_name[3] = 224;
4224 nv->port_name[4] = 139;
4225
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004226 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07004227
4228 nv->login_timeout = 4;
4229
4230 /*
4231 * Set default host adapter parameters
4232 */
4233 nv->host_p[1] = BIT_2;
4234 nv->reset_delay = 5;
4235 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07004236 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07004237 nv->link_down_timeout = 60;
4238
4239 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
4241
4242#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
4243 /*
4244 * The SN2 does not provide BIOS emulation which means you can't change
4245 * potentially bogus BIOS settings. Force the use of default settings
4246 * for link rate and frame size. Hope that the rest of the settings
4247 * are valid.
4248 */
4249 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04004250 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 if (IS_QLA23XX(ha))
4252 nv->special_options[1] = BIT_7;
4253 }
4254#endif
4255
4256 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004257 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
4259 /*
4260 * Setup driver NVRAM options.
4261 */
4262 nv->firmware_options[0] |= (BIT_6 | BIT_1);
4263 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4264 nv->firmware_options[1] |= (BIT_5 | BIT_0);
4265 nv->firmware_options[1] &= ~BIT_4;
4266
4267 if (IS_QLA23XX(ha)) {
4268 nv->firmware_options[0] |= BIT_2;
4269 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004270 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004271 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 if (IS_QLA2300(ha)) {
4274 if (ha->fb_rev == FPM_2310) {
4275 strcpy(ha->model_number, "QLA2310");
4276 } else {
4277 strcpy(ha->model_number, "QLA2300");
4278 }
4279 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004280 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08004281 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
4283 } else if (IS_QLA2200(ha)) {
4284 nv->firmware_options[0] |= BIT_2;
4285 /*
4286 * 'Point-to-point preferred, else loop' is not a safe
4287 * connection mode setting.
4288 */
4289 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4290 (BIT_5 | BIT_4)) {
4291 /* Force 'loop preferred, else point-to-point'. */
4292 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4293 nv->add_firmware_options[0] |= BIT_5;
4294 }
4295 strcpy(ha->model_number, "QLA22xx");
4296 } else /*if (IS_QLA2100(ha))*/ {
4297 strcpy(ha->model_number, "QLA2100");
4298 }
4299
4300 /*
4301 * Copy over NVRAM RISC parameter block to initialization control block.
4302 */
4303 dptr1 = (uint8_t *)icb;
4304 dptr2 = (uint8_t *)&nv->parameter_block_version;
4305 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4306 while (cnt--)
4307 *dptr1++ = *dptr2++;
4308
4309 /* Copy 2nd half. */
4310 dptr1 = (uint8_t *)icb->add_firmware_options;
4311 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4312 while (cnt--)
4313 *dptr1++ = *dptr2++;
4314
Andrew Vasquez5341e862006-05-17 15:09:16 -07004315 /* Use alternate WWN? */
4316 if (nv->host_p[1] & BIT_7) {
4317 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4318 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4319 }
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 /* Prepare nodename */
4322 if ((icb->firmware_options[1] & BIT_6) == 0) {
4323 /*
4324 * Firmware will apply the following mask if the nodename was
4325 * not provided.
4326 */
4327 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4328 icb->node_name[0] &= 0xF0;
4329 }
4330
4331 /*
4332 * Set host adapter parameters.
4333 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07004334
4335 /*
4336 * BIT_7 in the host-parameters section allows for modification to
4337 * internal driver logging.
4338 */
Andrew Vasquez01819442006-06-23 16:11:10 -07004339 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08004340 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4342 /* Always load RISC code on non ISP2[12]00 chips. */
4343 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4344 ha->flags.disable_risc_code_load = 0;
4345 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4346 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4347 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07004348 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07004349 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 ha->operating_mode =
4352 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4353
4354 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4355 sizeof(ha->fw_seriallink_options));
4356
4357 /* save HBA serial number */
4358 ha->serial0 = icb->port_name[5];
4359 ha->serial1 = icb->port_name[6];
4360 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004361 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4362 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Bart Van Asschead950362015-07-09 07:24:08 -07004364 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
4366 ha->retry_count = nv->retry_count;
4367
4368 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07004369 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 nv->login_timeout = ql2xlogintimeout;
4371 if (nv->login_timeout < 4)
4372 nv->login_timeout = 4;
4373 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Andrew Vasquez00a537b2008-02-28 14:06:11 -08004375 /* Set minimum RATOV to 100 tenths of a second. */
4376 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 ha->loop_reset_delay = nv->reset_delay;
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 /* Link Down Timeout = 0:
4381 *
4382 * When Port Down timer expires we will start returning
4383 * I/O's to OS with "DID_NO_CONNECT".
4384 *
4385 * Link Down Timeout != 0:
4386 *
4387 * The driver waits for the link to come up after link down
4388 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004389 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 if (nv->link_down_timeout == 0) {
4391 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04004392 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 } else {
4394 ha->link_down_timeout = nv->link_down_timeout;
4395 ha->loop_down_abort_time =
4396 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07004397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 /*
4400 * Need enough time to try and get the port back.
4401 */
4402 ha->port_down_retry_count = nv->port_down_retry_count;
4403 if (qlport_down_retry)
4404 ha->port_down_retry_count = qlport_down_retry;
4405 /* Set login_retry_count */
4406 ha->login_retry_count = nv->retry_count;
4407 if (ha->port_down_retry_count == nv->port_down_retry_count &&
4408 ha->port_down_retry_count > 3)
4409 ha->login_retry_count = ha->port_down_retry_count;
4410 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4411 ha->login_retry_count = ha->port_down_retry_count;
4412 if (ql2xloginretrycount)
4413 ha->login_retry_count = ql2xloginretrycount;
4414
Bart Van Asschead950362015-07-09 07:24:08 -07004415 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 icb->command_resource_count = 0;
4417 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07004418 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
4420 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4421 /* Enable RIO */
4422 icb->firmware_options[0] &= ~BIT_3;
4423 icb->add_firmware_options[0] &=
4424 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4425 icb->add_firmware_options[0] |= BIT_2;
4426 icb->response_accumulation_timer = 3;
4427 icb->interrupt_delay_timer = 5;
4428
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004429 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004431 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004432 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004433 ha->zio_mode = icb->add_firmware_options[0] &
4434 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4435 ha->zio_timer = icb->interrupt_delay_timer ?
4436 icb->interrupt_delay_timer: 2;
4437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 icb->add_firmware_options[0] &=
4439 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004440 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004441 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08004442 ha->zio_mode = QLA_ZIO_MODE_6;
4443
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004444 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004445 "ZIO mode %d enabled; timer delay (%d us).\n",
4446 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07004448 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4449 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004450 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 }
4452 }
4453
David Miller4e08df32007-04-16 12:37:43 -07004454 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004455 ql_log(ql_log_warn, vha, 0x0069,
4456 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07004457 }
4458 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004461static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004462qla2x00_rport_del(void *data)
4463{
4464 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004465 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004466 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004467
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004468 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07004469 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004470 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004471 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004472 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004473 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
4474 "%s %8phN. rport %p roles %x\n",
4475 __func__, fcport->port_name, rport,
4476 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08004477
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004478 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08004479 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004480}
4481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482/**
4483 * qla2x00_alloc_fcport() - Allocate a generic fcport.
Bart Van Assche2db62282018-01-23 16:33:51 -08004484 * @vha: HA context
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 * @flags: allocation flags
4486 *
4487 * Returns a pointer to the allocated fcport, or NULL, if none available.
4488 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08004489fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004490qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
4492 fc_port_t *fcport;
4493
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004494 fcport = kzalloc(sizeof(fc_port_t), flags);
4495 if (!fcport)
4496 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004499 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 fcport->port_type = FCT_UNKNOWN;
4501 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07004502 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004503 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
Quinn Tran726b8542017-01-19 22:28:00 -08004505 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
4506 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08004507 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08004508 fcport->disc_state = DSC_DELETED;
4509 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4510 fcport->deleted = QLA_SESS_DELETED;
4511 fcport->login_retry = vha->hw->login_retry_count;
4512 fcport->login_retry = 5;
4513 fcport->logout_on_delete = 1;
4514
4515 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004516 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08004517 "Failed to allocate ct_sns request.\n");
4518 kfree(fcport);
4519 fcport = NULL;
4520 }
4521 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
4522 INIT_LIST_HEAD(&fcport->gnl_entry);
4523 INIT_LIST_HEAD(&fcport->list);
4524
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004525 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526}
4527
Quinn Tran726b8542017-01-19 22:28:00 -08004528void
4529qla2x00_free_fcport(fc_port_t *fcport)
4530{
4531 if (fcport->ct_desc.ct_sns) {
4532 dma_free_coherent(&fcport->vha->hw->pdev->dev,
4533 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
4534 fcport->ct_desc.ct_sns_dma);
4535
4536 fcport->ct_desc.ct_sns = NULL;
4537 }
4538 kfree(fcport);
4539}
4540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541/*
4542 * qla2x00_configure_loop
4543 * Updates Fibre Channel Device Database with what is actually on loop.
4544 *
4545 * Input:
4546 * ha = adapter block pointer.
4547 *
4548 * Returns:
4549 * 0 = success.
4550 * 1 = error.
4551 * 2 = database was full and device was not configured.
4552 */
4553static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004554qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
4556 int rval;
4557 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004558 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 rval = QLA_SUCCESS;
4560
4561 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004562 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
4563 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004565 ql_dbg(ql_dbg_disc, vha, 0x2013,
4566 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 return (rval);
4568 }
4569 }
4570
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004571 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004572 ql_dbg(ql_dbg_disc, vha, 0x2014,
4573 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
4575 /*
4576 * If we have both an RSCN and PORT UPDATE pending then handle them
4577 * both at the same time.
4578 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004579 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
4580 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Michael Hernandez3064ff32009-12-15 21:29:44 -08004582 qla2x00_get_data_rate(vha);
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 /* Determine what we need to do */
4585 if (ha->current_topology == ISP_CFG_FL &&
4586 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 set_bit(RSCN_UPDATE, &flags);
4589
4590 } else if (ha->current_topology == ISP_CFG_F &&
4591 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 set_bit(RSCN_UPDATE, &flags);
4594 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4595
Andrew Vasquez21333b42006-05-17 15:09:56 -07004596 } else if (ha->current_topology == ISP_CFG_N) {
4597 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004598 if (ha->flags.rida_fmt2) {
4599 /* With Rida Format 2, the login is already triggered.
4600 * We know who is on the other side of the wire.
4601 * No need to login to do login to find out or drop into
4602 * qla2x00_configure_local_loop().
4603 */
4604 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4605 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4606 } else {
4607 if (qla_tgt_mode_enabled(vha)) {
4608 /* allow the other side to start the login */
4609 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4610 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
4611 }
4612 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004613 } else if (ha->current_topology == ISP_CFG_NL) {
4614 clear_bit(RSCN_UPDATE, &flags);
4615 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004616 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 set_bit(RSCN_UPDATE, &flags);
4619 set_bit(LOCAL_LOOP_UPDATE, &flags);
4620 }
4621
4622 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004623 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4624 ql_dbg(ql_dbg_disc, vha, 0x2015,
4625 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004627 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004628 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
4630
4631 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004632 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004633 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004634 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004636 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004637 else
4638 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
4640
4641 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004642 if (atomic_read(&vha->loop_down_timer) ||
4643 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 rval = QLA_FUNCTION_FAILED;
4645 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004646 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004647 ql_dbg(ql_dbg_disc, vha, 0x2069,
4648 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004649 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004650
4651 /*
4652 * Process any ATIO queue entries that came in
4653 * while we weren't online.
4654 */
Quinn Tranead03852017-01-19 22:28:01 -08004655 if (qla_tgt_mode_enabled(vha) ||
4656 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004657 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4658 spin_lock_irqsave(&ha->tgt.atio_lock,
4659 flags);
4660 qlt_24xx_process_atio_queue(vha, 0);
4661 spin_unlock_irqrestore(
4662 &ha->tgt.atio_lock, flags);
4663 } else {
4664 spin_lock_irqsave(&ha->hardware_lock,
4665 flags);
4666 qlt_24xx_process_atio_queue(vha, 1);
4667 spin_unlock_irqrestore(
4668 &ha->hardware_lock, flags);
4669 }
4670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 }
4672 }
4673
4674 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004675 ql_dbg(ql_dbg_disc, vha, 0x206a,
4676 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004678 ql_dbg(ql_dbg_disc, vha, 0x206b,
4679 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 }
4681
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004682 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004683 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004685 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004686 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004687 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690
4691 return (rval);
4692}
4693
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004694/*
4695 * N2N Login
4696 * Updates Fibre Channel Device Database with local loop devices.
4697 *
4698 * Input:
4699 * ha = adapter block pointer.
4700 *
4701 * Returns:
4702 */
4703static int qla24xx_n2n_handle_login(struct scsi_qla_host *vha,
4704 fc_port_t *fcport)
4705{
4706 struct qla_hw_data *ha = vha->hw;
4707 int res = QLA_SUCCESS, rval;
4708 int greater_wwpn = 0;
4709 int logged_in = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004711 if (ha->current_topology != ISP_CFG_N)
4712 return res;
4713
4714 if (wwn_to_u64(vha->port_name) >
4715 wwn_to_u64(vha->n2n_port_name)) {
4716 ql_dbg(ql_dbg_disc, vha, 0x2002,
4717 "HBA WWPN is greater %llx > target %llx\n",
4718 wwn_to_u64(vha->port_name),
4719 wwn_to_u64(vha->n2n_port_name));
4720 greater_wwpn = 1;
4721 fcport->d_id.b24 = vha->n2n_id;
4722 }
4723
4724 fcport->loop_id = vha->loop_id;
4725 fcport->fc4f_nvme = 0;
4726 fcport->query = 1;
4727
4728 ql_dbg(ql_dbg_disc, vha, 0x4001,
4729 "Initiate N2N login handler: HBA port_id=%06x loopid=%d\n",
4730 fcport->d_id.b24, vha->loop_id);
4731
4732 /* Fill in member data. */
4733 if (!greater_wwpn) {
4734 rval = qla2x00_get_port_database(vha, fcport, 0);
4735 ql_dbg(ql_dbg_disc, vha, 0x1051,
4736 "Remote login-state (%x/%x) port_id=%06x loop_id=%x, rval=%d\n",
4737 fcport->current_login_state, fcport->last_login_state,
4738 fcport->d_id.b24, fcport->loop_id, rval);
4739
4740 if (((fcport->current_login_state & 0xf) == 0x4) ||
4741 ((fcport->current_login_state & 0xf) == 0x6))
4742 logged_in = 1;
4743 }
4744
4745 if (logged_in || greater_wwpn) {
4746 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
4747 qla_nvme_register_hba(vha);
4748
4749 /* Set connected N_Port d_id */
4750 if (vha->flags.nvme_enabled)
4751 fcport->fc4f_nvme = 1;
4752
4753 fcport->scan_state = QLA_FCPORT_FOUND;
4754 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
4755 fcport->disc_state = DSC_GNL;
4756 fcport->n2n_flag = 1;
4757 fcport->flags = 3;
4758 vha->hw->flags.gpsc_supported = 0;
4759
4760 if (greater_wwpn) {
4761 ql_dbg(ql_dbg_disc, vha, 0x20e5,
4762 "%s %d PLOGI ELS %8phC\n",
4763 __func__, __LINE__, fcport->port_name);
4764
4765 res = qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
4766 fcport, fcport->d_id);
4767 }
4768
4769 if (res != QLA_SUCCESS) {
4770 ql_log(ql_log_info, vha, 0xd04d,
4771 "PLOGI Failed: portid=%06x - retrying\n",
4772 fcport->d_id.b24);
4773 res = QLA_SUCCESS;
4774 } else {
4775 /* State 0x6 means FCP PRLI complete */
4776 if ((fcport->current_login_state & 0xf) == 0x6) {
4777 ql_dbg(ql_dbg_disc, vha, 0x2118,
4778 "%s %d %8phC post GPDB work\n",
4779 __func__, __LINE__, fcport->port_name);
4780 fcport->chip_reset =
4781 vha->hw->base_qpair->chip_reset;
4782 qla24xx_post_gpdb_work(vha, fcport, 0);
4783 } else {
4784 ql_dbg(ql_dbg_disc, vha, 0x2118,
4785 "%s %d %8phC post NVMe PRLI\n",
4786 __func__, __LINE__, fcport->port_name);
4787 qla24xx_post_prli_work(vha, fcport);
4788 }
4789 }
4790 } else {
4791 /* Wait for next database change */
4792 set_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags);
4793 }
4794
4795 return res;
4796}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798/*
4799 * qla2x00_configure_local_loop
4800 * Updates Fibre Channel Device Database with local loop devices.
4801 *
4802 * Input:
4803 * ha = adapter block pointer.
4804 *
4805 * Returns:
4806 * 0 = success.
4807 */
4808static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004809qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
4811 int rval, rval2;
4812 int found_devs;
4813 int found;
4814 fc_port_t *fcport, *new_fcport;
4815
4816 uint16_t index;
4817 uint16_t entries;
4818 char *id_iter;
4819 uint16_t loop_id;
4820 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004821 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004822 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 found_devs = 0;
4825 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004826 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004829 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004830 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 &entries);
4832 if (rval != QLA_SUCCESS)
4833 goto cleanup_allocation;
4834
Quinn Tran83548fe2017-06-02 09:12:01 -07004835 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004836 "Entries in ID list (%d).\n", entries);
4837 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4838 (uint8_t *)ha->gid_list,
4839 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Quinn Tran9cd883f2017-12-28 12:33:24 -08004841 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4842 fcport->scan_state = QLA_FCPORT_SCAN;
4843 }
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004846 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004848 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004849 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 rval = QLA_MEMORY_ALLOC_FAILED;
4851 goto cleanup_allocation;
4852 }
4853 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4854
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004855 /* Inititae N2N login. */
4856 if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags)) {
4857 rval = qla24xx_n2n_handle_login(vha, new_fcport);
4858 if (rval != QLA_SUCCESS)
4859 goto cleanup_allocation;
4860 return QLA_SUCCESS;
4861 }
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 /* Add devices to port list. */
4864 id_iter = (char *)ha->gid_list;
4865 for (index = 0; index < entries; index++) {
4866 domain = ((struct gid_list_info *)id_iter)->domain;
4867 area = ((struct gid_list_info *)id_iter)->area;
4868 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004869 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 loop_id = (uint16_t)
4871 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004872 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 loop_id = le16_to_cpu(
4874 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004875 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 /* Bypass reserved domain fields. */
4878 if ((domain & 0xf0) == 0xf0)
4879 continue;
4880
4881 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004882 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004883 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 continue;
4885
4886 /* Bypass invalid local loop ID. */
4887 if (loop_id > LAST_LOCAL_LOOP_ID)
4888 continue;
4889
Quinn Tran41dc5292017-01-19 22:28:03 -08004890 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 /* Fill in member data. */
4893 new_fcport->d_id.b.domain = domain;
4894 new_fcport->d_id.b.area = area;
4895 new_fcport->d_id.b.al_pa = al_pa;
4896 new_fcport->loop_id = loop_id;
Quinn Tran9cd883f2017-12-28 12:33:24 -08004897 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran41dc5292017-01-19 22:28:03 -08004898
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004899 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004901 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004902 "Failed to retrieve fcport information "
4903 "-- get_port_database=%x, loop_id=0x%04x.\n",
4904 rval2, new_fcport->loop_id);
Duane Grigsbyedd05de2017-10-13 09:34:06 -07004905 /* Skip retry if N2N */
4906 if (ha->current_topology != ISP_CFG_N) {
4907 ql_dbg(ql_dbg_disc, vha, 0x2105,
4908 "Scheduling resync.\n");
4909 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4910 continue;
4911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
4913
Quinn Tran41dc5292017-01-19 22:28:03 -08004914 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 /* Check for matching device in port list. */
4916 found = 0;
4917 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004918 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 if (memcmp(new_fcport->port_name, fcport->port_name,
4920 WWN_SIZE))
4921 continue;
4922
Shyam Sundarddb9b122009-03-24 09:08:10 -07004923 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 fcport->loop_id = new_fcport->loop_id;
4925 fcport->port_type = new_fcport->port_type;
4926 fcport->d_id.b24 = new_fcport->d_id.b24;
4927 memcpy(fcport->node_name, new_fcport->node_name,
4928 WWN_SIZE);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004929 fcport->scan_state = QLA_FCPORT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 found++;
4931 break;
4932 }
4933
4934 if (!found) {
4935 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004936 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 /* Allocate a new replacement fcport. */
4939 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004940
4941 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4942
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004943 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004946 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004947 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 rval = QLA_MEMORY_ALLOC_FAILED;
4949 goto cleanup_allocation;
4950 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004951 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4953 }
4954
Quinn Tran41dc5292017-01-19 22:28:03 -08004955 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4956
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004957 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004958 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 found_devs++;
4961 }
4962
Quinn Tran9cd883f2017-12-28 12:33:24 -08004963 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4964 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4965 break;
4966
4967 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4968 if ((qla_dual_mode_enabled(vha) ||
4969 qla_ini_mode_enabled(vha)) &&
4970 atomic_read(&fcport->state) == FCS_ONLINE) {
4971 qla2x00_mark_device_lost(vha, fcport,
4972 ql2xplogiabsentdevice, 0);
4973 if (fcport->loop_id != FC_NO_LOOP_ID &&
4974 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4975 fcport->port_type != FCT_INITIATOR &&
4976 fcport->port_type != FCT_BROADCAST) {
4977 ql_dbg(ql_dbg_disc, vha, 0x20f0,
4978 "%s %d %8phC post del sess\n",
4979 __func__, __LINE__,
4980 fcport->port_name);
4981
Quinn Trand8630bb2017-12-28 12:33:43 -08004982 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran9cd883f2017-12-28 12:33:24 -08004983 continue;
4984 }
4985 }
4986 }
4987
4988 if (fcport->scan_state == QLA_FCPORT_FOUND)
4989 qla24xx_fcport_handle_login(vha, fcport);
4990 }
4991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004993 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
4995 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004996 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004997 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 }
4999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 return (rval);
5001}
5002
5003static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005004qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005005{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005006 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04005007 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005008 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005009
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07005010 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005011 return;
5012
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07005013 if (atomic_read(&fcport->state) != FCS_ONLINE)
5014 return;
5015
Andrew Vasquez39bd9622007-09-20 14:07:34 -07005016 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5017 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005018 return;
5019
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005020 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07005021 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005022 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005023 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005024 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5025 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005026 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005027 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005028 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05005029 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04005030 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005031 }
5032}
5033
Quinn Tran726b8542017-01-19 22:28:00 -08005034/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01005035static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005036qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05005037{
5038 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05005039 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005040 unsigned long flags;
8482e1182005-04-17 15:04:54 -05005041
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07005042 rport_ids.node_name = wwn_to_u64(fcport->node_name);
5043 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05005044 rport_ids.port_id = fcport->d_id.b.domain << 16 |
5045 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5046 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005047 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07005048 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005049 ql_log(ql_log_warn, vha, 0x2006,
5050 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07005051 return;
5052 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005053
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005054 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04005055 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08005056 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005057
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005058 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07005059
5060 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05005061 if (fcport->port_type == FCT_INITIATOR)
5062 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
5063 if (fcport->port_type == FCT_TARGET)
5064 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08005065
Quinn Tran83548fe2017-06-02 09:12:01 -07005066 ql_dbg(ql_dbg_disc, vha, 0x20ee,
5067 "%s %8phN. rport %p is %s mode\n",
5068 __func__, fcport->port_name, rport,
5069 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08005070
Andrew Vasquez77d74142005-07-08 18:00:36 -07005071 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05005072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
5074/*
Adrian Bunk23be3312006-11-24 02:46:01 +01005075 * qla2x00_update_fcport
5076 * Updates device on list.
5077 *
5078 * Input:
5079 * ha = adapter block pointer.
5080 * fcport = port structure pointer.
5081 *
5082 * Return:
5083 * 0 - Success
5084 * BIT_0 - error
5085 *
5086 * Context:
5087 * Kernel context.
5088 */
5089void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005090qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01005091{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005092 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005093
Quinn Tran726b8542017-01-19 22:28:00 -08005094 if (IS_SW_RESV_ADDR(fcport->d_id))
5095 return;
5096
Quinn Tran83548fe2017-06-02 09:12:01 -07005097 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08005098 __func__, fcport->port_name);
5099
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005100 if (IS_QLAFX00(vha->hw)) {
5101 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005102 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005103 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005104 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07005105 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08005106 fcport->disc_state = DSC_LOGIN_COMPLETE;
5107 fcport->deleted = 0;
5108 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01005109
Darren Trappdbe18012018-03-20 23:09:31 -07005110 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5111 qla2x00_iidma_fcport(vha, fcport);
5112
Duane Grigsbye84067d2017-06-21 13:48:43 -07005113 if (fcport->fc4f_nvme) {
5114 qla_nvme_register_remote(vha, fcport);
5115 return;
5116 }
5117
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005118 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005119
5120reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005121 switch (vha->host->active_mode) {
5122 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005123 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005124 break;
5125 case MODE_TARGET:
5126 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5127 !vha->vha_tgt.qla_tgt->tgt_stopped)
5128 qlt_fc_port_added(vha, fcport);
5129 break;
5130 case MODE_DUAL:
5131 qla2x00_reg_remote_port(vha, fcport);
5132 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5133 !vha->vha_tgt.qla_tgt->tgt_stopped)
5134 qlt_fc_port_added(vha, fcport);
5135 break;
5136 default:
5137 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005138 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005139}
5140
5141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 * qla2x00_configure_fabric
5143 * Setup SNS devices with loop ID's.
5144 *
5145 * Input:
5146 * ha = adapter block pointer.
5147 *
5148 * Returns:
5149 * 0 = success.
5150 * BIT_0 = error
5151 */
5152static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005153qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154{
Arun Easib3b02e62012-02-09 11:15:39 -08005155 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005156 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005158 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005160 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005161 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005164 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005165 loop_id = NPH_F_PORT;
5166 else
5167 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005168 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005170 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005171 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005173 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return (QLA_SUCCESS);
5175 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005176 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Quinn Tran41dc5292017-01-19 22:28:03 -08005178
5179 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5180 rval = qla2x00_send_change_request(vha, 0x3, 0);
5181 if (rval != QLA_SUCCESS)
5182 ql_log(ql_log_warn, vha, 0x121,
5183 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5184 rval);
5185 }
5186
5187
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005189 qla2x00_mgmt_svr_login(vha);
5190
Andrew Vasquezcca53352005-08-26 19:08:30 -07005191 /* FDMI support. */
5192 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005193 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5194 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005197 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005198 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5199 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005200 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5201 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5202 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5203 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005204 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005205 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005206 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005207 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5208 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005210 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005211 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005212 if (test_bit(LOOP_RESYNC_NEEDED,
5213 &vha->dpc_flags))
5214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005216 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005218 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005219 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005220 if (test_bit(LOOP_RESYNC_NEEDED,
5221 &vha->dpc_flags))
5222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005224 if (vha->flags.nvme_enabled) {
5225 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5226 ql_dbg(ql_dbg_disc, vha, 0x2049,
5227 "Register NVME FC Type Features failed.\n");
5228 }
5229 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005230 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005232 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005233 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005234 if (test_bit(LOOP_RESYNC_NEEDED,
5235 &vha->dpc_flags))
5236 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005237 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005239 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005240 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005241 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244 }
5245
Joe Carnuccio827210b2013-02-08 01:57:57 -05005246
Alexei Potashnikdf673272015-07-14 16:00:46 -04005247 /* Mark the time right before querying FW for connected ports.
5248 * This process is long, asynchronous and by the time it's done,
5249 * collected information might not be accurate anymore. E.g.
5250 * disconnected port might have re-connected and a brand new
5251 * session has been created. In this case session's generation
5252 * will be newer than discovery_gen. */
5253 qlt_do_generation_tick(vha, &discovery_gen);
5254
Quinn Trana4239942017-12-28 12:33:26 -08005255 if (USE_ASYNC_SCAN(ha)) {
5256 rval = QLA_SUCCESS;
5257 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5258 if (rval)
5259 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5260 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005261 list_for_each_entry(fcport, &vha->vp_fcports, list)
5262 fcport->scan_state = QLA_FCPORT_SCAN;
5263
Quinn Trana4239942017-12-28 12:33:26 -08005264 rval = qla2x00_find_all_fabric_devs(vha);
5265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 if (rval != QLA_SUCCESS)
5267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 } while (0);
5269
Duane Grigsbye84067d2017-06-21 13:48:43 -07005270 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5271 qla_nvme_register_hba(vha);
5272
Quinn Tran726b8542017-01-19 22:28:00 -08005273 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005274 ql_dbg(ql_dbg_disc, vha, 0x2068,
5275 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
5277 return (rval);
5278}
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280/*
5281 * qla2x00_find_all_fabric_devs
5282 *
5283 * Input:
5284 * ha = adapter block pointer.
5285 * dev = database device entry pointer.
5286 *
5287 * Returns:
5288 * 0 = success.
5289 *
5290 * Context:
5291 * Kernel context.
5292 */
5293static int
Quinn Tran726b8542017-01-19 22:28:00 -08005294qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295{
5296 int rval;
5297 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005298 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 int found;
5300
5301 sw_info_t *swl;
5302 int swl_idx;
5303 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005304 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005305 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005306 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005307 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
5309 rval = QLA_SUCCESS;
5310
5311 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005312 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005313 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005314 GFP_KERNEL);
5315 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005316 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005318 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005319 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005321 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005322 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005324 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5325 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005326 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005328 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5329 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005330 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005332 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5333 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005334 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5335 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005336 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5337 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005339
5340 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005341 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005342 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005343 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5344 return rval;
5345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
5347 swl_idx = 0;
5348
5349 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005350 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005352 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005353 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 return (QLA_MEMORY_ALLOC_FAILED);
5355 }
5356 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 /* Set start port ID scan at adapter ID. */
5358 first_dev = 1;
5359 last_dev = 0;
5360
5361 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005362 loop_id = ha->min_external_loopid;
5363 for (; loop_id <= ha->max_loop_id; loop_id++) {
5364 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 continue;
5366
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005367 if (ha->current_topology == ISP_CFG_FL &&
5368 (atomic_read(&vha->loop_down_timer) ||
5369 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005370 atomic_set(&vha->loop_down_timer, 0);
5371 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5372 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
5376 if (swl != NULL) {
5377 if (last_dev) {
5378 wrap.b24 = new_fcport->d_id.b24;
5379 } else {
5380 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5381 memcpy(new_fcport->node_name,
5382 swl[swl_idx].node_name, WWN_SIZE);
5383 memcpy(new_fcport->port_name,
5384 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005385 memcpy(new_fcport->fabric_port_name,
5386 swl[swl_idx].fabric_port_name, WWN_SIZE);
5387 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005388 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005390 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005391 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005392 if (vha->flags.nvme_enabled &&
5393 swl[swl_idx].fc4f_nvme) {
5394 new_fcport->fc4f_nvme =
5395 swl[swl_idx].fc4f_nvme;
5396 ql_log(ql_log_info, vha, 0x2131,
5397 "FOUND: NVME port %8phC as FC Type 28h\n",
5398 new_fcport->port_name);
5399 }
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5402 last_dev = 1;
5403 }
5404 swl_idx++;
5405 }
5406 } else {
5407 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005408 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005410 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005411 "SNS scan failed -- assuming "
5412 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 rval = QLA_SUCCESS;
5414 break;
5415 }
5416 }
5417
5418 /* If wrap on switch device list, exit. */
5419 if (first_dev) {
5420 wrap.b24 = new_fcport->d_id.b24;
5421 first_dev = 0;
5422 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005423 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005424 "Device wrap (%02x%02x%02x).\n",
5425 new_fcport->d_id.b.domain,
5426 new_fcport->d_id.b.area,
5427 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 break;
5429 }
5430
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005431 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005432 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 continue;
5434
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005435 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005436 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5437 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005438
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005439 /* Bypass if same domain and area of adapter. */
5440 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005441 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005442 ISP_CFG_FL)
5443 continue;
5444
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 /* Bypass reserved domain fields. */
5446 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5447 continue;
5448
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005449 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005450 if (ql2xgffidenable &&
5451 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5452 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005453 continue;
5454
Quinn Tran726b8542017-01-19 22:28:00 -08005455 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5456
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 /* Locate matching device in database. */
5458 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005459 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 if (memcmp(new_fcport->port_name, fcport->port_name,
5461 WWN_SIZE))
5462 continue;
5463
Joe Carnuccio827210b2013-02-08 01:57:57 -05005464 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 found++;
5467
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005468 /* Update port state. */
5469 memcpy(fcport->fabric_port_name,
5470 new_fcport->fabric_port_name, WWN_SIZE);
5471 fcport->fp_speed = new_fcport->fp_speed;
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005474 * If address the same and state FCS_ONLINE
5475 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 */
5477 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005478 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005479 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 break;
5481 }
5482
5483 /*
5484 * If device was not a fabric device before.
5485 */
5486 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5487 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005488 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 fcport->flags |= (FCF_FABRIC_DEVICE |
5490 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 break;
5492 }
5493
5494 /*
5495 * Port ID changed or device was marked to be updated;
5496 * Log it out if still logged in and mark it for
5497 * relogin later.
5498 */
Quinn Tran726b8542017-01-19 22:28:00 -08005499 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005500 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5501 "port changed FC ID, %8phC"
5502 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5503 fcport->port_name,
5504 fcport->d_id.b.domain,
5505 fcport->d_id.b.area,
5506 fcport->d_id.b.al_pa,
5507 fcport->loop_id,
5508 new_fcport->d_id.b.domain,
5509 new_fcport->d_id.b.area,
5510 new_fcport->d_id.b.al_pa);
5511 fcport->d_id.b24 = new_fcport->d_id.b24;
5512 break;
5513 }
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 fcport->d_id.b24 = new_fcport->d_id.b24;
5516 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 break;
5518 }
5519
Darren Trapp9dd96862018-03-20 23:09:32 -07005520 if (fcport->fc4f_nvme) {
5521 if (fcport->disc_state == DSC_DELETE_PEND) {
5522 fcport->disc_state = DSC_GNL;
5523 vha->fcport_count--;
5524 fcport->login_succ = 0;
5525 }
5526 }
5527
Quinn Tran726b8542017-01-19 22:28:00 -08005528 if (found) {
5529 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005533 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005534 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5535
5536 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5537
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
5539 /* Allocate a new replacement fcport. */
5540 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005541 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005543 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005544 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 return (QLA_MEMORY_ALLOC_FAILED);
5546 }
5547 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5548 new_fcport->d_id.b24 = nxt_d_id.b24;
5549 }
5550
Quinn Tran726b8542017-01-19 22:28:00 -08005551 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Quinn Tran726b8542017-01-19 22:28:00 -08005553 /*
5554 * Logout all previous fabric dev marked lost, except FCP2 devices.
5555 */
5556 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5557 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5558 break;
5559
5560 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5561 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5562 continue;
5563
5564 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5565 if ((qla_dual_mode_enabled(vha) ||
5566 qla_ini_mode_enabled(vha)) &&
5567 atomic_read(&fcport->state) == FCS_ONLINE) {
5568 qla2x00_mark_device_lost(vha, fcport,
5569 ql2xplogiabsentdevice, 0);
5570 if (fcport->loop_id != FC_NO_LOOP_ID &&
5571 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5572 fcport->port_type != FCT_INITIATOR &&
5573 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005574 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005575 "%s %d %8phC post del sess\n",
5576 __func__, __LINE__,
5577 fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08005578 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005579 continue;
5580 }
5581 }
5582 }
5583
5584 if (fcport->scan_state == QLA_FCPORT_FOUND)
5585 qla24xx_fcport_handle_login(vha, fcport);
5586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 return (rval);
5588}
5589
5590/*
5591 * qla2x00_find_new_loop_id
5592 * Scan through our port list and find a new usable loop ID.
5593 *
5594 * Input:
5595 * ha: adapter state pointer.
5596 * dev: port structure pointer.
5597 *
5598 * Returns:
5599 * qla2x00 local function return status code.
5600 *
5601 * Context:
5602 * Kernel context.
5603 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005604int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005605qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
5607 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005608 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005609 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 rval = QLA_SUCCESS;
5612
Chad Dupuis5f16b332012-08-22 14:21:00 -04005613 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Chad Dupuis5f16b332012-08-22 14:21:00 -04005615 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5616 LOOPID_MAP_SIZE);
5617 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5618 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5619 dev->loop_id = FC_NO_LOOP_ID;
5620 rval = QLA_FUNCTION_FAILED;
5621 } else
5622 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
Chad Dupuis5f16b332012-08-22 14:21:00 -04005624 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Chad Dupuis5f16b332012-08-22 14:21:00 -04005626 if (rval == QLA_SUCCESS)
5627 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5628 "Assigning new loopid=%x, portid=%x.\n",
5629 dev->loop_id, dev->d_id.b24);
5630 else
5631 ql_log(ql_log_warn, dev->vha, 0x2087,
5632 "No loop_id's available, portid=%x.\n",
5633 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
5635 return (rval);
5636}
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
5639/*
5640 * qla2x00_fabric_login
5641 * Issue fabric login command.
5642 *
5643 * Input:
5644 * ha = adapter block pointer.
5645 * device = pointer to FC device type structure.
5646 *
5647 * Returns:
5648 * 0 - Login successfully
5649 * 1 - Login failed
5650 * 2 - Initiator device
5651 * 3 - Fatal error
5652 */
5653int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005654qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 uint16_t *next_loopid)
5656{
5657 int rval;
5658 int retry;
5659 uint16_t tmp_loopid;
5660 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005661 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
5663 retry = 0;
5664 tmp_loopid = 0;
5665
5666 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005667 ql_dbg(ql_dbg_disc, vha, 0x2000,
5668 "Trying Fabric Login w/loop id 0x%04x for port "
5669 "%02x%02x%02x.\n",
5670 fcport->loop_id, fcport->d_id.b.domain,
5671 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005674 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 fcport->d_id.b.domain, fcport->d_id.b.area,
5676 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005677 if (rval != QLA_SUCCESS) {
5678 return rval;
5679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 if (mb[0] == MBS_PORT_ID_USED) {
5681 /*
5682 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005683 * recommends the driver perform an implicit login with
5684 * the specified ID again. The ID we just used is save
5685 * here so we return with an ID that can be tried by
5686 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 */
5688 retry++;
5689 tmp_loopid = fcport->loop_id;
5690 fcport->loop_id = mb[1];
5691
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005692 ql_dbg(ql_dbg_disc, vha, 0x2001,
5693 "Fabric Login: port in use - next loop "
5694 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005696 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5699 /*
5700 * Login succeeded.
5701 */
5702 if (retry) {
5703 /* A retry occurred before. */
5704 *next_loopid = tmp_loopid;
5705 } else {
5706 /*
5707 * No retry occurred before. Just increment the
5708 * ID value for next login.
5709 */
5710 *next_loopid = (fcport->loop_id + 1);
5711 }
5712
5713 if (mb[1] & BIT_0) {
5714 fcport->port_type = FCT_INITIATOR;
5715 } else {
5716 fcport->port_type = FCT_TARGET;
5717 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005718 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 }
5720 }
5721
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005722 if (mb[10] & BIT_0)
5723 fcport->supported_classes |= FC_COS_CLASS2;
5724 if (mb[10] & BIT_1)
5725 fcport->supported_classes |= FC_COS_CLASS3;
5726
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005727 if (IS_FWI2_CAPABLE(ha)) {
5728 if (mb[10] & BIT_7)
5729 fcport->flags |=
5730 FCF_CONF_COMP_SUPPORTED;
5731 }
5732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 rval = QLA_SUCCESS;
5734 break;
5735 } else if (mb[0] == MBS_LOOP_ID_USED) {
5736 /*
5737 * Loop ID already used, try next loop ID.
5738 */
5739 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005740 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 if (rval != QLA_SUCCESS) {
5742 /* Ran out of loop IDs to use */
5743 break;
5744 }
5745 } else if (mb[0] == MBS_COMMAND_ERROR) {
5746 /*
5747 * Firmware possibly timed out during login. If NO
5748 * retries are left to do then the device is declared
5749 * dead.
5750 */
5751 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005752 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005753 fcport->d_id.b.domain, fcport->d_id.b.area,
5754 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005755 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 rval = 1;
5758 break;
5759 } else {
5760 /*
5761 * unrecoverable / not handled error
5762 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005763 ql_dbg(ql_dbg_disc, vha, 0x2002,
5764 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5765 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5766 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5767 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005770 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005771 fcport->d_id.b.domain, fcport->d_id.b.area,
5772 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005773 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005774 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775
5776 rval = 3;
5777 break;
5778 }
5779 }
5780
5781 return (rval);
5782}
5783
5784/*
5785 * qla2x00_local_device_login
5786 * Issue local device login command.
5787 *
5788 * Input:
5789 * ha = adapter block pointer.
5790 * loop_id = loop id of device to login to.
5791 *
5792 * Returns (Where's the #define!!!!):
5793 * 0 - Login successfully
5794 * 1 - Login failed
5795 * 3 - Fatal error
5796 */
5797int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005798qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799{
5800 int rval;
5801 uint16_t mb[MAILBOX_REGISTER_COUNT];
5802
5803 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005804 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 if (rval == QLA_SUCCESS) {
5806 /* Interrogate mailbox registers for any errors */
5807 if (mb[0] == MBS_COMMAND_ERROR)
5808 rval = 1;
5809 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5810 /* device not in PCB table */
5811 rval = 3;
5812 }
5813
5814 return (rval);
5815}
5816
5817/*
5818 * qla2x00_loop_resync
5819 * Resync with fibre channel devices.
5820 *
5821 * Input:
5822 * ha = adapter block pointer.
5823 *
5824 * Returns:
5825 * 0 = success
5826 */
5827int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005828qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005830 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005832 struct req_que *req;
5833 struct rsp_que *rsp;
5834
Michael Hernandezd7459522016-12-12 14:40:07 -08005835 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005836 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005838 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5839 if (vha->flags.online) {
5840 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5842 wait_time = 256;
5843 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005844 if (!IS_QLAFX00(vha->hw)) {
5845 /*
5846 * Issue a marker after FW becomes
5847 * ready.
5848 */
5849 qla2x00_marker(vha, req, rsp, 0, 0,
5850 MK_SYNC_ALL);
5851 vha->marker_needed = 0;
5852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
5854 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005855 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005857 if (IS_QLAFX00(vha->hw))
5858 qlafx00_configure_devices(vha);
5859 else
5860 qla2x00_configure_loop(vha);
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005863 } while (!atomic_read(&vha->loop_down_timer) &&
5864 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5865 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5866 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 }
5869
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005870 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005873 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005874 ql_dbg(ql_dbg_disc, vha, 0x206c,
5875 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
5877 return (rval);
5878}
5879
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005880/*
5881* qla2x00_perform_loop_resync
5882* Description: This function will set the appropriate flags and call
5883* qla2x00_loop_resync. If successful loop will be resynced
5884* Arguments : scsi_qla_host_t pointer
5885* returm : Success or Failure
5886*/
5887
5888int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5889{
5890 int32_t rval = 0;
5891
5892 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5893 /*Configure the flags so that resync happens properly*/
5894 atomic_set(&ha->loop_down_timer, 0);
5895 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5896 atomic_set(&ha->loop_state, LOOP_UP);
5897 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5898 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5899 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5900
5901 rval = qla2x00_loop_resync(ha);
5902 } else
5903 atomic_set(&ha->loop_state, LOOP_DEAD);
5904
5905 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5906 }
5907
5908 return rval;
5909}
5910
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005912qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005913{
5914 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005915 struct scsi_qla_host *vha;
5916 struct qla_hw_data *ha = base_vha->hw;
5917 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005918
Arun Easifeafb7b2010-09-03 14:57:00 -07005919 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005920 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005921 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5922 atomic_inc(&vha->vref_count);
5923 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005924 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005925 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5926 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005927 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005928
Arun Easifeafb7b2010-09-03 14:57:00 -07005929 spin_lock_irqsave(&ha->vport_slock, flags);
5930 }
5931 }
5932 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005933 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005934 }
5935 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005936}
5937
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005938/* Assumes idc_lock always held on entry */
5939void
5940qla83xx_reset_ownership(scsi_qla_host_t *vha)
5941{
5942 struct qla_hw_data *ha = vha->hw;
5943 uint32_t drv_presence, drv_presence_mask;
5944 uint32_t dev_part_info1, dev_part_info2, class_type;
5945 uint32_t class_type_mask = 0x3;
5946 uint16_t fcoe_other_function = 0xffff, i;
5947
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005948 if (IS_QLA8044(ha)) {
5949 drv_presence = qla8044_rd_direct(vha,
5950 QLA8044_CRB_DRV_ACTIVE_INDEX);
5951 dev_part_info1 = qla8044_rd_direct(vha,
5952 QLA8044_CRB_DEV_PART_INFO_INDEX);
5953 dev_part_info2 = qla8044_rd_direct(vha,
5954 QLA8044_CRB_DEV_PART_INFO2);
5955 } else {
5956 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5957 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5958 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5959 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005960 for (i = 0; i < 8; i++) {
5961 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5962 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5963 (i != ha->portnum)) {
5964 fcoe_other_function = i;
5965 break;
5966 }
5967 }
5968 if (fcoe_other_function == 0xffff) {
5969 for (i = 0; i < 8; i++) {
5970 class_type = ((dev_part_info2 >> (i * 4)) &
5971 class_type_mask);
5972 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5973 ((i + 8) != ha->portnum)) {
5974 fcoe_other_function = i + 8;
5975 break;
5976 }
5977 }
5978 }
5979 /*
5980 * Prepare drv-presence mask based on fcoe functions present.
5981 * However consider only valid physical fcoe function numbers (0-15).
5982 */
5983 drv_presence_mask = ~((1 << (ha->portnum)) |
5984 ((fcoe_other_function == 0xffff) ?
5985 0 : (1 << (fcoe_other_function))));
5986
5987 /* We are the reset owner iff:
5988 * - No other protocol drivers present.
5989 * - This is the lowest among fcoe functions. */
5990 if (!(drv_presence & drv_presence_mask) &&
5991 (ha->portnum < fcoe_other_function)) {
5992 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5993 "This host is Reset owner.\n");
5994 ha->flags.nic_core_reset_owner = 1;
5995 }
5996}
5997
Saurav Kashyapfa492632012-11-21 02:40:29 -05005998static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005999__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6000{
6001 int rval = QLA_SUCCESS;
6002 struct qla_hw_data *ha = vha->hw;
6003 uint32_t drv_ack;
6004
6005 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6006 if (rval == QLA_SUCCESS) {
6007 drv_ack |= (1 << ha->portnum);
6008 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6009 }
6010
6011 return rval;
6012}
6013
Saurav Kashyapfa492632012-11-21 02:40:29 -05006014static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006015__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6016{
6017 int rval = QLA_SUCCESS;
6018 struct qla_hw_data *ha = vha->hw;
6019 uint32_t drv_ack;
6020
6021 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6022 if (rval == QLA_SUCCESS) {
6023 drv_ack &= ~(1 << ha->portnum);
6024 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6025 }
6026
6027 return rval;
6028}
6029
Saurav Kashyapfa492632012-11-21 02:40:29 -05006030static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006031qla83xx_dev_state_to_string(uint32_t dev_state)
6032{
6033 switch (dev_state) {
6034 case QLA8XXX_DEV_COLD:
6035 return "COLD/RE-INIT";
6036 case QLA8XXX_DEV_INITIALIZING:
6037 return "INITIALIZING";
6038 case QLA8XXX_DEV_READY:
6039 return "READY";
6040 case QLA8XXX_DEV_NEED_RESET:
6041 return "NEED RESET";
6042 case QLA8XXX_DEV_NEED_QUIESCENT:
6043 return "NEED QUIESCENT";
6044 case QLA8XXX_DEV_FAILED:
6045 return "FAILED";
6046 case QLA8XXX_DEV_QUIESCENT:
6047 return "QUIESCENT";
6048 default:
6049 return "Unknown";
6050 }
6051}
6052
6053/* Assumes idc-lock always held on entry */
6054void
6055qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6056{
6057 struct qla_hw_data *ha = vha->hw;
6058 uint32_t idc_audit_reg = 0, duration_secs = 0;
6059
6060 switch (audit_type) {
6061 case IDC_AUDIT_TIMESTAMP:
6062 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6063 idc_audit_reg = (ha->portnum) |
6064 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6065 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6066 break;
6067
6068 case IDC_AUDIT_COMPLETION:
6069 duration_secs = ((jiffies_to_msecs(jiffies) -
6070 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6071 idc_audit_reg = (ha->portnum) |
6072 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6073 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6074 break;
6075
6076 default:
6077 ql_log(ql_log_warn, vha, 0xb078,
6078 "Invalid audit type specified.\n");
6079 break;
6080 }
6081}
6082
6083/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006084static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006085qla83xx_initiating_reset(scsi_qla_host_t *vha)
6086{
6087 struct qla_hw_data *ha = vha->hw;
6088 uint32_t idc_control, dev_state;
6089
6090 __qla83xx_get_idc_control(vha, &idc_control);
6091 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6092 ql_log(ql_log_info, vha, 0xb080,
6093 "NIC Core reset has been disabled. idc-control=0x%x\n",
6094 idc_control);
6095 return QLA_FUNCTION_FAILED;
6096 }
6097
6098 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6099 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6100 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6101 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6102 QLA8XXX_DEV_NEED_RESET);
6103 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6104 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6105 } else {
6106 const char *state = qla83xx_dev_state_to_string(dev_state);
6107 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6108
6109 /* SV: XXX: Is timeout required here? */
6110 /* Wait for IDC state change READY -> NEED_RESET */
6111 while (dev_state == QLA8XXX_DEV_READY) {
6112 qla83xx_idc_unlock(vha, 0);
6113 msleep(200);
6114 qla83xx_idc_lock(vha, 0);
6115 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6116 }
6117 }
6118
6119 /* Send IDC ack by writing to drv-ack register */
6120 __qla83xx_set_drv_ack(vha);
6121
6122 return QLA_SUCCESS;
6123}
6124
6125int
6126__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6127{
6128 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6129}
6130
6131int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006132__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6133{
6134 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6135}
6136
Saurav Kashyapfa492632012-11-21 02:40:29 -05006137static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006138qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6139{
6140 uint32_t drv_presence = 0;
6141 struct qla_hw_data *ha = vha->hw;
6142
6143 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6144 if (drv_presence & (1 << ha->portnum))
6145 return QLA_SUCCESS;
6146 else
6147 return QLA_TEST_FAILED;
6148}
6149
6150int
6151qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6152{
6153 int rval = QLA_SUCCESS;
6154 struct qla_hw_data *ha = vha->hw;
6155
6156 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6157 "Entered %s().\n", __func__);
6158
6159 if (vha->device_flags & DFLG_DEV_FAILED) {
6160 ql_log(ql_log_warn, vha, 0xb059,
6161 "Device in unrecoverable FAILED state.\n");
6162 return QLA_FUNCTION_FAILED;
6163 }
6164
6165 qla83xx_idc_lock(vha, 0);
6166
6167 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6168 ql_log(ql_log_warn, vha, 0xb05a,
6169 "Function=0x%x has been removed from IDC participation.\n",
6170 ha->portnum);
6171 rval = QLA_FUNCTION_FAILED;
6172 goto exit;
6173 }
6174
6175 qla83xx_reset_ownership(vha);
6176
6177 rval = qla83xx_initiating_reset(vha);
6178
6179 /*
6180 * Perform reset if we are the reset-owner,
6181 * else wait till IDC state changes to READY/FAILED.
6182 */
6183 if (rval == QLA_SUCCESS) {
6184 rval = qla83xx_idc_state_handler(vha);
6185
6186 if (rval == QLA_SUCCESS)
6187 ha->flags.nic_core_hung = 0;
6188 __qla83xx_clear_drv_ack(vha);
6189 }
6190
6191exit:
6192 qla83xx_idc_unlock(vha, 0);
6193
6194 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6195
6196 return rval;
6197}
6198
Saurav Kashyap81178772012-08-22 14:21:04 -04006199int
6200qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6201{
6202 struct qla_hw_data *ha = vha->hw;
6203 int rval = QLA_FUNCTION_FAILED;
6204
6205 if (!IS_MCTP_CAPABLE(ha)) {
6206 /* This message can be removed from the final version */
6207 ql_log(ql_log_info, vha, 0x506d,
6208 "This board is not MCTP capable\n");
6209 return rval;
6210 }
6211
6212 if (!ha->mctp_dump) {
6213 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6214 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6215
6216 if (!ha->mctp_dump) {
6217 ql_log(ql_log_warn, vha, 0x506e,
6218 "Failed to allocate memory for mctp dump\n");
6219 return rval;
6220 }
6221 }
6222
6223#define MCTP_DUMP_STR_ADDR 0x00000000
6224 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6225 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6226 if (rval != QLA_SUCCESS) {
6227 ql_log(ql_log_warn, vha, 0x506f,
6228 "Failed to capture mctp dump\n");
6229 } else {
6230 ql_log(ql_log_info, vha, 0x5070,
6231 "Mctp dump capture for host (%ld/%p).\n",
6232 vha->host_no, ha->mctp_dump);
6233 ha->mctp_dumped = 1;
6234 }
6235
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006236 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006237 ha->flags.nic_core_reset_hdlr_active = 1;
6238 rval = qla83xx_restart_nic_firmware(vha);
6239 if (rval)
6240 /* NIC Core reset failed. */
6241 ql_log(ql_log_warn, vha, 0x5071,
6242 "Failed to restart nic firmware\n");
6243 else
6244 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6245 "Restarted NIC firmware successfully.\n");
6246 ha->flags.nic_core_reset_hdlr_active = 0;
6247 }
6248
6249 return rval;
6250
6251}
6252
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006253/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006254* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006255* Description: This function will block the new I/Os
6256* Its not aborting any I/Os as context
6257* is not destroyed during quiescence
6258* Arguments: scsi_qla_host_t
6259* return : void
6260*/
6261void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006262qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006263{
6264 struct qla_hw_data *ha = vha->hw;
6265 struct scsi_qla_host *vp;
6266
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006267 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6268 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006269
6270 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6271 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6272 atomic_set(&vha->loop_state, LOOP_DOWN);
6273 qla2x00_mark_all_devices_lost(vha, 0);
6274 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006275 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006276 } else {
6277 if (!atomic_read(&vha->loop_down_timer))
6278 atomic_set(&vha->loop_down_timer,
6279 LOOP_DOWN_TIME);
6280 }
6281 /* Wait for pending cmds to complete */
6282 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6283}
6284
Giridhar Malavalia9083012010-04-12 17:59:55 -07006285void
6286qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6287{
6288 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006289 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006290 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006291 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006292 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006293
Saurav Kashyape46ef002011-02-23 15:27:16 -08006294 /* For ISP82XX, driver waits for completion of the commands.
6295 * online flag should be set.
6296 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006297 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006298 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006299 ha->flags.chip_reset_done = 0;
6300 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006301 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006302
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006303 ql_log(ql_log_info, vha, 0x00af,
6304 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006305
Saurav Kashyape46ef002011-02-23 15:27:16 -08006306 /* For ISP82XX, reset_chip is just disabling interrupts.
6307 * Driver waits for the completion of the commands.
6308 * the interrupts need to be enabled.
6309 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006310 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006311 ha->isp_ops->reset_chip(vha);
6312
Quinn Tran9cd883f2017-12-28 12:33:24 -08006313 SAVE_TOPO(ha);
6314 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006315 ha->flags.n2n_ae = 0;
6316 ha->flags.lip_ae = 0;
6317 ha->current_topology = 0;
6318 ha->flags.fw_started = 0;
6319 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006320 ha->base_qpair->chip_reset++;
6321 for (i = 0; i < ha->max_qpairs; i++) {
6322 if (ha->queue_pair_map[i])
6323 ha->queue_pair_map[i]->chip_reset =
6324 ha->base_qpair->chip_reset;
6325 }
Quinn Tran726b8542017-01-19 22:28:00 -08006326
Giridhar Malavalia9083012010-04-12 17:59:55 -07006327 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6328 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6329 atomic_set(&vha->loop_state, LOOP_DOWN);
6330 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006331
6332 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006333 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006334 atomic_inc(&vp->vref_count);
6335 spin_unlock_irqrestore(&ha->vport_slock, flags);
6336
Giridhar Malavalia9083012010-04-12 17:59:55 -07006337 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006338
6339 spin_lock_irqsave(&ha->vport_slock, flags);
6340 atomic_dec(&vp->vref_count);
6341 }
6342 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006343 } else {
6344 if (!atomic_read(&vha->loop_down_timer))
6345 atomic_set(&vha->loop_down_timer,
6346 LOOP_DOWN_TIME);
6347 }
6348
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006349 /* Clear all async request states across all VPs. */
6350 list_for_each_entry(fcport, &vha->vp_fcports, list)
6351 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6352 spin_lock_irqsave(&ha->vport_slock, flags);
6353 list_for_each_entry(vp, &ha->vp_list, list) {
6354 atomic_inc(&vp->vref_count);
6355 spin_unlock_irqrestore(&ha->vport_slock, flags);
6356
6357 list_for_each_entry(fcport, &vp->vp_fcports, list)
6358 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6359
6360 spin_lock_irqsave(&ha->vport_slock, flags);
6361 atomic_dec(&vp->vref_count);
6362 }
6363 spin_unlock_irqrestore(&ha->vport_slock, flags);
6364
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006365 if (!ha->flags.eeh_busy) {
6366 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006367 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006368 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006369 ql_log(ql_log_info, vha, 0x00b4,
6370 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006371
Saurav Kashyape46ef002011-02-23 15:27:16 -08006372 /* Done waiting for pending commands.
6373 * Reset the online flag.
6374 */
6375 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006378 /* Requeue all commands in outstanding command list. */
6379 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6380 }
Arun Easib6a029e2014-09-25 06:14:52 -04006381 /* memory barrier */
6382 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383}
6384
6385/*
6386* qla2x00_abort_isp
6387* Resets ISP and aborts all outstanding commands.
6388*
6389* Input:
6390* ha = adapter block pointer.
6391*
6392* Returns:
6393* 0 = success
6394*/
6395int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006396qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006398 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006400 struct qla_hw_data *ha = vha->hw;
6401 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006402 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006403 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006405 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006406 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
Santosh Vernekara61712972012-08-22 14:21:13 -04006408 if (IS_QLA8031(ha)) {
6409 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6410 "Clearing fcoe driver presence.\n");
6411 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6412 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6413 "Error while clearing DRV-Presence.\n");
6414 }
6415
Andrew Vasquez85880802009-12-15 21:29:46 -08006416 if (unlikely(pci_channel_offline(ha->pdev) &&
6417 ha->flags.pci_channel_io_perm_failure)) {
6418 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6419 status = 0;
6420 return status;
6421 }
6422
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006423 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006424
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006425 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006427 if (!qla2x00_restart_isp(vha)) {
6428 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006430 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 /*
6432 * Issue marker command only when we are going
6433 * to start the I/O .
6434 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006435 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 }
6437
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006438 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006440 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006442 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006443 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006444
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006445 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6446 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006447 if (ha->fce) {
6448 ha->flags.fce_enabled = 1;
6449 memset(ha->fce, 0,
6450 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006451 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006452 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6453 &ha->fce_bufs);
6454 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006455 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006456 "Unable to reinitialize FCE "
6457 "(%d).\n", rval);
6458 ha->flags.fce_enabled = 0;
6459 }
6460 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006461
6462 if (ha->eft) {
6463 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006464 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006465 ha->eft_dma, EFT_NUM_BUFFERS);
6466 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006467 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006468 "Unable to reinitialize EFT "
6469 "(%d).\n", rval);
6470 }
6471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006473 vha->flags.online = 1;
6474 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006476 ql_log(ql_log_fatal, vha, 0x8035,
6477 "ISP error recover failed - "
6478 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006479 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 * The next call disables the board
6481 * completely.
6482 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006483 ha->isp_ops->reset_adapter(vha);
6484 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006486 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 status = 0;
6488 } else { /* schedule another ISP abort */
6489 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006490 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6491 "ISP abort - retry remaining %d.\n",
6492 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 status = 1;
6494 }
6495 } else {
6496 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006497 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6498 "ISP error recovery - retrying (%d) "
6499 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006500 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 status = 1;
6502 }
6503 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 }
6506
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006507 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006508 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006509 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006510 spin_lock_irqsave(&ha->vport_slock, flags);
6511 list_for_each_entry(vp, &ha->vp_list, list) {
6512 if (vp->vp_idx) {
6513 atomic_inc(&vp->vref_count);
6514 spin_unlock_irqrestore(&ha->vport_slock, flags);
6515
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006516 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006517
6518 spin_lock_irqsave(&ha->vport_slock, flags);
6519 atomic_dec(&vp->vref_count);
6520 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006521 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006522 spin_unlock_irqrestore(&ha->vport_slock, flags);
6523
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006524 if (IS_QLA8031(ha)) {
6525 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6526 "Setting back fcoe driver presence.\n");
6527 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6528 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6529 "Error while setting DRV-Presence.\n");
6530 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006531 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006532 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6533 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 }
6535
6536 return(status);
6537}
6538
6539/*
6540* qla2x00_restart_isp
6541* restarts the ISP after a reset
6542*
6543* Input:
6544* ha = adapter block pointer.
6545*
6546* Returns:
6547* 0 = success
6548*/
6549static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006550qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006552 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006553 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006554 struct req_que *req = ha->req_q_map[0];
6555 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
6557 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006558 if (qla2x00_isp_firmware(vha)) {
6559 vha->flags.online = 0;
6560 status = ha->isp_ops->chip_diag(vha);
6561 if (!status)
6562 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 }
6564
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006565 if (!status && !(status = qla2x00_init_rings(vha))) {
6566 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006567 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006568
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006569 /* Initialize the queues in use */
6570 qla25xx_init_queues(ha);
6571
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006572 status = qla2x00_fw_ready(vha);
6573 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006574 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006575 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006576 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 }
6578
6579 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006580 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 }
6583 return (status);
6584}
6585
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006586static int
6587qla25xx_init_queues(struct qla_hw_data *ha)
6588{
6589 struct rsp_que *rsp = NULL;
6590 struct req_que *req = NULL;
6591 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6592 int ret = -1;
6593 int i;
6594
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006595 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006596 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006597 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006598 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006599 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006600 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006601 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6602 "%s Rsp que: %d init failed.\n",
6603 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006604 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006605 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6606 "%s Rsp que: %d inited.\n",
6607 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006608 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006609 }
6610 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006611 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006612 if (req && test_bit(i, ha->req_qid_map)) {
6613 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006614 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006615 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006616 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006617 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6618 "%s Req que: %d init failed.\n",
6619 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006620 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006621 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6622 "%s Req que: %d inited.\n",
6623 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006624 }
6625 }
6626 return ret;
6627}
6628
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629/*
6630* qla2x00_reset_adapter
6631* Reset adapter.
6632*
6633* Input:
6634* ha = adapter block pointer.
6635*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006636void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006637qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638{
6639 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006640 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006641 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 spin_lock_irqsave(&ha->hardware_lock, flags);
6647 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6648 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6649 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6650 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6651 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6652}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006653
6654void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006655qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006656{
6657 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006658 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006659 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6660
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006661 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006662 return;
6663
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006664 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006665 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006666
6667 spin_lock_irqsave(&ha->hardware_lock, flags);
6668 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6669 RD_REG_DWORD(&reg->hccr);
6670 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6671 RD_REG_DWORD(&reg->hccr);
6672 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006673
6674 if (IS_NOPOLLING_TYPE(ha))
6675 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006676}
6677
David Miller4e08df32007-04-16 12:37:43 -07006678/* On sparc systems, obtain port and node WWN from firmware
6679 * properties.
6680 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006681static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6682 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006683{
6684#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006685 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006686 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006687 struct device_node *dp = pci_device_to_OF_node(pdev);
6688 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006689 int len;
6690
6691 val = of_get_property(dp, "port-wwn", &len);
6692 if (val && len >= WWN_SIZE)
6693 memcpy(nv->port_name, val, WWN_SIZE);
6694
6695 val = of_get_property(dp, "node-wwn", &len);
6696 if (val && len >= WWN_SIZE)
6697 memcpy(nv->node_name, val, WWN_SIZE);
6698#endif
6699}
6700
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006701int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006702qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006703{
David Miller4e08df32007-04-16 12:37:43 -07006704 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006705 struct init_cb_24xx *icb;
6706 struct nvram_24xx *nv;
6707 uint32_t *dptr;
6708 uint8_t *dptr1, *dptr2;
6709 uint32_t chksum;
6710 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006711 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006712
David Miller4e08df32007-04-16 12:37:43 -07006713 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006714 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006715 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006716
6717 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006718 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006719 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6720 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6721 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006722 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006723 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6724 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006725
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006726 ha->nvram_size = sizeof(struct nvram_24xx);
6727 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006728
Seokmann Ju281afe12007-07-26 13:43:34 -07006729 /* Get VPD data into cache */
6730 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006731 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006732 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6733
6734 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006735 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006736 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006737 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006738 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6739 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006740
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006741 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6742 "Contents of NVRAM\n");
6743 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6744 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006745
6746 /* Bad NVRAM data, set defaults parameters. */
6747 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6748 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006749 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006750 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006751 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006752 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006753 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6754 ql_log(ql_log_warn, vha, 0x006c,
6755 "Falling back to functioning (yet invalid -- WWPN) "
6756 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006757
6758 /*
6759 * Set default initialization control block.
6760 */
6761 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006762 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6763 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006764 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006765 nv->execution_throttle = cpu_to_le16(0xFFFF);
6766 nv->exchange_count = cpu_to_le16(0);
6767 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006768 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006769 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006770 nv->port_name[2] = 0x00;
6771 nv->port_name[3] = 0xe0;
6772 nv->port_name[4] = 0x8b;
6773 nv->port_name[5] = 0x1c;
6774 nv->port_name[6] = 0x55;
6775 nv->port_name[7] = 0x86;
6776 nv->node_name[0] = 0x20;
6777 nv->node_name[1] = 0x00;
6778 nv->node_name[2] = 0x00;
6779 nv->node_name[3] = 0xe0;
6780 nv->node_name[4] = 0x8b;
6781 nv->node_name[5] = 0x1c;
6782 nv->node_name[6] = 0x55;
6783 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006784 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006785 nv->login_retry_count = cpu_to_le16(8);
6786 nv->interrupt_delay_timer = cpu_to_le16(0);
6787 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006788 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006789 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6790 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6791 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6792 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6793 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6794 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006795 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006796 nv->max_luns_per_target = cpu_to_le16(128);
6797 nv->port_down_retry_count = cpu_to_le16(30);
6798 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006799
6800 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006801 }
6802
Quinn Tran726b8542017-01-19 22:28:00 -08006803 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006804 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006805 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006806 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006807 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006808 }
6809
6810 qlt_24xx_config_nvram_stage1(vha, nv);
6811
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006812 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006813 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006814
6815 /* Copy 1st segment. */
6816 dptr1 = (uint8_t *)icb;
6817 dptr2 = (uint8_t *)&nv->version;
6818 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6819 while (cnt--)
6820 *dptr1++ = *dptr2++;
6821
6822 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006823 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006824
6825 /* Copy 2nd segment. */
6826 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6827 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6828 cnt = (uint8_t *)&icb->reserved_3 -
6829 (uint8_t *)&icb->interrupt_delay_timer;
6830 while (cnt--)
6831 *dptr1++ = *dptr2++;
6832
6833 /*
6834 * Setup driver NVRAM options.
6835 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006836 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006837 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006838
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006839 qlt_24xx_config_nvram_stage2(vha, icb);
6840
Bart Van Asschead950362015-07-09 07:24:08 -07006841 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006842 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006843 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6844 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6845 }
6846
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006847 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006848 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006849 /*
6850 * Firmware will apply the following mask if the nodename was
6851 * not provided.
6852 */
6853 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6854 icb->node_name[0] &= 0xF0;
6855 }
6856
6857 /* Set host adapter parameters. */
6858 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006859 ha->flags.enable_lip_reset = 0;
6860 ha->flags.enable_lip_full_login =
6861 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6862 ha->flags.enable_target_reset =
6863 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006864 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006865 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006866
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006867 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6868 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006869
6870 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6871 sizeof(ha->fw_seriallink_options24));
6872
6873 /* save HBA serial number */
6874 ha->serial0 = icb->port_name[5];
6875 ha->serial1 = icb->port_name[6];
6876 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006877 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6878 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006879
Bart Van Asschead950362015-07-09 07:24:08 -07006880 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006881
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006882 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6883
6884 /* Set minimum login_timeout to 4 seconds. */
6885 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6886 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6887 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006888 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006889 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006890
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006891 /* Set minimum RATOV to 100 tenths of a second. */
6892 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006893
6894 ha->loop_reset_delay = nv->reset_delay;
6895
6896 /* Link Down Timeout = 0:
6897 *
6898 * When Port Down timer expires we will start returning
6899 * I/O's to OS with "DID_NO_CONNECT".
6900 *
6901 * Link Down Timeout != 0:
6902 *
6903 * The driver waits for the link to come up after link down
6904 * before returning I/Os to OS with "DID_NO_CONNECT".
6905 */
6906 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6907 ha->loop_down_abort_time =
6908 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6909 } else {
6910 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6911 ha->loop_down_abort_time =
6912 (LOOP_DOWN_TIME - ha->link_down_timeout);
6913 }
6914
6915 /* Need enough time to try and get the port back. */
6916 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6917 if (qlport_down_retry)
6918 ha->port_down_retry_count = qlport_down_retry;
6919
6920 /* Set login_retry_count */
6921 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6922 if (ha->port_down_retry_count ==
6923 le16_to_cpu(nv->port_down_retry_count) &&
6924 ha->port_down_retry_count > 3)
6925 ha->login_retry_count = ha->port_down_retry_count;
6926 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6927 ha->login_retry_count = ha->port_down_retry_count;
6928 if (ql2xloginretrycount)
6929 ha->login_retry_count = ql2xloginretrycount;
6930
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006931 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006932 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006933 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6934 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6935 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6936 le16_to_cpu(icb->interrupt_delay_timer): 2;
6937 }
Bart Van Asschead950362015-07-09 07:24:08 -07006938 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006939 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006940 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006941 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006942 ha->zio_mode = QLA_ZIO_MODE_6;
6943
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006944 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006945 "ZIO mode %d enabled; timer delay (%d us).\n",
6946 ha->zio_mode, ha->zio_timer * 100);
6947
6948 icb->firmware_options_2 |= cpu_to_le32(
6949 (uint32_t)ha->zio_mode);
6950 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006951 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006952 }
6953
David Miller4e08df32007-04-16 12:37:43 -07006954 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006955 ql_log(ql_log_warn, vha, 0x0070,
6956 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006957 }
6958 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006959}
6960
Sawan Chandak4243c112016-01-27 12:03:31 -05006961uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6962{
6963 struct qla27xx_image_status pri_image_status, sec_image_status;
6964 uint8_t valid_pri_image, valid_sec_image;
6965 uint32_t *wptr;
6966 uint32_t cnt, chksum, size;
6967 struct qla_hw_data *ha = vha->hw;
6968
6969 valid_pri_image = valid_sec_image = 1;
6970 ha->active_image = 0;
6971 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6972
6973 if (!ha->flt_region_img_status_pri) {
6974 valid_pri_image = 0;
6975 goto check_sec_image;
6976 }
6977
6978 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6979 ha->flt_region_img_status_pri, size);
6980
6981 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6982 ql_dbg(ql_dbg_init, vha, 0x018b,
6983 "Primary image signature (0x%x) not valid\n",
6984 pri_image_status.signature);
6985 valid_pri_image = 0;
6986 goto check_sec_image;
6987 }
6988
6989 wptr = (uint32_t *)(&pri_image_status);
6990 cnt = size;
6991
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006992 for (chksum = 0; cnt--; wptr++)
6993 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006994
Sawan Chandak4243c112016-01-27 12:03:31 -05006995 if (chksum) {
6996 ql_dbg(ql_dbg_init, vha, 0x018c,
6997 "Checksum validation failed for primary image (0x%x)\n",
6998 chksum);
6999 valid_pri_image = 0;
7000 }
7001
7002check_sec_image:
7003 if (!ha->flt_region_img_status_sec) {
7004 valid_sec_image = 0;
7005 goto check_valid_image;
7006 }
7007
7008 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7009 ha->flt_region_img_status_sec, size);
7010
7011 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7012 ql_dbg(ql_dbg_init, vha, 0x018d,
7013 "Secondary image signature(0x%x) not valid\n",
7014 sec_image_status.signature);
7015 valid_sec_image = 0;
7016 goto check_valid_image;
7017 }
7018
7019 wptr = (uint32_t *)(&sec_image_status);
7020 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007021 for (chksum = 0; cnt--; wptr++)
7022 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05007023 if (chksum) {
7024 ql_dbg(ql_dbg_init, vha, 0x018e,
7025 "Checksum validation failed for secondary image (0x%x)\n",
7026 chksum);
7027 valid_sec_image = 0;
7028 }
7029
7030check_valid_image:
7031 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7032 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7033 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7034 if (!ha->active_image ||
7035 pri_image_status.generation_number <
7036 sec_image_status.generation_number)
7037 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7038 }
7039
7040 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7041 ha->active_image == 0 ? "default bootld and fw" :
7042 ha->active_image == 1 ? "primary" :
7043 ha->active_image == 2 ? "secondary" :
7044 "Invalid");
7045
7046 return ha->active_image;
7047}
7048
Adrian Bunk413975a2006-06-30 02:33:06 -07007049static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007050qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7051 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007052{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007053 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007054 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007055 uint32_t *dcode, dlen;
7056 uint32_t risc_addr;
7057 uint32_t risc_size;
7058 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007059 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007060 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007061
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007062 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007063 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007064
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007065 rval = QLA_SUCCESS;
7066
7067 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007068 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007069 *srisc_addr = 0;
7070
Sawan Chandak4243c112016-01-27 12:03:31 -05007071 if (IS_QLA27XX(ha) &&
7072 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7073 faddr = ha->flt_region_fw_sec;
7074
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007075 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007076 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007077 for (i = 0; i < 4; i++)
7078 dcode[i] = be32_to_cpu(dcode[i]);
7079 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7080 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7081 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7082 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007083 ql_log(ql_log_fatal, vha, 0x008c,
7084 "Unable to verify the integrity of flash firmware "
7085 "image.\n");
7086 ql_log(ql_log_fatal, vha, 0x008d,
7087 "Firmware data: %08x %08x %08x %08x.\n",
7088 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007089
7090 return QLA_FUNCTION_FAILED;
7091 }
7092
7093 while (segments && rval == QLA_SUCCESS) {
7094 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007095 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007096
7097 risc_addr = be32_to_cpu(dcode[2]);
7098 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7099 risc_size = be32_to_cpu(dcode[3]);
7100
7101 fragment = 0;
7102 while (risc_size > 0 && rval == QLA_SUCCESS) {
7103 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7104 if (dlen > risc_size)
7105 dlen = risc_size;
7106
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007107 ql_dbg(ql_dbg_init, vha, 0x008e,
7108 "Loading risc segment@ risc addr %x "
7109 "number of dwords 0x%x offset 0x%x.\n",
7110 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007111
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007112 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007113 for (i = 0; i < dlen; i++)
7114 dcode[i] = swab32(dcode[i]);
7115
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007116 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007117 dlen);
7118 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007119 ql_log(ql_log_fatal, vha, 0x008f,
7120 "Failed to load segment %d of firmware.\n",
7121 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007122 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007123 }
7124
7125 faddr += dlen;
7126 risc_addr += dlen;
7127 risc_size -= dlen;
7128 fragment++;
7129 }
7130
7131 /* Next segment. */
7132 segments--;
7133 }
7134
Chad Dupuisf73cb692014-02-26 04:15:06 -05007135 if (!IS_QLA27XX(ha))
7136 return rval;
7137
7138 if (ha->fw_dump_template)
7139 vfree(ha->fw_dump_template);
7140 ha->fw_dump_template = NULL;
7141 ha->fw_dump_template_len = 0;
7142
7143 ql_dbg(ql_dbg_init, vha, 0x0161,
7144 "Loading fwdump template from %x\n", faddr);
7145 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7146 risc_size = be32_to_cpu(dcode[2]);
7147 ql_dbg(ql_dbg_init, vha, 0x0162,
7148 "-> array size %x dwords\n", risc_size);
7149 if (risc_size == 0 || risc_size == ~0)
7150 goto default_template;
7151
7152 dlen = (risc_size - 8) * sizeof(*dcode);
7153 ql_dbg(ql_dbg_init, vha, 0x0163,
7154 "-> template allocating %x bytes...\n", dlen);
7155 ha->fw_dump_template = vmalloc(dlen);
7156 if (!ha->fw_dump_template) {
7157 ql_log(ql_log_warn, vha, 0x0164,
7158 "Failed fwdump template allocate %x bytes.\n", risc_size);
7159 goto default_template;
7160 }
7161
7162 faddr += 7;
7163 risc_size -= 8;
7164 dcode = ha->fw_dump_template;
7165 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7166 for (i = 0; i < risc_size; i++)
7167 dcode[i] = le32_to_cpu(dcode[i]);
7168
7169 if (!qla27xx_fwdt_template_valid(dcode)) {
7170 ql_log(ql_log_warn, vha, 0x0165,
7171 "Failed fwdump template validate\n");
7172 goto default_template;
7173 }
7174
7175 dlen = qla27xx_fwdt_template_size(dcode);
7176 ql_dbg(ql_dbg_init, vha, 0x0166,
7177 "-> template size %x bytes\n", dlen);
7178 if (dlen > risc_size * sizeof(*dcode)) {
7179 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007180 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007181 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007182 goto default_template;
7183 }
7184 ha->fw_dump_template_len = dlen;
7185 return rval;
7186
7187default_template:
7188 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7189 if (ha->fw_dump_template)
7190 vfree(ha->fw_dump_template);
7191 ha->fw_dump_template = NULL;
7192 ha->fw_dump_template_len = 0;
7193
7194 dlen = qla27xx_fwdt_template_default_size();
7195 ql_dbg(ql_dbg_init, vha, 0x0169,
7196 "-> template allocating %x bytes...\n", dlen);
7197 ha->fw_dump_template = vmalloc(dlen);
7198 if (!ha->fw_dump_template) {
7199 ql_log(ql_log_warn, vha, 0x016a,
7200 "Failed fwdump template allocate %x bytes.\n", risc_size);
7201 goto failed_template;
7202 }
7203
7204 dcode = ha->fw_dump_template;
7205 risc_size = dlen / sizeof(*dcode);
7206 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7207 for (i = 0; i < risc_size; i++)
7208 dcode[i] = be32_to_cpu(dcode[i]);
7209
7210 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7211 ql_log(ql_log_warn, vha, 0x016b,
7212 "Failed fwdump template validate\n");
7213 goto failed_template;
7214 }
7215
7216 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7217 ql_dbg(ql_dbg_init, vha, 0x016c,
7218 "-> template size %x bytes\n", dlen);
7219 ha->fw_dump_template_len = dlen;
7220 return rval;
7221
7222failed_template:
7223 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7224 if (ha->fw_dump_template)
7225 vfree(ha->fw_dump_template);
7226 ha->fw_dump_template = NULL;
7227 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007228 return rval;
7229}
7230
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007231#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007232
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007233int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007234qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007235{
7236 int rval;
7237 int i, fragment;
7238 uint16_t *wcode, *fwcode;
7239 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7240 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007241 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007242 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007243
7244 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007245 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007246 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007247 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007248 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007249 ql_log(ql_log_info, vha, 0x0084,
7250 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007251 return QLA_FUNCTION_FAILED;
7252 }
7253
7254 rval = QLA_SUCCESS;
7255
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007256 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007257 *srisc_addr = 0;
7258 fwcode = (uint16_t *)blob->fw->data;
7259 fwclen = 0;
7260
7261 /* Validate firmware image by checking version. */
7262 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007263 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007264 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007265 blob->fw->size);
7266 goto fail_fw_integrity;
7267 }
7268 for (i = 0; i < 4; i++)
7269 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7270 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7271 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7272 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007273 ql_log(ql_log_fatal, vha, 0x0086,
7274 "Unable to verify integrity of firmware image.\n");
7275 ql_log(ql_log_fatal, vha, 0x0087,
7276 "Firmware data: %04x %04x %04x %04x.\n",
7277 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007278 goto fail_fw_integrity;
7279 }
7280
7281 seg = blob->segs;
7282 while (*seg && rval == QLA_SUCCESS) {
7283 risc_addr = *seg;
7284 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7285 risc_size = be16_to_cpu(fwcode[3]);
7286
7287 /* Validate firmware image size. */
7288 fwclen += risc_size * sizeof(uint16_t);
7289 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007290 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007291 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007292 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007293 goto fail_fw_integrity;
7294 }
7295
7296 fragment = 0;
7297 while (risc_size > 0 && rval == QLA_SUCCESS) {
7298 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7299 if (wlen > risc_size)
7300 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007301 ql_dbg(ql_dbg_init, vha, 0x0089,
7302 "Loading risc segment@ risc addr %x number of "
7303 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007304
7305 for (i = 0; i < wlen; i++)
7306 wcode[i] = swab16(fwcode[i]);
7307
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007308 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007309 wlen);
7310 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007311 ql_log(ql_log_fatal, vha, 0x008a,
7312 "Failed to load segment %d of firmware.\n",
7313 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007314 break;
7315 }
7316
7317 fwcode += wlen;
7318 risc_addr += wlen;
7319 risc_size -= wlen;
7320 fragment++;
7321 }
7322
7323 /* Next segment. */
7324 seg++;
7325 }
7326 return rval;
7327
7328fail_fw_integrity:
7329 return QLA_FUNCTION_FAILED;
7330}
7331
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007332static int
7333qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007334{
7335 int rval;
7336 int segments, fragment;
7337 uint32_t *dcode, dlen;
7338 uint32_t risc_addr;
7339 uint32_t risc_size;
7340 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007341 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007342 const uint32_t *fwcode;
7343 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007344 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007345 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007346
Andrew Vasquez54333832005-11-09 15:49:04 -08007347 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007348 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007349 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007350 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007351 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007352 ql_log(ql_log_warn, vha, 0x0091,
7353 "Firmware images can be retrieved from: "
7354 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007355
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007356 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007357 }
7358
Chad Dupuiscfb09192011-11-18 09:03:07 -08007359 ql_dbg(ql_dbg_init, vha, 0x0092,
7360 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007361
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007362 rval = QLA_SUCCESS;
7363
7364 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007365 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007366 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007367 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007368 fwclen = 0;
7369
7370 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007371 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007372 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007373 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007374 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007375 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007376 }
7377 for (i = 0; i < 4; i++)
7378 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7379 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7380 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7381 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7382 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007383 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007384 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007385 blob->fw->size);
7386 ql_log(ql_log_fatal, vha, 0x0095,
7387 "Firmware data: %08x %08x %08x %08x.\n",
7388 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007389 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007390 }
7391
7392 while (segments && rval == QLA_SUCCESS) {
7393 risc_addr = be32_to_cpu(fwcode[2]);
7394 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7395 risc_size = be32_to_cpu(fwcode[3]);
7396
7397 /* Validate firmware image size. */
7398 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007399 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007400 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007401 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007402 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007403 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007404 }
7405
7406 fragment = 0;
7407 while (risc_size > 0 && rval == QLA_SUCCESS) {
7408 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7409 if (dlen > risc_size)
7410 dlen = risc_size;
7411
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007412 ql_dbg(ql_dbg_init, vha, 0x0097,
7413 "Loading risc segment@ risc addr %x "
7414 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007415
7416 for (i = 0; i < dlen; i++)
7417 dcode[i] = swab32(fwcode[i]);
7418
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007419 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007420 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007421 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007422 ql_log(ql_log_fatal, vha, 0x0098,
7423 "Failed to load segment %d of firmware.\n",
7424 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007425 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007426 }
7427
7428 fwcode += dlen;
7429 risc_addr += dlen;
7430 risc_size -= dlen;
7431 fragment++;
7432 }
7433
7434 /* Next segment. */
7435 segments--;
7436 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007437
7438 if (!IS_QLA27XX(ha))
7439 return rval;
7440
7441 if (ha->fw_dump_template)
7442 vfree(ha->fw_dump_template);
7443 ha->fw_dump_template = NULL;
7444 ha->fw_dump_template_len = 0;
7445
7446 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007447 "Loading fwdump template from %x\n",
7448 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007449 risc_size = be32_to_cpu(fwcode[2]);
7450 ql_dbg(ql_dbg_init, vha, 0x172,
7451 "-> array size %x dwords\n", risc_size);
7452 if (risc_size == 0 || risc_size == ~0)
7453 goto default_template;
7454
7455 dlen = (risc_size - 8) * sizeof(*fwcode);
7456 ql_dbg(ql_dbg_init, vha, 0x0173,
7457 "-> template allocating %x bytes...\n", dlen);
7458 ha->fw_dump_template = vmalloc(dlen);
7459 if (!ha->fw_dump_template) {
7460 ql_log(ql_log_warn, vha, 0x0174,
7461 "Failed fwdump template allocate %x bytes.\n", risc_size);
7462 goto default_template;
7463 }
7464
7465 fwcode += 7;
7466 risc_size -= 8;
7467 dcode = ha->fw_dump_template;
7468 for (i = 0; i < risc_size; i++)
7469 dcode[i] = le32_to_cpu(fwcode[i]);
7470
7471 if (!qla27xx_fwdt_template_valid(dcode)) {
7472 ql_log(ql_log_warn, vha, 0x0175,
7473 "Failed fwdump template validate\n");
7474 goto default_template;
7475 }
7476
7477 dlen = qla27xx_fwdt_template_size(dcode);
7478 ql_dbg(ql_dbg_init, vha, 0x0176,
7479 "-> template size %x bytes\n", dlen);
7480 if (dlen > risc_size * sizeof(*fwcode)) {
7481 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007482 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007483 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007484 goto default_template;
7485 }
7486 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007487 return rval;
7488
Chad Dupuisf73cb692014-02-26 04:15:06 -05007489default_template:
7490 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7491 if (ha->fw_dump_template)
7492 vfree(ha->fw_dump_template);
7493 ha->fw_dump_template = NULL;
7494 ha->fw_dump_template_len = 0;
7495
7496 dlen = qla27xx_fwdt_template_default_size();
7497 ql_dbg(ql_dbg_init, vha, 0x0179,
7498 "-> template allocating %x bytes...\n", dlen);
7499 ha->fw_dump_template = vmalloc(dlen);
7500 if (!ha->fw_dump_template) {
7501 ql_log(ql_log_warn, vha, 0x017a,
7502 "Failed fwdump template allocate %x bytes.\n", risc_size);
7503 goto failed_template;
7504 }
7505
7506 dcode = ha->fw_dump_template;
7507 risc_size = dlen / sizeof(*fwcode);
7508 fwcode = qla27xx_fwdt_template_default();
7509 for (i = 0; i < risc_size; i++)
7510 dcode[i] = be32_to_cpu(fwcode[i]);
7511
7512 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7513 ql_log(ql_log_warn, vha, 0x017b,
7514 "Failed fwdump template validate\n");
7515 goto failed_template;
7516 }
7517
7518 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7519 ql_dbg(ql_dbg_init, vha, 0x017c,
7520 "-> template size %x bytes\n", dlen);
7521 ha->fw_dump_template_len = dlen;
7522 return rval;
7523
7524failed_template:
7525 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7526 if (ha->fw_dump_template)
7527 vfree(ha->fw_dump_template);
7528 ha->fw_dump_template = NULL;
7529 ha->fw_dump_template_len = 0;
7530 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007531}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007532
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007533int
7534qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7535{
7536 int rval;
7537
Andrew Vasqueze337d902009-04-06 22:33:49 -07007538 if (ql2xfwloadbin == 1)
7539 return qla81xx_load_risc(vha, srisc_addr);
7540
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007541 /*
7542 * FW Load priority:
7543 * 1) Firmware via request-firmware interface (.bin file).
7544 * 2) Firmware residing in flash.
7545 */
7546 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7547 if (rval == QLA_SUCCESS)
7548 return rval;
7549
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007550 return qla24xx_load_risc_flash(vha, srisc_addr,
7551 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007552}
7553
7554int
7555qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7556{
7557 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007558 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007559
Andrew Vasqueze337d902009-04-06 22:33:49 -07007560 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007561 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007562
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007563 /*
7564 * FW Load priority:
7565 * 1) Firmware residing in flash.
7566 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007567 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007568 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007569 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007570 if (rval == QLA_SUCCESS)
7571 return rval;
7572
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007573try_blob_fw:
7574 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7575 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7576 return rval;
7577
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007578 ql_log(ql_log_info, vha, 0x0099,
7579 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007580 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7581 if (rval != QLA_SUCCESS)
7582 return rval;
7583
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007584 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007585 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007586 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007587}
7588
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007589void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007590qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007591{
7592 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007593 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007594
Andrew Vasquez85880802009-12-15 21:29:46 -08007595 if (ha->flags.pci_channel_io_perm_failure)
7596 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007597 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007598 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007599 if (!ha->fw_major_version)
7600 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007601 if (!ha->flags.fw_started)
7602 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007603
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007604 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007605 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007606 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007607 ha->isp_ops->reset_chip(vha);
7608 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007609 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007610 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007611 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007612 ql_log(ql_log_info, vha, 0x8015,
7613 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007614 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007615 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007616
Quinn Tran4b60c822017-06-13 20:47:21 -07007617 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007618 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007619}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007620
7621int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007622qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007623{
7624 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007625 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007626 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007627 struct qla_hw_data *ha = vha->hw;
7628 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007629 struct req_que *req;
7630 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007631
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007632 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007633 return -EINVAL;
7634
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007635 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007636 if (vha->qpair)
7637 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007638 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007639 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007640 rsp = req->rsp;
7641
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007642 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007643 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007644 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007645 }
7646
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007647 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007648
7649 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007650 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7651 BIT_1);
7652 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7653 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7654 ql_dbg(ql_dbg_init, vha, 0x0120,
7655 "Failed SNS login: loop_id=%x, rval2=%d\n",
7656 NPH_SNS, rval2);
7657 else
7658 ql_dbg(ql_dbg_init, vha, 0x0103,
7659 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7660 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7661 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007662 return (QLA_FUNCTION_FAILED);
7663 }
7664
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007665 atomic_set(&vha->loop_down_timer, 0);
7666 atomic_set(&vha->loop_state, LOOP_UP);
7667 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7668 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7669 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007670
7671 return rval;
7672}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007673
7674/* 84XX Support **************************************************************/
7675
7676static LIST_HEAD(qla_cs84xx_list);
7677static DEFINE_MUTEX(qla_cs84xx_mutex);
7678
7679static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007680qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007681{
7682 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007683 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007684
7685 mutex_lock(&qla_cs84xx_mutex);
7686
7687 /* Find any shared 84xx chip. */
7688 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7689 if (cs84xx->bus == ha->pdev->bus) {
7690 kref_get(&cs84xx->kref);
7691 goto done;
7692 }
7693 }
7694
7695 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7696 if (!cs84xx)
7697 goto done;
7698
7699 kref_init(&cs84xx->kref);
7700 spin_lock_init(&cs84xx->access_lock);
7701 mutex_init(&cs84xx->fw_update_mutex);
7702 cs84xx->bus = ha->pdev->bus;
7703
7704 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7705done:
7706 mutex_unlock(&qla_cs84xx_mutex);
7707 return cs84xx;
7708}
7709
7710static void
7711__qla84xx_chip_release(struct kref *kref)
7712{
7713 struct qla_chip_state_84xx *cs84xx =
7714 container_of(kref, struct qla_chip_state_84xx, kref);
7715
7716 mutex_lock(&qla_cs84xx_mutex);
7717 list_del(&cs84xx->list);
7718 mutex_unlock(&qla_cs84xx_mutex);
7719 kfree(cs84xx);
7720}
7721
7722void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007723qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007724{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007725 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007726 if (ha->cs84xx)
7727 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7728}
7729
7730static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007731qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007732{
7733 int rval;
7734 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007735 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007736
7737 mutex_lock(&ha->cs84xx->fw_update_mutex);
7738
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007739 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007740
7741 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7742
7743 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7744 QLA_SUCCESS;
7745}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007746
7747/* 81XX Support **************************************************************/
7748
7749int
7750qla81xx_nvram_config(scsi_qla_host_t *vha)
7751{
7752 int rval;
7753 struct init_cb_81xx *icb;
7754 struct nvram_81xx *nv;
7755 uint32_t *dptr;
7756 uint8_t *dptr1, *dptr2;
7757 uint32_t chksum;
7758 uint16_t cnt;
7759 struct qla_hw_data *ha = vha->hw;
7760
7761 rval = QLA_SUCCESS;
7762 icb = (struct init_cb_81xx *)ha->init_cb;
7763 nv = ha->nvram;
7764
7765 /* Determine NVRAM starting address. */
7766 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007767 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007768 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7769 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007770
7771 /* Get VPD data into cache */
7772 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007773 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7774 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007775
7776 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007777 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007778 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007779 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007780 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7781 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007782
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007783 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7784 "Contents of NVRAM:\n");
7785 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7786 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007787
7788 /* Bad NVRAM data, set defaults parameters. */
7789 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7790 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007791 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007792 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007793 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007794 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007795 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007796 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007797 ql_log(ql_log_info, vha, 0x0074,
7798 "Falling back to functioning (yet invalid -- WWPN) "
7799 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007800
7801 /*
7802 * Set default initialization control block.
7803 */
7804 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007805 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7806 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007807 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007808 nv->execution_throttle = cpu_to_le16(0xFFFF);
7809 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007810 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007811 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007812 nv->port_name[2] = 0x00;
7813 nv->port_name[3] = 0xe0;
7814 nv->port_name[4] = 0x8b;
7815 nv->port_name[5] = 0x1c;
7816 nv->port_name[6] = 0x55;
7817 nv->port_name[7] = 0x86;
7818 nv->node_name[0] = 0x20;
7819 nv->node_name[1] = 0x00;
7820 nv->node_name[2] = 0x00;
7821 nv->node_name[3] = 0xe0;
7822 nv->node_name[4] = 0x8b;
7823 nv->node_name[5] = 0x1c;
7824 nv->node_name[6] = 0x55;
7825 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007826 nv->login_retry_count = cpu_to_le16(8);
7827 nv->interrupt_delay_timer = cpu_to_le16(0);
7828 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007829 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007830 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7831 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7832 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7833 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7834 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7835 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007836 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007837 nv->max_luns_per_target = cpu_to_le16(128);
7838 nv->port_down_retry_count = cpu_to_le16(30);
7839 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007840 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007841 nv->enode_mac[1] = 0xC0;
7842 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007843 nv->enode_mac[3] = 0x04;
7844 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007845 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007846
7847 rval = 1;
7848 }
7849
Arun Easi9e522cd2012-08-22 14:21:31 -04007850 if (IS_T10_PI_CAPABLE(ha))
7851 nv->frame_payload_size &= ~7;
7852
Arun Easiaa230bc2013-01-30 03:34:39 -05007853 qlt_81xx_config_nvram_stage1(vha, nv);
7854
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007855 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007856 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007857
7858 /* Copy 1st segment. */
7859 dptr1 = (uint8_t *)icb;
7860 dptr2 = (uint8_t *)&nv->version;
7861 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7862 while (cnt--)
7863 *dptr1++ = *dptr2++;
7864
7865 icb->login_retry_count = nv->login_retry_count;
7866
7867 /* Copy 2nd segment. */
7868 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7869 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7870 cnt = (uint8_t *)&icb->reserved_5 -
7871 (uint8_t *)&icb->interrupt_delay_timer;
7872 while (cnt--)
7873 *dptr1++ = *dptr2++;
7874
7875 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7876 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7877 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007878 icb->enode_mac[0] = 0x00;
7879 icb->enode_mac[1] = 0xC0;
7880 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007881 icb->enode_mac[3] = 0x04;
7882 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007883 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007884 }
7885
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007886 /* Use extended-initialization control block. */
7887 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7888
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007889 /*
7890 * Setup driver NVRAM options.
7891 */
7892 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007893 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007894
Arun Easiaa230bc2013-01-30 03:34:39 -05007895 qlt_81xx_config_nvram_stage2(vha, icb);
7896
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007897 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007898 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007899 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7900 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7901 }
7902
7903 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007904 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007905 /*
7906 * Firmware will apply the following mask if the nodename was
7907 * not provided.
7908 */
7909 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7910 icb->node_name[0] &= 0xF0;
7911 }
7912
7913 /* Set host adapter parameters. */
7914 ha->flags.disable_risc_code_load = 0;
7915 ha->flags.enable_lip_reset = 0;
7916 ha->flags.enable_lip_full_login =
7917 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7918 ha->flags.enable_target_reset =
7919 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7920 ha->flags.enable_led_scheme = 0;
7921 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7922
7923 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7924 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7925
7926 /* save HBA serial number */
7927 ha->serial0 = icb->port_name[5];
7928 ha->serial1 = icb->port_name[6];
7929 ha->serial2 = icb->port_name[7];
7930 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7931 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7932
Bart Van Asschead950362015-07-09 07:24:08 -07007933 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007934
7935 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7936
7937 /* Set minimum login_timeout to 4 seconds. */
7938 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7939 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7940 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007941 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007942 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007943
7944 /* Set minimum RATOV to 100 tenths of a second. */
7945 ha->r_a_tov = 100;
7946
7947 ha->loop_reset_delay = nv->reset_delay;
7948
7949 /* Link Down Timeout = 0:
7950 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007951 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007952 * I/O's to OS with "DID_NO_CONNECT".
7953 *
7954 * Link Down Timeout != 0:
7955 *
7956 * The driver waits for the link to come up after link down
7957 * before returning I/Os to OS with "DID_NO_CONNECT".
7958 */
7959 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7960 ha->loop_down_abort_time =
7961 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7962 } else {
7963 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7964 ha->loop_down_abort_time =
7965 (LOOP_DOWN_TIME - ha->link_down_timeout);
7966 }
7967
7968 /* Need enough time to try and get the port back. */
7969 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7970 if (qlport_down_retry)
7971 ha->port_down_retry_count = qlport_down_retry;
7972
7973 /* Set login_retry_count */
7974 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7975 if (ha->port_down_retry_count ==
7976 le16_to_cpu(nv->port_down_retry_count) &&
7977 ha->port_down_retry_count > 3)
7978 ha->login_retry_count = ha->port_down_retry_count;
7979 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7980 ha->login_retry_count = ha->port_down_retry_count;
7981 if (ql2xloginretrycount)
7982 ha->login_retry_count = ql2xloginretrycount;
7983
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007984 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007985 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007986 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007987
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007988 /* Enable ZIO. */
7989 if (!vha->flags.init_done) {
7990 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7991 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7992 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7993 le16_to_cpu(icb->interrupt_delay_timer): 2;
7994 }
Bart Van Asschead950362015-07-09 07:24:08 -07007995 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007996 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7997 vha->flags.process_response_queue = 0;
7998 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7999 ha->zio_mode = QLA_ZIO_MODE_6;
8000
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008001 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008002 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008003 ha->zio_mode,
8004 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008005
8006 icb->firmware_options_2 |= cpu_to_le32(
8007 (uint32_t)ha->zio_mode);
8008 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8009 vha->flags.process_response_queue = 1;
8010 }
8011
Quinn Tran41dc5292017-01-19 22:28:03 -08008012 /* enable RIDA Format2 */
8013 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
8014 icb->firmware_options_3 |= BIT_0;
8015
Duane Grigsbyedd05de2017-10-13 09:34:06 -07008016 if (IS_QLA27XX(ha)) {
8017 icb->firmware_options_3 |= BIT_8;
8018 ql_dbg(ql_log_info, vha, 0x0075,
8019 "Enabling direct connection.\n");
8020 }
8021
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008022 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008023 ql_log(ql_log_warn, vha, 0x0076,
8024 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008025 }
8026 return (rval);
8027}
8028
Giridhar Malavalia9083012010-04-12 17:59:55 -07008029int
8030qla82xx_restart_isp(scsi_qla_host_t *vha)
8031{
8032 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008033 struct qla_hw_data *ha = vha->hw;
8034 struct req_que *req = ha->req_q_map[0];
8035 struct rsp_que *rsp = ha->rsp_q_map[0];
8036 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008037 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008038
8039 status = qla2x00_init_rings(vha);
8040 if (!status) {
8041 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8042 ha->flags.chip_reset_done = 1;
8043
8044 status = qla2x00_fw_ready(vha);
8045 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008046 /* Issue a marker after FW becomes ready. */
8047 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008048 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008049 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008050 }
8051
8052 /* if no cable then assume it's good */
8053 if ((vha->device_flags & DFLG_NO_CABLE))
8054 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008055 }
8056
8057 if (!status) {
8058 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8059
8060 if (!atomic_read(&vha->loop_down_timer)) {
8061 /*
8062 * Issue marker command only when we are going
8063 * to start the I/O .
8064 */
8065 vha->marker_needed = 1;
8066 }
8067
Giridhar Malavalia9083012010-04-12 17:59:55 -07008068 ha->isp_ops->enable_intrs(ha);
8069
8070 ha->isp_abort_cnt = 0;
8071 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8072
Saurav Kashyap53296782011-05-10 11:30:06 -07008073 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008074 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008075
Giridhar Malavalia9083012010-04-12 17:59:55 -07008076 if (ha->fce) {
8077 ha->flags.fce_enabled = 1;
8078 memset(ha->fce, 0,
8079 fce_calc_size(ha->fce_bufs));
8080 rval = qla2x00_enable_fce_trace(vha,
8081 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8082 &ha->fce_bufs);
8083 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008084 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008085 "Unable to reinitialize FCE (%d).\n",
8086 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008087 ha->flags.fce_enabled = 0;
8088 }
8089 }
8090
8091 if (ha->eft) {
8092 memset(ha->eft, 0, EFT_SIZE);
8093 rval = qla2x00_enable_eft_trace(vha,
8094 ha->eft_dma, EFT_NUM_BUFFERS);
8095 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008096 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008097 "Unable to reinitialize EFT (%d).\n",
8098 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008099 }
8100 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008101 }
8102
8103 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008104 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008105 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008106
8107 spin_lock_irqsave(&ha->vport_slock, flags);
8108 list_for_each_entry(vp, &ha->vp_list, list) {
8109 if (vp->vp_idx) {
8110 atomic_inc(&vp->vref_count);
8111 spin_unlock_irqrestore(&ha->vport_slock, flags);
8112
Giridhar Malavalia9083012010-04-12 17:59:55 -07008113 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008114
8115 spin_lock_irqsave(&ha->vport_slock, flags);
8116 atomic_dec(&vp->vref_count);
8117 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008118 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008119 spin_unlock_irqrestore(&ha->vport_slock, flags);
8120
Giridhar Malavalia9083012010-04-12 17:59:55 -07008121 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008122 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008123 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008124 }
8125
8126 return status;
8127}
8128
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008129void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008130qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008131{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008132 struct qla_hw_data *ha = vha->hw;
8133
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008134 /* Hold status IOCBs until ABTS response received. */
8135 if (ql2xfwholdabts)
8136 ha->fw_options[3] |= BIT_12;
8137
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008138 /* Set Retry FLOGI in case of P2P connection */
8139 if (ha->operating_mode == P2P) {
8140 ha->fw_options[2] |= BIT_3;
8141 ql_dbg(ql_dbg_disc, vha, 0x2103,
8142 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8143 __func__, ha->fw_options[2]);
8144 }
8145
Quinn Tran41dc5292017-01-19 22:28:03 -08008146 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8147 if (ql2xmvasynctoatio) {
8148 if (qla_tgt_mode_enabled(vha) ||
8149 qla_dual_mode_enabled(vha))
8150 ha->fw_options[2] |= BIT_11;
8151 else
8152 ha->fw_options[2] &= ~BIT_11;
8153 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008154
Quinn Tranf7e761f2017-06-02 09:12:02 -07008155 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008156 qla_dual_mode_enabled(vha)) {
8157 /* FW auto send SCSI status during */
8158 ha->fw_options[1] |= BIT_8;
8159 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8160
8161 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008162 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008163 } else {
8164 ha->fw_options[1] &= ~BIT_8;
8165 ha->fw_options[10] &= 0x00ff;
8166
Quinn Tranf7e761f2017-06-02 09:12:02 -07008167 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008168 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008169
Quinn Tran41dc5292017-01-19 22:28:03 -08008170 if (ql2xetsenable) {
8171 /* Enable ETS Burst. */
8172 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8173 ha->fw_options[2] |= BIT_9;
8174 }
8175
Quinn Tran83548fe2017-06-02 09:12:01 -07008176 ql_dbg(ql_dbg_init, vha, 0x00e9,
8177 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8178 __func__, ha->fw_options[1], ha->fw_options[2],
8179 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008180
Andrew Vasquezae97c912010-02-18 10:07:28 -08008181 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008182}
Sarang Radke09ff7012010-03-19 17:03:59 -07008183
8184/*
8185 * qla24xx_get_fcp_prio
8186 * Gets the fcp cmd priority value for the logged in port.
8187 * Looks for a match of the port descriptors within
8188 * each of the fcp prio config entries. If a match is found,
8189 * the tag (priority) value is returned.
8190 *
8191 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008192 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008193 * fcport = port structure pointer.
8194 *
8195 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008196 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008197 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008198 *
8199 * Context:
8200 * Kernel context
8201 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008202static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008203qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8204{
8205 int i, entries;
8206 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008207 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008208 uint32_t pid1, pid2;
8209 uint64_t wwn1, wwn2;
8210 struct qla_fcp_prio_entry *pri_entry;
8211 struct qla_hw_data *ha = vha->hw;
8212
8213 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008214 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008215
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008216 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008217 entries = ha->fcp_prio_cfg->num_entries;
8218 pri_entry = &ha->fcp_prio_cfg->entry[0];
8219
8220 for (i = 0; i < entries; i++) {
8221 pid_match = wwn_match = 0;
8222
8223 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8224 pri_entry++;
8225 continue;
8226 }
8227
8228 /* check source pid for a match */
8229 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8230 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8231 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8232 if (pid1 == INVALID_PORT_ID)
8233 pid_match++;
8234 else if (pid1 == pid2)
8235 pid_match++;
8236 }
8237
8238 /* check destination pid for a match */
8239 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8240 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8241 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8242 if (pid1 == INVALID_PORT_ID)
8243 pid_match++;
8244 else if (pid1 == pid2)
8245 pid_match++;
8246 }
8247
8248 /* check source WWN for a match */
8249 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8250 wwn1 = wwn_to_u64(vha->port_name);
8251 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8252 if (wwn2 == (uint64_t)-1)
8253 wwn_match++;
8254 else if (wwn1 == wwn2)
8255 wwn_match++;
8256 }
8257
8258 /* check destination WWN for a match */
8259 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8260 wwn1 = wwn_to_u64(fcport->port_name);
8261 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8262 if (wwn2 == (uint64_t)-1)
8263 wwn_match++;
8264 else if (wwn1 == wwn2)
8265 wwn_match++;
8266 }
8267
8268 if (pid_match == 2 || wwn_match == 2) {
8269 /* Found a matching entry */
8270 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8271 priority = pri_entry->tag;
8272 break;
8273 }
8274
8275 pri_entry++;
8276 }
8277
8278 return priority;
8279}
8280
8281/*
8282 * qla24xx_update_fcport_fcp_prio
8283 * Activates fcp priority for the logged in fc port
8284 *
8285 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008286 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008287 * fcp = port structure pointer.
8288 *
8289 * Return:
8290 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8291 *
8292 * Context:
8293 * Kernel context.
8294 */
8295int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008296qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008297{
8298 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008299 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008300 uint16_t mb[5];
8301
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008302 if (fcport->port_type != FCT_TARGET ||
8303 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008304 return QLA_FUNCTION_FAILED;
8305
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008306 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008307 if (priority < 0)
8308 return QLA_FUNCTION_FAILED;
8309
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008310 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008311 fcport->fcp_prio = priority & 0xf;
8312 return QLA_SUCCESS;
8313 }
8314
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008315 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008316 if (ret == QLA_SUCCESS) {
8317 if (fcport->fcp_prio != priority)
8318 ql_dbg(ql_dbg_user, vha, 0x709e,
8319 "Updated FCP_CMND priority - value=%d loop_id=%d "
8320 "port_id=%02x%02x%02x.\n", priority,
8321 fcport->loop_id, fcport->d_id.b.domain,
8322 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008323 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008324 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008325 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008326 "Unable to update FCP_CMND priority - ret=0x%x for "
8327 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8328 fcport->d_id.b.domain, fcport->d_id.b.area,
8329 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008330 return ret;
8331}
8332
8333/*
8334 * qla24xx_update_all_fcp_prio
8335 * Activates fcp priority for all the logged in ports
8336 *
8337 * Input:
8338 * ha = adapter block pointer.
8339 *
8340 * Return:
8341 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8342 *
8343 * Context:
8344 * Kernel context.
8345 */
8346int
8347qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8348{
8349 int ret;
8350 fc_port_t *fcport;
8351
8352 ret = QLA_FUNCTION_FAILED;
8353 /* We need to set priority for all logged in ports */
8354 list_for_each_entry(fcport, &vha->vp_fcports, list)
8355 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8356
8357 return ret;
8358}
Michael Hernandezd7459522016-12-12 14:40:07 -08008359
Quinn Tran82de8022017-06-13 20:47:17 -07008360struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8361 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008362{
8363 int rsp_id = 0;
8364 int req_id = 0;
8365 int i;
8366 struct qla_hw_data *ha = vha->hw;
8367 uint16_t qpair_id = 0;
8368 struct qla_qpair *qpair = NULL;
8369 struct qla_msix_entry *msix;
8370
8371 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8372 ql_log(ql_log_warn, vha, 0x00181,
8373 "FW/Driver is not multi-queue capable.\n");
8374 return NULL;
8375 }
8376
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008377 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008378 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8379 if (qpair == NULL) {
8380 ql_log(ql_log_warn, vha, 0x0182,
8381 "Failed to allocate memory for queue pair.\n");
8382 return NULL;
8383 }
8384 memset(qpair, 0, sizeof(struct qla_qpair));
8385
8386 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008387 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008388 qpair->qp_lock_ptr = &qpair->qp_lock;
8389 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008390 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008391
8392 /* Assign available que pair id */
8393 mutex_lock(&ha->mq_lock);
8394 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008395 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008396 mutex_unlock(&ha->mq_lock);
8397 ql_log(ql_log_warn, vha, 0x0183,
8398 "No resources to create additional q pair.\n");
8399 goto fail_qid_map;
8400 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008401 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008402 set_bit(qpair_id, ha->qpair_qid_map);
8403 ha->queue_pair_map[qpair_id] = qpair;
8404 qpair->id = qpair_id;
8405 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008406 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008407 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008408 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008409 qpair->chip_reset = ha->base_qpair->chip_reset;
8410 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8411 qpair->enable_explicit_conf =
8412 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008413
8414 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008415 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008416 if (msix->in_use)
8417 continue;
8418 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008419 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008420 "Vector %x selected for qpair\n", msix->vector);
8421 break;
8422 }
8423 if (!qpair->msix) {
8424 ql_log(ql_log_warn, vha, 0x0184,
8425 "Out of MSI-X vectors!.\n");
8426 goto fail_msix;
8427 }
8428
8429 qpair->msix->in_use = 1;
8430 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008431 qpair->pdev = ha->pdev;
8432 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8433 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008434
8435 mutex_unlock(&ha->mq_lock);
8436
8437 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008438 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008439 if (!rsp_id) {
8440 ql_log(ql_log_warn, vha, 0x0185,
8441 "Failed to create response queue.\n");
8442 goto fail_rsp;
8443 }
8444
8445 qpair->rsp = ha->rsp_q_map[rsp_id];
8446
8447 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008448 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8449 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008450 if (!req_id) {
8451 ql_log(ql_log_warn, vha, 0x0186,
8452 "Failed to create request queue.\n");
8453 goto fail_req;
8454 }
8455
8456 qpair->req = ha->req_q_map[req_id];
8457 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008458 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008459 /* init qpair to this cpu. Will adjust at run time. */
8460 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008461
8462 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8463 if (ha->fw_attributes & BIT_4)
8464 qpair->difdix_supported = 1;
8465 }
8466
8467 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8468 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008469 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008470 "Failed to create srb mempool for qpair %d\n",
8471 qpair->id);
8472 goto fail_mempool;
8473 }
8474
8475 /* Mark as online */
8476 qpair->online = 1;
8477
8478 if (!vha->flags.qpairs_available)
8479 vha->flags.qpairs_available = 1;
8480
8481 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8482 "Request/Response queue pair created, id %d\n",
8483 qpair->id);
8484 ql_dbg(ql_dbg_init, vha, 0x0187,
8485 "Request/Response queue pair created, id %d\n",
8486 qpair->id);
8487 }
8488 return qpair;
8489
8490fail_mempool:
8491fail_req:
8492 qla25xx_delete_rsp_que(vha, qpair->rsp);
8493fail_rsp:
8494 mutex_lock(&ha->mq_lock);
8495 qpair->msix->in_use = 0;
8496 list_del(&qpair->qp_list_elem);
8497 if (list_empty(&vha->qp_list))
8498 vha->flags.qpairs_available = 0;
8499fail_msix:
8500 ha->queue_pair_map[qpair_id] = NULL;
8501 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008502 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008503 mutex_unlock(&ha->mq_lock);
8504fail_qid_map:
8505 kfree(qpair);
8506 return NULL;
8507}
8508
8509int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8510{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008511 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008512 struct qla_hw_data *ha = qpair->hw;
8513
8514 qpair->delete_in_progress = 1;
8515 while (atomic_read(&qpair->ref_count))
8516 msleep(500);
8517
8518 ret = qla25xx_delete_req_que(vha, qpair->req);
8519 if (ret != QLA_SUCCESS)
8520 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008521
Michael Hernandezd7459522016-12-12 14:40:07 -08008522 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8523 if (ret != QLA_SUCCESS)
8524 goto fail;
8525
8526 mutex_lock(&ha->mq_lock);
8527 ha->queue_pair_map[qpair->id] = NULL;
8528 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008529 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008530 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008531 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008532 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008533 vha->flags.qpairs_req_created = 0;
8534 vha->flags.qpairs_rsp_created = 0;
8535 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008536 mempool_destroy(qpair->srb_mempool);
8537 kfree(qpair);
8538 mutex_unlock(&ha->mq_lock);
8539
8540 return QLA_SUCCESS;
8541fail:
8542 return ret;
8543}