blob: 590aa904fdef1f099f4780022b4d4ca1d1a98329 [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
Duane Grigsbye84067d2017-06-21 13:48:43 -07005110 if (fcport->fc4f_nvme) {
5111 qla_nvme_register_remote(vha, fcport);
5112 return;
5113 }
5114
Joe Carnuccio1f93da522012-11-21 02:40:38 -05005115 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005116 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08005117 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005118
5119reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08005120 switch (vha->host->active_mode) {
5121 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005122 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005123 break;
5124 case MODE_TARGET:
5125 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5126 !vha->vha_tgt.qla_tgt->tgt_stopped)
5127 qlt_fc_port_added(vha, fcport);
5128 break;
5129 case MODE_DUAL:
5130 qla2x00_reg_remote_port(vha, fcport);
5131 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5132 !vha->vha_tgt.qla_tgt->tgt_stopped)
5133 qlt_fc_port_added(vha, fcport);
5134 break;
5135 default:
5136 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04005137 }
Adrian Bunk23be3312006-11-24 02:46:01 +01005138}
5139
5140/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 * qla2x00_configure_fabric
5142 * Setup SNS devices with loop ID's.
5143 *
5144 * Input:
5145 * ha = adapter block pointer.
5146 *
5147 * Returns:
5148 * 0 = success.
5149 * BIT_0 = error
5150 */
5151static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005152qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
Arun Easib3b02e62012-02-09 11:15:39 -08005154 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005155 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005157 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005159 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04005160 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
5162 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07005163 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005164 loop_id = NPH_F_PORT;
5165 else
5166 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005167 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005169 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005170 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005172 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return (QLA_SUCCESS);
5174 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005175 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
Quinn Tran41dc5292017-01-19 22:28:03 -08005177
5178 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5179 rval = qla2x00_send_change_request(vha, 0x3, 0);
5180 if (rval != QLA_SUCCESS)
5181 ql_log(ql_log_warn, vha, 0x121,
5182 "Failed to enable receiving of RSCN requests: 0x%x.\n",
5183 rval);
5184 }
5185
5186
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 do {
Quinn Tran726b8542017-01-19 22:28:00 -08005188 qla2x00_mgmt_svr_login(vha);
5189
Andrew Vasquezcca53352005-08-26 19:08:30 -07005190 /* FDMI support. */
5191 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005192 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5193 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07005194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 /* Ensure we are logged into the SNS. */
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005196 loop_id = NPH_SNS_LID(ha);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005197 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5198 0xfc, mb, BIT_1|BIT_0);
Joe Carnuccioa14c7712017-08-23 15:05:12 -07005199 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5200 ql_dbg(ql_dbg_disc, vha, 0x20a1,
5201 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5202 loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005203 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05005204 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08005205 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005206 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5207 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005209 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005210 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005211 if (test_bit(LOOP_RESYNC_NEEDED,
5212 &vha->dpc_flags))
5213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005215 if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005217 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005218 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005219 if (test_bit(LOOP_RESYNC_NEEDED,
5220 &vha->dpc_flags))
5221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
Duane Grigsbyd3bae932017-06-21 13:48:44 -07005223 if (vha->flags.nvme_enabled) {
5224 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5225 ql_dbg(ql_dbg_disc, vha, 0x2049,
5226 "Register NVME FC Type Features failed.\n");
5227 }
5228 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005229 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005231 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005232 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005233 if (test_bit(LOOP_RESYNC_NEEDED,
5234 &vha->dpc_flags))
5235 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005236 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07005238 ql_dbg(ql_dbg_disc, vha, 0x209b,
Colin Ian King0bf0efa2017-06-30 14:47:41 +01005239 "Register Symbolic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005240 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 }
5243 }
5244
Joe Carnuccio827210b2013-02-08 01:57:57 -05005245
Alexei Potashnikdf673272015-07-14 16:00:46 -04005246 /* Mark the time right before querying FW for connected ports.
5247 * This process is long, asynchronous and by the time it's done,
5248 * collected information might not be accurate anymore. E.g.
5249 * disconnected port might have re-connected and a brand new
5250 * session has been created. In this case session's generation
5251 * will be newer than discovery_gen. */
5252 qlt_do_generation_tick(vha, &discovery_gen);
5253
Quinn Trana4239942017-12-28 12:33:26 -08005254 if (USE_ASYNC_SCAN(ha)) {
5255 rval = QLA_SUCCESS;
5256 rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI);
5257 if (rval)
5258 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5259 } else {
Quinn Tranf352eeb2017-12-28 12:33:35 -08005260 list_for_each_entry(fcport, &vha->vp_fcports, list)
5261 fcport->scan_state = QLA_FCPORT_SCAN;
5262
Quinn Trana4239942017-12-28 12:33:26 -08005263 rval = qla2x00_find_all_fabric_devs(vha);
5264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 if (rval != QLA_SUCCESS)
5266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 } while (0);
5268
Duane Grigsbye84067d2017-06-21 13:48:43 -07005269 if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5270 qla_nvme_register_hba(vha);
5271
Quinn Tran726b8542017-01-19 22:28:00 -08005272 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005273 ql_dbg(ql_dbg_disc, vha, 0x2068,
5274 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
5276 return (rval);
5277}
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279/*
5280 * qla2x00_find_all_fabric_devs
5281 *
5282 * Input:
5283 * ha = adapter block pointer.
5284 * dev = database device entry pointer.
5285 *
5286 * Returns:
5287 * 0 = success.
5288 *
5289 * Context:
5290 * Kernel context.
5291 */
5292static int
Quinn Tran726b8542017-01-19 22:28:00 -08005293qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294{
5295 int rval;
5296 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08005297 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 int found;
5299
5300 sw_info_t *swl;
5301 int swl_idx;
5302 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07005303 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005304 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005305 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08005306 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
5308 rval = QLA_SUCCESS;
5309
5310 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08005311 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08005312 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08005313 GFP_KERNEL);
5314 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02005315 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07005317 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005318 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08005320 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005321 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005323 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5324 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005325 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005327 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5328 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005329 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005331 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5332 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08005333 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5334 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005335 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5336 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005338
5339 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005340 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005341 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07005342 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5343 return rval;
5344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 }
5346 swl_idx = 0;
5347
5348 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005349 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005351 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005352 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 return (QLA_MEMORY_ALLOC_FAILED);
5354 }
5355 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 /* Set start port ID scan at adapter ID. */
5357 first_dev = 1;
5358 last_dev = 0;
5359
5360 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005361 loop_id = ha->min_external_loopid;
5362 for (; loop_id <= ha->max_loop_id; loop_id++) {
5363 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 continue;
5365
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07005366 if (ha->current_topology == ISP_CFG_FL &&
5367 (atomic_read(&vha->loop_down_timer) ||
5368 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005369 atomic_set(&vha->loop_down_timer, 0);
5370 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5371 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08005373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 if (swl != NULL) {
5376 if (last_dev) {
5377 wrap.b24 = new_fcport->d_id.b24;
5378 } else {
5379 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
5380 memcpy(new_fcport->node_name,
5381 swl[swl_idx].node_name, WWN_SIZE);
5382 memcpy(new_fcport->port_name,
5383 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005384 memcpy(new_fcport->fabric_port_name,
5385 swl[swl_idx].fabric_port_name, WWN_SIZE);
5386 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005387 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005389 new_fcport->nvme_flag = 0;
Darren Trap1a28faa2017-08-30 10:16:48 -07005390 new_fcport->fc4f_nvme = 0;
Duane Grigsbya5d42f42017-06-21 13:48:41 -07005391 if (vha->flags.nvme_enabled &&
5392 swl[swl_idx].fc4f_nvme) {
5393 new_fcport->fc4f_nvme =
5394 swl[swl_idx].fc4f_nvme;
5395 ql_log(ql_log_info, vha, 0x2131,
5396 "FOUND: NVME port %8phC as FC Type 28h\n",
5397 new_fcport->port_name);
5398 }
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
5401 last_dev = 1;
5402 }
5403 swl_idx++;
5404 }
5405 } else {
5406 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005407 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005409 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005410 "SNS scan failed -- assuming "
5411 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 rval = QLA_SUCCESS;
5413 break;
5414 }
5415 }
5416
5417 /* If wrap on switch device list, exit. */
5418 if (first_dev) {
5419 wrap.b24 = new_fcport->d_id.b24;
5420 first_dev = 0;
5421 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005422 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005423 "Device wrap (%02x%02x%02x).\n",
5424 new_fcport->d_id.b.domain,
5425 new_fcport->d_id.b.area,
5426 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 break;
5428 }
5429
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005430 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005431 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 continue;
5433
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005434 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05005435 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
5436 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07005437
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005438 /* Bypass if same domain and area of adapter. */
5439 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005440 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07005441 ISP_CFG_FL)
5442 continue;
5443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 /* Bypass reserved domain fields. */
5445 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
5446 continue;
5447
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005448 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07005449 if (ql2xgffidenable &&
5450 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
5451 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05005452 continue;
5453
Quinn Tran726b8542017-01-19 22:28:00 -08005454 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 /* Locate matching device in database. */
5457 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005458 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 if (memcmp(new_fcport->port_name, fcport->port_name,
5460 WWN_SIZE))
5461 continue;
5462
Joe Carnuccio827210b2013-02-08 01:57:57 -05005463 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08005464
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 found++;
5466
Andrew Vasquezd8b45212006-10-02 12:00:43 -07005467 /* Update port state. */
5468 memcpy(fcport->fabric_port_name,
5469 new_fcport->fabric_port_name, WWN_SIZE);
5470 fcport->fp_speed = new_fcport->fp_speed;
5471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04005473 * If address the same and state FCS_ONLINE
5474 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 */
5476 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04005477 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08005478 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 break;
5480 }
5481
5482 /*
5483 * If device was not a fabric device before.
5484 */
5485 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
5486 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04005487 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 fcport->flags |= (FCF_FABRIC_DEVICE |
5489 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 break;
5491 }
5492
5493 /*
5494 * Port ID changed or device was marked to be updated;
5495 * Log it out if still logged in and mark it for
5496 * relogin later.
5497 */
Quinn Tran726b8542017-01-19 22:28:00 -08005498 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04005499 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
5500 "port changed FC ID, %8phC"
5501 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
5502 fcport->port_name,
5503 fcport->d_id.b.domain,
5504 fcport->d_id.b.area,
5505 fcport->d_id.b.al_pa,
5506 fcport->loop_id,
5507 new_fcport->d_id.b.domain,
5508 new_fcport->d_id.b.area,
5509 new_fcport->d_id.b.al_pa);
5510 fcport->d_id.b24 = new_fcport->d_id.b24;
5511 break;
5512 }
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 fcport->d_id.b24 = new_fcport->d_id.b24;
5515 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 break;
5517 }
5518
Quinn Tran726b8542017-01-19 22:28:00 -08005519 if (found) {
5520 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08005522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04005524 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08005525 list_add_tail(&new_fcport->list, &vha->vp_fcports);
5526
5527 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 /* Allocate a new replacement fcport. */
5531 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005532 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005534 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005535 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 return (QLA_MEMORY_ALLOC_FAILED);
5537 }
5538 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5539 new_fcport->d_id.b24 = nxt_d_id.b24;
5540 }
5541
Quinn Tran726b8542017-01-19 22:28:00 -08005542 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Quinn Tran726b8542017-01-19 22:28:00 -08005544 /*
5545 * Logout all previous fabric dev marked lost, except FCP2 devices.
5546 */
5547 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5548 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5549 break;
5550
5551 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
5552 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
5553 continue;
5554
5555 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5556 if ((qla_dual_mode_enabled(vha) ||
5557 qla_ini_mode_enabled(vha)) &&
5558 atomic_read(&fcport->state) == FCS_ONLINE) {
5559 qla2x00_mark_device_lost(vha, fcport,
5560 ql2xplogiabsentdevice, 0);
5561 if (fcport->loop_id != FC_NO_LOOP_ID &&
5562 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5563 fcport->port_type != FCT_INITIATOR &&
5564 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07005565 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08005566 "%s %d %8phC post del sess\n",
5567 __func__, __LINE__,
5568 fcport->port_name);
Quinn Trand8630bb2017-12-28 12:33:43 -08005569 qlt_schedule_sess_for_deletion(fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08005570 continue;
5571 }
5572 }
5573 }
5574
5575 if (fcport->scan_state == QLA_FCPORT_FOUND)
5576 qla24xx_fcport_handle_login(vha, fcport);
5577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 return (rval);
5579}
5580
5581/*
5582 * qla2x00_find_new_loop_id
5583 * Scan through our port list and find a new usable loop ID.
5584 *
5585 * Input:
5586 * ha: adapter state pointer.
5587 * dev: port structure pointer.
5588 *
5589 * Returns:
5590 * qla2x00 local function return status code.
5591 *
5592 * Context:
5593 * Kernel context.
5594 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07005595int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005596qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597{
5598 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005599 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07005600 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 rval = QLA_SUCCESS;
5603
Chad Dupuis5f16b332012-08-22 14:21:00 -04005604 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Chad Dupuis5f16b332012-08-22 14:21:00 -04005606 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
5607 LOOPID_MAP_SIZE);
5608 if (dev->loop_id >= LOOPID_MAP_SIZE ||
5609 qla2x00_is_reserved_id(vha, dev->loop_id)) {
5610 dev->loop_id = FC_NO_LOOP_ID;
5611 rval = QLA_FUNCTION_FAILED;
5612 } else
5613 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
Chad Dupuis5f16b332012-08-22 14:21:00 -04005615 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
Chad Dupuis5f16b332012-08-22 14:21:00 -04005617 if (rval == QLA_SUCCESS)
5618 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
5619 "Assigning new loopid=%x, portid=%x.\n",
5620 dev->loop_id, dev->d_id.b24);
5621 else
5622 ql_log(ql_log_warn, dev->vha, 0x2087,
5623 "No loop_id's available, portid=%x.\n",
5624 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 return (rval);
5627}
5628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
5630/*
5631 * qla2x00_fabric_login
5632 * Issue fabric login command.
5633 *
5634 * Input:
5635 * ha = adapter block pointer.
5636 * device = pointer to FC device type structure.
5637 *
5638 * Returns:
5639 * 0 - Login successfully
5640 * 1 - Login failed
5641 * 2 - Initiator device
5642 * 3 - Fatal error
5643 */
5644int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005645qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 uint16_t *next_loopid)
5647{
5648 int rval;
5649 int retry;
5650 uint16_t tmp_loopid;
5651 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005652 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
5654 retry = 0;
5655 tmp_loopid = 0;
5656
5657 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005658 ql_dbg(ql_dbg_disc, vha, 0x2000,
5659 "Trying Fabric Login w/loop id 0x%04x for port "
5660 "%02x%02x%02x.\n",
5661 fcport->loop_id, fcport->d_id.b.domain,
5662 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
5664 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08005665 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 fcport->d_id.b.domain, fcport->d_id.b.area,
5667 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08005668 if (rval != QLA_SUCCESS) {
5669 return rval;
5670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 if (mb[0] == MBS_PORT_ID_USED) {
5672 /*
5673 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005674 * recommends the driver perform an implicit login with
5675 * the specified ID again. The ID we just used is save
5676 * here so we return with an ID that can be tried by
5677 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 */
5679 retry++;
5680 tmp_loopid = fcport->loop_id;
5681 fcport->loop_id = mb[1];
5682
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005683 ql_dbg(ql_dbg_disc, vha, 0x2001,
5684 "Fabric Login: port in use - next loop "
5685 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005687 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
5690 /*
5691 * Login succeeded.
5692 */
5693 if (retry) {
5694 /* A retry occurred before. */
5695 *next_loopid = tmp_loopid;
5696 } else {
5697 /*
5698 * No retry occurred before. Just increment the
5699 * ID value for next login.
5700 */
5701 *next_loopid = (fcport->loop_id + 1);
5702 }
5703
5704 if (mb[1] & BIT_0) {
5705 fcport->port_type = FCT_INITIATOR;
5706 } else {
5707 fcport->port_type = FCT_TARGET;
5708 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07005709 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
5711 }
5712
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07005713 if (mb[10] & BIT_0)
5714 fcport->supported_classes |= FC_COS_CLASS2;
5715 if (mb[10] & BIT_1)
5716 fcport->supported_classes |= FC_COS_CLASS3;
5717
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04005718 if (IS_FWI2_CAPABLE(ha)) {
5719 if (mb[10] & BIT_7)
5720 fcport->flags |=
5721 FCF_CONF_COMP_SUPPORTED;
5722 }
5723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 rval = QLA_SUCCESS;
5725 break;
5726 } else if (mb[0] == MBS_LOOP_ID_USED) {
5727 /*
5728 * Loop ID already used, try next loop ID.
5729 */
5730 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005731 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 if (rval != QLA_SUCCESS) {
5733 /* Ran out of loop IDs to use */
5734 break;
5735 }
5736 } else if (mb[0] == MBS_COMMAND_ERROR) {
5737 /*
5738 * Firmware possibly timed out during login. If NO
5739 * retries are left to do then the device is declared
5740 * dead.
5741 */
5742 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005743 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005744 fcport->d_id.b.domain, fcport->d_id.b.area,
5745 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005746 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 rval = 1;
5749 break;
5750 } else {
5751 /*
5752 * unrecoverable / not handled error
5753 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005754 ql_dbg(ql_dbg_disc, vha, 0x2002,
5755 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5756 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5757 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5758 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
5760 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005761 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005762 fcport->d_id.b.domain, fcport->d_id.b.area,
5763 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005764 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005765 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 rval = 3;
5768 break;
5769 }
5770 }
5771
5772 return (rval);
5773}
5774
5775/*
5776 * qla2x00_local_device_login
5777 * Issue local device login command.
5778 *
5779 * Input:
5780 * ha = adapter block pointer.
5781 * loop_id = loop id of device to login to.
5782 *
5783 * Returns (Where's the #define!!!!):
5784 * 0 - Login successfully
5785 * 1 - Login failed
5786 * 3 - Fatal error
5787 */
5788int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005789qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
5791 int rval;
5792 uint16_t mb[MAILBOX_REGISTER_COUNT];
5793
5794 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005795 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 if (rval == QLA_SUCCESS) {
5797 /* Interrogate mailbox registers for any errors */
5798 if (mb[0] == MBS_COMMAND_ERROR)
5799 rval = 1;
5800 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5801 /* device not in PCB table */
5802 rval = 3;
5803 }
5804
5805 return (rval);
5806}
5807
5808/*
5809 * qla2x00_loop_resync
5810 * Resync with fibre channel devices.
5811 *
5812 * Input:
5813 * ha = adapter block pointer.
5814 *
5815 * Returns:
5816 * 0 = success
5817 */
5818int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005819qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005821 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005823 struct req_que *req;
5824 struct rsp_que *rsp;
5825
Michael Hernandezd7459522016-12-12 14:40:07 -08005826 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005827 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005829 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5830 if (vha->flags.online) {
5831 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5833 wait_time = 256;
5834 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005835 if (!IS_QLAFX00(vha->hw)) {
5836 /*
5837 * Issue a marker after FW becomes
5838 * ready.
5839 */
5840 qla2x00_marker(vha, req, rsp, 0, 0,
5841 MK_SYNC_ALL);
5842 vha->marker_needed = 0;
5843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005846 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005848 if (IS_QLAFX00(vha->hw))
5849 qlafx00_configure_devices(vha);
5850 else
5851 qla2x00_configure_loop(vha);
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005854 } while (!atomic_read(&vha->loop_down_timer) &&
5855 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5856 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5857 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 }
5860
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005861 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005864 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005865 ql_dbg(ql_dbg_disc, vha, 0x206c,
5866 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 return (rval);
5869}
5870
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005871/*
5872* qla2x00_perform_loop_resync
5873* Description: This function will set the appropriate flags and call
5874* qla2x00_loop_resync. If successful loop will be resynced
5875* Arguments : scsi_qla_host_t pointer
5876* returm : Success or Failure
5877*/
5878
5879int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5880{
5881 int32_t rval = 0;
5882
5883 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5884 /*Configure the flags so that resync happens properly*/
5885 atomic_set(&ha->loop_down_timer, 0);
5886 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5887 atomic_set(&ha->loop_state, LOOP_UP);
5888 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5889 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5890 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5891
5892 rval = qla2x00_loop_resync(ha);
5893 } else
5894 atomic_set(&ha->loop_state, LOOP_DEAD);
5895
5896 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5897 }
5898
5899 return rval;
5900}
5901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005903qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005904{
5905 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005906 struct scsi_qla_host *vha;
5907 struct qla_hw_data *ha = base_vha->hw;
5908 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005909
Arun Easifeafb7b2010-09-03 14:57:00 -07005910 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005911 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005912 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5913 atomic_inc(&vha->vref_count);
5914 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005915 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005916 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5917 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005918 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005919
Arun Easifeafb7b2010-09-03 14:57:00 -07005920 spin_lock_irqsave(&ha->vport_slock, flags);
5921 }
5922 }
5923 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005924 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005925 }
5926 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005927}
5928
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005929/* Assumes idc_lock always held on entry */
5930void
5931qla83xx_reset_ownership(scsi_qla_host_t *vha)
5932{
5933 struct qla_hw_data *ha = vha->hw;
5934 uint32_t drv_presence, drv_presence_mask;
5935 uint32_t dev_part_info1, dev_part_info2, class_type;
5936 uint32_t class_type_mask = 0x3;
5937 uint16_t fcoe_other_function = 0xffff, i;
5938
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005939 if (IS_QLA8044(ha)) {
5940 drv_presence = qla8044_rd_direct(vha,
5941 QLA8044_CRB_DRV_ACTIVE_INDEX);
5942 dev_part_info1 = qla8044_rd_direct(vha,
5943 QLA8044_CRB_DEV_PART_INFO_INDEX);
5944 dev_part_info2 = qla8044_rd_direct(vha,
5945 QLA8044_CRB_DEV_PART_INFO2);
5946 } else {
5947 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5948 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5949 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5950 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005951 for (i = 0; i < 8; i++) {
5952 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5953 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5954 (i != ha->portnum)) {
5955 fcoe_other_function = i;
5956 break;
5957 }
5958 }
5959 if (fcoe_other_function == 0xffff) {
5960 for (i = 0; i < 8; i++) {
5961 class_type = ((dev_part_info2 >> (i * 4)) &
5962 class_type_mask);
5963 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5964 ((i + 8) != ha->portnum)) {
5965 fcoe_other_function = i + 8;
5966 break;
5967 }
5968 }
5969 }
5970 /*
5971 * Prepare drv-presence mask based on fcoe functions present.
5972 * However consider only valid physical fcoe function numbers (0-15).
5973 */
5974 drv_presence_mask = ~((1 << (ha->portnum)) |
5975 ((fcoe_other_function == 0xffff) ?
5976 0 : (1 << (fcoe_other_function))));
5977
5978 /* We are the reset owner iff:
5979 * - No other protocol drivers present.
5980 * - This is the lowest among fcoe functions. */
5981 if (!(drv_presence & drv_presence_mask) &&
5982 (ha->portnum < fcoe_other_function)) {
5983 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5984 "This host is Reset owner.\n");
5985 ha->flags.nic_core_reset_owner = 1;
5986 }
5987}
5988
Saurav Kashyapfa492632012-11-21 02:40:29 -05005989static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005990__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5991{
5992 int rval = QLA_SUCCESS;
5993 struct qla_hw_data *ha = vha->hw;
5994 uint32_t drv_ack;
5995
5996 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5997 if (rval == QLA_SUCCESS) {
5998 drv_ack |= (1 << ha->portnum);
5999 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6000 }
6001
6002 return rval;
6003}
6004
Saurav Kashyapfa492632012-11-21 02:40:29 -05006005static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006006__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6007{
6008 int rval = QLA_SUCCESS;
6009 struct qla_hw_data *ha = vha->hw;
6010 uint32_t drv_ack;
6011
6012 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6013 if (rval == QLA_SUCCESS) {
6014 drv_ack &= ~(1 << ha->portnum);
6015 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6016 }
6017
6018 return rval;
6019}
6020
Saurav Kashyapfa492632012-11-21 02:40:29 -05006021static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006022qla83xx_dev_state_to_string(uint32_t dev_state)
6023{
6024 switch (dev_state) {
6025 case QLA8XXX_DEV_COLD:
6026 return "COLD/RE-INIT";
6027 case QLA8XXX_DEV_INITIALIZING:
6028 return "INITIALIZING";
6029 case QLA8XXX_DEV_READY:
6030 return "READY";
6031 case QLA8XXX_DEV_NEED_RESET:
6032 return "NEED RESET";
6033 case QLA8XXX_DEV_NEED_QUIESCENT:
6034 return "NEED QUIESCENT";
6035 case QLA8XXX_DEV_FAILED:
6036 return "FAILED";
6037 case QLA8XXX_DEV_QUIESCENT:
6038 return "QUIESCENT";
6039 default:
6040 return "Unknown";
6041 }
6042}
6043
6044/* Assumes idc-lock always held on entry */
6045void
6046qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6047{
6048 struct qla_hw_data *ha = vha->hw;
6049 uint32_t idc_audit_reg = 0, duration_secs = 0;
6050
6051 switch (audit_type) {
6052 case IDC_AUDIT_TIMESTAMP:
6053 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6054 idc_audit_reg = (ha->portnum) |
6055 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6056 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6057 break;
6058
6059 case IDC_AUDIT_COMPLETION:
6060 duration_secs = ((jiffies_to_msecs(jiffies) -
6061 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6062 idc_audit_reg = (ha->portnum) |
6063 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6064 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6065 break;
6066
6067 default:
6068 ql_log(ql_log_warn, vha, 0xb078,
6069 "Invalid audit type specified.\n");
6070 break;
6071 }
6072}
6073
6074/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05006075static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006076qla83xx_initiating_reset(scsi_qla_host_t *vha)
6077{
6078 struct qla_hw_data *ha = vha->hw;
6079 uint32_t idc_control, dev_state;
6080
6081 __qla83xx_get_idc_control(vha, &idc_control);
6082 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6083 ql_log(ql_log_info, vha, 0xb080,
6084 "NIC Core reset has been disabled. idc-control=0x%x\n",
6085 idc_control);
6086 return QLA_FUNCTION_FAILED;
6087 }
6088
6089 /* Set NEED-RESET iff in READY state and we are the reset-owner */
6090 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6091 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6092 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6093 QLA8XXX_DEV_NEED_RESET);
6094 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6095 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6096 } else {
6097 const char *state = qla83xx_dev_state_to_string(dev_state);
6098 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6099
6100 /* SV: XXX: Is timeout required here? */
6101 /* Wait for IDC state change READY -> NEED_RESET */
6102 while (dev_state == QLA8XXX_DEV_READY) {
6103 qla83xx_idc_unlock(vha, 0);
6104 msleep(200);
6105 qla83xx_idc_lock(vha, 0);
6106 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6107 }
6108 }
6109
6110 /* Send IDC ack by writing to drv-ack register */
6111 __qla83xx_set_drv_ack(vha);
6112
6113 return QLA_SUCCESS;
6114}
6115
6116int
6117__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6118{
6119 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6120}
6121
6122int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006123__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6124{
6125 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6126}
6127
Saurav Kashyapfa492632012-11-21 02:40:29 -05006128static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006129qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6130{
6131 uint32_t drv_presence = 0;
6132 struct qla_hw_data *ha = vha->hw;
6133
6134 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6135 if (drv_presence & (1 << ha->portnum))
6136 return QLA_SUCCESS;
6137 else
6138 return QLA_TEST_FAILED;
6139}
6140
6141int
6142qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6143{
6144 int rval = QLA_SUCCESS;
6145 struct qla_hw_data *ha = vha->hw;
6146
6147 ql_dbg(ql_dbg_p3p, vha, 0xb058,
6148 "Entered %s().\n", __func__);
6149
6150 if (vha->device_flags & DFLG_DEV_FAILED) {
6151 ql_log(ql_log_warn, vha, 0xb059,
6152 "Device in unrecoverable FAILED state.\n");
6153 return QLA_FUNCTION_FAILED;
6154 }
6155
6156 qla83xx_idc_lock(vha, 0);
6157
6158 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6159 ql_log(ql_log_warn, vha, 0xb05a,
6160 "Function=0x%x has been removed from IDC participation.\n",
6161 ha->portnum);
6162 rval = QLA_FUNCTION_FAILED;
6163 goto exit;
6164 }
6165
6166 qla83xx_reset_ownership(vha);
6167
6168 rval = qla83xx_initiating_reset(vha);
6169
6170 /*
6171 * Perform reset if we are the reset-owner,
6172 * else wait till IDC state changes to READY/FAILED.
6173 */
6174 if (rval == QLA_SUCCESS) {
6175 rval = qla83xx_idc_state_handler(vha);
6176
6177 if (rval == QLA_SUCCESS)
6178 ha->flags.nic_core_hung = 0;
6179 __qla83xx_clear_drv_ack(vha);
6180 }
6181
6182exit:
6183 qla83xx_idc_unlock(vha, 0);
6184
6185 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6186
6187 return rval;
6188}
6189
Saurav Kashyap81178772012-08-22 14:21:04 -04006190int
6191qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6192{
6193 struct qla_hw_data *ha = vha->hw;
6194 int rval = QLA_FUNCTION_FAILED;
6195
6196 if (!IS_MCTP_CAPABLE(ha)) {
6197 /* This message can be removed from the final version */
6198 ql_log(ql_log_info, vha, 0x506d,
6199 "This board is not MCTP capable\n");
6200 return rval;
6201 }
6202
6203 if (!ha->mctp_dump) {
6204 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6205 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6206
6207 if (!ha->mctp_dump) {
6208 ql_log(ql_log_warn, vha, 0x506e,
6209 "Failed to allocate memory for mctp dump\n");
6210 return rval;
6211 }
6212 }
6213
6214#define MCTP_DUMP_STR_ADDR 0x00000000
6215 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6216 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6217 if (rval != QLA_SUCCESS) {
6218 ql_log(ql_log_warn, vha, 0x506f,
6219 "Failed to capture mctp dump\n");
6220 } else {
6221 ql_log(ql_log_info, vha, 0x5070,
6222 "Mctp dump capture for host (%ld/%p).\n",
6223 vha->host_no, ha->mctp_dump);
6224 ha->mctp_dumped = 1;
6225 }
6226
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04006227 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04006228 ha->flags.nic_core_reset_hdlr_active = 1;
6229 rval = qla83xx_restart_nic_firmware(vha);
6230 if (rval)
6231 /* NIC Core reset failed. */
6232 ql_log(ql_log_warn, vha, 0x5071,
6233 "Failed to restart nic firmware\n");
6234 else
6235 ql_dbg(ql_dbg_p3p, vha, 0xb084,
6236 "Restarted NIC firmware successfully.\n");
6237 ha->flags.nic_core_reset_hdlr_active = 0;
6238 }
6239
6240 return rval;
6241
6242}
6243
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006244/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006245* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006246* Description: This function will block the new I/Os
6247* Its not aborting any I/Os as context
6248* is not destroyed during quiescence
6249* Arguments: scsi_qla_host_t
6250* return : void
6251*/
6252void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006253qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006254{
6255 struct qla_hw_data *ha = vha->hw;
6256 struct scsi_qla_host *vp;
6257
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006258 ql_dbg(ql_dbg_dpc, vha, 0x401d,
6259 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006260
6261 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6262 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6263 atomic_set(&vha->loop_state, LOOP_DOWN);
6264 qla2x00_mark_all_devices_lost(vha, 0);
6265 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04006266 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006267 } else {
6268 if (!atomic_read(&vha->loop_down_timer))
6269 atomic_set(&vha->loop_down_timer,
6270 LOOP_DOWN_TIME);
6271 }
6272 /* Wait for pending cmds to complete */
6273 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
6274}
6275
Giridhar Malavalia9083012010-04-12 17:59:55 -07006276void
6277qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6278{
6279 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006280 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07006281 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006282 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006283 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006284
Saurav Kashyape46ef002011-02-23 15:27:16 -08006285 /* For ISP82XX, driver waits for completion of the commands.
6286 * online flag should be set.
6287 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006288 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08006289 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006290 ha->flags.chip_reset_done = 0;
6291 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04006292 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006293
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006294 ql_log(ql_log_info, vha, 0x00af,
6295 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006296
Saurav Kashyape46ef002011-02-23 15:27:16 -08006297 /* For ISP82XX, reset_chip is just disabling interrupts.
6298 * Driver waits for the completion of the commands.
6299 * the interrupts need to be enabled.
6300 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006301 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006302 ha->isp_ops->reset_chip(vha);
6303
Quinn Tran9cd883f2017-12-28 12:33:24 -08006304 SAVE_TOPO(ha);
6305 ha->flags.rida_fmt2 = 0;
Quinn Tranec7193e2017-03-15 09:48:55 -07006306 ha->flags.n2n_ae = 0;
6307 ha->flags.lip_ae = 0;
6308 ha->current_topology = 0;
6309 ha->flags.fw_started = 0;
6310 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07006311 ha->base_qpair->chip_reset++;
6312 for (i = 0; i < ha->max_qpairs; i++) {
6313 if (ha->queue_pair_map[i])
6314 ha->queue_pair_map[i]->chip_reset =
6315 ha->base_qpair->chip_reset;
6316 }
Quinn Tran726b8542017-01-19 22:28:00 -08006317
Giridhar Malavalia9083012010-04-12 17:59:55 -07006318 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6319 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6320 atomic_set(&vha->loop_state, LOOP_DOWN);
6321 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006322
6323 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08006324 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07006325 atomic_inc(&vp->vref_count);
6326 spin_unlock_irqrestore(&ha->vport_slock, flags);
6327
Giridhar Malavalia9083012010-04-12 17:59:55 -07006328 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07006329
6330 spin_lock_irqsave(&ha->vport_slock, flags);
6331 atomic_dec(&vp->vref_count);
6332 }
6333 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07006334 } else {
6335 if (!atomic_read(&vha->loop_down_timer))
6336 atomic_set(&vha->loop_down_timer,
6337 LOOP_DOWN_TIME);
6338 }
6339
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08006340 /* Clear all async request states across all VPs. */
6341 list_for_each_entry(fcport, &vha->vp_fcports, list)
6342 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6343 spin_lock_irqsave(&ha->vport_slock, flags);
6344 list_for_each_entry(vp, &ha->vp_list, list) {
6345 atomic_inc(&vp->vref_count);
6346 spin_unlock_irqrestore(&ha->vport_slock, flags);
6347
6348 list_for_each_entry(fcport, &vp->vp_fcports, list)
6349 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6350
6351 spin_lock_irqsave(&ha->vport_slock, flags);
6352 atomic_dec(&vp->vref_count);
6353 }
6354 spin_unlock_irqrestore(&ha->vport_slock, flags);
6355
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006356 if (!ha->flags.eeh_busy) {
6357 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006358 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08006359 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006360 ql_log(ql_log_info, vha, 0x00b4,
6361 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07006362
Saurav Kashyape46ef002011-02-23 15:27:16 -08006363 /* Done waiting for pending commands.
6364 * Reset the online flag.
6365 */
6366 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07006367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07006369 /* Requeue all commands in outstanding command list. */
6370 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
6371 }
Arun Easib6a029e2014-09-25 06:14:52 -04006372 /* memory barrier */
6373 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374}
6375
6376/*
6377* qla2x00_abort_isp
6378* Resets ISP and aborts all outstanding commands.
6379*
6380* Input:
6381* ha = adapter block pointer.
6382*
6383* Returns:
6384* 0 = success
6385*/
6386int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006387qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388{
Andrew Vasquez476e8972006-08-23 14:54:55 -07006389 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006391 struct qla_hw_data *ha = vha->hw;
6392 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006393 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07006394 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006396 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07006397 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
Santosh Vernekara61712972012-08-22 14:21:13 -04006399 if (IS_QLA8031(ha)) {
6400 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
6401 "Clearing fcoe driver presence.\n");
6402 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
6403 ql_dbg(ql_dbg_p3p, vha, 0xb073,
6404 "Error while clearing DRV-Presence.\n");
6405 }
6406
Andrew Vasquez85880802009-12-15 21:29:46 -08006407 if (unlikely(pci_channel_offline(ha->pdev) &&
6408 ha->flags.pci_channel_io_perm_failure)) {
6409 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6410 status = 0;
6411 return status;
6412 }
6413
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006414 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08006415
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006416 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006418 if (!qla2x00_restart_isp(vha)) {
6419 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006421 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 /*
6423 * Issue marker command only when we are going
6424 * to start the I/O .
6425 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006426 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 }
6428
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006429 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006431 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006433 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006434 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07006435
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08006436 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
6437 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006438 if (ha->fce) {
6439 ha->flags.fce_enabled = 1;
6440 memset(ha->fce, 0,
6441 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006442 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006443 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
6444 &ha->fce_bufs);
6445 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006446 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08006447 "Unable to reinitialize FCE "
6448 "(%d).\n", rval);
6449 ha->flags.fce_enabled = 0;
6450 }
6451 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006452
6453 if (ha->eft) {
6454 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006455 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006456 ha->eft_dma, EFT_NUM_BUFFERS);
6457 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006458 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07006459 "Unable to reinitialize EFT "
6460 "(%d).\n", rval);
6461 }
6462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006464 vha->flags.online = 1;
6465 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006467 ql_log(ql_log_fatal, vha, 0x8035,
6468 "ISP error recover failed - "
6469 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006470 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 * The next call disables the board
6472 * completely.
6473 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006474 ha->isp_ops->reset_adapter(vha);
6475 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006477 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 status = 0;
6479 } else { /* schedule another ISP abort */
6480 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006481 ql_dbg(ql_dbg_taskm, vha, 0x8020,
6482 "ISP abort - retry remaining %d.\n",
6483 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 status = 1;
6485 }
6486 } else {
6487 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006488 ql_dbg(ql_dbg_taskm, vha, 0x8021,
6489 "ISP error recovery - retrying (%d) "
6490 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006491 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 status = 1;
6493 }
6494 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07006495
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 }
6497
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006498 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006499 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Quinn Tran1608cc42017-08-23 15:05:03 -07006500 qla2x00_configure_hba(vha);
Arun Easifeafb7b2010-09-03 14:57:00 -07006501 spin_lock_irqsave(&ha->vport_slock, flags);
6502 list_for_each_entry(vp, &ha->vp_list, list) {
6503 if (vp->vp_idx) {
6504 atomic_inc(&vp->vref_count);
6505 spin_unlock_irqrestore(&ha->vport_slock, flags);
6506
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006507 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07006508
6509 spin_lock_irqsave(&ha->vport_slock, flags);
6510 atomic_dec(&vp->vref_count);
6511 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006512 }
Arun Easifeafb7b2010-09-03 14:57:00 -07006513 spin_unlock_irqrestore(&ha->vport_slock, flags);
6514
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04006515 if (IS_QLA8031(ha)) {
6516 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
6517 "Setting back fcoe driver presence.\n");
6518 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
6519 ql_dbg(ql_dbg_p3p, vha, 0xb074,
6520 "Error while setting DRV-Presence.\n");
6521 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006522 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08006523 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
6524 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 }
6526
6527 return(status);
6528}
6529
6530/*
6531* qla2x00_restart_isp
6532* restarts the ISP after a reset
6533*
6534* Input:
6535* ha = adapter block pointer.
6536*
6537* Returns:
6538* 0 = success
6539*/
6540static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006541qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08006543 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006544 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006545 struct req_que *req = ha->req_q_map[0];
6546 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
6548 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006549 if (qla2x00_isp_firmware(vha)) {
6550 vha->flags.online = 0;
6551 status = ha->isp_ops->chip_diag(vha);
6552 if (!status)
6553 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 }
6555
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006556 if (!status && !(status = qla2x00_init_rings(vha))) {
6557 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07006558 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04006559
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006560 /* Initialize the queues in use */
6561 qla25xx_init_queues(ha);
6562
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006563 status = qla2x00_fw_ready(vha);
6564 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006565 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006566 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04006567 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 }
6569
6570 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006571 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 }
6574 return (status);
6575}
6576
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006577static int
6578qla25xx_init_queues(struct qla_hw_data *ha)
6579{
6580 struct rsp_que *rsp = NULL;
6581 struct req_que *req = NULL;
6582 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6583 int ret = -1;
6584 int i;
6585
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006586 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006587 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006588 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006589 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006590 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006591 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006592 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
6593 "%s Rsp que: %d init failed.\n",
6594 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006595 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006596 ql_dbg(ql_dbg_init, base_vha, 0x0100,
6597 "%s Rsp que: %d inited.\n",
6598 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006599 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07006600 }
6601 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006602 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05006603 if (req && test_bit(i, ha->req_qid_map)) {
6604 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006605 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08006606 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006607 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006608 ql_dbg(ql_dbg_init, base_vha, 0x0101,
6609 "%s Req que: %d init failed.\n",
6610 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006611 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006612 ql_dbg(ql_dbg_init, base_vha, 0x0102,
6613 "%s Req que: %d inited.\n",
6614 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006615 }
6616 }
6617 return ret;
6618}
6619
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620/*
6621* qla2x00_reset_adapter
6622* Reset adapter.
6623*
6624* Input:
6625* ha = adapter block pointer.
6626*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07006627void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006628qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629{
6630 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006631 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07006632 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006634 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006635 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 spin_lock_irqsave(&ha->hardware_lock, flags);
6638 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
6639 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6640 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
6641 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
6642 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6643}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006644
6645void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006646qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006647{
6648 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006649 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006650 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
6651
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04006652 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07006653 return;
6654
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006655 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07006656 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006657
6658 spin_lock_irqsave(&ha->hardware_lock, flags);
6659 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
6660 RD_REG_DWORD(&reg->hccr);
6661 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
6662 RD_REG_DWORD(&reg->hccr);
6663 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08006664
6665 if (IS_NOPOLLING_TYPE(ha))
6666 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006667}
6668
David Miller4e08df32007-04-16 12:37:43 -07006669/* On sparc systems, obtain port and node WWN from firmware
6670 * properties.
6671 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006672static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
6673 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07006674{
6675#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006676 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07006677 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07006678 struct device_node *dp = pci_device_to_OF_node(pdev);
6679 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07006680 int len;
6681
6682 val = of_get_property(dp, "port-wwn", &len);
6683 if (val && len >= WWN_SIZE)
6684 memcpy(nv->port_name, val, WWN_SIZE);
6685
6686 val = of_get_property(dp, "node-wwn", &len);
6687 if (val && len >= WWN_SIZE)
6688 memcpy(nv->node_name, val, WWN_SIZE);
6689#endif
6690}
6691
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006692int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006693qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006694{
David Miller4e08df32007-04-16 12:37:43 -07006695 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006696 struct init_cb_24xx *icb;
6697 struct nvram_24xx *nv;
6698 uint32_t *dptr;
6699 uint8_t *dptr1, *dptr2;
6700 uint32_t chksum;
6701 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006702 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006703
David Miller4e08df32007-04-16 12:37:43 -07006704 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006705 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07006706 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006707
6708 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05006709 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006710 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
6711 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
6712 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006713 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08006714 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
6715 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006716
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07006717 ha->nvram_size = sizeof(struct nvram_24xx);
6718 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006719
Seokmann Ju281afe12007-07-26 13:43:34 -07006720 /* Get VPD data into cache */
6721 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006722 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07006723 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
6724
6725 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006726 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006727 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006728 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006729 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
6730 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006731
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006732 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
6733 "Contents of NVRAM\n");
6734 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
6735 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006736
6737 /* Bad NVRAM data, set defaults parameters. */
6738 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
6739 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07006740 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006741 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006742 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04006743 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006744 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
6745 ql_log(ql_log_warn, vha, 0x006c,
6746 "Falling back to functioning (yet invalid -- WWPN) "
6747 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07006748
6749 /*
6750 * Set default initialization control block.
6751 */
6752 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006753 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6754 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006755 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006756 nv->execution_throttle = cpu_to_le16(0xFFFF);
6757 nv->exchange_count = cpu_to_le16(0);
6758 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006759 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006760 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006761 nv->port_name[2] = 0x00;
6762 nv->port_name[3] = 0xe0;
6763 nv->port_name[4] = 0x8b;
6764 nv->port_name[5] = 0x1c;
6765 nv->port_name[6] = 0x55;
6766 nv->port_name[7] = 0x86;
6767 nv->node_name[0] = 0x20;
6768 nv->node_name[1] = 0x00;
6769 nv->node_name[2] = 0x00;
6770 nv->node_name[3] = 0xe0;
6771 nv->node_name[4] = 0x8b;
6772 nv->node_name[5] = 0x1c;
6773 nv->node_name[6] = 0x55;
6774 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006775 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006776 nv->login_retry_count = cpu_to_le16(8);
6777 nv->interrupt_delay_timer = cpu_to_le16(0);
6778 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006779 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006780 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6781 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6782 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6783 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6784 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6785 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006786 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006787 nv->max_luns_per_target = cpu_to_le16(128);
6788 nv->port_down_retry_count = cpu_to_le16(30);
6789 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006790
6791 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006792 }
6793
Quinn Tran726b8542017-01-19 22:28:00 -08006794 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006795 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006796 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006797 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006798 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006799 }
6800
6801 qlt_24xx_config_nvram_stage1(vha, nv);
6802
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006803 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006804 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006805
6806 /* Copy 1st segment. */
6807 dptr1 = (uint8_t *)icb;
6808 dptr2 = (uint8_t *)&nv->version;
6809 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6810 while (cnt--)
6811 *dptr1++ = *dptr2++;
6812
6813 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006814 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006815
6816 /* Copy 2nd segment. */
6817 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6818 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6819 cnt = (uint8_t *)&icb->reserved_3 -
6820 (uint8_t *)&icb->interrupt_delay_timer;
6821 while (cnt--)
6822 *dptr1++ = *dptr2++;
6823
6824 /*
6825 * Setup driver NVRAM options.
6826 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006827 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006828 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006829
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006830 qlt_24xx_config_nvram_stage2(vha, icb);
6831
Bart Van Asschead950362015-07-09 07:24:08 -07006832 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006833 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006834 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6835 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6836 }
6837
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006838 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006839 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006840 /*
6841 * Firmware will apply the following mask if the nodename was
6842 * not provided.
6843 */
6844 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6845 icb->node_name[0] &= 0xF0;
6846 }
6847
6848 /* Set host adapter parameters. */
6849 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006850 ha->flags.enable_lip_reset = 0;
6851 ha->flags.enable_lip_full_login =
6852 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6853 ha->flags.enable_target_reset =
6854 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006855 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006856 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006857
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006858 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6859 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006860
6861 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6862 sizeof(ha->fw_seriallink_options24));
6863
6864 /* save HBA serial number */
6865 ha->serial0 = icb->port_name[5];
6866 ha->serial1 = icb->port_name[6];
6867 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006868 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6869 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006870
Bart Van Asschead950362015-07-09 07:24:08 -07006871 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006872
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006873 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6874
6875 /* Set minimum login_timeout to 4 seconds. */
6876 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6877 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6878 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006879 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006880 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006881
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006882 /* Set minimum RATOV to 100 tenths of a second. */
6883 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006884
6885 ha->loop_reset_delay = nv->reset_delay;
6886
6887 /* Link Down Timeout = 0:
6888 *
6889 * When Port Down timer expires we will start returning
6890 * I/O's to OS with "DID_NO_CONNECT".
6891 *
6892 * Link Down Timeout != 0:
6893 *
6894 * The driver waits for the link to come up after link down
6895 * before returning I/Os to OS with "DID_NO_CONNECT".
6896 */
6897 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6898 ha->loop_down_abort_time =
6899 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6900 } else {
6901 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6902 ha->loop_down_abort_time =
6903 (LOOP_DOWN_TIME - ha->link_down_timeout);
6904 }
6905
6906 /* Need enough time to try and get the port back. */
6907 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6908 if (qlport_down_retry)
6909 ha->port_down_retry_count = qlport_down_retry;
6910
6911 /* Set login_retry_count */
6912 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6913 if (ha->port_down_retry_count ==
6914 le16_to_cpu(nv->port_down_retry_count) &&
6915 ha->port_down_retry_count > 3)
6916 ha->login_retry_count = ha->port_down_retry_count;
6917 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6918 ha->login_retry_count = ha->port_down_retry_count;
6919 if (ql2xloginretrycount)
6920 ha->login_retry_count = ql2xloginretrycount;
6921
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006922 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006923 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006924 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6925 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6926 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6927 le16_to_cpu(icb->interrupt_delay_timer): 2;
6928 }
Bart Van Asschead950362015-07-09 07:24:08 -07006929 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006930 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006931 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006932 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006933 ha->zio_mode = QLA_ZIO_MODE_6;
6934
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006935 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006936 "ZIO mode %d enabled; timer delay (%d us).\n",
6937 ha->zio_mode, ha->zio_timer * 100);
6938
6939 icb->firmware_options_2 |= cpu_to_le32(
6940 (uint32_t)ha->zio_mode);
6941 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006942 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006943 }
6944
David Miller4e08df32007-04-16 12:37:43 -07006945 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006946 ql_log(ql_log_warn, vha, 0x0070,
6947 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006948 }
6949 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006950}
6951
Sawan Chandak4243c112016-01-27 12:03:31 -05006952uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6953{
6954 struct qla27xx_image_status pri_image_status, sec_image_status;
6955 uint8_t valid_pri_image, valid_sec_image;
6956 uint32_t *wptr;
6957 uint32_t cnt, chksum, size;
6958 struct qla_hw_data *ha = vha->hw;
6959
6960 valid_pri_image = valid_sec_image = 1;
6961 ha->active_image = 0;
6962 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6963
6964 if (!ha->flt_region_img_status_pri) {
6965 valid_pri_image = 0;
6966 goto check_sec_image;
6967 }
6968
6969 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6970 ha->flt_region_img_status_pri, size);
6971
6972 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6973 ql_dbg(ql_dbg_init, vha, 0x018b,
6974 "Primary image signature (0x%x) not valid\n",
6975 pri_image_status.signature);
6976 valid_pri_image = 0;
6977 goto check_sec_image;
6978 }
6979
6980 wptr = (uint32_t *)(&pri_image_status);
6981 cnt = size;
6982
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006983 for (chksum = 0; cnt--; wptr++)
6984 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006985
Sawan Chandak4243c112016-01-27 12:03:31 -05006986 if (chksum) {
6987 ql_dbg(ql_dbg_init, vha, 0x018c,
6988 "Checksum validation failed for primary image (0x%x)\n",
6989 chksum);
6990 valid_pri_image = 0;
6991 }
6992
6993check_sec_image:
6994 if (!ha->flt_region_img_status_sec) {
6995 valid_sec_image = 0;
6996 goto check_valid_image;
6997 }
6998
6999 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7000 ha->flt_region_img_status_sec, size);
7001
7002 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
7003 ql_dbg(ql_dbg_init, vha, 0x018d,
7004 "Secondary image signature(0x%x) not valid\n",
7005 sec_image_status.signature);
7006 valid_sec_image = 0;
7007 goto check_valid_image;
7008 }
7009
7010 wptr = (uint32_t *)(&sec_image_status);
7011 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007012 for (chksum = 0; cnt--; wptr++)
7013 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05007014 if (chksum) {
7015 ql_dbg(ql_dbg_init, vha, 0x018e,
7016 "Checksum validation failed for secondary image (0x%x)\n",
7017 chksum);
7018 valid_sec_image = 0;
7019 }
7020
7021check_valid_image:
7022 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
7023 ha->active_image = QLA27XX_PRIMARY_IMAGE;
7024 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
7025 if (!ha->active_image ||
7026 pri_image_status.generation_number <
7027 sec_image_status.generation_number)
7028 ha->active_image = QLA27XX_SECONDARY_IMAGE;
7029 }
7030
7031 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
7032 ha->active_image == 0 ? "default bootld and fw" :
7033 ha->active_image == 1 ? "primary" :
7034 ha->active_image == 2 ? "secondary" :
7035 "Invalid");
7036
7037 return ha->active_image;
7038}
7039
Adrian Bunk413975a2006-06-30 02:33:06 -07007040static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007041qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7042 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007043{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007044 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007045 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007046 uint32_t *dcode, dlen;
7047 uint32_t risc_addr;
7048 uint32_t risc_size;
7049 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007050 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007051 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007052
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007053 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007054 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007055
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007056 rval = QLA_SUCCESS;
7057
7058 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007059 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007060 *srisc_addr = 0;
7061
Sawan Chandak4243c112016-01-27 12:03:31 -05007062 if (IS_QLA27XX(ha) &&
7063 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
7064 faddr = ha->flt_region_fw_sec;
7065
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007066 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007067 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007068 for (i = 0; i < 4; i++)
7069 dcode[i] = be32_to_cpu(dcode[i]);
7070 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7071 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7072 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7073 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007074 ql_log(ql_log_fatal, vha, 0x008c,
7075 "Unable to verify the integrity of flash firmware "
7076 "image.\n");
7077 ql_log(ql_log_fatal, vha, 0x008d,
7078 "Firmware data: %08x %08x %08x %08x.\n",
7079 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007080
7081 return QLA_FUNCTION_FAILED;
7082 }
7083
7084 while (segments && rval == QLA_SUCCESS) {
7085 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007086 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007087
7088 risc_addr = be32_to_cpu(dcode[2]);
7089 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7090 risc_size = be32_to_cpu(dcode[3]);
7091
7092 fragment = 0;
7093 while (risc_size > 0 && rval == QLA_SUCCESS) {
7094 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7095 if (dlen > risc_size)
7096 dlen = risc_size;
7097
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007098 ql_dbg(ql_dbg_init, vha, 0x008e,
7099 "Loading risc segment@ risc addr %x "
7100 "number of dwords 0x%x offset 0x%x.\n",
7101 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007102
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007103 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007104 for (i = 0; i < dlen; i++)
7105 dcode[i] = swab32(dcode[i]);
7106
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007107 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007108 dlen);
7109 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007110 ql_log(ql_log_fatal, vha, 0x008f,
7111 "Failed to load segment %d of firmware.\n",
7112 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007113 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007114 }
7115
7116 faddr += dlen;
7117 risc_addr += dlen;
7118 risc_size -= dlen;
7119 fragment++;
7120 }
7121
7122 /* Next segment. */
7123 segments--;
7124 }
7125
Chad Dupuisf73cb692014-02-26 04:15:06 -05007126 if (!IS_QLA27XX(ha))
7127 return rval;
7128
7129 if (ha->fw_dump_template)
7130 vfree(ha->fw_dump_template);
7131 ha->fw_dump_template = NULL;
7132 ha->fw_dump_template_len = 0;
7133
7134 ql_dbg(ql_dbg_init, vha, 0x0161,
7135 "Loading fwdump template from %x\n", faddr);
7136 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7137 risc_size = be32_to_cpu(dcode[2]);
7138 ql_dbg(ql_dbg_init, vha, 0x0162,
7139 "-> array size %x dwords\n", risc_size);
7140 if (risc_size == 0 || risc_size == ~0)
7141 goto default_template;
7142
7143 dlen = (risc_size - 8) * sizeof(*dcode);
7144 ql_dbg(ql_dbg_init, vha, 0x0163,
7145 "-> template allocating %x bytes...\n", dlen);
7146 ha->fw_dump_template = vmalloc(dlen);
7147 if (!ha->fw_dump_template) {
7148 ql_log(ql_log_warn, vha, 0x0164,
7149 "Failed fwdump template allocate %x bytes.\n", risc_size);
7150 goto default_template;
7151 }
7152
7153 faddr += 7;
7154 risc_size -= 8;
7155 dcode = ha->fw_dump_template;
7156 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
7157 for (i = 0; i < risc_size; i++)
7158 dcode[i] = le32_to_cpu(dcode[i]);
7159
7160 if (!qla27xx_fwdt_template_valid(dcode)) {
7161 ql_log(ql_log_warn, vha, 0x0165,
7162 "Failed fwdump template validate\n");
7163 goto default_template;
7164 }
7165
7166 dlen = qla27xx_fwdt_template_size(dcode);
7167 ql_dbg(ql_dbg_init, vha, 0x0166,
7168 "-> template size %x bytes\n", dlen);
7169 if (dlen > risc_size * sizeof(*dcode)) {
7170 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007171 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007172 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007173 goto default_template;
7174 }
7175 ha->fw_dump_template_len = dlen;
7176 return rval;
7177
7178default_template:
7179 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
7180 if (ha->fw_dump_template)
7181 vfree(ha->fw_dump_template);
7182 ha->fw_dump_template = NULL;
7183 ha->fw_dump_template_len = 0;
7184
7185 dlen = qla27xx_fwdt_template_default_size();
7186 ql_dbg(ql_dbg_init, vha, 0x0169,
7187 "-> template allocating %x bytes...\n", dlen);
7188 ha->fw_dump_template = vmalloc(dlen);
7189 if (!ha->fw_dump_template) {
7190 ql_log(ql_log_warn, vha, 0x016a,
7191 "Failed fwdump template allocate %x bytes.\n", risc_size);
7192 goto failed_template;
7193 }
7194
7195 dcode = ha->fw_dump_template;
7196 risc_size = dlen / sizeof(*dcode);
7197 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
7198 for (i = 0; i < risc_size; i++)
7199 dcode[i] = be32_to_cpu(dcode[i]);
7200
7201 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7202 ql_log(ql_log_warn, vha, 0x016b,
7203 "Failed fwdump template validate\n");
7204 goto failed_template;
7205 }
7206
7207 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7208 ql_dbg(ql_dbg_init, vha, 0x016c,
7209 "-> template size %x bytes\n", dlen);
7210 ha->fw_dump_template_len = dlen;
7211 return rval;
7212
7213failed_template:
7214 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
7215 if (ha->fw_dump_template)
7216 vfree(ha->fw_dump_template);
7217 ha->fw_dump_template = NULL;
7218 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007219 return rval;
7220}
7221
Giridhar Malavalie9454a82013-02-08 01:57:47 -05007222#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007223
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007224int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007225qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08007226{
7227 int rval;
7228 int i, fragment;
7229 uint16_t *wcode, *fwcode;
7230 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
7231 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007232 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007233 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08007234
7235 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007236 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007237 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007238 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007239 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007240 ql_log(ql_log_info, vha, 0x0084,
7241 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08007242 return QLA_FUNCTION_FAILED;
7243 }
7244
7245 rval = QLA_SUCCESS;
7246
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007247 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08007248 *srisc_addr = 0;
7249 fwcode = (uint16_t *)blob->fw->data;
7250 fwclen = 0;
7251
7252 /* Validate firmware image by checking version. */
7253 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007254 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007255 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007256 blob->fw->size);
7257 goto fail_fw_integrity;
7258 }
7259 for (i = 0; i < 4; i++)
7260 wcode[i] = be16_to_cpu(fwcode[i + 4]);
7261 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
7262 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
7263 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007264 ql_log(ql_log_fatal, vha, 0x0086,
7265 "Unable to verify integrity of firmware image.\n");
7266 ql_log(ql_log_fatal, vha, 0x0087,
7267 "Firmware data: %04x %04x %04x %04x.\n",
7268 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08007269 goto fail_fw_integrity;
7270 }
7271
7272 seg = blob->segs;
7273 while (*seg && rval == QLA_SUCCESS) {
7274 risc_addr = *seg;
7275 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
7276 risc_size = be16_to_cpu(fwcode[3]);
7277
7278 /* Validate firmware image size. */
7279 fwclen += risc_size * sizeof(uint16_t);
7280 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007281 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08007282 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007283 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08007284 goto fail_fw_integrity;
7285 }
7286
7287 fragment = 0;
7288 while (risc_size > 0 && rval == QLA_SUCCESS) {
7289 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
7290 if (wlen > risc_size)
7291 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007292 ql_dbg(ql_dbg_init, vha, 0x0089,
7293 "Loading risc segment@ risc addr %x number of "
7294 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08007295
7296 for (i = 0; i < wlen; i++)
7297 wcode[i] = swab16(fwcode[i]);
7298
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007299 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08007300 wlen);
7301 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007302 ql_log(ql_log_fatal, vha, 0x008a,
7303 "Failed to load segment %d of firmware.\n",
7304 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08007305 break;
7306 }
7307
7308 fwcode += wlen;
7309 risc_addr += wlen;
7310 risc_size -= wlen;
7311 fragment++;
7312 }
7313
7314 /* Next segment. */
7315 seg++;
7316 }
7317 return rval;
7318
7319fail_fw_integrity:
7320 return QLA_FUNCTION_FAILED;
7321}
7322
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007323static int
7324qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007325{
7326 int rval;
7327 int segments, fragment;
7328 uint32_t *dcode, dlen;
7329 uint32_t risc_addr;
7330 uint32_t risc_size;
7331 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08007332 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007333 const uint32_t *fwcode;
7334 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007335 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007336 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007337
Andrew Vasquez54333832005-11-09 15:49:04 -08007338 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007339 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08007340 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007341 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01007342 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007343 ql_log(ql_log_warn, vha, 0x0091,
7344 "Firmware images can be retrieved from: "
7345 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07007346
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007347 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007348 }
7349
Chad Dupuiscfb09192011-11-18 09:03:07 -08007350 ql_dbg(ql_dbg_init, vha, 0x0092,
7351 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007352
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007353 rval = QLA_SUCCESS;
7354
7355 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007356 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007357 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08007358 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007359 fwclen = 0;
7360
7361 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08007362 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007363 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007364 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08007365 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007366 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007367 }
7368 for (i = 0; i < 4; i++)
7369 dcode[i] = be32_to_cpu(fwcode[i + 4]);
7370 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
7371 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
7372 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
7373 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007374 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007375 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007376 blob->fw->size);
7377 ql_log(ql_log_fatal, vha, 0x0095,
7378 "Firmware data: %08x %08x %08x %08x.\n",
7379 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007380 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007381 }
7382
7383 while (segments && rval == QLA_SUCCESS) {
7384 risc_addr = be32_to_cpu(fwcode[2]);
7385 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
7386 risc_size = be32_to_cpu(fwcode[3]);
7387
7388 /* Validate firmware image size. */
7389 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08007390 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007391 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08007392 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08007393 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05007394 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007395 }
7396
7397 fragment = 0;
7398 while (risc_size > 0 && rval == QLA_SUCCESS) {
7399 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
7400 if (dlen > risc_size)
7401 dlen = risc_size;
7402
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007403 ql_dbg(ql_dbg_init, vha, 0x0097,
7404 "Loading risc segment@ risc addr %x "
7405 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007406
7407 for (i = 0; i < dlen; i++)
7408 dcode[i] = swab32(fwcode[i]);
7409
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007410 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08007411 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007412 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007413 ql_log(ql_log_fatal, vha, 0x0098,
7414 "Failed to load segment %d of firmware.\n",
7415 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04007416 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007417 }
7418
7419 fwcode += dlen;
7420 risc_addr += dlen;
7421 risc_size -= dlen;
7422 fragment++;
7423 }
7424
7425 /* Next segment. */
7426 segments--;
7427 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05007428
7429 if (!IS_QLA27XX(ha))
7430 return rval;
7431
7432 if (ha->fw_dump_template)
7433 vfree(ha->fw_dump_template);
7434 ha->fw_dump_template = NULL;
7435 ha->fw_dump_template_len = 0;
7436
7437 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04007438 "Loading fwdump template from %x\n",
7439 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007440 risc_size = be32_to_cpu(fwcode[2]);
7441 ql_dbg(ql_dbg_init, vha, 0x172,
7442 "-> array size %x dwords\n", risc_size);
7443 if (risc_size == 0 || risc_size == ~0)
7444 goto default_template;
7445
7446 dlen = (risc_size - 8) * sizeof(*fwcode);
7447 ql_dbg(ql_dbg_init, vha, 0x0173,
7448 "-> template allocating %x bytes...\n", dlen);
7449 ha->fw_dump_template = vmalloc(dlen);
7450 if (!ha->fw_dump_template) {
7451 ql_log(ql_log_warn, vha, 0x0174,
7452 "Failed fwdump template allocate %x bytes.\n", risc_size);
7453 goto default_template;
7454 }
7455
7456 fwcode += 7;
7457 risc_size -= 8;
7458 dcode = ha->fw_dump_template;
7459 for (i = 0; i < risc_size; i++)
7460 dcode[i] = le32_to_cpu(fwcode[i]);
7461
7462 if (!qla27xx_fwdt_template_valid(dcode)) {
7463 ql_log(ql_log_warn, vha, 0x0175,
7464 "Failed fwdump template validate\n");
7465 goto default_template;
7466 }
7467
7468 dlen = qla27xx_fwdt_template_size(dcode);
7469 ql_dbg(ql_dbg_init, vha, 0x0176,
7470 "-> template size %x bytes\n", dlen);
7471 if (dlen > risc_size * sizeof(*fwcode)) {
7472 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07007473 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07007474 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05007475 goto default_template;
7476 }
7477 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007478 return rval;
7479
Chad Dupuisf73cb692014-02-26 04:15:06 -05007480default_template:
7481 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
7482 if (ha->fw_dump_template)
7483 vfree(ha->fw_dump_template);
7484 ha->fw_dump_template = NULL;
7485 ha->fw_dump_template_len = 0;
7486
7487 dlen = qla27xx_fwdt_template_default_size();
7488 ql_dbg(ql_dbg_init, vha, 0x0179,
7489 "-> template allocating %x bytes...\n", dlen);
7490 ha->fw_dump_template = vmalloc(dlen);
7491 if (!ha->fw_dump_template) {
7492 ql_log(ql_log_warn, vha, 0x017a,
7493 "Failed fwdump template allocate %x bytes.\n", risc_size);
7494 goto failed_template;
7495 }
7496
7497 dcode = ha->fw_dump_template;
7498 risc_size = dlen / sizeof(*fwcode);
7499 fwcode = qla27xx_fwdt_template_default();
7500 for (i = 0; i < risc_size; i++)
7501 dcode[i] = be32_to_cpu(fwcode[i]);
7502
7503 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
7504 ql_log(ql_log_warn, vha, 0x017b,
7505 "Failed fwdump template validate\n");
7506 goto failed_template;
7507 }
7508
7509 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
7510 ql_dbg(ql_dbg_init, vha, 0x017c,
7511 "-> template size %x bytes\n", dlen);
7512 ha->fw_dump_template_len = dlen;
7513 return rval;
7514
7515failed_template:
7516 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
7517 if (ha->fw_dump_template)
7518 vfree(ha->fw_dump_template);
7519 ha->fw_dump_template = NULL;
7520 ha->fw_dump_template_len = 0;
7521 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07007522}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007523
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007524int
7525qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7526{
7527 int rval;
7528
Andrew Vasqueze337d902009-04-06 22:33:49 -07007529 if (ql2xfwloadbin == 1)
7530 return qla81xx_load_risc(vha, srisc_addr);
7531
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007532 /*
7533 * FW Load priority:
7534 * 1) Firmware via request-firmware interface (.bin file).
7535 * 2) Firmware residing in flash.
7536 */
7537 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7538 if (rval == QLA_SUCCESS)
7539 return rval;
7540
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007541 return qla24xx_load_risc_flash(vha, srisc_addr,
7542 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007543}
7544
7545int
7546qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
7547{
7548 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007549 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007550
Andrew Vasqueze337d902009-04-06 22:33:49 -07007551 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007552 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07007553
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007554 /*
7555 * FW Load priority:
7556 * 1) Firmware residing in flash.
7557 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007558 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007559 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007560 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007561 if (rval == QLA_SUCCESS)
7562 return rval;
7563
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007564try_blob_fw:
7565 rval = qla24xx_load_risc_blob(vha, srisc_addr);
7566 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
7567 return rval;
7568
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007569 ql_log(ql_log_info, vha, 0x0099,
7570 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007571 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
7572 if (rval != QLA_SUCCESS)
7573 return rval;
7574
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007575 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007576 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07007577 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08007578}
7579
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007580void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007581qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007582{
7583 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007584 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007585
Andrew Vasquez85880802009-12-15 21:29:46 -08007586 if (ha->flags.pci_channel_io_perm_failure)
7587 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07007588 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007589 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07007590 if (!ha->fw_major_version)
7591 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07007592 if (!ha->flags.fw_started)
7593 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007594
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007595 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08007596 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07007597 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007598 ha->isp_ops->reset_chip(vha);
7599 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007600 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007601 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007602 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007603 ql_log(ql_log_info, vha, 0x8015,
7604 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007605 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007606 }
Quinn Tranec7193e2017-03-15 09:48:55 -07007607
Quinn Tran4b60c822017-06-13 20:47:21 -07007608 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07007609 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07007610}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007611
7612int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007613qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007614{
7615 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08007616 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007617 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007618 struct qla_hw_data *ha = vha->hw;
7619 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007620 struct req_que *req;
7621 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007622
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007623 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007624 return -EINVAL;
7625
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007626 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08007627 if (vha->qpair)
7628 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007629 else
Michael Hernandezd7459522016-12-12 14:40:07 -08007630 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07007631 rsp = req->rsp;
7632
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007633 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007634 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08007635 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007636 }
7637
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007638 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007639
7640 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08007641 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
7642 BIT_1);
7643 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
7644 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
7645 ql_dbg(ql_dbg_init, vha, 0x0120,
7646 "Failed SNS login: loop_id=%x, rval2=%d\n",
7647 NPH_SNS, rval2);
7648 else
7649 ql_dbg(ql_dbg_init, vha, 0x0103,
7650 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
7651 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
7652 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007653 return (QLA_FUNCTION_FAILED);
7654 }
7655
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007656 atomic_set(&vha->loop_down_timer, 0);
7657 atomic_set(&vha->loop_state, LOOP_UP);
7658 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7659 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
7660 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07007661
7662 return rval;
7663}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007664
7665/* 84XX Support **************************************************************/
7666
7667static LIST_HEAD(qla_cs84xx_list);
7668static DEFINE_MUTEX(qla_cs84xx_mutex);
7669
7670static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007671qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007672{
7673 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007674 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007675
7676 mutex_lock(&qla_cs84xx_mutex);
7677
7678 /* Find any shared 84xx chip. */
7679 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
7680 if (cs84xx->bus == ha->pdev->bus) {
7681 kref_get(&cs84xx->kref);
7682 goto done;
7683 }
7684 }
7685
7686 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
7687 if (!cs84xx)
7688 goto done;
7689
7690 kref_init(&cs84xx->kref);
7691 spin_lock_init(&cs84xx->access_lock);
7692 mutex_init(&cs84xx->fw_update_mutex);
7693 cs84xx->bus = ha->pdev->bus;
7694
7695 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
7696done:
7697 mutex_unlock(&qla_cs84xx_mutex);
7698 return cs84xx;
7699}
7700
7701static void
7702__qla84xx_chip_release(struct kref *kref)
7703{
7704 struct qla_chip_state_84xx *cs84xx =
7705 container_of(kref, struct qla_chip_state_84xx, kref);
7706
7707 mutex_lock(&qla_cs84xx_mutex);
7708 list_del(&cs84xx->list);
7709 mutex_unlock(&qla_cs84xx_mutex);
7710 kfree(cs84xx);
7711}
7712
7713void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007714qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007715{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007716 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007717 if (ha->cs84xx)
7718 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
7719}
7720
7721static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007722qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007723{
7724 int rval;
7725 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007726 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007727
7728 mutex_lock(&ha->cs84xx->fw_update_mutex);
7729
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08007730 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07007731
7732 mutex_unlock(&ha->cs84xx->fw_update_mutex);
7733
7734 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
7735 QLA_SUCCESS;
7736}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007737
7738/* 81XX Support **************************************************************/
7739
7740int
7741qla81xx_nvram_config(scsi_qla_host_t *vha)
7742{
7743 int rval;
7744 struct init_cb_81xx *icb;
7745 struct nvram_81xx *nv;
7746 uint32_t *dptr;
7747 uint8_t *dptr1, *dptr2;
7748 uint32_t chksum;
7749 uint16_t cnt;
7750 struct qla_hw_data *ha = vha->hw;
7751
7752 rval = QLA_SUCCESS;
7753 icb = (struct init_cb_81xx *)ha->init_cb;
7754 nv = ha->nvram;
7755
7756 /* Determine NVRAM starting address. */
7757 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007758 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007759 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7760 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007761
7762 /* Get VPD data into cache */
7763 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007764 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7765 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007766
7767 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007768 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007769 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007770 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007771 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7772 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007773
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007774 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7775 "Contents of NVRAM:\n");
7776 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7777 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007778
7779 /* Bad NVRAM data, set defaults parameters. */
7780 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7781 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007782 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007783 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007784 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007785 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007786 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007787 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007788 ql_log(ql_log_info, vha, 0x0074,
7789 "Falling back to functioning (yet invalid -- WWPN) "
7790 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007791
7792 /*
7793 * Set default initialization control block.
7794 */
7795 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007796 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7797 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007798 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007799 nv->execution_throttle = cpu_to_le16(0xFFFF);
7800 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007801 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007802 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007803 nv->port_name[2] = 0x00;
7804 nv->port_name[3] = 0xe0;
7805 nv->port_name[4] = 0x8b;
7806 nv->port_name[5] = 0x1c;
7807 nv->port_name[6] = 0x55;
7808 nv->port_name[7] = 0x86;
7809 nv->node_name[0] = 0x20;
7810 nv->node_name[1] = 0x00;
7811 nv->node_name[2] = 0x00;
7812 nv->node_name[3] = 0xe0;
7813 nv->node_name[4] = 0x8b;
7814 nv->node_name[5] = 0x1c;
7815 nv->node_name[6] = 0x55;
7816 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007817 nv->login_retry_count = cpu_to_le16(8);
7818 nv->interrupt_delay_timer = cpu_to_le16(0);
7819 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007820 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007821 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7822 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7823 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7824 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7825 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7826 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007827 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007828 nv->max_luns_per_target = cpu_to_le16(128);
7829 nv->port_down_retry_count = cpu_to_le16(30);
7830 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007831 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007832 nv->enode_mac[1] = 0xC0;
7833 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007834 nv->enode_mac[3] = 0x04;
7835 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007836 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007837
7838 rval = 1;
7839 }
7840
Arun Easi9e522cd2012-08-22 14:21:31 -04007841 if (IS_T10_PI_CAPABLE(ha))
7842 nv->frame_payload_size &= ~7;
7843
Arun Easiaa230bc2013-01-30 03:34:39 -05007844 qlt_81xx_config_nvram_stage1(vha, nv);
7845
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007846 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007847 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007848
7849 /* Copy 1st segment. */
7850 dptr1 = (uint8_t *)icb;
7851 dptr2 = (uint8_t *)&nv->version;
7852 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7853 while (cnt--)
7854 *dptr1++ = *dptr2++;
7855
7856 icb->login_retry_count = nv->login_retry_count;
7857
7858 /* Copy 2nd segment. */
7859 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7860 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7861 cnt = (uint8_t *)&icb->reserved_5 -
7862 (uint8_t *)&icb->interrupt_delay_timer;
7863 while (cnt--)
7864 *dptr1++ = *dptr2++;
7865
7866 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7867 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7868 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007869 icb->enode_mac[0] = 0x00;
7870 icb->enode_mac[1] = 0xC0;
7871 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007872 icb->enode_mac[3] = 0x04;
7873 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007874 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007875 }
7876
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007877 /* Use extended-initialization control block. */
7878 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7879
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007880 /*
7881 * Setup driver NVRAM options.
7882 */
7883 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007884 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007885
Arun Easiaa230bc2013-01-30 03:34:39 -05007886 qlt_81xx_config_nvram_stage2(vha, icb);
7887
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007888 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007889 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007890 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7891 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7892 }
7893
7894 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007895 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007896 /*
7897 * Firmware will apply the following mask if the nodename was
7898 * not provided.
7899 */
7900 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7901 icb->node_name[0] &= 0xF0;
7902 }
7903
7904 /* Set host adapter parameters. */
7905 ha->flags.disable_risc_code_load = 0;
7906 ha->flags.enable_lip_reset = 0;
7907 ha->flags.enable_lip_full_login =
7908 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7909 ha->flags.enable_target_reset =
7910 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7911 ha->flags.enable_led_scheme = 0;
7912 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7913
7914 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7915 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7916
7917 /* save HBA serial number */
7918 ha->serial0 = icb->port_name[5];
7919 ha->serial1 = icb->port_name[6];
7920 ha->serial2 = icb->port_name[7];
7921 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7922 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7923
Bart Van Asschead950362015-07-09 07:24:08 -07007924 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007925
7926 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7927
7928 /* Set minimum login_timeout to 4 seconds. */
7929 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7930 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7931 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007932 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007933 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007934
7935 /* Set minimum RATOV to 100 tenths of a second. */
7936 ha->r_a_tov = 100;
7937
7938 ha->loop_reset_delay = nv->reset_delay;
7939
7940 /* Link Down Timeout = 0:
7941 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007942 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007943 * I/O's to OS with "DID_NO_CONNECT".
7944 *
7945 * Link Down Timeout != 0:
7946 *
7947 * The driver waits for the link to come up after link down
7948 * before returning I/Os to OS with "DID_NO_CONNECT".
7949 */
7950 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7951 ha->loop_down_abort_time =
7952 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7953 } else {
7954 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7955 ha->loop_down_abort_time =
7956 (LOOP_DOWN_TIME - ha->link_down_timeout);
7957 }
7958
7959 /* Need enough time to try and get the port back. */
7960 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7961 if (qlport_down_retry)
7962 ha->port_down_retry_count = qlport_down_retry;
7963
7964 /* Set login_retry_count */
7965 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7966 if (ha->port_down_retry_count ==
7967 le16_to_cpu(nv->port_down_retry_count) &&
7968 ha->port_down_retry_count > 3)
7969 ha->login_retry_count = ha->port_down_retry_count;
7970 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7971 ha->login_retry_count = ha->port_down_retry_count;
7972 if (ql2xloginretrycount)
7973 ha->login_retry_count = ql2xloginretrycount;
7974
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007975 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007976 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007977 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007978
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007979 /* Enable ZIO. */
7980 if (!vha->flags.init_done) {
7981 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7982 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7983 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7984 le16_to_cpu(icb->interrupt_delay_timer): 2;
7985 }
Bart Van Asschead950362015-07-09 07:24:08 -07007986 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007987 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7988 vha->flags.process_response_queue = 0;
7989 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7990 ha->zio_mode = QLA_ZIO_MODE_6;
7991
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007992 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007993 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007994 ha->zio_mode,
7995 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007996
7997 icb->firmware_options_2 |= cpu_to_le32(
7998 (uint32_t)ha->zio_mode);
7999 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8000 vha->flags.process_response_queue = 1;
8001 }
8002
Quinn Tran41dc5292017-01-19 22:28:03 -08008003 /* enable RIDA Format2 */
8004 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
8005 icb->firmware_options_3 |= BIT_0;
8006
Duane Grigsbyedd05de2017-10-13 09:34:06 -07008007 if (IS_QLA27XX(ha)) {
8008 icb->firmware_options_3 |= BIT_8;
8009 ql_dbg(ql_log_info, vha, 0x0075,
8010 "Enabling direct connection.\n");
8011 }
8012
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008013 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008014 ql_log(ql_log_warn, vha, 0x0076,
8015 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008016 }
8017 return (rval);
8018}
8019
Giridhar Malavalia9083012010-04-12 17:59:55 -07008020int
8021qla82xx_restart_isp(scsi_qla_host_t *vha)
8022{
8023 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008024 struct qla_hw_data *ha = vha->hw;
8025 struct req_que *req = ha->req_q_map[0];
8026 struct rsp_que *rsp = ha->rsp_q_map[0];
8027 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07008028 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008029
8030 status = qla2x00_init_rings(vha);
8031 if (!status) {
8032 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8033 ha->flags.chip_reset_done = 1;
8034
8035 status = qla2x00_fw_ready(vha);
8036 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07008037 /* Issue a marker after FW becomes ready. */
8038 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008039 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04008040 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008041 }
8042
8043 /* if no cable then assume it's good */
8044 if ((vha->device_flags & DFLG_NO_CABLE))
8045 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07008046 }
8047
8048 if (!status) {
8049 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8050
8051 if (!atomic_read(&vha->loop_down_timer)) {
8052 /*
8053 * Issue marker command only when we are going
8054 * to start the I/O .
8055 */
8056 vha->marker_needed = 1;
8057 }
8058
Giridhar Malavalia9083012010-04-12 17:59:55 -07008059 ha->isp_ops->enable_intrs(ha);
8060
8061 ha->isp_abort_cnt = 0;
8062 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8063
Saurav Kashyap53296782011-05-10 11:30:06 -07008064 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07008065 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07008066
Giridhar Malavalia9083012010-04-12 17:59:55 -07008067 if (ha->fce) {
8068 ha->flags.fce_enabled = 1;
8069 memset(ha->fce, 0,
8070 fce_calc_size(ha->fce_bufs));
8071 rval = qla2x00_enable_fce_trace(vha,
8072 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8073 &ha->fce_bufs);
8074 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008075 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008076 "Unable to reinitialize FCE (%d).\n",
8077 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008078 ha->flags.fce_enabled = 0;
8079 }
8080 }
8081
8082 if (ha->eft) {
8083 memset(ha->eft, 0, EFT_SIZE);
8084 rval = qla2x00_enable_eft_trace(vha,
8085 ha->eft_dma, EFT_NUM_BUFFERS);
8086 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008087 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008088 "Unable to reinitialize EFT (%d).\n",
8089 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07008090 }
8091 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008092 }
8093
8094 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008095 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008096 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07008097
8098 spin_lock_irqsave(&ha->vport_slock, flags);
8099 list_for_each_entry(vp, &ha->vp_list, list) {
8100 if (vp->vp_idx) {
8101 atomic_inc(&vp->vref_count);
8102 spin_unlock_irqrestore(&ha->vport_slock, flags);
8103
Giridhar Malavalia9083012010-04-12 17:59:55 -07008104 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07008105
8106 spin_lock_irqsave(&ha->vport_slock, flags);
8107 atomic_dec(&vp->vref_count);
8108 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07008109 }
Arun Easifeafb7b2010-09-03 14:57:00 -07008110 spin_unlock_irqrestore(&ha->vport_slock, flags);
8111
Giridhar Malavalia9083012010-04-12 17:59:55 -07008112 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08008113 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008114 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07008115 }
8116
8117 return status;
8118}
8119
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008120void
Andrew Vasquezae97c912010-02-18 10:07:28 -08008121qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008122{
Andrew Vasquezae97c912010-02-18 10:07:28 -08008123 struct qla_hw_data *ha = vha->hw;
8124
Himanshu Madhanif198caf2016-01-27 12:03:30 -05008125 /* Hold status IOCBs until ABTS response received. */
8126 if (ql2xfwholdabts)
8127 ha->fw_options[3] |= BIT_12;
8128
Giridhar Malavali088d09d2016-07-06 11:14:20 -04008129 /* Set Retry FLOGI in case of P2P connection */
8130 if (ha->operating_mode == P2P) {
8131 ha->fw_options[2] |= BIT_3;
8132 ql_dbg(ql_dbg_disc, vha, 0x2103,
8133 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
8134 __func__, ha->fw_options[2]);
8135 }
8136
Quinn Tran41dc5292017-01-19 22:28:03 -08008137 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
8138 if (ql2xmvasynctoatio) {
8139 if (qla_tgt_mode_enabled(vha) ||
8140 qla_dual_mode_enabled(vha))
8141 ha->fw_options[2] |= BIT_11;
8142 else
8143 ha->fw_options[2] &= ~BIT_11;
8144 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08008145
Quinn Tranf7e761f2017-06-02 09:12:02 -07008146 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07008147 qla_dual_mode_enabled(vha)) {
8148 /* FW auto send SCSI status during */
8149 ha->fw_options[1] |= BIT_8;
8150 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
8151
8152 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07008153 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008154 } else {
8155 ha->fw_options[1] &= ~BIT_8;
8156 ha->fw_options[10] &= 0x00ff;
8157
Quinn Tranf7e761f2017-06-02 09:12:02 -07008158 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07008159 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07008160
Quinn Tran41dc5292017-01-19 22:28:03 -08008161 if (ql2xetsenable) {
8162 /* Enable ETS Burst. */
8163 memset(ha->fw_options, 0, sizeof(ha->fw_options));
8164 ha->fw_options[2] |= BIT_9;
8165 }
8166
Quinn Tran83548fe2017-06-02 09:12:01 -07008167 ql_dbg(ql_dbg_init, vha, 0x00e9,
8168 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
8169 __func__, ha->fw_options[1], ha->fw_options[2],
8170 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08008171
Andrew Vasquezae97c912010-02-18 10:07:28 -08008172 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08008173}
Sarang Radke09ff7012010-03-19 17:03:59 -07008174
8175/*
8176 * qla24xx_get_fcp_prio
8177 * Gets the fcp cmd priority value for the logged in port.
8178 * Looks for a match of the port descriptors within
8179 * each of the fcp prio config entries. If a match is found,
8180 * the tag (priority) value is returned.
8181 *
8182 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008183 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008184 * fcport = port structure pointer.
8185 *
8186 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07008187 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008188 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07008189 *
8190 * Context:
8191 * Kernel context
8192 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008193static int
Sarang Radke09ff7012010-03-19 17:03:59 -07008194qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8195{
8196 int i, entries;
8197 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008198 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008199 uint32_t pid1, pid2;
8200 uint64_t wwn1, wwn2;
8201 struct qla_fcp_prio_entry *pri_entry;
8202 struct qla_hw_data *ha = vha->hw;
8203
8204 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008205 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008206
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008207 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07008208 entries = ha->fcp_prio_cfg->num_entries;
8209 pri_entry = &ha->fcp_prio_cfg->entry[0];
8210
8211 for (i = 0; i < entries; i++) {
8212 pid_match = wwn_match = 0;
8213
8214 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8215 pri_entry++;
8216 continue;
8217 }
8218
8219 /* check source pid for a match */
8220 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8221 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8222 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8223 if (pid1 == INVALID_PORT_ID)
8224 pid_match++;
8225 else if (pid1 == pid2)
8226 pid_match++;
8227 }
8228
8229 /* check destination pid for a match */
8230 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8231 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8232 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8233 if (pid1 == INVALID_PORT_ID)
8234 pid_match++;
8235 else if (pid1 == pid2)
8236 pid_match++;
8237 }
8238
8239 /* check source WWN for a match */
8240 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8241 wwn1 = wwn_to_u64(vha->port_name);
8242 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8243 if (wwn2 == (uint64_t)-1)
8244 wwn_match++;
8245 else if (wwn1 == wwn2)
8246 wwn_match++;
8247 }
8248
8249 /* check destination WWN for a match */
8250 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
8251 wwn1 = wwn_to_u64(fcport->port_name);
8252 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
8253 if (wwn2 == (uint64_t)-1)
8254 wwn_match++;
8255 else if (wwn1 == wwn2)
8256 wwn_match++;
8257 }
8258
8259 if (pid_match == 2 || wwn_match == 2) {
8260 /* Found a matching entry */
8261 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
8262 priority = pri_entry->tag;
8263 break;
8264 }
8265
8266 pri_entry++;
8267 }
8268
8269 return priority;
8270}
8271
8272/*
8273 * qla24xx_update_fcport_fcp_prio
8274 * Activates fcp priority for the logged in fc port
8275 *
8276 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008277 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07008278 * fcp = port structure pointer.
8279 *
8280 * Return:
8281 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8282 *
8283 * Context:
8284 * Kernel context.
8285 */
8286int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008287qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07008288{
8289 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008290 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07008291 uint16_t mb[5];
8292
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008293 if (fcport->port_type != FCT_TARGET ||
8294 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07008295 return QLA_FUNCTION_FAILED;
8296
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008297 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07008298 if (priority < 0)
8299 return QLA_FUNCTION_FAILED;
8300
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04008301 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008302 fcport->fcp_prio = priority & 0xf;
8303 return QLA_SUCCESS;
8304 }
8305
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08008306 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08008307 if (ret == QLA_SUCCESS) {
8308 if (fcport->fcp_prio != priority)
8309 ql_dbg(ql_dbg_user, vha, 0x709e,
8310 "Updated FCP_CMND priority - value=%d loop_id=%d "
8311 "port_id=%02x%02x%02x.\n", priority,
8312 fcport->loop_id, fcport->d_id.b.domain,
8313 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08008314 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08008315 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07008316 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08008317 "Unable to update FCP_CMND priority - ret=0x%x for "
8318 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
8319 fcport->d_id.b.domain, fcport->d_id.b.area,
8320 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07008321 return ret;
8322}
8323
8324/*
8325 * qla24xx_update_all_fcp_prio
8326 * Activates fcp priority for all the logged in ports
8327 *
8328 * Input:
8329 * ha = adapter block pointer.
8330 *
8331 * Return:
8332 * QLA_SUCCESS or QLA_FUNCTION_FAILED
8333 *
8334 * Context:
8335 * Kernel context.
8336 */
8337int
8338qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
8339{
8340 int ret;
8341 fc_port_t *fcport;
8342
8343 ret = QLA_FUNCTION_FAILED;
8344 /* We need to set priority for all logged in ports */
8345 list_for_each_entry(fcport, &vha->vp_fcports, list)
8346 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
8347
8348 return ret;
8349}
Michael Hernandezd7459522016-12-12 14:40:07 -08008350
Quinn Tran82de8022017-06-13 20:47:17 -07008351struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
8352 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08008353{
8354 int rsp_id = 0;
8355 int req_id = 0;
8356 int i;
8357 struct qla_hw_data *ha = vha->hw;
8358 uint16_t qpair_id = 0;
8359 struct qla_qpair *qpair = NULL;
8360 struct qla_msix_entry *msix;
8361
8362 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
8363 ql_log(ql_log_warn, vha, 0x00181,
8364 "FW/Driver is not multi-queue capable.\n");
8365 return NULL;
8366 }
8367
Himanshu Madhanic38d1ba2017-10-13 15:43:22 -07008368 if (ql2xmqsupport || ql2xnvmeenable) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008369 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
8370 if (qpair == NULL) {
8371 ql_log(ql_log_warn, vha, 0x0182,
8372 "Failed to allocate memory for queue pair.\n");
8373 return NULL;
8374 }
8375 memset(qpair, 0, sizeof(struct qla_qpair));
8376
8377 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08008378 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07008379 qpair->qp_lock_ptr = &qpair->qp_lock;
8380 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07008381 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08008382
8383 /* Assign available que pair id */
8384 mutex_lock(&ha->mq_lock);
8385 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008386 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008387 mutex_unlock(&ha->mq_lock);
8388 ql_log(ql_log_warn, vha, 0x0183,
8389 "No resources to create additional q pair.\n");
8390 goto fail_qid_map;
8391 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07008392 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08008393 set_bit(qpair_id, ha->qpair_qid_map);
8394 ha->queue_pair_map[qpair_id] = qpair;
8395 qpair->id = qpair_id;
8396 qpair->vp_idx = vp_idx;
himanshu.madhani@cavium.come6373f332017-08-23 15:04:57 -07008397 qpair->fw_started = ha->flags.fw_started;
Quinn Trane326d222017-06-13 20:47:18 -07008398 INIT_LIST_HEAD(&qpair->hints_list);
Duane Grigsbycf19c452017-08-23 15:04:58 -07008399 INIT_LIST_HEAD(&qpair->nvme_done_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07008400 qpair->chip_reset = ha->base_qpair->chip_reset;
8401 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
8402 qpair->enable_explicit_conf =
8403 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08008404
8405 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08008406 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08008407 if (msix->in_use)
8408 continue;
8409 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07008410 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08008411 "Vector %x selected for qpair\n", msix->vector);
8412 break;
8413 }
8414 if (!qpair->msix) {
8415 ql_log(ql_log_warn, vha, 0x0184,
8416 "Out of MSI-X vectors!.\n");
8417 goto fail_msix;
8418 }
8419
8420 qpair->msix->in_use = 1;
8421 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07008422 qpair->pdev = ha->pdev;
8423 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
8424 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08008425
8426 mutex_unlock(&ha->mq_lock);
8427
8428 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07008429 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008430 if (!rsp_id) {
8431 ql_log(ql_log_warn, vha, 0x0185,
8432 "Failed to create response queue.\n");
8433 goto fail_rsp;
8434 }
8435
8436 qpair->rsp = ha->rsp_q_map[rsp_id];
8437
8438 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07008439 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
8440 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08008441 if (!req_id) {
8442 ql_log(ql_log_warn, vha, 0x0186,
8443 "Failed to create request queue.\n");
8444 goto fail_req;
8445 }
8446
8447 qpair->req = ha->req_q_map[req_id];
8448 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07008449 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07008450 /* init qpair to this cpu. Will adjust at run time. */
8451 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08008452
8453 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
8454 if (ha->fw_attributes & BIT_4)
8455 qpair->difdix_supported = 1;
8456 }
8457
8458 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
8459 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07008460 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08008461 "Failed to create srb mempool for qpair %d\n",
8462 qpair->id);
8463 goto fail_mempool;
8464 }
8465
8466 /* Mark as online */
8467 qpair->online = 1;
8468
8469 if (!vha->flags.qpairs_available)
8470 vha->flags.qpairs_available = 1;
8471
8472 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
8473 "Request/Response queue pair created, id %d\n",
8474 qpair->id);
8475 ql_dbg(ql_dbg_init, vha, 0x0187,
8476 "Request/Response queue pair created, id %d\n",
8477 qpair->id);
8478 }
8479 return qpair;
8480
8481fail_mempool:
8482fail_req:
8483 qla25xx_delete_rsp_que(vha, qpair->rsp);
8484fail_rsp:
8485 mutex_lock(&ha->mq_lock);
8486 qpair->msix->in_use = 0;
8487 list_del(&qpair->qp_list_elem);
8488 if (list_empty(&vha->qp_list))
8489 vha->flags.qpairs_available = 0;
8490fail_msix:
8491 ha->queue_pair_map[qpair_id] = NULL;
8492 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008493 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008494 mutex_unlock(&ha->mq_lock);
8495fail_qid_map:
8496 kfree(qpair);
8497 return NULL;
8498}
8499
8500int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
8501{
Sawan Chandakd65237c2017-06-13 20:47:19 -07008502 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08008503 struct qla_hw_data *ha = qpair->hw;
8504
8505 qpair->delete_in_progress = 1;
8506 while (atomic_read(&qpair->ref_count))
8507 msleep(500);
8508
8509 ret = qla25xx_delete_req_que(vha, qpair->req);
8510 if (ret != QLA_SUCCESS)
8511 goto fail;
himanshu.madhani@cavium.com7867b982017-12-04 14:45:16 -08008512
Michael Hernandezd7459522016-12-12 14:40:07 -08008513 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
8514 if (ret != QLA_SUCCESS)
8515 goto fail;
8516
8517 mutex_lock(&ha->mq_lock);
8518 ha->queue_pair_map[qpair->id] = NULL;
8519 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07008520 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08008521 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07008522 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08008523 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07008524 vha->flags.qpairs_req_created = 0;
8525 vha->flags.qpairs_rsp_created = 0;
8526 }
Michael Hernandezd7459522016-12-12 14:40:07 -08008527 mempool_destroy(qpair->srb_mempool);
8528 kfree(qpair);
8529 mutex_unlock(&ha->mq_lock);
8530
8531 return QLA_SUCCESS;
8532fail:
8533 return ret;
8534}