blob: 4a659460d6e67a5ef9ca45cfc95742d6885ab772 [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 *);
Quinn Tran726b8542017-01-19 22:28:00 -080039static int qla24xx_post_gpdb_work(struct scsi_qla_host *, fc_port_t *, u8);
40static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41 struct event_arg *);
Harihara Kadayam4d4df192008-04-03 13:13:26 -070042
Andrew Vasquezac280b62009-08-20 11:06:05 -070043/* SRB Extensions ---------------------------------------------------------- */
44
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080045void
46qla2x00_sp_timeout(unsigned long __data)
Andrew Vasquezac280b62009-08-20 11:06:05 -070047{
48 srb_t *sp = (srb_t *)__data;
Madhuranath Iyengar49163922010-05-04 15:01:28 -070049 struct srb_iocb *iocb;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080050 scsi_qla_host_t *vha = sp->vha;
Andrew Vasquezac280b62009-08-20 11:06:05 -070051 struct req_que *req;
52 unsigned long flags;
53
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080054 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
55 req = vha->hw->req_q_map[0];
Andrew Vasquezac280b62009-08-20 11:06:05 -070056 req->outstanding_cmds[sp->handle] = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080057 iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar49163922010-05-04 15:01:28 -070058 iocb->timeout(sp);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080059 sp->free(sp);
60 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -070061}
62
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080063void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080064qla2x00_sp_free(void *ptr)
Andrew Vasquezac280b62009-08-20 11:06:05 -070065{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080066 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080067 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -070068
Chad Dupuis4d97cc52010-10-15 11:27:41 -070069 del_timer(&iocb->timer);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080070 qla2x00_rel_sp(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -070071}
72
Andrew Vasquezac280b62009-08-20 11:06:05 -070073/* Asynchronous Login/Logout Routines -------------------------------------- */
74
Saurav Kashyapa9b6f722012-08-22 14:21:01 -040075unsigned long
Andrew Vasquez5b914902010-05-28 15:08:30 -070076qla2x00_get_async_timeout(struct scsi_qla_host *vha)
77{
78 unsigned long tmo;
79 struct qla_hw_data *ha = vha->hw;
80
81 /* Firmware should use switch negotiated r_a_tov for timeout. */
82 tmo = ha->r_a_tov / 10 * 2;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -040083 if (IS_QLAFX00(ha)) {
84 tmo = FX00_DEF_RATOV * 2;
85 } else if (!IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez5b914902010-05-28 15:08:30 -070086 /*
87 * Except for earlier ISPs where the timeout is seeded from the
88 * initialization control block.
89 */
90 tmo = ha->login_timeout;
91 }
92 return tmo;
93}
Andrew Vasquezac280b62009-08-20 11:06:05 -070094
Quinn Tran726b8542017-01-19 22:28:00 -080095void
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080096qla2x00_async_iocb_timeout(void *data)
Andrew Vasquezac280b62009-08-20 11:06:05 -070097{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -080098 srb_t *sp = data;
Andrew Vasquezac280b62009-08-20 11:06:05 -070099 fc_port_t *fcport = sp->fcport;
Quinn Tran726b8542017-01-19 22:28:00 -0800100 struct srb_iocb *lio = &sp->u.iocb_cmd;
101 struct event_arg ea;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700102
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700103 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
Quinn Tran726b8542017-01-19 22:28:00 -0800104 "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
105 sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700106
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700107 fcport->flags &= ~FCF_ASYNC_SENT;
Quinn Tran726b8542017-01-19 22:28:00 -0800108
109 switch (sp->type) {
110 case SRB_LOGIN_CMD:
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700111 /* Retry as needed. */
112 lio->u.logio.data[0] = MBS_COMMAND_ERROR;
113 lio->u.logio.data[1] = lio->u.logio.flags & SRB_LOGIN_RETRIED ?
114 QLA_LOGIO_LOGIN_RETRIED : 0;
Quinn Tran726b8542017-01-19 22:28:00 -0800115 memset(&ea, 0, sizeof(ea));
116 ea.event = FCME_PLOGI_DONE;
117 ea.fcport = sp->fcport;
118 ea.data[0] = lio->u.logio.data[0];
119 ea.data[1] = lio->u.logio.data[1];
120 ea.sp = sp;
121 qla24xx_handle_plogi_done_event(fcport->vha, &ea);
122 break;
123 case SRB_LOGOUT_CMD:
Alexei Potashnika6ca8872015-07-14 16:00:44 -0400124 qlt_logo_completion_handler(fcport, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800125 break;
126 case SRB_CT_PTHRU_CMD:
127 case SRB_MB_IOCB:
128 case SRB_NACK_PLOGI:
129 case SRB_NACK_PRLI:
130 case SRB_NACK_LOGO:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800131 sp->done(sp, QLA_FUNCTION_TIMEOUT);
Quinn Tran726b8542017-01-19 22:28:00 -0800132 break;
Andrew Vasquez6ac52602010-05-28 15:08:19 -0700133 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700134}
135
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700136static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800137qla2x00_async_login_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700138{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800139 srb_t *sp = ptr;
140 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800141 struct srb_iocb *lio = &sp->u.iocb_cmd;
Quinn Tran726b8542017-01-19 22:28:00 -0800142 struct event_arg ea;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700143
Quinn Tran83548fe2017-06-02 09:12:01 -0700144 ql_dbg(ql_dbg_disc, vha, 0x20dd,
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800145 "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
Quinn Tran726b8542017-01-19 22:28:00 -0800146
147 sp->fcport->flags &= ~FCF_ASYNC_SENT;
148 if (!test_bit(UNLOADING, &vha->dpc_flags)) {
149 memset(&ea, 0, sizeof(ea));
150 ea.event = FCME_PLOGI_DONE;
151 ea.fcport = sp->fcport;
152 ea.data[0] = lio->u.logio.data[0];
153 ea.data[1] = lio->u.logio.data[1];
154 ea.iop[0] = lio->u.logio.iop[0];
155 ea.iop[1] = lio->u.logio.iop[1];
156 ea.sp = sp;
157 qla2x00_fcport_event_handler(vha, &ea);
158 }
159
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800160 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700161}
162
Andrew Vasquezac280b62009-08-20 11:06:05 -0700163int
164qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
165 uint16_t *data)
166{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700167 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700168 struct srb_iocb *lio;
Quinn Tran726b8542017-01-19 22:28:00 -0800169 int rval = QLA_FUNCTION_FAILED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700170
Quinn Tran726b8542017-01-19 22:28:00 -0800171 if (!vha->flags.online)
172 goto done;
173
174 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
175 (fcport->fw_login_state == DSC_LS_PLOGI_COMP) ||
176 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
177 goto done;
178
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800179 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700180 if (!sp)
181 goto done;
182
Quinn Tran726b8542017-01-19 22:28:00 -0800183 fcport->flags |= FCF_ASYNC_SENT;
184 fcport->logout_completed = 0;
185
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800186 sp->type = SRB_LOGIN_CMD;
187 sp->name = "login";
188 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
189
190 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700191 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800192 sp->done = qla2x00_async_login_sp_done;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700193 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700194 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700195 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700196 rval = qla2x00_start_sp(sp);
Chad Dupuis080c9512016-01-27 12:03:37 -0500197 if (rval != QLA_SUCCESS) {
198 fcport->flags &= ~FCF_ASYNC_SENT;
199 fcport->flags |= FCF_LOGIN_NEEDED;
200 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700201 goto done_free_sp;
Chad Dupuis080c9512016-01-27 12:03:37 -0500202 }
Andrew Vasquezac280b62009-08-20 11:06:05 -0700203
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700204 ql_dbg(ql_dbg_disc, vha, 0x2072,
Quinn Tran726b8542017-01-19 22:28:00 -0800205 "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
206 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800207 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
208 fcport->login_retry);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700209 return rval;
210
211done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800212 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700213done:
Quinn Tran726b8542017-01-19 22:28:00 -0800214 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700215 return rval;
216}
217
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700218static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800219qla2x00_async_logout_sp_done(void *ptr, int res)
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700220{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800221 srb_t *sp = ptr;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800222 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700223
Quinn Tran726b8542017-01-19 22:28:00 -0800224 sp->fcport->flags &= ~FCF_ASYNC_SENT;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800225 if (!test_bit(UNLOADING, &sp->vha->dpc_flags))
226 qla2x00_post_async_logout_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800227 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800228 sp->free(sp);
Andrew Vasquez99b0bec2010-05-04 15:01:25 -0700229}
230
Andrew Vasquezac280b62009-08-20 11:06:05 -0700231int
232qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
233{
Andrew Vasquezac280b62009-08-20 11:06:05 -0700234 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700235 struct srb_iocb *lio;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700236 int rval;
237
238 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800239 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800240 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700241 if (!sp)
242 goto done;
243
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800244 sp->type = SRB_LOGOUT_CMD;
245 sp->name = "logout";
246 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
247
248 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700249 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800250 sp->done = qla2x00_async_logout_sp_done;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700251 rval = qla2x00_start_sp(sp);
252 if (rval != QLA_SUCCESS)
253 goto done_free_sp;
254
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700255 ql_dbg(ql_dbg_disc, vha, 0x2070,
Quinn Tran726b8542017-01-19 22:28:00 -0800256 "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
Chad Dupuiscfb09192011-11-18 09:03:07 -0800257 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
Quinn Tran726b8542017-01-19 22:28:00 -0800258 fcport->d_id.b.area, fcport->d_id.b.al_pa,
259 fcport->port_name);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700260 return rval;
261
262done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800263 sp->free(sp);
Andrew Vasquezac280b62009-08-20 11:06:05 -0700264done:
Quinn Tran726b8542017-01-19 22:28:00 -0800265 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquezac280b62009-08-20 11:06:05 -0700266 return rval;
267}
268
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700269static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800270qla2x00_async_adisc_sp_done(void *ptr, int res)
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700271{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800272 srb_t *sp = ptr;
273 struct scsi_qla_host *vha = sp->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800274 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700275
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800276 if (!test_bit(UNLOADING, &vha->dpc_flags))
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800277 qla2x00_post_async_adisc_done_work(sp->vha, sp->fcport,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800278 lio->u.logio.data);
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800279 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700280}
281
282int
283qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
284 uint16_t *data)
285{
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700286 srb_t *sp;
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700287 struct srb_iocb *lio;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700288 int rval;
289
290 rval = QLA_FUNCTION_FAILED;
Quinn Tran726b8542017-01-19 22:28:00 -0800291 fcport->flags |= FCF_ASYNC_SENT;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800292 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700293 if (!sp)
294 goto done;
295
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800296 sp->type = SRB_ADISC_CMD;
297 sp->name = "adisc";
298 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
299
300 lio = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700301 lio->timeout = qla2x00_async_iocb_timeout;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800302 sp->done = qla2x00_async_adisc_sp_done;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700303 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
Madhuranath Iyengar49163922010-05-04 15:01:28 -0700304 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700305 rval = qla2x00_start_sp(sp);
306 if (rval != QLA_SUCCESS)
307 goto done_free_sp;
308
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700309 ql_dbg(ql_dbg_disc, vha, 0x206f,
Chad Dupuiscfb09192011-11-18 09:03:07 -0800310 "Async-adisc - hdl=%x loopid=%x portid=%02x%02x%02x.\n",
311 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
312 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700313 return rval;
314
315done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800316 sp->free(sp);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700317done:
Quinn Tran726b8542017-01-19 22:28:00 -0800318 fcport->flags &= ~FCF_ASYNC_SENT;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -0700319 return rval;
320}
321
Quinn Tran726b8542017-01-19 22:28:00 -0800322static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
323 struct event_arg *ea)
324{
325 fc_port_t *fcport, *conflict_fcport;
326 struct get_name_list_extended *e;
327 u16 i, n, found = 0, loop_id;
328 port_id_t id;
329 u64 wwn;
330 u8 opt = 0;
331
332 fcport = ea->fcport;
333
334 if (ea->rc) { /* rval */
335 if (fcport->login_retry == 0) {
336 fcport->login_retry = vha->hw->login_retry_count;
Quinn Tran83548fe2017-06-02 09:12:01 -0700337 ql_dbg(ql_dbg_disc, vha, 0x20de,
338 "GNL failed Port login retry %8phN, retry cnt=%d.\n",
339 fcport->port_name, fcport->login_retry);
Quinn Tran726b8542017-01-19 22:28:00 -0800340 }
341 return;
342 }
343
344 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700345 ql_dbg(ql_dbg_disc, vha, 0x20df,
Quinn Tran726b8542017-01-19 22:28:00 -0800346 "%s %8phC rscn gen changed rscn %d|%d \n",
347 __func__, fcport->port_name,
348 fcport->last_rscn_gen, fcport->rscn_gen);
349 qla24xx_post_gidpn_work(vha, fcport);
350 return;
351 } else if (fcport->last_login_gen != fcport->login_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700352 ql_dbg(ql_dbg_disc, vha, 0x20e0,
353 "%s %8phC login gen changed login %d|%d\n",
354 __func__, fcport->port_name,
355 fcport->last_login_gen, fcport->login_gen);
Quinn Tran726b8542017-01-19 22:28:00 -0800356 return;
357 }
358
359 n = ea->data[0] / sizeof(struct get_name_list_extended);
360
Quinn Tran83548fe2017-06-02 09:12:01 -0700361 ql_dbg(ql_dbg_disc, vha, 0x20e1,
Quinn Tran726b8542017-01-19 22:28:00 -0800362 "%s %d %8phC n %d %02x%02x%02x lid %d \n",
363 __func__, __LINE__, fcport->port_name, n,
364 fcport->d_id.b.domain, fcport->d_id.b.area,
365 fcport->d_id.b.al_pa, fcport->loop_id);
366
367 for (i = 0; i < n; i++) {
368 e = &vha->gnl.l[i];
369 wwn = wwn_to_u64(e->port_name);
370
371 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
372 continue;
373
374 found = 1;
375 id.b.domain = e->port_id[2];
376 id.b.area = e->port_id[1];
377 id.b.al_pa = e->port_id[0];
378 id.b.rsvd_1 = 0;
379
380 loop_id = le16_to_cpu(e->nport_handle);
381 loop_id = (loop_id & 0x7fff);
382
Quinn Tran83548fe2017-06-02 09:12:01 -0700383 ql_dbg(ql_dbg_disc, vha, 0x20e2,
384 "%s found %8phC CLS [%d|%d] ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
385 __func__, fcport->port_name,
386 e->current_login_state, fcport->fw_login_state,
387 id.b.domain, id.b.area, id.b.al_pa,
388 fcport->d_id.b.domain, fcport->d_id.b.area,
389 fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -0800390
391 if ((id.b24 != fcport->d_id.b24) ||
392 ((fcport->loop_id != FC_NO_LOOP_ID) &&
393 (fcport->loop_id != loop_id))) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700394 ql_dbg(ql_dbg_disc, vha, 0x20e3,
395 "%s %d %8phC post del sess\n",
396 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800397 qlt_schedule_sess_for_deletion(fcport, 1);
398 return;
399 }
400
401 fcport->loop_id = loop_id;
402
403 wwn = wwn_to_u64(fcport->port_name);
404 qlt_find_sess_invalidate_other(vha, wwn,
405 id, loop_id, &conflict_fcport);
406
407 if (conflict_fcport) {
408 /*
409 * Another share fcport share the same loop_id &
410 * nport id. Conflict fcport needs to finish
411 * cleanup before this fcport can proceed to login.
412 */
413 conflict_fcport->conflict = fcport;
414 fcport->login_pause = 1;
415 }
416
417 switch (e->current_login_state) {
418 case DSC_LS_PRLI_COMP:
Quinn Tran83548fe2017-06-02 09:12:01 -0700419 ql_dbg(ql_dbg_disc, vha, 0x20e4,
420 "%s %d %8phC post gpdb\n",
421 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800422 opt = PDO_FORCE_ADISC;
423 qla24xx_post_gpdb_work(vha, fcport, opt);
424 break;
425
426 case DSC_LS_PORT_UNAVAIL:
427 default:
428 if (fcport->loop_id == FC_NO_LOOP_ID) {
429 qla2x00_find_new_loop_id(vha, fcport);
430 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
431 }
Quinn Tran83548fe2017-06-02 09:12:01 -0700432 ql_dbg(ql_dbg_disc, vha, 0x20e5,
433 "%s %d %8phC\n",
434 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800435 qla24xx_fcport_handle_login(vha, fcport);
436 break;
437 }
438 }
439
440 if (!found) {
441 /* fw has no record of this port */
442 if (fcport->loop_id == FC_NO_LOOP_ID) {
443 qla2x00_find_new_loop_id(vha, fcport);
444 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
445 } else {
446 for (i = 0; i < n; i++) {
447 e = &vha->gnl.l[i];
448 id.b.domain = e->port_id[0];
449 id.b.area = e->port_id[1];
450 id.b.al_pa = e->port_id[2];
451 id.b.rsvd_1 = 0;
452 loop_id = le16_to_cpu(e->nport_handle);
453
454 if (fcport->d_id.b24 == id.b24) {
455 conflict_fcport =
456 qla2x00_find_fcport_by_wwpn(vha,
457 e->port_name, 0);
458
Quinn Tran83548fe2017-06-02 09:12:01 -0700459 ql_dbg(ql_dbg_disc, vha, 0x20e6,
Quinn Tran726b8542017-01-19 22:28:00 -0800460 "%s %d %8phC post del sess\n",
461 __func__, __LINE__,
462 conflict_fcport->port_name);
463 qlt_schedule_sess_for_deletion
464 (conflict_fcport, 1);
465 }
466
467 if (fcport->loop_id == loop_id) {
468 /* FW already picked this loop id for another fcport */
469 qla2x00_find_new_loop_id(vha, fcport);
470 }
471 }
472 }
473 qla24xx_fcport_handle_login(vha, fcport);
474 }
475} /* gnl_event */
476
477static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800478qla24xx_async_gnl_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800479{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800480 struct srb *sp = s;
481 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800482 unsigned long flags;
483 struct fc_port *fcport = NULL, *tf;
484 u16 i, n = 0, loop_id;
485 struct event_arg ea;
486 struct get_name_list_extended *e;
487 u64 wwn;
488 struct list_head h;
489
Quinn Tran83548fe2017-06-02 09:12:01 -0700490 ql_dbg(ql_dbg_disc, vha, 0x20e7,
Quinn Tran726b8542017-01-19 22:28:00 -0800491 "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
492 sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
493 sp->u.iocb_cmd.u.mbx.in_mb[2]);
494
495 memset(&ea, 0, sizeof(ea));
496 ea.sp = sp;
497 ea.rc = res;
498 ea.event = FCME_GNL_DONE;
499
500 if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
501 sizeof(struct get_name_list_extended)) {
502 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
503 sizeof(struct get_name_list_extended);
504 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
505 }
506
507 for (i = 0; i < n; i++) {
508 e = &vha->gnl.l[i];
509 loop_id = le16_to_cpu(e->nport_handle);
510 /* mask out reserve bit */
511 loop_id = (loop_id & 0x7fff);
512 set_bit(loop_id, vha->hw->loop_id_map);
513 wwn = wwn_to_u64(e->port_name);
514
Quinn Tran83548fe2017-06-02 09:12:01 -0700515 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
Quinn Tran726b8542017-01-19 22:28:00 -0800516 "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
517 __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
518 e->port_id[0], e->current_login_state, e->last_login_state,
519 (loop_id & 0x7fff));
520 }
521
522 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
523 vha->gnl.sent = 0;
524
525 INIT_LIST_HEAD(&h);
526 fcport = tf = NULL;
527 if (!list_empty(&vha->gnl.fcports))
528 list_splice_init(&vha->gnl.fcports, &h);
529
530 list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
531 list_del_init(&fcport->gnl_entry);
532 fcport->flags &= ~FCF_ASYNC_SENT;
533 ea.fcport = fcport;
534
535 qla2x00_fcport_event_handler(vha, &ea);
536 }
537
538 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
539
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800540 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800541}
542
543int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
544{
545 srb_t *sp;
546 struct srb_iocb *mbx;
547 int rval = QLA_FUNCTION_FAILED;
548 unsigned long flags;
549 u16 *mb;
550
551 if (!vha->flags.online)
552 goto done;
553
Quinn Tran83548fe2017-06-02 09:12:01 -0700554 ql_dbg(ql_dbg_disc, vha, 0x20d9,
Quinn Tran726b8542017-01-19 22:28:00 -0800555 "Async-gnlist WWPN %8phC \n", fcport->port_name);
556
557 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
558 fcport->flags |= FCF_ASYNC_SENT;
559 fcport->disc_state = DSC_GNL;
560 fcport->last_rscn_gen = fcport->rscn_gen;
561 fcport->last_login_gen = fcport->login_gen;
562
563 list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
564 if (vha->gnl.sent) {
565 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
566 rval = QLA_SUCCESS;
567 goto done;
568 }
569 vha->gnl.sent = 1;
570 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
571
572 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
573 if (!sp)
574 goto done;
575 sp->type = SRB_MB_IOCB;
576 sp->name = "gnlist";
577 sp->gen1 = fcport->rscn_gen;
578 sp->gen2 = fcport->login_gen;
579
580 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
581
582 mb = sp->u.iocb_cmd.u.mbx.out_mb;
583 mb[0] = MBC_PORT_NODE_NAME_LIST;
584 mb[1] = BIT_2 | BIT_3;
585 mb[2] = MSW(vha->gnl.ldma);
586 mb[3] = LSW(vha->gnl.ldma);
587 mb[6] = MSW(MSD(vha->gnl.ldma));
588 mb[7] = LSW(MSD(vha->gnl.ldma));
589 mb[8] = vha->gnl.size;
590 mb[9] = vha->vp_idx;
591
592 mbx = &sp->u.iocb_cmd;
593 mbx->timeout = qla2x00_async_iocb_timeout;
594
595 sp->done = qla24xx_async_gnl_sp_done;
596
597 rval = qla2x00_start_sp(sp);
598 if (rval != QLA_SUCCESS)
599 goto done_free_sp;
600
Quinn Tran83548fe2017-06-02 09:12:01 -0700601 ql_dbg(ql_dbg_disc, vha, 0x20da,
602 "Async-%s - OUT WWPN %8phC hndl %x\n",
603 sp->name, fcport->port_name, sp->handle);
Quinn Tran726b8542017-01-19 22:28:00 -0800604
605 return rval;
606
607done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800608 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800609done:
610 fcport->flags &= ~FCF_ASYNC_SENT;
611 return rval;
612}
613
614int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
615{
616 struct qla_work_evt *e;
617
618 e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
619 if (!e)
620 return QLA_FUNCTION_FAILED;
621
622 e->u.fcport.fcport = fcport;
623 return qla2x00_post_work(vha, e);
624}
625
626static
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800627void qla24xx_async_gpdb_sp_done(void *s, int res)
Quinn Tran726b8542017-01-19 22:28:00 -0800628{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800629 struct srb *sp = s;
630 struct scsi_qla_host *vha = sp->vha;
Quinn Tran726b8542017-01-19 22:28:00 -0800631 struct qla_hw_data *ha = vha->hw;
Quinn Tran726b8542017-01-19 22:28:00 -0800632 struct port_database_24xx *pd;
633 fc_port_t *fcport = sp->fcport;
634 u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
635 int rval = QLA_SUCCESS;
636 struct event_arg ea;
637
Quinn Tran83548fe2017-06-02 09:12:01 -0700638 ql_dbg(ql_dbg_disc, vha, 0x20db,
Quinn Tran726b8542017-01-19 22:28:00 -0800639 "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
640 sp->name, res, fcport->port_name, mb[1], mb[2]);
641
642 fcport->flags &= ~FCF_ASYNC_SENT;
643
644 if (res) {
645 rval = res;
646 goto gpd_error_out;
647 }
648
649 pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
650
Quinn Tran15f30a52017-03-15 09:48:52 -0700651 rval = __qla24xx_parse_gpdb(vha, fcport, pd);
Quinn Tran726b8542017-01-19 22:28:00 -0800652
653gpd_error_out:
654 memset(&ea, 0, sizeof(ea));
655 ea.event = FCME_GPDB_DONE;
656 ea.rc = rval;
657 ea.fcport = fcport;
658 ea.sp = sp;
659
660 qla2x00_fcport_event_handler(vha, &ea);
661
662 dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
663 sp->u.iocb_cmd.u.mbx.in_dma);
664
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800665 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800666}
667
668static int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport,
669 u8 opt)
670{
671 struct qla_work_evt *e;
672
673 e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
674 if (!e)
675 return QLA_FUNCTION_FAILED;
676
677 e->u.fcport.fcport = fcport;
678 e->u.fcport.opt = opt;
679 return qla2x00_post_work(vha, e);
680}
681
682int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
683{
684 srb_t *sp;
685 struct srb_iocb *mbx;
686 int rval = QLA_FUNCTION_FAILED;
687 u16 *mb;
688 dma_addr_t pd_dma;
689 struct port_database_24xx *pd;
690 struct qla_hw_data *ha = vha->hw;
691
692 if (!vha->flags.online)
693 goto done;
694
695 fcport->flags |= FCF_ASYNC_SENT;
696 fcport->disc_state = DSC_GPDB;
697
698 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
699 if (!sp)
700 goto done;
701
702 pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
703 if (pd == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700704 ql_log(ql_log_warn, vha, 0xd043,
705 "Failed to allocate port database structure.\n");
Quinn Tran726b8542017-01-19 22:28:00 -0800706 goto done_free_sp;
707 }
708 memset(pd, 0, max(PORT_DATABASE_SIZE, PORT_DATABASE_24XX_SIZE));
709
710 sp->type = SRB_MB_IOCB;
711 sp->name = "gpdb";
712 sp->gen1 = fcport->rscn_gen;
713 sp->gen2 = fcport->login_gen;
714 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
715
716 mb = sp->u.iocb_cmd.u.mbx.out_mb;
717 mb[0] = MBC_GET_PORT_DATABASE;
718 mb[1] = fcport->loop_id;
719 mb[2] = MSW(pd_dma);
720 mb[3] = LSW(pd_dma);
721 mb[6] = MSW(MSD(pd_dma));
722 mb[7] = LSW(MSD(pd_dma));
723 mb[9] = vha->vp_idx;
724 mb[10] = opt;
725
726 mbx = &sp->u.iocb_cmd;
727 mbx->timeout = qla2x00_async_iocb_timeout;
728 mbx->u.mbx.in = (void *)pd;
729 mbx->u.mbx.in_dma = pd_dma;
730
731 sp->done = qla24xx_async_gpdb_sp_done;
732
733 rval = qla2x00_start_sp(sp);
734 if (rval != QLA_SUCCESS)
735 goto done_free_sp;
736
Quinn Tran83548fe2017-06-02 09:12:01 -0700737 ql_dbg(ql_dbg_disc, vha, 0x20dc,
738 "Async-%s %8phC hndl %x opt %x\n",
739 sp->name, fcport->port_name, sp->handle, opt);
Quinn Tran726b8542017-01-19 22:28:00 -0800740
741 return rval;
742
743done_free_sp:
744 if (pd)
745 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
746
Joe Carnuccio25ff6af2017-01-19 22:28:04 -0800747 sp->free(sp);
Quinn Tran726b8542017-01-19 22:28:00 -0800748done:
749 fcport->flags &= ~FCF_ASYNC_SENT;
750 qla24xx_post_gpdb_work(vha, fcport, opt);
751 return rval;
752}
753
754static
755void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
756{
757 int rval = ea->rc;
758 fc_port_t *fcport = ea->fcport;
759 unsigned long flags;
760
761 fcport->flags &= ~FCF_ASYNC_SENT;
762
Quinn Tran83548fe2017-06-02 09:12:01 -0700763 ql_dbg(ql_dbg_disc, vha, 0x20d2,
Quinn Tran726b8542017-01-19 22:28:00 -0800764 "%s %8phC DS %d LS %d rval %d\n", __func__, fcport->port_name,
765 fcport->disc_state, fcport->fw_login_state, rval);
766
767 if (ea->sp->gen2 != fcport->login_gen) {
768 /* target side must have changed it. */
Quinn Tran83548fe2017-06-02 09:12:01 -0700769 ql_dbg(ql_dbg_disc, vha, 0x20d3,
Quinn Tran726b8542017-01-19 22:28:00 -0800770 "%s %8phC generation changed rscn %d|%d login %d|%d \n",
771 __func__, fcport->port_name, fcport->last_rscn_gen,
772 fcport->rscn_gen, fcport->last_login_gen,
773 fcport->login_gen);
774 return;
775 } else if (ea->sp->gen1 != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700776 ql_dbg(ql_dbg_disc, vha, 0x20d4, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -0800777 __func__, __LINE__, fcport->port_name);
778 qla24xx_post_gidpn_work(vha, fcport);
779 return;
780 }
781
782 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700783 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
Quinn Tran726b8542017-01-19 22:28:00 -0800784 __func__, __LINE__, fcport->port_name);
785 qlt_schedule_sess_for_deletion_lock(fcport);
786 return;
787 }
788
789 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
790 ea->fcport->login_gen++;
791 ea->fcport->deleted = 0;
792 ea->fcport->logout_on_delete = 1;
793
794 if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
795 vha->fcport_count++;
796 ea->fcport->login_succ = 1;
797
798 if (!IS_IIDMA_CAPABLE(vha->hw) ||
799 !vha->hw->flags.gpsc_supported) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700800 ql_dbg(ql_dbg_disc, vha, 0x20d6,
Quinn Tran726b8542017-01-19 22:28:00 -0800801 "%s %d %8phC post upd_fcport fcp_cnt %d\n",
802 __func__, __LINE__, fcport->port_name,
803 vha->fcport_count);
804
805 qla24xx_post_upd_fcport_work(vha, fcport);
806 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700807 ql_dbg(ql_dbg_disc, vha, 0x20d7,
Quinn Tran726b8542017-01-19 22:28:00 -0800808 "%s %d %8phC post gpsc fcp_cnt %d\n",
809 __func__, __LINE__, fcport->port_name,
810 vha->fcport_count);
811
812 qla24xx_post_gpsc_work(vha, fcport);
813 }
814 }
815 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
816} /* gpdb event */
817
818int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
819{
820 if (fcport->login_retry == 0)
821 return 0;
822
823 if (fcport->scan_state != QLA_FCPORT_FOUND)
824 return 0;
825
Quinn Tran83548fe2017-06-02 09:12:01 -0700826 ql_dbg(ql_dbg_disc, vha, 0x20d8,
Quinn Tran726b8542017-01-19 22:28:00 -0800827 "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d|%d retry %d lid %d\n",
828 __func__, fcport->port_name, fcport->disc_state,
829 fcport->fw_login_state, fcport->login_pause, fcport->flags,
830 fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
831 fcport->last_login_gen, fcport->login_gen, fcport->login_retry,
832 fcport->loop_id);
833
834 fcport->login_retry--;
835
836 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -0800837 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
838 return 0;
839
Quinn Tran5b334692017-03-15 09:48:48 -0700840 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
841 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline))
842 return 0;
843 }
844
Quinn Tran726b8542017-01-19 22:28:00 -0800845 /* for pure Target Mode. Login will not be initiated */
846 if (vha->host->active_mode == MODE_TARGET)
847 return 0;
848
849 if (fcport->flags & FCF_ASYNC_SENT) {
850 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
851 return 0;
852 }
853
854 switch (fcport->disc_state) {
855 case DSC_DELETED:
856 if (fcport->loop_id == FC_NO_LOOP_ID) {
Quinn Tran83548fe2017-06-02 09:12:01 -0700857 ql_dbg(ql_dbg_disc, vha, 0x20bd,
858 "%s %d %8phC post gnl\n",
859 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800860 qla24xx_async_gnl(vha, fcport);
861 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700862 ql_dbg(ql_dbg_disc, vha, 0x20bf,
863 "%s %d %8phC post login\n",
864 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800865 fcport->disc_state = DSC_LOGIN_PEND;
866 qla2x00_post_async_login_work(vha, fcport, NULL);
867 }
868 break;
869
870 case DSC_GNL:
871 if (fcport->login_pause) {
872 fcport->last_rscn_gen = fcport->rscn_gen;
873 fcport->last_login_gen = fcport->login_gen;
874 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
875 break;
876 }
877
878 if (fcport->flags & FCF_FCP2_DEVICE) {
879 u8 opt = PDO_FORCE_ADISC;
880
Quinn Tran83548fe2017-06-02 09:12:01 -0700881 ql_dbg(ql_dbg_disc, vha, 0x20c9,
882 "%s %d %8phC post gpdb\n",
883 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800884
885 fcport->disc_state = DSC_GPDB;
886 qla24xx_post_gpdb_work(vha, fcport, opt);
887 } else {
Quinn Tran83548fe2017-06-02 09:12:01 -0700888 ql_dbg(ql_dbg_disc, vha, 0x20cf,
889 "%s %d %8phC post login\n",
890 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800891 fcport->disc_state = DSC_LOGIN_PEND;
892 qla2x00_post_async_login_work(vha, fcport, NULL);
893 }
894
895 break;
896
897 case DSC_LOGIN_FAILED:
Quinn Tran83548fe2017-06-02 09:12:01 -0700898 ql_dbg(ql_dbg_disc, vha, 0x20d0,
899 "%s %d %8phC post gidpn\n",
900 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800901
902 qla24xx_post_gidpn_work(vha, fcport);
903 break;
904
905 case DSC_LOGIN_COMPLETE:
906 /* recheck login state */
Quinn Tran83548fe2017-06-02 09:12:01 -0700907 ql_dbg(ql_dbg_disc, vha, 0x20d1,
908 "%s %d %8phC post gpdb\n",
909 __func__, __LINE__, fcport->port_name);
Quinn Tran726b8542017-01-19 22:28:00 -0800910
911 qla24xx_post_gpdb_work(vha, fcport, PDO_FORCE_ADISC);
912 break;
913
914 default:
915 break;
916 }
917
918 return 0;
919}
920
921static
922void qla24xx_handle_rscn_event(fc_port_t *fcport, struct event_arg *ea)
923{
924 fcport->rscn_gen++;
925
Quinn Tran83548fe2017-06-02 09:12:01 -0700926 ql_dbg(ql_dbg_disc, fcport->vha, 0x210c,
927 "%s %8phC DS %d LS %d\n",
928 __func__, fcport->port_name, fcport->disc_state,
929 fcport->fw_login_state);
Quinn Tran726b8542017-01-19 22:28:00 -0800930
931 if (fcport->flags & FCF_ASYNC_SENT)
932 return;
933
934 switch (fcport->disc_state) {
935 case DSC_DELETED:
936 case DSC_LOGIN_COMPLETE:
937 qla24xx_post_gidpn_work(fcport->vha, fcport);
938 break;
939
940 default:
941 break;
942 }
943}
944
945int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
946 u8 *port_name, void *pla)
947{
948 struct qla_work_evt *e;
949 e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
950 if (!e)
951 return QLA_FUNCTION_FAILED;
952
953 e->u.new_sess.id = *id;
954 e->u.new_sess.pla = pla;
955 memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
956
957 return qla2x00_post_work(vha, e);
958}
959
960static
961int qla24xx_handle_delete_done_event(scsi_qla_host_t *vha,
962 struct event_arg *ea)
963{
964 fc_port_t *fcport = ea->fcport;
965
966 if (test_bit(UNLOADING, &vha->dpc_flags))
967 return 0;
968
969 switch (vha->host->active_mode) {
970 case MODE_INITIATOR:
971 case MODE_DUAL:
972 if (fcport->scan_state == QLA_FCPORT_FOUND)
973 qla24xx_fcport_handle_login(vha, fcport);
974 break;
975
976 case MODE_TARGET:
977 default:
978 /* no-op */
979 break;
980 }
981
982 return 0;
983}
984
985static
986void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
987 struct event_arg *ea)
988{
989 fc_port_t *fcport = ea->fcport;
990
991 if (fcport->scan_state != QLA_FCPORT_FOUND) {
992 fcport->login_retry++;
993 return;
994 }
995
Quinn Tran83548fe2017-06-02 09:12:01 -0700996 ql_dbg(ql_dbg_disc, vha, 0x2102,
997 "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
998 __func__, fcport->port_name, fcport->disc_state,
999 fcport->fw_login_state, fcport->login_pause,
1000 fcport->deleted, fcport->conflict,
1001 fcport->last_rscn_gen, fcport->rscn_gen,
1002 fcport->last_login_gen, fcport->login_gen,
1003 fcport->flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001004
1005 if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
Quinn Tran726b8542017-01-19 22:28:00 -08001006 (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1007 return;
1008
Quinn Tran5b334692017-03-15 09:48:48 -07001009 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1010 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline))
1011 return;
1012 }
1013
Quinn Tran726b8542017-01-19 22:28:00 -08001014 if (fcport->flags & FCF_ASYNC_SENT) {
1015 fcport->login_retry++;
1016 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1017 return;
1018 }
1019
1020 if (fcport->disc_state == DSC_DELETE_PEND) {
1021 fcport->login_retry++;
1022 return;
1023 }
1024
1025 if (fcport->last_rscn_gen != fcport->rscn_gen) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001026 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001027 __func__, __LINE__, fcport->port_name);
1028
1029 qla24xx_async_gidpn(vha, fcport);
1030 return;
1031 }
1032
1033 qla24xx_fcport_handle_login(vha, fcport);
1034}
1035
Quinn Tran41dc5292017-01-19 22:28:03 -08001036void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
Quinn Tran726b8542017-01-19 22:28:00 -08001037{
Quinn Tran41dc5292017-01-19 22:28:03 -08001038 fc_port_t *fcport, *f, *tf;
1039 uint32_t id = 0, mask, rid;
Quinn Tran726b8542017-01-19 22:28:00 -08001040 int rc;
1041
1042 switch (ea->event) {
1043 case FCME_RELOGIN:
Quinn Tranb98ae0d2017-06-02 09:12:00 -07001044 case FCME_RSCN:
1045 case FCME_GIDPN_DONE:
1046 case FCME_GPSC_DONE:
1047 case FCME_GPNID_DONE:
1048 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
1049 test_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))
1050 return;
1051 break;
1052 default:
1053 break;
1054 }
1055
1056 switch (ea->event) {
1057 case FCME_RELOGIN:
Quinn Tran726b8542017-01-19 22:28:00 -08001058 if (test_bit(UNLOADING, &vha->dpc_flags))
1059 return;
1060
1061 qla24xx_handle_relogin_event(vha, ea);
1062 break;
1063 case FCME_RSCN:
1064 if (test_bit(UNLOADING, &vha->dpc_flags))
1065 return;
Quinn Tran41dc5292017-01-19 22:28:03 -08001066 switch (ea->id.b.rsvd_1) {
1067 case RSCN_PORT_ADDR:
1068 fcport = qla2x00_find_fcport_by_nportid(vha, &ea->id, 1);
1069 if (!fcport) {
1070 /* cable moved */
1071 rc = qla24xx_post_gpnid_work(vha, &ea->id);
1072 if (rc) {
Quinn Tran83548fe2017-06-02 09:12:01 -07001073 ql_log(ql_log_warn, vha, 0xd044,
1074 "RSCN GPNID work failed %02x%02x%02x\n",
1075 ea->id.b.domain, ea->id.b.area,
1076 ea->id.b.al_pa);
Quinn Tran41dc5292017-01-19 22:28:03 -08001077 }
1078 } else {
1079 ea->fcport = fcport;
1080 qla24xx_handle_rscn_event(fcport, ea);
Quinn Tran726b8542017-01-19 22:28:00 -08001081 }
Quinn Tran41dc5292017-01-19 22:28:03 -08001082 break;
1083 case RSCN_AREA_ADDR:
1084 case RSCN_DOM_ADDR:
1085 if (ea->id.b.rsvd_1 == RSCN_AREA_ADDR) {
1086 mask = 0xffff00;
Quinn Tran83548fe2017-06-02 09:12:01 -07001087 ql_dbg(ql_dbg_async, vha, 0x5044,
1088 "RSCN: Area 0x%06x was affected\n",
1089 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001090 } else {
1091 mask = 0xff0000;
Quinn Tran83548fe2017-06-02 09:12:01 -07001092 ql_dbg(ql_dbg_async, vha, 0x507a,
1093 "RSCN: Domain 0x%06x was affected\n",
1094 ea->id.b24);
Quinn Tran41dc5292017-01-19 22:28:03 -08001095 }
1096
1097 rid = ea->id.b24 & mask;
1098 list_for_each_entry_safe(f, tf, &vha->vp_fcports,
1099 list) {
1100 id = f->d_id.b24 & mask;
1101 if (rid == id) {
1102 ea->fcport = f;
1103 qla24xx_handle_rscn_event(f, ea);
1104 }
1105 }
1106 break;
1107 case RSCN_FAB_ADDR:
1108 default:
Quinn Tran83548fe2017-06-02 09:12:01 -07001109 ql_log(ql_log_warn, vha, 0xd045,
1110 "RSCN: Fabric was affected. Addr format %d\n",
1111 ea->id.b.rsvd_1);
Quinn Tran41dc5292017-01-19 22:28:03 -08001112 qla2x00_mark_all_devices_lost(vha, 1);
1113 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1114 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Quinn Tran726b8542017-01-19 22:28:00 -08001115 }
1116 break;
1117 case FCME_GIDPN_DONE:
1118 qla24xx_handle_gidpn_event(vha, ea);
1119 break;
1120 case FCME_GNL_DONE:
1121 qla24xx_handle_gnl_done_event(vha, ea);
1122 break;
1123 case FCME_GPSC_DONE:
1124 qla24xx_post_upd_fcport_work(vha, ea->fcport);
1125 break;
1126 case FCME_PLOGI_DONE: /* Initiator side sent LLIOCB */
1127 qla24xx_handle_plogi_done_event(vha, ea);
1128 break;
1129 case FCME_GPDB_DONE:
1130 qla24xx_handle_gpdb_event(vha, ea);
1131 break;
1132 case FCME_GPNID_DONE:
1133 qla24xx_handle_gpnid_event(vha, ea);
1134 break;
1135 case FCME_DELETE_DONE:
1136 qla24xx_handle_delete_done_event(vha, ea);
1137 break;
1138 default:
1139 BUG_ON(1);
1140 break;
1141 }
1142}
1143
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001144static void
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001145qla2x00_tmf_iocb_timeout(void *data)
1146{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001147 srb_t *sp = data;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001148 struct srb_iocb *tmf = &sp->u.iocb_cmd;
1149
1150 tmf->u.tmf.comp_status = CS_TIMEOUT;
1151 complete(&tmf->u.tmf.comp);
1152}
1153
1154static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001155qla2x00_tmf_sp_done(void *ptr, int res)
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001156{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001157 srb_t *sp = ptr;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001158 struct srb_iocb *tmf = &sp->u.iocb_cmd;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001159
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001160 complete(&tmf->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001161}
1162
1163int
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001164qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001165 uint32_t tag)
1166{
1167 struct scsi_qla_host *vha = fcport->vha;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001168 struct srb_iocb *tm_iocb;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001169 srb_t *sp;
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001170 int rval = QLA_FUNCTION_FAILED;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001171
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001172 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001173 if (!sp)
1174 goto done;
1175
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001176 tm_iocb = &sp->u.iocb_cmd;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001177 sp->type = SRB_TM_CMD;
1178 sp->name = "tmf";
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001179 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1180 tm_iocb->u.tmf.flags = flags;
1181 tm_iocb->u.tmf.lun = lun;
1182 tm_iocb->u.tmf.data = tag;
1183 sp->done = qla2x00_tmf_sp_done;
1184 tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1185 init_completion(&tm_iocb->u.tmf.comp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001186
1187 rval = qla2x00_start_sp(sp);
1188 if (rval != QLA_SUCCESS)
1189 goto done_free_sp;
1190
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001191 ql_dbg(ql_dbg_taskm, vha, 0x802f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001192 "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1193 sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1194 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Armen Baloyanfaef62d2014-02-26 04:15:17 -05001195
1196 wait_for_completion(&tm_iocb->u.tmf.comp);
1197
1198 rval = tm_iocb->u.tmf.comp_status == CS_COMPLETE ?
1199 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1200
1201 if ((rval != QLA_SUCCESS) || tm_iocb->u.tmf.data) {
1202 ql_dbg(ql_dbg_taskm, vha, 0x8030,
1203 "TM IOCB failed (%x).\n", rval);
1204 }
1205
1206 if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1207 flags = tm_iocb->u.tmf.flags;
1208 lun = (uint16_t)tm_iocb->u.tmf.lun;
1209
1210 /* Issue Marker IOCB */
1211 qla2x00_marker(vha, vha->hw->req_q_map[0],
1212 vha->hw->rsp_q_map[0], sp->fcport->loop_id, lun,
1213 flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1214 }
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001215
1216done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001217 sp->free(sp);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001218done:
1219 return rval;
1220}
1221
Armen Baloyan4440e462014-02-26 04:15:18 -05001222static void
1223qla24xx_abort_iocb_timeout(void *data)
1224{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001225 srb_t *sp = data;
Armen Baloyan4440e462014-02-26 04:15:18 -05001226 struct srb_iocb *abt = &sp->u.iocb_cmd;
1227
1228 abt->u.abt.comp_status = CS_TIMEOUT;
1229 complete(&abt->u.abt.comp);
1230}
1231
1232static void
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001233qla24xx_abort_sp_done(void *ptr, int res)
Armen Baloyan4440e462014-02-26 04:15:18 -05001234{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001235 srb_t *sp = ptr;
Armen Baloyan4440e462014-02-26 04:15:18 -05001236 struct srb_iocb *abt = &sp->u.iocb_cmd;
1237
1238 complete(&abt->u.abt.comp);
1239}
1240
Quinn Tran15f30a52017-03-15 09:48:52 -07001241int
Armen Baloyan4440e462014-02-26 04:15:18 -05001242qla24xx_async_abort_cmd(srb_t *cmd_sp)
1243{
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001244 scsi_qla_host_t *vha = cmd_sp->vha;
Armen Baloyan4440e462014-02-26 04:15:18 -05001245 fc_port_t *fcport = cmd_sp->fcport;
1246 struct srb_iocb *abt_iocb;
1247 srb_t *sp;
1248 int rval = QLA_FUNCTION_FAILED;
1249
1250 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1251 if (!sp)
1252 goto done;
1253
1254 abt_iocb = &sp->u.iocb_cmd;
1255 sp->type = SRB_ABT_CMD;
1256 sp->name = "abort";
1257 qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1258 abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1259 sp->done = qla24xx_abort_sp_done;
1260 abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1261 init_completion(&abt_iocb->u.abt.comp);
1262
1263 rval = qla2x00_start_sp(sp);
1264 if (rval != QLA_SUCCESS)
1265 goto done_free_sp;
1266
1267 ql_dbg(ql_dbg_async, vha, 0x507c,
1268 "Abort command issued - hdl=%x, target_id=%x\n",
1269 cmd_sp->handle, fcport->tgt_id);
1270
1271 wait_for_completion(&abt_iocb->u.abt.comp);
1272
1273 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1274 QLA_SUCCESS : QLA_FUNCTION_FAILED;
1275
1276done_free_sp:
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08001277 sp->free(sp);
Armen Baloyan4440e462014-02-26 04:15:18 -05001278done:
1279 return rval;
1280}
1281
1282int
1283qla24xx_async_abort_command(srb_t *sp)
1284{
1285 unsigned long flags = 0;
1286
1287 uint32_t handle;
1288 fc_port_t *fcport = sp->fcport;
1289 struct scsi_qla_host *vha = fcport->vha;
1290 struct qla_hw_data *ha = vha->hw;
1291 struct req_que *req = vha->req;
1292
1293 spin_lock_irqsave(&ha->hardware_lock, flags);
1294 for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1295 if (req->outstanding_cmds[handle] == sp)
1296 break;
1297 }
1298 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1299 if (handle == req->num_outstanding_cmds) {
1300 /* Command not found. */
1301 return QLA_FUNCTION_FAILED;
1302 }
1303 if (sp->type == SRB_FXIOCB_DCMD)
1304 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1305 FXDISC_ABORT_IOCTL);
1306
1307 return qla24xx_async_abort_cmd(sp);
1308}
1309
Quinn Tran726b8542017-01-19 22:28:00 -08001310static void
1311qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001312{
Quinn Tran726b8542017-01-19 22:28:00 -08001313 port_id_t cid; /* conflict Nport id */
Andrew Vasquezac280b62009-08-20 11:06:05 -07001314
Quinn Tran726b8542017-01-19 22:28:00 -08001315 switch (ea->data[0]) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001316 case MBS_COMMAND_COMPLETE:
Andrew Vasqueza4f92a32011-03-30 11:46:31 -07001317 /*
1318 * Driver must validate login state - If PRLI not complete,
1319 * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1320 * requests.
1321 */
Quinn Tran83548fe2017-06-02 09:12:01 -07001322 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1323 "%s %d %8phC post gpdb\n",
1324 __func__, __LINE__, ea->fcport->port_name);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07001325 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
Quinn Tran726b8542017-01-19 22:28:00 -08001326 ea->fcport->logout_on_delete = 1;
1327 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001328 break;
1329 case MBS_COMMAND_ERROR:
Quinn Tran83548fe2017-06-02 09:12:01 -07001330 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
Quinn Tran726b8542017-01-19 22:28:00 -08001331 __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1332
1333 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1334 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1335 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001336 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1337 else
Quinn Tran726b8542017-01-19 22:28:00 -08001338 qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001339 break;
1340 case MBS_LOOP_ID_USED:
Quinn Tran726b8542017-01-19 22:28:00 -08001341 /* data[1] = IO PARAM 1 = nport ID */
1342 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1343 cid.b.area = (ea->iop[1] >> 8) & 0xff;
1344 cid.b.al_pa = ea->iop[1] & 0xff;
1345 cid.b.rsvd_1 = 0;
1346
Quinn Tran83548fe2017-06-02 09:12:01 -07001347 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1348 "%s %d %8phC LoopID 0x%x in use post gnl\n",
1349 __func__, __LINE__, ea->fcport->port_name,
1350 ea->fcport->loop_id);
Quinn Tran726b8542017-01-19 22:28:00 -08001351
1352 if (IS_SW_RESV_ADDR(cid)) {
1353 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1354 ea->fcport->loop_id = FC_NO_LOOP_ID;
1355 } else {
1356 qla2x00_clear_loop_id(ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001357 }
Quinn Tran726b8542017-01-19 22:28:00 -08001358 qla24xx_post_gnl_work(vha, ea->fcport);
1359 break;
1360 case MBS_PORT_ID_USED:
Quinn Tran83548fe2017-06-02 09:12:01 -07001361 ql_dbg(ql_dbg_disc, vha, 0x20ed,
1362 "%s %d %8phC NPortId %02x%02x%02x inuse post gidpn\n",
1363 __func__, __LINE__, ea->fcport->port_name,
1364 ea->fcport->d_id.b.domain, ea->fcport->d_id.b.area,
1365 ea->fcport->d_id.b.al_pa);
Quinn Tran726b8542017-01-19 22:28:00 -08001366
1367 qla2x00_clear_loop_id(ea->fcport);
1368 qla24xx_post_gidpn_work(vha, ea->fcport);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001369 break;
1370 }
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001371 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001372}
1373
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001374void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001375qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1376 uint16_t *data)
1377{
Quinn Tran726b8542017-01-19 22:28:00 -08001378 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Alexei Potashnika6ca8872015-07-14 16:00:44 -04001379 qlt_logo_completion_handler(fcport, data[0]);
Quinn Tran726b8542017-01-19 22:28:00 -08001380 fcport->login_gen++;
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001381 return;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001382}
1383
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001384void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001385qla2x00_async_adisc_done(struct scsi_qla_host *vha, fc_port_t *fcport,
1386 uint16_t *data)
1387{
1388 if (data[0] == MBS_COMMAND_COMPLETE) {
1389 qla2x00_update_fcport(vha, fcport);
1390
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001391 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001392 }
1393
1394 /* Retry login. */
1395 fcport->flags &= ~FCF_ASYNC_SENT;
1396 if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
1397 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1398 else
Andrew Vasquez80d79442011-03-30 11:46:17 -07001399 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001400
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001401 return;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001402}
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404/****************************************************************************/
1405/* QLogic ISP2x00 Hardware Support Functions. */
1406/****************************************************************************/
1407
Saurav Kashyapfa492632012-11-21 02:40:29 -05001408static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001409qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
1410{
1411 int rval = QLA_SUCCESS;
1412 struct qla_hw_data *ha = vha->hw;
1413 uint32_t idc_major_ver, idc_minor_ver;
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001414 uint16_t config[4];
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001415
1416 qla83xx_idc_lock(vha, 0);
1417
1418 /* SV: TODO: Assign initialization timeout from
1419 * flash-info / other param
1420 */
1421 ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
1422 ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
1423
1424 /* Set our fcoe function presence */
1425 if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
1426 ql_dbg(ql_dbg_p3p, vha, 0xb077,
1427 "Error while setting DRV-Presence.\n");
1428 rval = QLA_FUNCTION_FAILED;
1429 goto exit;
1430 }
1431
1432 /* Decide the reset ownership */
1433 qla83xx_reset_ownership(vha);
1434
1435 /*
1436 * On first protocol driver load:
1437 * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
1438 * register.
1439 * Others: Check compatibility with current IDC Major version.
1440 */
1441 qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
1442 if (ha->flags.nic_core_reset_owner) {
1443 /* Set IDC Major version */
1444 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
1445 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
1446
1447 /* Clearing IDC-Lock-Recovery register */
1448 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
1449 } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
1450 /*
1451 * Clear further IDC participation if we are not compatible with
1452 * the current IDC Major Version.
1453 */
1454 ql_log(ql_log_warn, vha, 0xb07d,
1455 "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
1456 idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
1457 __qla83xx_clear_drv_presence(vha);
1458 rval = QLA_FUNCTION_FAILED;
1459 goto exit;
1460 }
1461 /* Each function sets its supported Minor version. */
1462 qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
1463 idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
1464 qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
1465
Saurav Kashyap711aa7f2012-08-22 14:21:15 -04001466 if (ha->flags.nic_core_reset_owner) {
1467 memset(config, 0, sizeof(config));
1468 if (!qla81xx_get_port_config(vha, config))
1469 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
1470 QLA8XXX_DEV_READY);
1471 }
1472
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001473 rval = qla83xx_idc_state_handler(vha);
1474
1475exit:
1476 qla83xx_idc_unlock(vha, 0);
1477
1478 return rval;
1479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481/*
1482* qla2x00_initialize_adapter
1483* Initialize board.
1484*
1485* Input:
1486* ha = adapter block pointer.
1487*
1488* Returns:
1489* 0 = success
1490*/
1491int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001492qla2x00_initialize_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001495 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001496 struct req_que *req = ha->req_q_map[0];
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001497
Joe Carnucciofc90ada2016-07-06 11:14:23 -04001498 memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
1499 memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* Clear adapter flags. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001502 vha->flags.online = 0;
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001503 ha->flags.chip_reset_done = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001504 vha->flags.reset_active = 0;
Andrew Vasquez85880802009-12-15 21:29:46 -08001505 ha->flags.pci_channel_io_perm_failure = 0;
1506 ha->flags.eeh_busy = 0;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04001507 vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001508 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1509 atomic_set(&vha->loop_state, LOOP_DOWN);
1510 vha->device_flags = DFLG_NO_CABLE;
1511 vha->dpc_flags = 0;
1512 vha->flags.management_server_logged_in = 0;
1513 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 ha->isp_abort_cnt = 0;
1515 ha->beacon_blink_led = 0;
1516
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001517 set_bit(0, ha->req_qid_map);
1518 set_bit(0, ha->rsp_qid_map);
1519
Chad Dupuiscfb09192011-11-18 09:03:07 -08001520 ql_dbg(ql_dbg_init, vha, 0x0040,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001521 "Configuring PCI space...\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001522 rval = ha->isp_ops->pci_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001524 ql_log(ql_log_warn, vha, 0x0044,
1525 "Unable to configure PCI space.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return (rval);
1527 }
1528
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001529 ha->isp_ops->reset_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001531 rval = qla2xxx_get_flash_info(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001532 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001533 ql_log(ql_log_fatal, vha, 0x004f,
1534 "Unable to validate FLASH data.\n");
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001535 return rval;
1536 }
1537
1538 if (IS_QLA8044(ha)) {
1539 qla8044_read_reset_template(vha);
1540
1541 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
1542 * If DONRESET_BIT0 is set, drivers should not set dev_state
1543 * to NEED_RESET. But if NEED_RESET is set, drivers should
1544 * should honor the reset. */
1545 if (ql2xdontresethba == 1)
1546 qla8044_set_idc_dontreset(vha);
Andrew Vasquezc00d8992008-09-11 21:22:49 -07001547 }
1548
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001549 ha->isp_ops->get_flash_version(vha, req->ring);
Chad Dupuiscfb09192011-11-18 09:03:07 -08001550 ql_dbg(ql_dbg_init, vha, 0x0061,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001551 "Configure NVRAM parameters...\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001552
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001553 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001555 if (ha->flags.disable_serdes) {
1556 /* Mask HBA via NVRAM settings? */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001557 ql_log(ql_log_info, vha, 0x0077,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001558 "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07001559 return QLA_FUNCTION_FAILED;
1560 }
1561
Chad Dupuiscfb09192011-11-18 09:03:07 -08001562 ql_dbg(ql_dbg_init, vha, 0x0078,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001563 "Verifying loaded RISC code...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001565 if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
1566 rval = ha->isp_ops->chip_diag(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001567 if (rval)
1568 return (rval);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001569 rval = qla2x00_setup_chip(vha);
Andrew Vasquezd19044c2006-11-22 08:22:19 -08001570 if (rval)
1571 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07001573
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001574 if (IS_QLA84XX(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001575 ha->cs84xx = qla84xx_get_chip(vha);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001576 if (!ha->cs84xx) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001577 ql_log(ql_log_warn, vha, 0x00d0,
Harihara Kadayam4d4df192008-04-03 13:13:26 -07001578 "Unable to configure ISP84XX.\n");
1579 return QLA_FUNCTION_FAILED;
1580 }
1581 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001582
Quinn Tranead03852017-01-19 22:28:01 -08001583 if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04001584 rval = qla2x00_init_rings(vha);
1585
Lalit Chandivade2533cf62009-03-24 09:08:07 -07001586 ha->flags.chip_reset_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001588 if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001589 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001590 rval = qla84xx_init_chip(vha);
1591 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001592 ql_log(ql_log_warn, vha, 0x00d4,
1593 "Unable to initialize ISP84XX.\n");
Bart Van Assche8d2b21d2015-06-04 15:58:09 -07001594 qla84xx_put_chip(vha);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001595 }
1596 }
1597
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001598 /* Load the NIC Core f/w if we are the first protocol driver. */
1599 if (IS_QLA8031(ha)) {
1600 rval = qla83xx_nic_core_fw_load(vha);
1601 if (rval)
1602 ql_log(ql_log_warn, vha, 0x0124,
1603 "Error in initializing NIC Core f/w.\n");
1604 }
1605
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +05001606 if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
1607 qla24xx_read_fcp_prio_cfg(vha);
Sarang Radke09ff7012010-03-19 17:03:59 -07001608
Joe Carnuccioc46e65c2013-08-27 01:37:35 -04001609 if (IS_P3P_TYPE(ha))
1610 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
1611 else
1612 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return (rval);
1615}
1616
1617/**
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001618 * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * @ha: HA context
1620 *
1621 * Returns 0 on success.
1622 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001623int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001624qla2100_pci_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Andrew Vasqueza157b102007-05-07 07:43:01 -07001626 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001627 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001628 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001629 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07001632 pci_try_set_mwi(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07001635 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1637
Andrew Vasquez737faec2008-10-24 15:13:45 -07001638 pci_disable_rom(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001640 /* Get PCI bus information. */
1641 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07001642 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001643 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1644
1645 return QLA_SUCCESS;
1646}
1647
1648/**
1649 * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
1650 * @ha: HA context
1651 *
1652 * Returns 0 on success.
1653 */
1654int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001655qla2300_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001656{
Andrew Vasqueza157b102007-05-07 07:43:01 -07001657 uint16_t w;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001658 unsigned long flags = 0;
1659 uint32_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001660 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001661 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001662
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001663 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07001664 pci_try_set_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001665
1666 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07001667 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001668
1669 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1670 w &= ~PCI_COMMAND_INTX_DISABLE;
Andrew Vasqueza157b102007-05-07 07:43:01 -07001671 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001672
1673 /*
1674 * If this is a 2300 card and not 2312, reset the
1675 * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
1676 * the 2310 also reports itself as a 2300 so we need to get the
1677 * fb revision level -- a 6 indicates it really is a 2300 and
1678 * not a 2310.
1679 */
1680 if (IS_QLA2300(ha)) {
1681 spin_lock_irqsave(&ha->hardware_lock, flags);
1682
1683 /* Pause RISC. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001684 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001685 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07001686 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001687 break;
1688
1689 udelay(10);
1690 }
1691
1692 /* Select FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001693 WRT_REG_WORD(&reg->ctrl_status, 0x20);
1694 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001695
1696 /* Get the fb rev level */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001697 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001698
1699 if (ha->fb_rev == FPM_2300)
Andrew Vasqueza157b102007-05-07 07:43:01 -07001700 pci_clear_mwi(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001701
1702 /* Deselect FPM registers. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001703 WRT_REG_WORD(&reg->ctrl_status, 0x0);
1704 RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001705
1706 /* Release RISC module. */
Andrew Vasquez3d716442005-07-06 10:30:26 -07001707 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001708 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquez3d716442005-07-06 10:30:26 -07001709 if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001710 break;
1711
1712 udelay(10);
1713 }
1714
1715 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1716 }
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001717
1718 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
1719
Andrew Vasquez737faec2008-10-24 15:13:45 -07001720 pci_disable_rom(ha->pdev);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001721
1722 /* Get PCI bus information. */
1723 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez3d716442005-07-06 10:30:26 -07001724 ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001725 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1726
1727 return QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001731 * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
1732 * @ha: HA context
1733 *
1734 * Returns 0 on success.
1735 */
1736int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001737qla24xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001738{
Andrew Vasqueza157b102007-05-07 07:43:01 -07001739 uint16_t w;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001740 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001741 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001742 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001743
1744 pci_set_master(ha->pdev);
Andrew Vasquezaf6177d2007-07-19 15:06:02 -07001745 pci_try_set_mwi(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001746
1747 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
Andrew Vasqueza157b102007-05-07 07:43:01 -07001748 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001749 w &= ~PCI_COMMAND_INTX_DISABLE;
1750 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1751
1752 pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
1753
1754 /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
Andrew Vasquezf85ec182007-07-19 15:06:01 -07001755 if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
1756 pcix_set_mmrbc(ha->pdev, 2048);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001757
1758 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07001759 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04001760 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001761
Andrew Vasquez737faec2008-10-24 15:13:45 -07001762 pci_disable_rom(ha->pdev);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001763
Auke Kok44c10132007-06-08 15:46:36 -07001764 ha->chip_revision = ha->pdev->revision;
Andrew Vasqueza8488ab2007-01-29 10:22:19 -08001765
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001766 /* Get PCI bus information. */
1767 spin_lock_irqsave(&ha->hardware_lock, flags);
1768 ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
1769 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1770
1771 return QLA_SUCCESS;
1772}
1773
1774/**
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001775 * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
1776 * @ha: HA context
1777 *
1778 * Returns 0 on success.
1779 */
1780int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001781qla25xx_pci_config(scsi_qla_host_t *vha)
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001782{
1783 uint16_t w;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001784 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001785
1786 pci_set_master(ha->pdev);
1787 pci_try_set_mwi(ha->pdev);
1788
1789 pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
1790 w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
1791 w &= ~PCI_COMMAND_INTX_DISABLE;
1792 pci_write_config_word(ha->pdev, PCI_COMMAND, w);
1793
1794 /* PCIe -- adjust Maximum Read Request Size (2048). */
Jon Masone67f1322012-07-10 14:57:56 -07001795 if (pci_is_pcie(ha->pdev))
Chad Dupuis5ffd3a52012-08-22 14:21:26 -04001796 pcie_set_readrq(ha->pdev, 4096);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001797
Andrew Vasquez737faec2008-10-24 15:13:45 -07001798 pci_disable_rom(ha->pdev);
Andrew Vasquezc3a2f0d2007-07-19 20:37:34 -07001799
1800 ha->chip_revision = ha->pdev->revision;
1801
1802 return QLA_SUCCESS;
1803}
1804
1805/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * qla2x00_isp_firmware() - Choose firmware image.
1807 * @ha: HA context
1808 *
1809 * Returns 0 on success.
1810 */
1811static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001812qla2x00_isp_firmware(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 int rval;
Andrew Vasquez42e421b2008-07-10 16:56:01 -07001815 uint16_t loop_id, topo, sw_cap;
1816 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001817 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /* Assume loading risc code */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001820 rval = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 if (ha->flags.disable_risc_code_load) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001823 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* Verify checksum of loaded RISC code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001826 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
Andrew Vasquez42e421b2008-07-10 16:56:01 -07001827 if (rval == QLA_SUCCESS) {
1828 /* And, verify we are not in ROM code. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001829 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
Andrew Vasquez42e421b2008-07-10 16:56:01 -07001830 &area, &domain, &topo, &sw_cap);
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001834 if (rval)
1835 ql_dbg(ql_dbg_init, vha, 0x007a,
1836 "**** Load RISC code ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 return (rval);
1839}
1840
1841/**
1842 * qla2x00_reset_chip() - Reset ISP chip.
1843 * @ha: HA context
1844 *
1845 * Returns 0 on success.
1846 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001847void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001848qla2x00_reset_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001851 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001852 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 uint32_t cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 uint16_t cmd;
1855
Andrew Vasquez85880802009-12-15 21:29:46 -08001856 if (unlikely(pci_channel_offline(ha->pdev)))
1857 return;
1858
Andrew Vasquezfd34f552007-07-19 15:06:00 -07001859 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 spin_lock_irqsave(&ha->hardware_lock, flags);
1862
1863 /* Turn off master enable */
1864 cmd = 0;
1865 pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
1866 cmd &= ~PCI_COMMAND_MASTER;
1867 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
1868
1869 if (!IS_QLA2100(ha)) {
1870 /* Pause RISC. */
1871 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
1872 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
1873 for (cnt = 0; cnt < 30000; cnt++) {
1874 if ((RD_REG_WORD(&reg->hccr) &
1875 HCCR_RISC_PAUSE) != 0)
1876 break;
1877 udelay(100);
1878 }
1879 } else {
1880 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
1881 udelay(10);
1882 }
1883
1884 /* Select FPM registers. */
1885 WRT_REG_WORD(&reg->ctrl_status, 0x20);
1886 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1887
1888 /* FPM Soft Reset. */
1889 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
1890 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
1891
1892 /* Toggle Fpm Reset. */
1893 if (!IS_QLA2200(ha)) {
1894 WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
1895 RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
1896 }
1897
1898 /* Select frame buffer registers. */
1899 WRT_REG_WORD(&reg->ctrl_status, 0x10);
1900 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1901
1902 /* Reset frame buffer FIFOs. */
1903 if (IS_QLA2200(ha)) {
1904 WRT_FB_CMD_REG(ha, reg, 0xa000);
1905 RD_FB_CMD_REG(ha, reg); /* PCI Posting. */
1906 } else {
1907 WRT_FB_CMD_REG(ha, reg, 0x00fc);
1908
1909 /* Read back fb_cmd until zero or 3 seconds max */
1910 for (cnt = 0; cnt < 3000; cnt++) {
1911 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
1912 break;
1913 udelay(100);
1914 }
1915 }
1916
1917 /* Select RISC module registers. */
1918 WRT_REG_WORD(&reg->ctrl_status, 0);
1919 RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
1920
1921 /* Reset RISC processor. */
1922 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
1923 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
1924
1925 /* Release RISC processor. */
1926 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
1927 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
1928 }
1929
1930 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
1931 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
1932
1933 /* Reset ISP chip. */
1934 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
1935
1936 /* Wait for RISC to recover from reset. */
1937 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
1938 /*
1939 * It is necessary to for a delay here since the card doesn't
1940 * respond to PCI reads during a reset. On some architectures
1941 * this will result in an MCA.
1942 */
1943 udelay(20);
1944 for (cnt = 30000; cnt; cnt--) {
1945 if ((RD_REG_WORD(&reg->ctrl_status) &
1946 CSR_ISP_SOFT_RESET) == 0)
1947 break;
1948 udelay(100);
1949 }
1950 } else
1951 udelay(10);
1952
1953 /* Reset RISC processor. */
1954 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
1955
1956 WRT_REG_WORD(&reg->semaphore, 0);
1957
1958 /* Release RISC processor. */
1959 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
1960 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
1961
1962 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
1963 for (cnt = 0; cnt < 30000; cnt++) {
Andrew Vasquezffb39f02006-05-17 15:09:06 -07001964 if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 udelay(100);
1968 }
1969 } else
1970 udelay(100);
1971
1972 /* Turn on master enable */
1973 cmd |= PCI_COMMAND_MASTER;
1974 pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
1975
1976 /* Disable RISC pause on FPM parity error. */
1977 if (!IS_QLA2100(ha)) {
1978 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
1979 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
1980 }
1981
1982 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1983}
1984
1985/**
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07001986 * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
1987 *
1988 * Returns 0 on success.
1989 */
Saurav Kashyapfa492632012-11-21 02:40:29 -05001990static int
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07001991qla81xx_reset_mpi(scsi_qla_host_t *vha)
1992{
1993 uint16_t mb[4] = {0x1010, 0, 1, 0};
1994
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001995 if (!IS_QLA81XX(vha->hw))
1996 return QLA_SUCCESS;
1997
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07001998 return qla81xx_write_mpi_register(vha, mb);
1999}
2000
2001/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002002 * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002003 * @ha: HA context
2004 *
2005 * Returns 0 on success.
2006 */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002007static inline int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002008qla24xx_reset_risc(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002009{
2010 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002011 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002012 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
Bart Van Assche52c82822015-07-09 07:23:26 -07002013 uint32_t cnt;
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002014 uint16_t wd;
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002015 static int abts_cnt; /* ISP abort retry counts */
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002016 int rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002017
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002018 spin_lock_irqsave(&ha->hardware_lock, flags);
2019
2020 /* Reset RISC. */
2021 WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2022 for (cnt = 0; cnt < 30000; cnt++) {
2023 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2024 break;
2025
2026 udelay(10);
2027 }
2028
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002029 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2030 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2031
2032 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2033 "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2034 RD_REG_DWORD(&reg->hccr),
2035 RD_REG_DWORD(&reg->ctrl_status),
2036 (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2037
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002038 WRT_REG_DWORD(&reg->ctrl_status,
2039 CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002040 pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002041
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002042 udelay(100);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002043
Andrew Vasquez88c26662005-07-08 17:59:26 -07002044 /* Wait for firmware to complete NVRAM accesses. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002045 RD_REG_WORD(&reg->mailbox0);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002046 for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2047 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez88c26662005-07-08 17:59:26 -07002048 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002049 if (cnt)
2050 udelay(5);
2051 else
2052 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez88c26662005-07-08 17:59:26 -07002053 }
2054
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002055 if (rval == QLA_SUCCESS)
2056 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2057
2058 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2059 "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2060 RD_REG_DWORD(&reg->hccr),
2061 RD_REG_DWORD(&reg->mailbox0));
2062
Andrew Vasquez335a1cc2005-11-08 14:37:48 -08002063 /* Wait for soft-reset to complete. */
Bart Van Assche52c82822015-07-09 07:23:26 -07002064 RD_REG_DWORD(&reg->ctrl_status);
Quinn Tran200ffb12016-12-23 18:06:12 -08002065 for (cnt = 0; cnt < 60; cnt++) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002066 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002067 if ((RD_REG_DWORD(&reg->ctrl_status) &
2068 CSRX_ISP_SOFT_RESET) == 0)
2069 break;
2070
2071 udelay(5);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002072 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002073 if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2074 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2075
2076 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2077 "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2078 RD_REG_DWORD(&reg->hccr),
2079 RD_REG_DWORD(&reg->ctrl_status));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002080
Madhuranath Iyengarb1d469892010-09-03 15:20:54 -07002081 /* If required, do an MPI FW reset now */
2082 if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2083 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2084 if (++abts_cnt < 5) {
2085 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2086 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2087 } else {
2088 /*
2089 * We exhausted the ISP abort retries. We have to
2090 * set the board offline.
2091 */
2092 abts_cnt = 0;
2093 vha->flags.online = 0;
2094 }
2095 }
2096 }
2097
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002098 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2099 RD_REG_DWORD(&reg->hccr);
2100
2101 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2102 RD_REG_DWORD(&reg->hccr);
2103
2104 WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2105 RD_REG_DWORD(&reg->hccr);
2106
Bart Van Assche52c82822015-07-09 07:23:26 -07002107 RD_REG_WORD(&reg->mailbox0);
Quinn Tran200ffb12016-12-23 18:06:12 -08002108 for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002109 rval == QLA_SUCCESS; cnt--) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002110 barrier();
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002111 if (cnt)
2112 udelay(5);
2113 else
2114 rval = QLA_FUNCTION_TIMEOUT;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002115 }
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002116 if (rval == QLA_SUCCESS)
2117 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2118
2119 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2120 "Host Risc 0x%x, mailbox0 0x%x\n",
2121 RD_REG_DWORD(&reg->hccr),
2122 RD_REG_WORD(&reg->mailbox0));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002123
2124 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002125
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002126 ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2127 "Driver in %s mode\n",
2128 IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2129
Andrew Vasquez124f85e2009-01-05 11:18:06 -08002130 if (IS_NOPOLLING_TYPE(ha))
2131 ha->isp_ops->enable_intrs(ha);
Himanshu Madhanid14e72f2015-04-09 15:00:03 -04002132
2133 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002134}
2135
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002136static void
2137qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2138{
2139 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2140
2141 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2142 *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2143
2144}
2145
2146static void
2147qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2148{
2149 struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2150
2151 WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2152 WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2153}
2154
2155static void
2156qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2157{
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002158 uint32_t wd32 = 0;
2159 uint delta_msec = 100;
2160 uint elapsed_msec = 0;
2161 uint timeout_msec;
2162 ulong n;
2163
Joe Carnucciocc790762015-08-04 13:37:53 -04002164 if (vha->hw->pdev->subsystem_device != 0x0175 &&
2165 vha->hw->pdev->subsystem_device != 0x0240)
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002166 return;
2167
Joe Carnuccio8dd7e3a2015-08-04 13:37:54 -04002168 WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2169 udelay(100);
2170
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002171attempt:
2172 timeout_msec = TIMEOUT_SEMAPHORE;
2173 n = timeout_msec / delta_msec;
2174 while (n--) {
2175 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2176 qla25xx_read_risc_sema_reg(vha, &wd32);
2177 if (wd32 & RISC_SEMAPHORE)
2178 break;
2179 msleep(delta_msec);
2180 elapsed_msec += delta_msec;
2181 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2182 goto force;
2183 }
2184
2185 if (!(wd32 & RISC_SEMAPHORE))
2186 goto force;
2187
2188 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2189 goto acquired;
2190
2191 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2192 timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2193 n = timeout_msec / delta_msec;
2194 while (n--) {
2195 qla25xx_read_risc_sema_reg(vha, &wd32);
2196 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2197 break;
2198 msleep(delta_msec);
2199 elapsed_msec += delta_msec;
2200 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2201 goto force;
2202 }
2203
2204 if (wd32 & RISC_SEMAPHORE_FORCE)
2205 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2206
2207 goto attempt;
2208
2209force:
2210 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2211
2212acquired:
2213 return;
2214}
2215
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002216/**
Andrew Vasquez88c26662005-07-08 17:59:26 -07002217 * qla24xx_reset_chip() - Reset ISP24xx chip.
2218 * @ha: HA context
2219 *
2220 * Returns 0 on success.
2221 */
2222void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002223qla24xx_reset_chip(scsi_qla_host_t *vha)
Andrew Vasquez88c26662005-07-08 17:59:26 -07002224{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002225 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez85880802009-12-15 21:29:46 -08002226
2227 if (pci_channel_offline(ha->pdev) &&
2228 ha->flags.pci_channel_io_perm_failure) {
2229 return;
2230 }
2231
Andrew Vasquezfd34f552007-07-19 15:06:00 -07002232 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002233
Joe Carnuccio4ea2c9c2012-11-21 02:40:37 -05002234 qla25xx_manipulate_risc_semaphore(vha);
2235
Andrew Vasquez88c26662005-07-08 17:59:26 -07002236 /* Perform RISC reset. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002237 qla24xx_reset_risc(vha);
Andrew Vasquez88c26662005-07-08 17:59:26 -07002238}
2239
2240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 * qla2x00_chip_diag() - Test chip for proper operation.
2242 * @ha: HA context
2243 *
2244 * Returns 0 on success.
2245 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002246int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002247qla2x00_chip_diag(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
2249 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002250 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07002251 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 unsigned long flags = 0;
2253 uint16_t data;
2254 uint32_t cnt;
2255 uint16_t mb[5];
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002256 struct req_que *req = ha->req_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
2258 /* Assume a failed state */
2259 rval = QLA_FUNCTION_FAILED;
2260
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002261 ql_dbg(ql_dbg_init, vha, 0x007b,
2262 "Testing device at %lx.\n", (u_long)&reg->flash_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 spin_lock_irqsave(&ha->hardware_lock, flags);
2265
2266 /* Reset ISP chip. */
2267 WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2268
2269 /*
2270 * We need to have a delay here since the card will not respond while
2271 * in reset causing an MCA on some architectures.
2272 */
2273 udelay(20);
2274 data = qla2x00_debounce_register(&reg->ctrl_status);
2275 for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2276 udelay(5);
2277 data = RD_REG_WORD(&reg->ctrl_status);
2278 barrier();
2279 }
2280
2281 if (!cnt)
2282 goto chip_diag_failed;
2283
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002284 ql_dbg(ql_dbg_init, vha, 0x007c,
2285 "Reset register cleared by chip reset.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /* Reset RISC processor. */
2288 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2289 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2290
2291 /* Workaround for QLA2312 PCI parity error */
2292 if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2293 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2294 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2295 udelay(5);
2296 data = RD_MAILBOX_REG(ha, reg, 0);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002297 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299 } else
2300 udelay(10);
2301
2302 if (!cnt)
2303 goto chip_diag_failed;
2304
2305 /* Check product ID of chip */
Milan P Gandhi5a68a1c2017-03-31 14:37:04 -07002306 ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2309 mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2310 mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2311 mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2312 if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2313 mb[3] != PROD_ID_3) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002314 ql_log(ql_log_warn, vha, 0x0062,
2315 "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2316 mb[1], mb[2], mb[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 goto chip_diag_failed;
2319 }
2320 ha->product_id[0] = mb[1];
2321 ha->product_id[1] = mb[2];
2322 ha->product_id[2] = mb[3];
2323 ha->product_id[3] = mb[4];
2324
2325 /* Adjust fw RISC transfer size */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002326 if (req->length > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2328 else
2329 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002330 req->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 if (IS_QLA2200(ha) &&
2333 RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2334 /* Limit firmware transfer size with a 2200A */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002335 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
andrew.vasquez@qlogic.comea5b6382006-03-09 14:27:08 -08002337 ha->device_type |= DT_ISP2200A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 ha->fw_transfer_size = 128;
2339 }
2340
2341 /* Wrap Incoming Mailboxes Test. */
2342 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2343
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002344 ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002345 rval = qla2x00_mbx_reg_test(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002346 if (rval)
2347 ql_log(ql_log_warn, vha, 0x0080,
2348 "Failed mailbox send register test.\n");
2349 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /* Flag a successful rval */
2351 rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 spin_lock_irqsave(&ha->hardware_lock, flags);
2353
2354chip_diag_failed:
2355 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002356 ql_log(ql_log_info, vha, 0x0081,
2357 "Chip diagnostics **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2360
2361 return (rval);
2362}
2363
2364/**
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002365 * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2366 * @ha: HA context
2367 *
2368 * Returns 0 on success.
2369 */
2370int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002371qla24xx_chip_diag(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002372{
2373 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002374 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002375 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002376
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002377 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002378 return QLA_SUCCESS;
2379
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002380 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002381
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002382 rval = qla2x00_mbx_reg_test(vha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002383 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002384 ql_log(ql_log_warn, vha, 0x0082,
2385 "Failed mailbox send register test.\n");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002386 } else {
2387 /* Flag a successful rval */
2388 rval = QLA_SUCCESS;
2389 }
2390
2391 return rval;
2392}
2393
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002394void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002395qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002396{
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002397 int rval;
2398 uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002399 eft_size, fce_size, mq_size;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002400 dma_addr_t tc_dma;
2401 void *tc;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002402 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002403 struct req_que *req = ha->req_q_map[0];
2404 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002405
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002406 if (ha->fw_dump) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002407 ql_dbg(ql_dbg_init, vha, 0x00bd,
2408 "Firmware dump already allocated.\n");
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002409 return;
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002410 }
2411
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002412 ha->fw_dumped = 0;
Hiral Patel61f098d2014-04-11 16:54:21 -04002413 ha->fw_dump_cap_flags = 0;
Chad Dupuisf73cb692014-02-26 04:15:06 -05002414 dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
2415 req_q_size = rsp_q_size = 0;
2416
2417 if (IS_QLA27XX(ha))
2418 goto try_fce;
2419
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002420 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2421 fixed_size = sizeof(struct qla2100_fw_dump);
2422 } else if (IS_QLA23XX(ha)) {
2423 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
2424 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
2425 sizeof(uint16_t);
Andrew Vasqueze4289242007-07-19 15:05:56 -07002426 } else if (IS_FWI2_CAPABLE(ha)) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002427 if (IS_QLA83XX(ha) || IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002428 fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
2429 else if (IS_QLA81XX(ha))
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08002430 fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
2431 else if (IS_QLA25XX(ha))
2432 fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
2433 else
2434 fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
Chad Dupuisf73cb692014-02-26 04:15:06 -05002435
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002436 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
2437 sizeof(uint32_t);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002438 if (ha->mqenable) {
Himanshu Madhanib20f02e2015-06-10 11:05:18 -04002439 if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha))
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002440 mq_size = sizeof(struct qla2xxx_mq_chain);
Giridhar Malavali050c9bb2012-02-09 11:15:33 -08002441 /*
2442 * Allocate maximum buffer size for all queues.
2443 * Resizing must be done at end-of-dump processing.
2444 */
2445 mq_size += ha->max_req_queues *
2446 (req->length * sizeof(request_t));
2447 mq_size += ha->max_rsp_queues *
2448 (rsp->length * sizeof(response_t));
2449 }
Arun Easi00876ae2013-03-25 02:21:37 -04002450 if (ha->tgt.atio_ring)
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04002451 mq_size += ha->tgt.atio_q_length * sizeof(request_t);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002452 /* Allocate memory for Fibre Channel Event Buffer. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05002453 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
2454 !IS_QLA27XX(ha))
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002455 goto try_eft;
2456
Chad Dupuisf73cb692014-02-26 04:15:06 -05002457try_fce:
2458 if (ha->fce)
2459 dma_free_coherent(&ha->pdev->dev,
2460 FCE_SIZE, ha->fce, ha->fce_dma);
2461
2462 /* Allocate memory for Fibre Channel Event Buffer. */
Joe Perches0ea85b52014-06-15 13:37:51 -07002463 tc = dma_zalloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
2464 GFP_KERNEL);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002465 if (!tc) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002466 ql_log(ql_log_warn, vha, 0x00be,
2467 "Unable to allocate (%d KB) for FCE.\n",
2468 FCE_SIZE / 1024);
Anirban Chakraborty17d98632008-12-18 10:06:15 -08002469 goto try_eft;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002470 }
2471
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002472 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002473 ha->fce_mb, &ha->fce_bufs);
2474 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002475 ql_log(ql_log_warn, vha, 0x00bf,
2476 "Unable to initialize FCE (%d).\n", rval);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002477 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
2478 tc_dma);
2479 ha->flags.fce_enabled = 0;
Anirban Chakraborty17d98632008-12-18 10:06:15 -08002480 goto try_eft;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002481 }
Chad Dupuiscfb09192011-11-18 09:03:07 -08002482 ql_dbg(ql_dbg_init, vha, 0x00c0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002483 "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002484
Giridhar Malavali7d9dade2009-03-24 09:07:58 -07002485 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002486 ha->flags.fce_enabled = 1;
2487 ha->fce_dma = tc_dma;
2488 ha->fce = tc;
Chad Dupuisf73cb692014-02-26 04:15:06 -05002489
Andrew Vasquez436a7b12008-07-10 16:55:54 -07002490try_eft:
Chad Dupuisf73cb692014-02-26 04:15:06 -05002491 if (ha->eft)
2492 dma_free_coherent(&ha->pdev->dev,
2493 EFT_SIZE, ha->eft, ha->eft_dma);
2494
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002495 /* Allocate memory for Extended Trace Buffer. */
Joe Perches0ea85b52014-06-15 13:37:51 -07002496 tc = dma_zalloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
2497 GFP_KERNEL);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002498 if (!tc) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002499 ql_log(ql_log_warn, vha, 0x00c1,
2500 "Unable to allocate (%d KB) for EFT.\n",
2501 EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002502 goto cont_alloc;
2503 }
2504
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002505 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002506 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002507 ql_log(ql_log_warn, vha, 0x00c2,
2508 "Unable to initialize EFT (%d).\n", rval);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002509 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
2510 tc_dma);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002511 goto cont_alloc;
2512 }
Chad Dupuiscfb09192011-11-18 09:03:07 -08002513 ql_dbg(ql_dbg_init, vha, 0x00c3,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002514 "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002515
2516 eft_size = EFT_SIZE;
Andrew Vasquezdf613b92008-01-17 09:02:17 -08002517 ha->eft_dma = tc_dma;
2518 ha->eft = tc;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002519 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002520
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002521cont_alloc:
Chad Dupuisf73cb692014-02-26 04:15:06 -05002522 if (IS_QLA27XX(ha)) {
2523 if (!ha->fw_dump_template) {
2524 ql_log(ql_log_warn, vha, 0x00ba,
2525 "Failed missing fwdump template\n");
2526 return;
2527 }
2528 dump_size = qla27xx_fwdt_calculate_dump_size(vha);
2529 ql_dbg(ql_dbg_init, vha, 0x00fa,
2530 "-> allocating fwdump (%x bytes)...\n", dump_size);
2531 goto allocate;
2532 }
2533
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002534 req_q_size = req->length * sizeof(request_t);
2535 rsp_q_size = rsp->length * sizeof(response_t);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002536 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002537 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
Andrew Vasquezbb99de62009-01-05 11:18:08 -08002538 ha->chain_offset = dump_size;
2539 dump_size += mq_size + fce_size;
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002540
Chad Dupuisf73cb692014-02-26 04:15:06 -05002541allocate:
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002542 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002543 if (!ha->fw_dump) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002544 ql_log(ql_log_warn, vha, 0x00c4,
2545 "Unable to allocate (%d KB) for firmware dump.\n",
2546 dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002547
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002548 if (ha->fce) {
2549 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2550 ha->fce_dma);
2551 ha->fce = NULL;
2552 ha->fce_dma = 0;
2553 }
2554
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002555 if (ha->eft) {
2556 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
2557 ha->eft_dma);
2558 ha->eft = NULL;
2559 ha->eft_dma = 0;
2560 }
2561 return;
2562 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002563 ha->fw_dump_len = dump_size;
Chad Dupuiscfb09192011-11-18 09:03:07 -08002564 ql_dbg(ql_dbg_init, vha, 0x00c5,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002565 "Allocated (%d KB) for firmware dump.\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002566
Chad Dupuisf73cb692014-02-26 04:15:06 -05002567 if (IS_QLA27XX(ha))
2568 return;
2569
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002570 ha->fw_dump->signature[0] = 'Q';
2571 ha->fw_dump->signature[1] = 'L';
2572 ha->fw_dump->signature[2] = 'G';
2573 ha->fw_dump->signature[3] = 'C';
Bart Van Asschead950362015-07-09 07:24:08 -07002574 ha->fw_dump->version = htonl(1);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002575
2576 ha->fw_dump->fixed_size = htonl(fixed_size);
2577 ha->fw_dump->mem_size = htonl(mem_size);
2578 ha->fw_dump->req_q_size = htonl(req_q_size);
2579 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
2580
2581 ha->fw_dump->eft_size = htonl(eft_size);
2582 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
2583 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
2584
2585 ha->fw_dump->header_size =
2586 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002587}
2588
Andrew Vasquez18e75552009-06-03 09:55:30 -07002589static int
2590qla81xx_mpi_sync(scsi_qla_host_t *vha)
2591{
2592#define MPS_MASK 0xe0
2593 int rval;
2594 uint16_t dc;
2595 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07002596
2597 if (!IS_QLA81XX(vha->hw))
2598 return QLA_SUCCESS;
2599
2600 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
2601 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002602 ql_log(ql_log_warn, vha, 0x0105,
2603 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002604 goto done;
2605 }
2606
2607 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
2608 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
2609 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002610 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002611 goto done_release;
2612 }
2613
2614 dc &= MPS_MASK;
2615 if (dc == (dw & MPS_MASK))
2616 goto done_release;
2617
2618 dw &= ~MPS_MASK;
2619 dw |= dc;
2620 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
2621 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002622 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002623 }
2624
2625done_release:
2626 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
2627 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002628 ql_log(ql_log_warn, vha, 0x006d,
2629 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002630 }
2631
2632done:
2633 return rval;
2634}
2635
Chad Dupuis8d93f552013-01-30 03:34:37 -05002636int
2637qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
2638{
2639 /* Don't try to reallocate the array */
2640 if (req->outstanding_cmds)
2641 return QLA_SUCCESS;
2642
Michael Hernandezd7459522016-12-12 14:40:07 -08002643 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05002644 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
2645 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05002646 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
2647 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002648 else
Quinn Tran03e8c682015-12-17 14:56:59 -05002649 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002650 }
2651
2652 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
2653 req->num_outstanding_cmds, GFP_KERNEL);
2654
2655 if (!req->outstanding_cmds) {
2656 /*
2657 * Try to allocate a minimal size just so we can get through
2658 * initialization.
2659 */
2660 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
2661 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
2662 req->num_outstanding_cmds, GFP_KERNEL);
2663
2664 if (!req->outstanding_cmds) {
2665 ql_log(ql_log_fatal, NULL, 0x0126,
2666 "Failed to allocate memory for "
2667 "outstanding_cmds for req_que %p.\n", req);
2668 req->num_outstanding_cmds = 0;
2669 return QLA_FUNCTION_FAILED;
2670 }
2671 }
2672
2673 return QLA_SUCCESS;
2674}
2675
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002676/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 * qla2x00_setup_chip() - Load and start RISC firmware.
2678 * @ha: HA context
2679 *
2680 * Returns 0 on success.
2681 */
2682static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002683qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002685 int rval;
2686 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002687 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08002688 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2689 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07002690 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08002691
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002692 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07002693 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05002694 if (rval == QLA_SUCCESS) {
2695 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002696 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05002697 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07002698 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002699 }
2700
Andrew Vasquez3db06522008-01-31 12:33:49 -08002701 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
2702 /* Disable SRAM, Instruction RAM and GP RAM parity. */
2703 spin_lock_irqsave(&ha->hardware_lock, flags);
2704 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
2705 RD_REG_WORD(&reg->hccr);
2706 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Andrew Vasquez18e75552009-06-03 09:55:30 -07002709 qla81xx_mpi_sync(vha);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002712 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002713 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002714 ql_dbg(ql_dbg_init, vha, 0x00c9,
2715 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002717 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (rval == QLA_SUCCESS) {
2719 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002720 ql_dbg(ql_dbg_init, vha, 0x00ca,
2721 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05002723 if (ql2xexlogins)
2724 ha->flags.exlogins_enabled = 1;
2725
Quinn Tran99e1b682017-06-02 09:12:03 -07002726 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05002727 ha->flags.exchoffld_enabled = 1;
2728
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002729 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07002731 if (rval == QLA_SUCCESS) {
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05002732 rval = qla2x00_set_exlogins_buffer(vha);
2733 if (rval != QLA_SUCCESS)
2734 goto failed;
2735
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05002736 rval = qla2x00_set_exchoffld_buffer(vha);
2737 if (rval != QLA_SUCCESS)
2738 goto failed;
2739
Giridhar Malavalia9083012010-04-12 17:59:55 -07002740enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07002741 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002742 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07002743 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002744 else
2745 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07002746 if (rval != QLA_SUCCESS)
2747 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002748 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002749 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07002750 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002751 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07002752 if ((!ha->max_npiv_vports) ||
2753 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002754 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07002755 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002756 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07002757 }
Quinn Tran03e8c682015-12-17 14:56:59 -05002758 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07002759
Chad Dupuis8d93f552013-01-30 03:34:37 -05002760 /*
2761 * Allocate the array of outstanding commands
2762 * now that we know the firmware resources.
2763 */
2764 rval = qla2x00_alloc_outstanding_cmds(ha,
2765 vha->req);
2766 if (rval != QLA_SUCCESS)
2767 goto failed;
2768
Saurav Kashyapbe5ea3c2011-11-18 09:02:11 -08002769 if (!fw_major_version && ql2xallocfwdump
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002770 && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07002771 qla2x00_alloc_fw_dump(vha);
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04002772 } else {
2773 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
2775 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002776 ql_log(ql_log_fatal, vha, 0x00cd,
2777 "ISP Firmware failed checksum.\n");
2778 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04002780 } else
2781 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Andrew Vasquez3db06522008-01-31 12:33:49 -08002783 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
2784 /* Enable proper parity. */
2785 spin_lock_irqsave(&ha->hardware_lock, flags);
2786 if (IS_QLA2300(ha))
2787 /* SRAM parity */
2788 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
2789 else
2790 /* SRAM, Instruction RAM and GP RAM parity */
2791 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
2792 RD_REG_WORD(&reg->hccr);
2793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2794 }
2795
Chad Dupuisf3982d82014-09-25 05:16:57 -04002796 if (IS_QLA27XX(ha))
2797 ha->flags.fac_supported = 1;
2798 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07002799 uint32_t size;
2800
2801 rval = qla81xx_fac_get_sector_size(vha, &size);
2802 if (rval == QLA_SUCCESS) {
2803 ha->flags.fac_supported = 1;
2804 ha->fdt_block_size = size << 2;
2805 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002806 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07002807 "Unsupported FAC firmware (%d.%02d.%02d).\n",
2808 ha->fw_major_version, ha->fw_minor_version,
2809 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05002810
Chad Dupuisf73cb692014-02-26 04:15:06 -05002811 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002812 ha->flags.fac_supported = 0;
2813 rval = QLA_SUCCESS;
2814 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07002815 }
2816 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07002817failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002819 ql_log(ql_log_fatal, vha, 0x00cf,
2820 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822
2823 return (rval);
2824}
2825
2826/**
2827 * qla2x00_init_response_q_entries() - Initializes response queue entries.
2828 * @ha: HA context
2829 *
2830 * Beginning of request ring has initialization control block already built
2831 * by nvram config routine.
2832 *
2833 * Returns 0 on success.
2834 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002835void
2836qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 uint16_t cnt;
2839 response_t *pkt;
2840
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002841 rsp->ring_ptr = rsp->ring;
2842 rsp->ring_index = 0;
2843 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002844 pkt = rsp->ring_ptr;
2845 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 pkt->signature = RESPONSE_PROCESSED;
2847 pkt++;
2848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
2851/**
2852 * qla2x00_update_fw_options() - Read and process firmware options.
2853 * @ha: HA context
2854 *
2855 * Returns 0 on success.
2856 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002857void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002858qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
2860 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002861 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002864 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2867 return;
2868
2869 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002870 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
2871 "Serial link options.\n");
2872 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
2873 (uint8_t *)&ha->fw_seriallink_options,
2874 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
2877 if (ha->fw_seriallink_options[3] & BIT_2) {
2878 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
2879
2880 /* 1G settings */
2881 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
2882 emphasis = (ha->fw_seriallink_options[2] &
2883 (BIT_4 | BIT_3)) >> 3;
2884 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002885 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 rx_sens = (ha->fw_seriallink_options[0] &
2887 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
2888 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
2889 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
2890 if (rx_sens == 0x0)
2891 rx_sens = 0x3;
2892 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
2893 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
2894 ha->fw_options[10] |= BIT_5 |
2895 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
2896 (tx_sens & (BIT_1 | BIT_0));
2897
2898 /* 2G settings */
2899 swing = (ha->fw_seriallink_options[2] &
2900 (BIT_7 | BIT_6 | BIT_5)) >> 5;
2901 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
2902 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002903 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 rx_sens = (ha->fw_seriallink_options[1] &
2905 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
2906 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
2907 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
2908 if (rx_sens == 0x0)
2909 rx_sens = 0x3;
2910 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
2911 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
2912 ha->fw_options[11] |= BIT_5 |
2913 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
2914 (tx_sens & (BIT_1 | BIT_0));
2915 }
2916
2917 /* FCP2 options. */
2918 /* Return command IOCBs without waiting for an ABTS to complete. */
2919 ha->fw_options[3] |= BIT_13;
2920
2921 /* LED scheme. */
2922 if (ha->flags.enable_led_scheme)
2923 ha->fw_options[2] |= BIT_12;
2924
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08002925 /* Detect ISP6312. */
2926 if (IS_QLA6312(ha))
2927 ha->fw_options[2] |= BIT_13;
2928
Giridhar Malavali088d09d2016-07-06 11:14:20 -04002929 /* Set Retry FLOGI in case of P2P connection */
2930 if (ha->operating_mode == P2P) {
2931 ha->fw_options[2] |= BIT_3;
2932 ql_dbg(ql_dbg_disc, vha, 0x2100,
2933 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
2934 __func__, ha->fw_options[2]);
2935 }
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002938 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002941void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002942qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002943{
2944 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002945 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002946
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002947 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002948 return;
2949
Himanshu Madhanif198caf2016-01-27 12:03:30 -05002950 /* Hold status IOCBs until ABTS response received. */
2951 if (ql2xfwholdabts)
2952 ha->fw_options[3] |= BIT_12;
2953
Giridhar Malavali088d09d2016-07-06 11:14:20 -04002954 /* Set Retry FLOGI in case of P2P connection */
2955 if (ha->operating_mode == P2P) {
2956 ha->fw_options[2] |= BIT_3;
2957 ql_dbg(ql_dbg_disc, vha, 0x2101,
2958 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
2959 __func__, ha->fw_options[2]);
2960 }
2961
Quinn Tran41dc5292017-01-19 22:28:03 -08002962 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07002963 if (ql2xmvasynctoatio &&
2964 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08002965 if (qla_tgt_mode_enabled(vha) ||
2966 qla_dual_mode_enabled(vha))
2967 ha->fw_options[2] |= BIT_11;
2968 else
2969 ha->fw_options[2] &= ~BIT_11;
2970 }
2971
Quinn Tranf7e761f2017-06-02 09:12:02 -07002972 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
2973 /*
2974 * Tell FW to track each exchange to prevent
2975 * driver from using stale exchange.
2976 */
2977 if (qla_tgt_mode_enabled(vha) ||
2978 qla_dual_mode_enabled(vha))
2979 ha->fw_options[2] |= BIT_4;
2980 else
2981 ha->fw_options[2] &= ~BIT_4;
2982 }
2983
Quinn Tran83548fe2017-06-02 09:12:01 -07002984 ql_dbg(ql_dbg_init, vha, 0x00e8,
2985 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
2986 __func__, ha->fw_options[1], ha->fw_options[2],
2987 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07002988
2989 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
2990 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08002991
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002992 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08002993 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002994 return;
2995
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002996 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08002997 le16_to_cpu(ha->fw_seriallink_options24[1]),
2998 le16_to_cpu(ha->fw_seriallink_options24[2]),
2999 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003000 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003001 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003002 "Unable to update Serial Link options (%x).\n", rval);
3003 }
3004}
3005
3006void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003007qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003008{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003009 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003010 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003011 struct req_que *req = ha->req_q_map[0];
3012 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003013
3014 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003015 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3016 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003017 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3018 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3019 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3020 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3021 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3022 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003023
3024 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3025 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3026 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3027 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3028 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3029}
3030
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003031void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003032qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003033{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003034 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003035 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003036 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3037 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003038 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003039 uint16_t rid = 0;
3040 struct req_que *req = ha->req_q_map[0];
3041 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003042
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003043 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003044 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003045 icb->request_q_outpointer = cpu_to_le16(0);
3046 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003047 icb->request_q_length = cpu_to_le16(req->length);
3048 icb->response_q_length = cpu_to_le16(rsp->length);
3049 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3050 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3051 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3052 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003053
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003054 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003055 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003056 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3057 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3058 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3059
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003060 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003061 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003062
Chad Dupuisf73cb692014-02-26 04:15:06 -05003063 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003064 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3065 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003066 if (ha->flags.msix_enabled) {
3067 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003068 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003069 "Registering vector 0x%x for base que.\n",
3070 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003071 icb->msix = cpu_to_le16(msix->entry);
3072 }
3073 /* Use alternate PCI bus number */
3074 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003075 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003076 /* Use alternate PCI devfn */
3077 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003078 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003079
Anirban Chakraborty31557542009-12-02 10:36:55 -08003080 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003081 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3082 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003083 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003084 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003085 ql_dbg(ql_dbg_init, vha, 0x00fe,
3086 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003087 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003088 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003089 }
Bart Van Asschead950362015-07-09 07:24:08 -07003090 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003091
3092 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3093 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3094 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3095 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3096 } else {
3097 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3098 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3099 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3100 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3101 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003102 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003103
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003104 /* PCI posting */
3105 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003106}
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108/**
3109 * qla2x00_init_rings() - Initializes firmware.
3110 * @ha: HA context
3111 *
3112 * Beginning of request ring has initialization control block already built
3113 * by nvram config routine.
3114 *
3115 * Returns 0 on success.
3116 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003117int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003118qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
3120 int rval;
3121 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003122 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003123 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003124 struct req_que *req;
3125 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003126 struct mid_init_cb_24xx *mid_init_cb =
3127 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 spin_lock_irqsave(&ha->hardware_lock, flags);
3130
3131 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003132 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003133 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003134 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003135 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003136 req->out_ptr = (void *)(req->ring + req->length);
3137 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003138 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003139 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003141 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003142
3143 /* Initialize firmware. */
3144 req->ring_ptr = req->ring;
3145 req->ring_index = 0;
3146 req->cnt = req->length;
3147 }
3148
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003149 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003150 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003151 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003152 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003153 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3154 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003155 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003156 if (IS_QLAFX00(ha))
3157 qlafx00_init_response_q_entries(rsp);
3158 else
3159 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003162 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3163 ha->tgt.atio_ring_index = 0;
3164 /* Initialize ATIO queue entries */
3165 qlt_init_atio_q_entries(vha);
3166
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003167 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3170
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003171 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3172
3173 if (IS_QLAFX00(ha)) {
3174 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3175 goto next_check;
3176 }
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003179 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003181 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003182 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003183 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003184 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003185 }
3186
Andrew Vasquez24a08132009-03-24 09:08:16 -07003187 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003188 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003189 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003190 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003191 ha->flags.dport_enabled =
3192 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3193 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3194 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3195 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003196 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003197 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003198 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003199 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003200 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003201
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003202 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003203next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003205 ql_log(ql_log_fatal, vha, 0x00d2,
3206 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003208 ql_dbg(ql_dbg_init, vha, 0x00d3,
3209 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003210 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
3212
3213 return (rval);
3214}
3215
3216/**
3217 * qla2x00_fw_ready() - Waits for firmware ready.
3218 * @ha: HA context
3219 *
3220 * Returns 0 on success.
3221 */
3222static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003223qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
3225 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003226 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 uint16_t min_wait; /* Minimum wait time if loop is down */
3228 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003229 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003230 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003232 if (IS_QLAFX00(vha->hw))
3233 return qlafx00_fw_ready(vha);
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 rval = QLA_SUCCESS;
3236
Chad Dupuis334614912015-04-09 14:59:57 -04003237 /* Time to wait for loop down */
3238 if (IS_P3P_TYPE(ha))
3239 min_wait = 30;
3240 else
3241 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 /*
3244 * Firmware should take at most one RATOV to login, plus 5 seconds for
3245 * our own processing.
3246 */
3247 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3248 wait_time = min_wait;
3249 }
3250
3251 /* Min wait time if loop down */
3252 mtime = jiffies + (min_wait * HZ);
3253
3254 /* wait time before firmware ready */
3255 wtime = jiffies + (wait_time * HZ);
3256
3257 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003258 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003259 ql_log(ql_log_info, vha, 0x801e,
3260 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003263 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003264 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003266 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003267 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003269 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003270 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3271 "fw_state=%x 84xx=%x.\n", state[0],
3272 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003273 if ((state[2] & FSTATE_LOGGED_IN) &&
3274 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003275 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3276 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003277
3278 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003279 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003280 if (rval != QLA_SUCCESS) {
3281 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003282 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003283 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003284 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003285 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003286
3287 /* Add time taken to initialize. */
3288 cs84xx_time = jiffies - cs84xx_time;
3289 wtime += cs84xx_time;
3290 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003291 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003292 "Increasing wait time by %ld. "
3293 "New time %ld.\n", cs84xx_time,
3294 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003295 }
3296 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003297 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3298 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003300 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 &ha->login_timeout, &ha->r_a_tov);
3302
3303 rval = QLA_SUCCESS;
3304 break;
3305 }
3306
3307 rval = QLA_FUNCTION_FAILED;
3308
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003309 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003310 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003312 * other than Wait for Login.
3313 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003315 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 "Cable is unplugged...\n");
3317
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003318 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 break;
3320 }
3321 }
3322 } else {
3323 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003324 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003325 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 break;
3327 }
3328
3329 if (time_after_eq(jiffies, wtime))
3330 break;
3331
3332 /* Delay for a while */
3333 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 } while (1);
3335
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003336 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003337 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3338 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Chad Dupuiscfb09192011-11-18 09:03:07 -08003340 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003341 ql_log(ql_log_warn, vha, 0x803b,
3342 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344
3345 return (rval);
3346}
3347
3348/*
3349* qla2x00_configure_hba
3350* Setup adapter context.
3351*
3352* Input:
3353* ha = adapter state pointer.
3354*
3355* Returns:
3356* 0 = success
3357*
3358* Context:
3359* Kernel context.
3360*/
3361static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003362qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
3364 int rval;
3365 uint16_t loop_id;
3366 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003367 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 uint8_t al_pa;
3369 uint8_t area;
3370 uint8_t domain;
3371 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003372 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003373 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003374 port_id_t id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003377 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003378 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003380 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003381 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003382 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003383 ql_dbg(ql_dbg_disc, vha, 0x2008,
3384 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003385 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003386 ql_log(ql_log_warn, vha, 0x2009,
3387 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003388 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3389 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3390 ql_log(ql_log_warn, vha, 0x1151,
3391 "Doing link init.\n");
3392 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3393 return rval;
3394 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003395 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return (rval);
3398 }
3399
3400 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003401 ql_log(ql_log_info, vha, 0x200a,
3402 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return (QLA_FUNCTION_FAILED);
3404 }
3405
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003406 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 /* initialize */
3409 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3410 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003411 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
3413 switch (topo) {
3414 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003415 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 ha->current_topology = ISP_CFG_NL;
3417 strcpy(connect_type, "(Loop)");
3418 break;
3419
3420 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003421 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003422 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 ha->current_topology = ISP_CFG_FL;
3424 strcpy(connect_type, "(FL_Port)");
3425 break;
3426
3427 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003428 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 ha->operating_mode = P2P;
3430 ha->current_topology = ISP_CFG_N;
3431 strcpy(connect_type, "(N_Port-to-N_Port)");
3432 break;
3433
3434 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003435 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003436 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 ha->operating_mode = P2P;
3438 ha->current_topology = ISP_CFG_F;
3439 strcpy(connect_type, "(F_Port)");
3440 break;
3441
3442 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003443 ql_dbg(ql_dbg_disc, vha, 0x200f,
3444 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 ha->current_topology = ISP_CFG_NL;
3446 strcpy(connect_type, "(Loop)");
3447 break;
3448 }
3449
3450 /* Save Host port and loop ID. */
3451 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07003452 id.b.domain = domain;
3453 id.b.area = area;
3454 id.b.al_pa = al_pa;
3455 id.b.rsvd_1 = 0;
3456 qlt_update_host_map(vha, id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003457
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003458 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003459 ql_log(ql_log_info, vha, 0x2010,
3460 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003461 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 return(rval);
3464}
3465
Giridhar Malavalia9083012010-04-12 17:59:55 -07003466inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003467qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
3468 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003469{
3470 char *st, *en;
3471 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003472 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07003473 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003474 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003475
3476 if (memcmp(model, BINZERO, len) != 0) {
3477 strncpy(ha->model_number, model, len);
3478 st = en = ha->model_number;
3479 en += len - 1;
3480 while (en > st) {
3481 if (*en != 0x20 && *en != 0x00)
3482 break;
3483 *en-- = '\0';
3484 }
3485
3486 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07003487 if (use_tbl &&
3488 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003489 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003490 strncpy(ha->model_desc,
3491 qla2x00_model_name[index * 2 + 1],
3492 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003493 } else {
3494 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07003495 if (use_tbl &&
3496 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003497 index < QLA_MODEL_NAMES) {
3498 strcpy(ha->model_number,
3499 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003500 strncpy(ha->model_desc,
3501 qla2x00_model_name[index * 2 + 1],
3502 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003503 } else {
3504 strcpy(ha->model_number, def);
3505 }
3506 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003507 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003508 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003509 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003510}
3511
David Miller4e08df32007-04-16 12:37:43 -07003512/* On sparc systems, obtain port and node WWN from firmware
3513 * properties.
3514 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003515static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07003516{
3517#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003518 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07003519 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003520 struct device_node *dp = pci_device_to_OF_node(pdev);
3521 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003522 int len;
3523
3524 val = of_get_property(dp, "port-wwn", &len);
3525 if (val && len >= WWN_SIZE)
3526 memcpy(nv->port_name, val, WWN_SIZE);
3527
3528 val = of_get_property(dp, "node-wwn", &len);
3529 if (val && len >= WWN_SIZE)
3530 memcpy(nv->node_name, val, WWN_SIZE);
3531#endif
3532}
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534/*
3535* NVRAM configuration for ISP 2xxx
3536*
3537* Input:
3538* ha = adapter block pointer.
3539*
3540* Output:
3541* initialization control block in response_ring
3542* host adapters parameters in host adapter block
3543*
3544* Returns:
3545* 0 = success.
3546*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003547int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003548qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
David Miller4e08df32007-04-16 12:37:43 -07003550 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003551 uint8_t chksum = 0;
3552 uint16_t cnt;
3553 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003554 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003555 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07003556 nvram_t *nv = ha->nvram;
3557 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003558 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
David Miller4e08df32007-04-16 12:37:43 -07003560 rval = QLA_SUCCESS;
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003563 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 ha->nvram_base = 0;
3565 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
3566 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
3567 ha->nvram_base = 0x80;
3568
3569 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003570 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003571 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
3572 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003574 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
3575 "Contents of NVRAM.\n");
3576 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
3577 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
3579 /* Bad NVRAM data, set defaults parameters. */
3580 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
3581 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
3582 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003583 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04003584 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003585 "detected: checksum=0x%x id=%c version=0x%x.\n",
3586 chksum, nv->id[0], nv->nvram_version);
3587 ql_log(ql_log_warn, vha, 0x0065,
3588 "Falling back to "
3589 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07003590
3591 /*
3592 * Set default initialization control block.
3593 */
3594 memset(nv, 0, ha->nvram_size);
3595 nv->parameter_block_version = ICB_VERSION;
3596
3597 if (IS_QLA23XX(ha)) {
3598 nv->firmware_options[0] = BIT_2 | BIT_1;
3599 nv->firmware_options[1] = BIT_7 | BIT_5;
3600 nv->add_firmware_options[0] = BIT_5;
3601 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003602 nv->frame_payload_size = 2048;
David Miller4e08df32007-04-16 12:37:43 -07003603 nv->special_options[1] = BIT_7;
3604 } else if (IS_QLA2200(ha)) {
3605 nv->firmware_options[0] = BIT_2 | BIT_1;
3606 nv->firmware_options[1] = BIT_7 | BIT_5;
3607 nv->add_firmware_options[0] = BIT_5;
3608 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003609 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07003610 } else if (IS_QLA2100(ha)) {
3611 nv->firmware_options[0] = BIT_3 | BIT_1;
3612 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003613 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07003614 }
3615
Bart Van Asschead950362015-07-09 07:24:08 -07003616 nv->max_iocb_allocation = cpu_to_le16(256);
3617 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07003618 nv->retry_count = 8;
3619 nv->retry_delay = 1;
3620
3621 nv->port_name[0] = 33;
3622 nv->port_name[3] = 224;
3623 nv->port_name[4] = 139;
3624
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003625 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07003626
3627 nv->login_timeout = 4;
3628
3629 /*
3630 * Set default host adapter parameters
3631 */
3632 nv->host_p[1] = BIT_2;
3633 nv->reset_delay = 5;
3634 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07003635 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07003636 nv->link_down_timeout = 60;
3637
3638 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
3640
3641#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3642 /*
3643 * The SN2 does not provide BIOS emulation which means you can't change
3644 * potentially bogus BIOS settings. Force the use of default settings
3645 * for link rate and frame size. Hope that the rest of the settings
3646 * are valid.
3647 */
3648 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04003649 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if (IS_QLA23XX(ha))
3651 nv->special_options[1] = BIT_7;
3652 }
3653#endif
3654
3655 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003656 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 /*
3659 * Setup driver NVRAM options.
3660 */
3661 nv->firmware_options[0] |= (BIT_6 | BIT_1);
3662 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
3663 nv->firmware_options[1] |= (BIT_5 | BIT_0);
3664 nv->firmware_options[1] &= ~BIT_4;
3665
3666 if (IS_QLA23XX(ha)) {
3667 nv->firmware_options[0] |= BIT_2;
3668 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003669 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003670 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672 if (IS_QLA2300(ha)) {
3673 if (ha->fb_rev == FPM_2310) {
3674 strcpy(ha->model_number, "QLA2310");
3675 } else {
3676 strcpy(ha->model_number, "QLA2300");
3677 }
3678 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003679 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003680 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
3682 } else if (IS_QLA2200(ha)) {
3683 nv->firmware_options[0] |= BIT_2;
3684 /*
3685 * 'Point-to-point preferred, else loop' is not a safe
3686 * connection mode setting.
3687 */
3688 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
3689 (BIT_5 | BIT_4)) {
3690 /* Force 'loop preferred, else point-to-point'. */
3691 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
3692 nv->add_firmware_options[0] |= BIT_5;
3693 }
3694 strcpy(ha->model_number, "QLA22xx");
3695 } else /*if (IS_QLA2100(ha))*/ {
3696 strcpy(ha->model_number, "QLA2100");
3697 }
3698
3699 /*
3700 * Copy over NVRAM RISC parameter block to initialization control block.
3701 */
3702 dptr1 = (uint8_t *)icb;
3703 dptr2 = (uint8_t *)&nv->parameter_block_version;
3704 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
3705 while (cnt--)
3706 *dptr1++ = *dptr2++;
3707
3708 /* Copy 2nd half. */
3709 dptr1 = (uint8_t *)icb->add_firmware_options;
3710 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
3711 while (cnt--)
3712 *dptr1++ = *dptr2++;
3713
Andrew Vasquez5341e862006-05-17 15:09:16 -07003714 /* Use alternate WWN? */
3715 if (nv->host_p[1] & BIT_7) {
3716 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3717 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3718 }
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 /* Prepare nodename */
3721 if ((icb->firmware_options[1] & BIT_6) == 0) {
3722 /*
3723 * Firmware will apply the following mask if the nodename was
3724 * not provided.
3725 */
3726 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3727 icb->node_name[0] &= 0xF0;
3728 }
3729
3730 /*
3731 * Set host adapter parameters.
3732 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07003733
3734 /*
3735 * BIT_7 in the host-parameters section allows for modification to
3736 * internal driver logging.
3737 */
Andrew Vasquez01819442006-06-23 16:11:10 -07003738 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08003739 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
3741 /* Always load RISC code on non ISP2[12]00 chips. */
3742 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
3743 ha->flags.disable_risc_code_load = 0;
3744 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
3745 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
3746 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07003747 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003748 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 ha->operating_mode =
3751 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3752
3753 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
3754 sizeof(ha->fw_seriallink_options));
3755
3756 /* save HBA serial number */
3757 ha->serial0 = icb->port_name[5];
3758 ha->serial1 = icb->port_name[6];
3759 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003760 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
3761 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Bart Van Asschead950362015-07-09 07:24:08 -07003763 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 ha->retry_count = nv->retry_count;
3766
3767 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07003768 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 nv->login_timeout = ql2xlogintimeout;
3770 if (nv->login_timeout < 4)
3771 nv->login_timeout = 4;
3772 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Andrew Vasquez00a537b2008-02-28 14:06:11 -08003774 /* Set minimum RATOV to 100 tenths of a second. */
3775 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 ha->loop_reset_delay = nv->reset_delay;
3778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 /* Link Down Timeout = 0:
3780 *
3781 * When Port Down timer expires we will start returning
3782 * I/O's to OS with "DID_NO_CONNECT".
3783 *
3784 * Link Down Timeout != 0:
3785 *
3786 * The driver waits for the link to come up after link down
3787 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003788 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 if (nv->link_down_timeout == 0) {
3790 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04003791 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 } else {
3793 ha->link_down_timeout = nv->link_down_timeout;
3794 ha->loop_down_abort_time =
3795 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 /*
3799 * Need enough time to try and get the port back.
3800 */
3801 ha->port_down_retry_count = nv->port_down_retry_count;
3802 if (qlport_down_retry)
3803 ha->port_down_retry_count = qlport_down_retry;
3804 /* Set login_retry_count */
3805 ha->login_retry_count = nv->retry_count;
3806 if (ha->port_down_retry_count == nv->port_down_retry_count &&
3807 ha->port_down_retry_count > 3)
3808 ha->login_retry_count = ha->port_down_retry_count;
3809 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3810 ha->login_retry_count = ha->port_down_retry_count;
3811 if (ql2xloginretrycount)
3812 ha->login_retry_count = ql2xloginretrycount;
3813
Bart Van Asschead950362015-07-09 07:24:08 -07003814 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 icb->command_resource_count = 0;
3816 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003817 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
3819 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3820 /* Enable RIO */
3821 icb->firmware_options[0] &= ~BIT_3;
3822 icb->add_firmware_options[0] &=
3823 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
3824 icb->add_firmware_options[0] |= BIT_2;
3825 icb->response_accumulation_timer = 3;
3826 icb->interrupt_delay_timer = 5;
3827
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003828 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003830 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003831 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003832 ha->zio_mode = icb->add_firmware_options[0] &
3833 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3834 ha->zio_timer = icb->interrupt_delay_timer ?
3835 icb->interrupt_delay_timer: 2;
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 icb->add_firmware_options[0] &=
3838 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003839 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003840 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003841 ha->zio_mode = QLA_ZIO_MODE_6;
3842
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003843 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003844 "ZIO mode %d enabled; timer delay (%d us).\n",
3845 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003847 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
3848 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003849 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851 }
3852
David Miller4e08df32007-04-16 12:37:43 -07003853 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003854 ql_log(ql_log_warn, vha, 0x0069,
3855 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07003856 }
3857 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003860static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003861qla2x00_rport_del(void *data)
3862{
3863 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003864 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08003865 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003866
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08003867 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07003868 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003869 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08003870 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08003871 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003872 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
3873 "%s %8phN. rport %p roles %x\n",
3874 __func__, fcport->port_name, rport,
3875 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08003876
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003877 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08003878 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003879}
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881/**
3882 * qla2x00_alloc_fcport() - Allocate a generic fcport.
3883 * @ha: HA context
3884 * @flags: allocation flags
3885 *
3886 * Returns a pointer to the allocated fcport, or NULL, if none available.
3887 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08003888fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003889qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 fc_port_t *fcport;
3892
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02003893 fcport = kzalloc(sizeof(fc_port_t), flags);
3894 if (!fcport)
3895 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003898 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 fcport->port_type = FCT_UNKNOWN;
3900 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07003901 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07003902 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Quinn Tran726b8542017-01-19 22:28:00 -08003904 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
3905 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08003906 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08003907 fcport->disc_state = DSC_DELETED;
3908 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
3909 fcport->deleted = QLA_SESS_DELETED;
3910 fcport->login_retry = vha->hw->login_retry_count;
3911 fcport->login_retry = 5;
3912 fcport->logout_on_delete = 1;
3913
3914 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003915 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08003916 "Failed to allocate ct_sns request.\n");
3917 kfree(fcport);
3918 fcport = NULL;
3919 }
3920 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
3921 INIT_LIST_HEAD(&fcport->gnl_entry);
3922 INIT_LIST_HEAD(&fcport->list);
3923
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02003924 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
Quinn Tran726b8542017-01-19 22:28:00 -08003927void
3928qla2x00_free_fcport(fc_port_t *fcport)
3929{
3930 if (fcport->ct_desc.ct_sns) {
3931 dma_free_coherent(&fcport->vha->hw->pdev->dev,
3932 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
3933 fcport->ct_desc.ct_sns_dma);
3934
3935 fcport->ct_desc.ct_sns = NULL;
3936 }
3937 kfree(fcport);
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940/*
3941 * qla2x00_configure_loop
3942 * Updates Fibre Channel Device Database with what is actually on loop.
3943 *
3944 * Input:
3945 * ha = adapter block pointer.
3946 *
3947 * Returns:
3948 * 0 = success.
3949 * 1 = error.
3950 * 2 = database was full and device was not configured.
3951 */
3952static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003953qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 int rval;
3956 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003957 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 rval = QLA_SUCCESS;
3959
3960 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003961 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
3962 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003964 ql_dbg(ql_dbg_disc, vha, 0x2013,
3965 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return (rval);
3967 }
3968 }
3969
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003970 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003971 ql_dbg(ql_dbg_disc, vha, 0x2014,
3972 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 /*
3975 * If we have both an RSCN and PORT UPDATE pending then handle them
3976 * both at the same time.
3977 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003978 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3979 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Michael Hernandez3064ff32009-12-15 21:29:44 -08003981 qla2x00_get_data_rate(vha);
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 /* Determine what we need to do */
3984 if (ha->current_topology == ISP_CFG_FL &&
3985 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 set_bit(RSCN_UPDATE, &flags);
3988
3989 } else if (ha->current_topology == ISP_CFG_F &&
3990 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
3991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 set_bit(RSCN_UPDATE, &flags);
3993 clear_bit(LOCAL_LOOP_UPDATE, &flags);
3994
Andrew Vasquez21333b42006-05-17 15:09:56 -07003995 } else if (ha->current_topology == ISP_CFG_N) {
3996 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08003997 } else if (ha->current_topology == ISP_CFG_NL) {
3998 clear_bit(RSCN_UPDATE, &flags);
3999 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004000 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 set_bit(RSCN_UPDATE, &flags);
4003 set_bit(LOCAL_LOOP_UPDATE, &flags);
4004 }
4005
4006 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004007 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4008 ql_dbg(ql_dbg_disc, vha, 0x2015,
4009 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004011 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004012 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 }
4014
4015 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004016 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004017 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004018 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004020 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004021 else
4022 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
4024
4025 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004026 if (atomic_read(&vha->loop_down_timer) ||
4027 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 rval = QLA_FUNCTION_FAILED;
4029 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004030 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004031 ql_dbg(ql_dbg_disc, vha, 0x2069,
4032 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004033 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004034
4035 /*
4036 * Process any ATIO queue entries that came in
4037 * while we weren't online.
4038 */
Quinn Tranead03852017-01-19 22:28:01 -08004039 if (qla_tgt_mode_enabled(vha) ||
4040 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004041 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4042 spin_lock_irqsave(&ha->tgt.atio_lock,
4043 flags);
4044 qlt_24xx_process_atio_queue(vha, 0);
4045 spin_unlock_irqrestore(
4046 &ha->tgt.atio_lock, flags);
4047 } else {
4048 spin_lock_irqsave(&ha->hardware_lock,
4049 flags);
4050 qlt_24xx_process_atio_queue(vha, 1);
4051 spin_unlock_irqrestore(
4052 &ha->hardware_lock, flags);
4053 }
4054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 }
4056 }
4057
4058 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004059 ql_dbg(ql_dbg_disc, vha, 0x206a,
4060 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004062 ql_dbg(ql_dbg_disc, vha, 0x206b,
4063 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 }
4065
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004066 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004067 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004069 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004070 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004071 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 }
4074
4075 return (rval);
4076}
4077
4078
4079
4080/*
4081 * qla2x00_configure_local_loop
4082 * Updates Fibre Channel Device Database with local loop devices.
4083 *
4084 * Input:
4085 * ha = adapter block pointer.
4086 *
4087 * Returns:
4088 * 0 = success.
4089 */
4090static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004091qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
4093 int rval, rval2;
4094 int found_devs;
4095 int found;
4096 fc_port_t *fcport, *new_fcport;
4097
4098 uint16_t index;
4099 uint16_t entries;
4100 char *id_iter;
4101 uint16_t loop_id;
4102 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004103 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004104 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 found_devs = 0;
4107 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004108 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004111 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004112 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 &entries);
4114 if (rval != QLA_SUCCESS)
4115 goto cleanup_allocation;
4116
Quinn Tran83548fe2017-06-02 09:12:01 -07004117 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004118 "Entries in ID list (%d).\n", entries);
4119 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4120 (uint8_t *)ha->gid_list,
4121 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004124 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004126 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004127 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 rval = QLA_MEMORY_ALLOC_FAILED;
4129 goto cleanup_allocation;
4130 }
4131 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4132
4133 /*
4134 * Mark local devices that were present with FCF_DEVICE_LOST for now.
4135 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004136 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 if (atomic_read(&fcport->state) == FCS_ONLINE &&
4138 fcport->port_type != FCT_BROADCAST &&
4139 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
4140
Quinn Tran83548fe2017-06-02 09:12:01 -07004141 ql_dbg(ql_dbg_disc, vha, 0x2096,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004142 "Marking port lost loop_id=0x%04x.\n",
4143 fcport->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Quinn Tran41dc5292017-01-19 22:28:03 -08004145 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
4147 }
4148
4149 /* Add devices to port list. */
4150 id_iter = (char *)ha->gid_list;
4151 for (index = 0; index < entries; index++) {
4152 domain = ((struct gid_list_info *)id_iter)->domain;
4153 area = ((struct gid_list_info *)id_iter)->area;
4154 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004155 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 loop_id = (uint16_t)
4157 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004158 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 loop_id = le16_to_cpu(
4160 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004161 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
4163 /* Bypass reserved domain fields. */
4164 if ((domain & 0xf0) == 0xf0)
4165 continue;
4166
4167 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004168 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004169 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 continue;
4171
4172 /* Bypass invalid local loop ID. */
4173 if (loop_id > LAST_LOCAL_LOOP_ID)
4174 continue;
4175
Quinn Tran41dc5292017-01-19 22:28:03 -08004176 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 /* Fill in member data. */
4179 new_fcport->d_id.b.domain = domain;
4180 new_fcport->d_id.b.area = area;
4181 new_fcport->d_id.b.al_pa = al_pa;
4182 new_fcport->loop_id = loop_id;
Quinn Tran41dc5292017-01-19 22:28:03 -08004183
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004184 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004186 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004187 "Failed to retrieve fcport information "
4188 "-- get_port_database=%x, loop_id=0x%04x.\n",
4189 rval2, new_fcport->loop_id);
Quinn Tran83548fe2017-06-02 09:12:01 -07004190 ql_dbg(ql_dbg_disc, vha, 0x2105,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004191 "Scheduling resync.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004192 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 continue;
4194 }
4195
Quinn Tran41dc5292017-01-19 22:28:03 -08004196 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 /* Check for matching device in port list. */
4198 found = 0;
4199 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004200 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 if (memcmp(new_fcport->port_name, fcport->port_name,
4202 WWN_SIZE))
4203 continue;
4204
Shyam Sundarddb9b122009-03-24 09:08:10 -07004205 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 fcport->loop_id = new_fcport->loop_id;
4207 fcport->port_type = new_fcport->port_type;
4208 fcport->d_id.b24 = new_fcport->d_id.b24;
4209 memcpy(fcport->node_name, new_fcport->node_name,
4210 WWN_SIZE);
4211
Quinn Tran41dc5292017-01-19 22:28:03 -08004212 if (!fcport->login_succ) {
4213 vha->fcport_count++;
4214 fcport->login_succ = 1;
4215 fcport->disc_state = DSC_LOGIN_COMPLETE;
4216 }
4217
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 found++;
4219 break;
4220 }
4221
4222 if (!found) {
4223 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004224 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226 /* Allocate a new replacement fcport. */
4227 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004228 if (!fcport->login_succ) {
4229 vha->fcport_count++;
4230 fcport->login_succ = 1;
4231 fcport->disc_state = DSC_LOGIN_COMPLETE;
4232 }
4233
4234 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4235
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004236 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004239 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004240 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 rval = QLA_MEMORY_ALLOC_FAILED;
4242 goto cleanup_allocation;
4243 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004244 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4246 }
4247
Quinn Tran41dc5292017-01-19 22:28:03 -08004248 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4249
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004250 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004251 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004252
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004253 qla2x00_update_fcport(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
4255 found_devs++;
4256 }
4257
4258cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004259 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004262 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004263 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return (rval);
4267}
4268
4269static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004270qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004271{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004272 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004273 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004274 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004275
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004276 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004277 return;
4278
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07004279 if (atomic_read(&fcport->state) != FCS_ONLINE)
4280 return;
4281
Andrew Vasquez39bd9622007-09-20 14:07:34 -07004282 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
4283 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004284 return;
4285
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004286 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004287 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004288 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004289 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004290 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
4291 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004292 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004293 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004294 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05004295 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004296 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004297 }
4298}
4299
Quinn Tran726b8542017-01-19 22:28:00 -08004300/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01004301static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004302qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05004303{
4304 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05004305 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004306 unsigned long flags;
8482e1182005-04-17 15:04:54 -05004307
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07004308 rport_ids.node_name = wwn_to_u64(fcport->node_name);
4309 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05004310 rport_ids.port_id = fcport->d_id.b.domain << 16 |
4311 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
4312 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004313 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07004314 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004315 ql_log(ql_log_warn, vha, 0x2006,
4316 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07004317 return;
4318 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004319
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004320 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004321 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004322 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004323
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004324 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07004325
4326 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05004327 if (fcport->port_type == FCT_INITIATOR)
4328 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4329 if (fcport->port_type == FCT_TARGET)
4330 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08004331
Quinn Tran83548fe2017-06-02 09:12:01 -07004332 ql_dbg(ql_dbg_disc, vha, 0x20ee,
4333 "%s %8phN. rport %p is %s mode\n",
4334 __func__, fcport->port_name, rport,
4335 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08004336
Andrew Vasquez77d74142005-07-08 18:00:36 -07004337 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05004338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340/*
Adrian Bunk23be3312006-11-24 02:46:01 +01004341 * qla2x00_update_fcport
4342 * Updates device on list.
4343 *
4344 * Input:
4345 * ha = adapter block pointer.
4346 * fcport = port structure pointer.
4347 *
4348 * Return:
4349 * 0 - Success
4350 * BIT_0 - error
4351 *
4352 * Context:
4353 * Kernel context.
4354 */
4355void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004356qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01004357{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004358 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004359
Quinn Tran726b8542017-01-19 22:28:00 -08004360 if (IS_SW_RESV_ADDR(fcport->d_id))
4361 return;
4362
Quinn Tran83548fe2017-06-02 09:12:01 -07004363 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08004364 __func__, fcport->port_name);
4365
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004366 if (IS_QLAFX00(vha->hw)) {
4367 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004368 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004369 }
Adrian Bunk23be3312006-11-24 02:46:01 +01004370 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07004371 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08004372 fcport->disc_state = DSC_LOGIN_COMPLETE;
4373 fcport->deleted = 0;
4374 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01004375
Joe Carnuccio1f93da522012-11-21 02:40:38 -05004376 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004377 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08004378 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004379
4380reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08004381 switch (vha->host->active_mode) {
4382 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004383 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08004384 break;
4385 case MODE_TARGET:
4386 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
4387 !vha->vha_tgt.qla_tgt->tgt_stopped)
4388 qlt_fc_port_added(vha, fcport);
4389 break;
4390 case MODE_DUAL:
4391 qla2x00_reg_remote_port(vha, fcport);
4392 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
4393 !vha->vha_tgt.qla_tgt->tgt_stopped)
4394 qlt_fc_port_added(vha, fcport);
4395 break;
4396 default:
4397 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004398 }
Adrian Bunk23be3312006-11-24 02:46:01 +01004399}
4400
4401/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 * qla2x00_configure_fabric
4403 * Setup SNS devices with loop ID's.
4404 *
4405 * Input:
4406 * ha = adapter block pointer.
4407 *
4408 * Returns:
4409 * 0 = success.
4410 * BIT_0 = error
4411 */
4412static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004413qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Arun Easib3b02e62012-02-09 11:15:39 -08004415 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08004416 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004418 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004420 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004421 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07004424 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004425 loop_id = NPH_F_PORT;
4426 else
4427 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004428 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004430 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004431 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004433 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 return (QLA_SUCCESS);
4435 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004436 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Quinn Tran41dc5292017-01-19 22:28:03 -08004438
4439 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
4440 rval = qla2x00_send_change_request(vha, 0x3, 0);
4441 if (rval != QLA_SUCCESS)
4442 ql_log(ql_log_warn, vha, 0x121,
4443 "Failed to enable receiving of RSCN requests: 0x%x.\n",
4444 rval);
4445 }
4446
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 do {
Quinn Tran726b8542017-01-19 22:28:00 -08004449 qla2x00_mgmt_svr_login(vha);
4450
Andrew Vasquezcca53352005-08-26 19:08:30 -07004451 /* FDMI support. */
4452 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004453 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
4454 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07004455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 /* Ensure we are logged into the SNS. */
Andrew Vasqueze4289242007-07-19 15:05:56 -07004457 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004458 loop_id = NPH_SNS;
4459 else
4460 loop_id = SIMPLE_NAME_SERVER;
Chad Dupuis0b91d112012-02-09 11:15:42 -08004461 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
4462 0xfc, mb, BIT_1|BIT_0);
4463 if (rval != QLA_SUCCESS) {
4464 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05004465 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08004466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 if (mb[0] != MBS_COMMAND_COMPLETE) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004468 ql_dbg(ql_dbg_disc, vha, 0x20a1,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004469 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x "
4470 "mb[6]=%x mb[7]=%x.\n", loop_id, mb[0], mb[1],
4471 mb[2], mb[6], mb[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return (QLA_SUCCESS);
4473 }
4474
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004475 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
4476 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004478 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004479 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004480 if (test_bit(LOOP_RESYNC_NEEDED,
4481 &vha->dpc_flags))
4482 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004484 if (qla2x00_rff_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004486 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004487 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004488 if (test_bit(LOOP_RESYNC_NEEDED,
4489 &vha->dpc_flags))
4490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004492 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004494 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004495 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004496 if (test_bit(LOOP_RESYNC_NEEDED,
4497 &vha->dpc_flags))
4498 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004499 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004501 ql_dbg(ql_dbg_disc, vha, 0x209b,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004502 "Register Symobilic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004503 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 }
4506 }
4507
Joe Carnuccio827210b2013-02-08 01:57:57 -05004508 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4509 fcport->scan_state = QLA_FCPORT_SCAN;
4510 }
4511
Alexei Potashnikdf673272015-07-14 16:00:46 -04004512 /* Mark the time right before querying FW for connected ports.
4513 * This process is long, asynchronous and by the time it's done,
4514 * collected information might not be accurate anymore. E.g.
4515 * disconnected port might have re-connected and a brand new
4516 * session has been created. In this case session's generation
4517 * will be newer than discovery_gen. */
4518 qlt_do_generation_tick(vha, &discovery_gen);
4519
Quinn Tran726b8542017-01-19 22:28:00 -08004520 rval = qla2x00_find_all_fabric_devs(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (rval != QLA_SUCCESS)
4522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 } while (0);
4524
Quinn Tran726b8542017-01-19 22:28:00 -08004525 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004526 ql_dbg(ql_dbg_disc, vha, 0x2068,
4527 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529 return (rval);
4530}
4531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532/*
4533 * qla2x00_find_all_fabric_devs
4534 *
4535 * Input:
4536 * ha = adapter block pointer.
4537 * dev = database device entry pointer.
4538 *
4539 * Returns:
4540 * 0 = success.
4541 *
4542 * Context:
4543 * Kernel context.
4544 */
4545static int
Quinn Tran726b8542017-01-19 22:28:00 -08004546qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
4548 int rval;
4549 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004550 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 int found;
4552
4553 sw_info_t *swl;
4554 int swl_idx;
4555 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07004556 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004557 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05004558 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08004559 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 rval = QLA_SUCCESS;
4562
4563 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08004564 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08004565 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08004566 GFP_KERNEL);
4567 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004568 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07004570 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004571 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08004573 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004574 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004576 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4577 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004578 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004580 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4581 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004582 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004584 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4585 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08004586 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
4587 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004588 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4589 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004591
4592 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004593 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004594 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004595 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4596 return rval;
4597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 }
4599 swl_idx = 0;
4600
4601 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004602 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004604 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004605 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return (QLA_MEMORY_ALLOC_FAILED);
4607 }
4608 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /* Set start port ID scan at adapter ID. */
4610 first_dev = 1;
4611 last_dev = 0;
4612
4613 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004614 loop_id = ha->min_external_loopid;
4615 for (; loop_id <= ha->max_loop_id; loop_id++) {
4616 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 continue;
4618
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07004619 if (ha->current_topology == ISP_CFG_FL &&
4620 (atomic_read(&vha->loop_down_timer) ||
4621 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08004622 atomic_set(&vha->loop_down_timer, 0);
4623 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4624 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08004626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628 if (swl != NULL) {
4629 if (last_dev) {
4630 wrap.b24 = new_fcport->d_id.b24;
4631 } else {
4632 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
4633 memcpy(new_fcport->node_name,
4634 swl[swl_idx].node_name, WWN_SIZE);
4635 memcpy(new_fcport->port_name,
4636 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004637 memcpy(new_fcport->fabric_port_name,
4638 swl[swl_idx].fabric_port_name, WWN_SIZE);
4639 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004640 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
4643 last_dev = 1;
4644 }
4645 swl_idx++;
4646 }
4647 } else {
4648 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004649 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004651 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004652 "SNS scan failed -- assuming "
4653 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 rval = QLA_SUCCESS;
4655 break;
4656 }
4657 }
4658
4659 /* If wrap on switch device list, exit. */
4660 if (first_dev) {
4661 wrap.b24 = new_fcport->d_id.b24;
4662 first_dev = 0;
4663 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004664 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004665 "Device wrap (%02x%02x%02x).\n",
4666 new_fcport->d_id.b.domain,
4667 new_fcport->d_id.b.area,
4668 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 break;
4670 }
4671
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004672 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004673 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 continue;
4675
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004676 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05004677 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
4678 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004679
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004680 /* Bypass if same domain and area of adapter. */
4681 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004682 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004683 ISP_CFG_FL)
4684 continue;
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /* Bypass reserved domain fields. */
4687 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
4688 continue;
4689
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004690 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07004691 if (ql2xgffidenable &&
4692 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
4693 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004694 continue;
4695
Quinn Tran726b8542017-01-19 22:28:00 -08004696 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 /* Locate matching device in database. */
4699 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004700 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 if (memcmp(new_fcport->port_name, fcport->port_name,
4702 WWN_SIZE))
4703 continue;
4704
Joe Carnuccio827210b2013-02-08 01:57:57 -05004705 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08004706
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 found++;
4708
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004709 /* Update port state. */
4710 memcpy(fcport->fabric_port_name,
4711 new_fcport->fabric_port_name, WWN_SIZE);
4712 fcport->fp_speed = new_fcport->fp_speed;
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04004715 * If address the same and state FCS_ONLINE
4716 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 */
4718 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04004719 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08004720 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 break;
4722 }
4723
4724 /*
4725 * If device was not a fabric device before.
4726 */
4727 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
4728 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04004729 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 fcport->flags |= (FCF_FABRIC_DEVICE |
4731 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 break;
4733 }
4734
4735 /*
4736 * Port ID changed or device was marked to be updated;
4737 * Log it out if still logged in and mark it for
4738 * relogin later.
4739 */
Quinn Tran726b8542017-01-19 22:28:00 -08004740 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04004741 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
4742 "port changed FC ID, %8phC"
4743 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
4744 fcport->port_name,
4745 fcport->d_id.b.domain,
4746 fcport->d_id.b.area,
4747 fcport->d_id.b.al_pa,
4748 fcport->loop_id,
4749 new_fcport->d_id.b.domain,
4750 new_fcport->d_id.b.area,
4751 new_fcport->d_id.b.al_pa);
4752 fcport->d_id.b24 = new_fcport->d_id.b24;
4753 break;
4754 }
4755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 fcport->d_id.b24 = new_fcport->d_id.b24;
4757 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 break;
4759 }
4760
Quinn Tran726b8542017-01-19 22:28:00 -08004761 if (found) {
4762 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08004764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04004766 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08004767 list_add_tail(&new_fcport->list, &vha->vp_fcports);
4768
4769 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 /* Allocate a new replacement fcport. */
4773 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004774 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004776 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004777 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 return (QLA_MEMORY_ALLOC_FAILED);
4779 }
4780 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
4781 new_fcport->d_id.b24 = nxt_d_id.b24;
4782 }
4783
Quinn Tran726b8542017-01-19 22:28:00 -08004784 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Quinn Tran726b8542017-01-19 22:28:00 -08004786 /*
4787 * Logout all previous fabric dev marked lost, except FCP2 devices.
4788 */
4789 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4790 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4791 break;
4792
4793 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
4794 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
4795 continue;
4796
4797 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4798 if ((qla_dual_mode_enabled(vha) ||
4799 qla_ini_mode_enabled(vha)) &&
4800 atomic_read(&fcport->state) == FCS_ONLINE) {
4801 qla2x00_mark_device_lost(vha, fcport,
4802 ql2xplogiabsentdevice, 0);
4803 if (fcport->loop_id != FC_NO_LOOP_ID &&
4804 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4805 fcport->port_type != FCT_INITIATOR &&
4806 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004807 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08004808 "%s %d %8phC post del sess\n",
4809 __func__, __LINE__,
4810 fcport->port_name);
4811
4812 qlt_schedule_sess_for_deletion_lock
4813 (fcport);
4814 continue;
4815 }
4816 }
4817 }
4818
4819 if (fcport->scan_state == QLA_FCPORT_FOUND)
4820 qla24xx_fcport_handle_login(vha, fcport);
4821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return (rval);
4823}
4824
4825/*
4826 * qla2x00_find_new_loop_id
4827 * Scan through our port list and find a new usable loop ID.
4828 *
4829 * Input:
4830 * ha: adapter state pointer.
4831 * dev: port structure pointer.
4832 *
4833 * Returns:
4834 * qla2x00 local function return status code.
4835 *
4836 * Context:
4837 * Kernel context.
4838 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07004839int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004840qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
4842 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004843 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07004844 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
4846 rval = QLA_SUCCESS;
4847
Chad Dupuis5f16b332012-08-22 14:21:00 -04004848 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Chad Dupuis5f16b332012-08-22 14:21:00 -04004850 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
4851 LOOPID_MAP_SIZE);
4852 if (dev->loop_id >= LOOPID_MAP_SIZE ||
4853 qla2x00_is_reserved_id(vha, dev->loop_id)) {
4854 dev->loop_id = FC_NO_LOOP_ID;
4855 rval = QLA_FUNCTION_FAILED;
4856 } else
4857 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Chad Dupuis5f16b332012-08-22 14:21:00 -04004859 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Chad Dupuis5f16b332012-08-22 14:21:00 -04004861 if (rval == QLA_SUCCESS)
4862 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
4863 "Assigning new loopid=%x, portid=%x.\n",
4864 dev->loop_id, dev->d_id.b24);
4865 else
4866 ql_log(ql_log_warn, dev->vha, 0x2087,
4867 "No loop_id's available, portid=%x.\n",
4868 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 return (rval);
4871}
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874/*
4875 * qla2x00_fabric_login
4876 * Issue fabric login command.
4877 *
4878 * Input:
4879 * ha = adapter block pointer.
4880 * device = pointer to FC device type structure.
4881 *
4882 * Returns:
4883 * 0 - Login successfully
4884 * 1 - Login failed
4885 * 2 - Initiator device
4886 * 3 - Fatal error
4887 */
4888int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004889qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 uint16_t *next_loopid)
4891{
4892 int rval;
4893 int retry;
4894 uint16_t tmp_loopid;
4895 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004896 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 retry = 0;
4899 tmp_loopid = 0;
4900
4901 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004902 ql_dbg(ql_dbg_disc, vha, 0x2000,
4903 "Trying Fabric Login w/loop id 0x%04x for port "
4904 "%02x%02x%02x.\n",
4905 fcport->loop_id, fcport->d_id.b.domain,
4906 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08004909 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 fcport->d_id.b.domain, fcport->d_id.b.area,
4911 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08004912 if (rval != QLA_SUCCESS) {
4913 return rval;
4914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 if (mb[0] == MBS_PORT_ID_USED) {
4916 /*
4917 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004918 * recommends the driver perform an implicit login with
4919 * the specified ID again. The ID we just used is save
4920 * here so we return with an ID that can be tried by
4921 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 */
4923 retry++;
4924 tmp_loopid = fcport->loop_id;
4925 fcport->loop_id = mb[1];
4926
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004927 ql_dbg(ql_dbg_disc, vha, 0x2001,
4928 "Fabric Login: port in use - next loop "
4929 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004931 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
4934 /*
4935 * Login succeeded.
4936 */
4937 if (retry) {
4938 /* A retry occurred before. */
4939 *next_loopid = tmp_loopid;
4940 } else {
4941 /*
4942 * No retry occurred before. Just increment the
4943 * ID value for next login.
4944 */
4945 *next_loopid = (fcport->loop_id + 1);
4946 }
4947
4948 if (mb[1] & BIT_0) {
4949 fcport->port_type = FCT_INITIATOR;
4950 } else {
4951 fcport->port_type = FCT_TARGET;
4952 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07004953 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
4955 }
4956
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004957 if (mb[10] & BIT_0)
4958 fcport->supported_classes |= FC_COS_CLASS2;
4959 if (mb[10] & BIT_1)
4960 fcport->supported_classes |= FC_COS_CLASS3;
4961
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004962 if (IS_FWI2_CAPABLE(ha)) {
4963 if (mb[10] & BIT_7)
4964 fcport->flags |=
4965 FCF_CONF_COMP_SUPPORTED;
4966 }
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 rval = QLA_SUCCESS;
4969 break;
4970 } else if (mb[0] == MBS_LOOP_ID_USED) {
4971 /*
4972 * Loop ID already used, try next loop ID.
4973 */
4974 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004975 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 if (rval != QLA_SUCCESS) {
4977 /* Ran out of loop IDs to use */
4978 break;
4979 }
4980 } else if (mb[0] == MBS_COMMAND_ERROR) {
4981 /*
4982 * Firmware possibly timed out during login. If NO
4983 * retries are left to do then the device is declared
4984 * dead.
4985 */
4986 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004987 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07004988 fcport->d_id.b.domain, fcport->d_id.b.area,
4989 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004990 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 rval = 1;
4993 break;
4994 } else {
4995 /*
4996 * unrecoverable / not handled error
4997 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004998 ql_dbg(ql_dbg_disc, vha, 0x2002,
4999 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5000 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5001 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5002 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
5004 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005005 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005006 fcport->d_id.b.domain, fcport->d_id.b.area,
5007 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005008 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005009 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 rval = 3;
5012 break;
5013 }
5014 }
5015
5016 return (rval);
5017}
5018
5019/*
5020 * qla2x00_local_device_login
5021 * Issue local device login command.
5022 *
5023 * Input:
5024 * ha = adapter block pointer.
5025 * loop_id = loop id of device to login to.
5026 *
5027 * Returns (Where's the #define!!!!):
5028 * 0 - Login successfully
5029 * 1 - Login failed
5030 * 3 - Fatal error
5031 */
5032int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005033qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
5035 int rval;
5036 uint16_t mb[MAILBOX_REGISTER_COUNT];
5037
5038 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005039 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 if (rval == QLA_SUCCESS) {
5041 /* Interrogate mailbox registers for any errors */
5042 if (mb[0] == MBS_COMMAND_ERROR)
5043 rval = 1;
5044 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5045 /* device not in PCB table */
5046 rval = 3;
5047 }
5048
5049 return (rval);
5050}
5051
5052/*
5053 * qla2x00_loop_resync
5054 * Resync with fibre channel devices.
5055 *
5056 * Input:
5057 * ha = adapter block pointer.
5058 *
5059 * Returns:
5060 * 0 = success
5061 */
5062int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005063qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005065 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005067 struct req_que *req;
5068 struct rsp_que *rsp;
5069
Michael Hernandezd7459522016-12-12 14:40:07 -08005070 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005071 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005073 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5074 if (vha->flags.online) {
5075 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5077 wait_time = 256;
5078 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005079 if (!IS_QLAFX00(vha->hw)) {
5080 /*
5081 * Issue a marker after FW becomes
5082 * ready.
5083 */
5084 qla2x00_marker(vha, req, rsp, 0, 0,
5085 MK_SYNC_ALL);
5086 vha->marker_needed = 0;
5087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
5089 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005090 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005092 if (IS_QLAFX00(vha->hw))
5093 qlafx00_configure_devices(vha);
5094 else
5095 qla2x00_configure_loop(vha);
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005098 } while (!atomic_read(&vha->loop_down_timer) &&
5099 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5100 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5101 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
5104
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005105 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005108 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005109 ql_dbg(ql_dbg_disc, vha, 0x206c,
5110 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
5112 return (rval);
5113}
5114
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005115/*
5116* qla2x00_perform_loop_resync
5117* Description: This function will set the appropriate flags and call
5118* qla2x00_loop_resync. If successful loop will be resynced
5119* Arguments : scsi_qla_host_t pointer
5120* returm : Success or Failure
5121*/
5122
5123int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5124{
5125 int32_t rval = 0;
5126
5127 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5128 /*Configure the flags so that resync happens properly*/
5129 atomic_set(&ha->loop_down_timer, 0);
5130 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5131 atomic_set(&ha->loop_state, LOOP_UP);
5132 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5133 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5134 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5135
5136 rval = qla2x00_loop_resync(ha);
5137 } else
5138 atomic_set(&ha->loop_state, LOOP_DEAD);
5139
5140 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5141 }
5142
5143 return rval;
5144}
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005147qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005148{
5149 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005150 struct scsi_qla_host *vha;
5151 struct qla_hw_data *ha = base_vha->hw;
5152 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005153
Arun Easifeafb7b2010-09-03 14:57:00 -07005154 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005155 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005156 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5157 atomic_inc(&vha->vref_count);
5158 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005159 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005160 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5161 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005162 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005163
Arun Easifeafb7b2010-09-03 14:57:00 -07005164 spin_lock_irqsave(&ha->vport_slock, flags);
5165 }
5166 }
5167 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005168 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005169 }
5170 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005171}
5172
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005173/* Assumes idc_lock always held on entry */
5174void
5175qla83xx_reset_ownership(scsi_qla_host_t *vha)
5176{
5177 struct qla_hw_data *ha = vha->hw;
5178 uint32_t drv_presence, drv_presence_mask;
5179 uint32_t dev_part_info1, dev_part_info2, class_type;
5180 uint32_t class_type_mask = 0x3;
5181 uint16_t fcoe_other_function = 0xffff, i;
5182
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005183 if (IS_QLA8044(ha)) {
5184 drv_presence = qla8044_rd_direct(vha,
5185 QLA8044_CRB_DRV_ACTIVE_INDEX);
5186 dev_part_info1 = qla8044_rd_direct(vha,
5187 QLA8044_CRB_DEV_PART_INFO_INDEX);
5188 dev_part_info2 = qla8044_rd_direct(vha,
5189 QLA8044_CRB_DEV_PART_INFO2);
5190 } else {
5191 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5192 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5193 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5194 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005195 for (i = 0; i < 8; i++) {
5196 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5197 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5198 (i != ha->portnum)) {
5199 fcoe_other_function = i;
5200 break;
5201 }
5202 }
5203 if (fcoe_other_function == 0xffff) {
5204 for (i = 0; i < 8; i++) {
5205 class_type = ((dev_part_info2 >> (i * 4)) &
5206 class_type_mask);
5207 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5208 ((i + 8) != ha->portnum)) {
5209 fcoe_other_function = i + 8;
5210 break;
5211 }
5212 }
5213 }
5214 /*
5215 * Prepare drv-presence mask based on fcoe functions present.
5216 * However consider only valid physical fcoe function numbers (0-15).
5217 */
5218 drv_presence_mask = ~((1 << (ha->portnum)) |
5219 ((fcoe_other_function == 0xffff) ?
5220 0 : (1 << (fcoe_other_function))));
5221
5222 /* We are the reset owner iff:
5223 * - No other protocol drivers present.
5224 * - This is the lowest among fcoe functions. */
5225 if (!(drv_presence & drv_presence_mask) &&
5226 (ha->portnum < fcoe_other_function)) {
5227 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5228 "This host is Reset owner.\n");
5229 ha->flags.nic_core_reset_owner = 1;
5230 }
5231}
5232
Saurav Kashyapfa492632012-11-21 02:40:29 -05005233static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005234__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5235{
5236 int rval = QLA_SUCCESS;
5237 struct qla_hw_data *ha = vha->hw;
5238 uint32_t drv_ack;
5239
5240 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5241 if (rval == QLA_SUCCESS) {
5242 drv_ack |= (1 << ha->portnum);
5243 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5244 }
5245
5246 return rval;
5247}
5248
Saurav Kashyapfa492632012-11-21 02:40:29 -05005249static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005250__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5251{
5252 int rval = QLA_SUCCESS;
5253 struct qla_hw_data *ha = vha->hw;
5254 uint32_t drv_ack;
5255
5256 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5257 if (rval == QLA_SUCCESS) {
5258 drv_ack &= ~(1 << ha->portnum);
5259 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5260 }
5261
5262 return rval;
5263}
5264
Saurav Kashyapfa492632012-11-21 02:40:29 -05005265static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005266qla83xx_dev_state_to_string(uint32_t dev_state)
5267{
5268 switch (dev_state) {
5269 case QLA8XXX_DEV_COLD:
5270 return "COLD/RE-INIT";
5271 case QLA8XXX_DEV_INITIALIZING:
5272 return "INITIALIZING";
5273 case QLA8XXX_DEV_READY:
5274 return "READY";
5275 case QLA8XXX_DEV_NEED_RESET:
5276 return "NEED RESET";
5277 case QLA8XXX_DEV_NEED_QUIESCENT:
5278 return "NEED QUIESCENT";
5279 case QLA8XXX_DEV_FAILED:
5280 return "FAILED";
5281 case QLA8XXX_DEV_QUIESCENT:
5282 return "QUIESCENT";
5283 default:
5284 return "Unknown";
5285 }
5286}
5287
5288/* Assumes idc-lock always held on entry */
5289void
5290qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
5291{
5292 struct qla_hw_data *ha = vha->hw;
5293 uint32_t idc_audit_reg = 0, duration_secs = 0;
5294
5295 switch (audit_type) {
5296 case IDC_AUDIT_TIMESTAMP:
5297 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
5298 idc_audit_reg = (ha->portnum) |
5299 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
5300 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
5301 break;
5302
5303 case IDC_AUDIT_COMPLETION:
5304 duration_secs = ((jiffies_to_msecs(jiffies) -
5305 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
5306 idc_audit_reg = (ha->portnum) |
5307 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
5308 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
5309 break;
5310
5311 default:
5312 ql_log(ql_log_warn, vha, 0xb078,
5313 "Invalid audit type specified.\n");
5314 break;
5315 }
5316}
5317
5318/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05005319static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005320qla83xx_initiating_reset(scsi_qla_host_t *vha)
5321{
5322 struct qla_hw_data *ha = vha->hw;
5323 uint32_t idc_control, dev_state;
5324
5325 __qla83xx_get_idc_control(vha, &idc_control);
5326 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
5327 ql_log(ql_log_info, vha, 0xb080,
5328 "NIC Core reset has been disabled. idc-control=0x%x\n",
5329 idc_control);
5330 return QLA_FUNCTION_FAILED;
5331 }
5332
5333 /* Set NEED-RESET iff in READY state and we are the reset-owner */
5334 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5335 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
5336 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
5337 QLA8XXX_DEV_NEED_RESET);
5338 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
5339 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
5340 } else {
5341 const char *state = qla83xx_dev_state_to_string(dev_state);
5342 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
5343
5344 /* SV: XXX: Is timeout required here? */
5345 /* Wait for IDC state change READY -> NEED_RESET */
5346 while (dev_state == QLA8XXX_DEV_READY) {
5347 qla83xx_idc_unlock(vha, 0);
5348 msleep(200);
5349 qla83xx_idc_lock(vha, 0);
5350 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5351 }
5352 }
5353
5354 /* Send IDC ack by writing to drv-ack register */
5355 __qla83xx_set_drv_ack(vha);
5356
5357 return QLA_SUCCESS;
5358}
5359
5360int
5361__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
5362{
5363 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
5364}
5365
5366int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005367__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
5368{
5369 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
5370}
5371
Saurav Kashyapfa492632012-11-21 02:40:29 -05005372static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005373qla83xx_check_driver_presence(scsi_qla_host_t *vha)
5374{
5375 uint32_t drv_presence = 0;
5376 struct qla_hw_data *ha = vha->hw;
5377
5378 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5379 if (drv_presence & (1 << ha->portnum))
5380 return QLA_SUCCESS;
5381 else
5382 return QLA_TEST_FAILED;
5383}
5384
5385int
5386qla83xx_nic_core_reset(scsi_qla_host_t *vha)
5387{
5388 int rval = QLA_SUCCESS;
5389 struct qla_hw_data *ha = vha->hw;
5390
5391 ql_dbg(ql_dbg_p3p, vha, 0xb058,
5392 "Entered %s().\n", __func__);
5393
5394 if (vha->device_flags & DFLG_DEV_FAILED) {
5395 ql_log(ql_log_warn, vha, 0xb059,
5396 "Device in unrecoverable FAILED state.\n");
5397 return QLA_FUNCTION_FAILED;
5398 }
5399
5400 qla83xx_idc_lock(vha, 0);
5401
5402 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
5403 ql_log(ql_log_warn, vha, 0xb05a,
5404 "Function=0x%x has been removed from IDC participation.\n",
5405 ha->portnum);
5406 rval = QLA_FUNCTION_FAILED;
5407 goto exit;
5408 }
5409
5410 qla83xx_reset_ownership(vha);
5411
5412 rval = qla83xx_initiating_reset(vha);
5413
5414 /*
5415 * Perform reset if we are the reset-owner,
5416 * else wait till IDC state changes to READY/FAILED.
5417 */
5418 if (rval == QLA_SUCCESS) {
5419 rval = qla83xx_idc_state_handler(vha);
5420
5421 if (rval == QLA_SUCCESS)
5422 ha->flags.nic_core_hung = 0;
5423 __qla83xx_clear_drv_ack(vha);
5424 }
5425
5426exit:
5427 qla83xx_idc_unlock(vha, 0);
5428
5429 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
5430
5431 return rval;
5432}
5433
Saurav Kashyap81178772012-08-22 14:21:04 -04005434int
5435qla2xxx_mctp_dump(scsi_qla_host_t *vha)
5436{
5437 struct qla_hw_data *ha = vha->hw;
5438 int rval = QLA_FUNCTION_FAILED;
5439
5440 if (!IS_MCTP_CAPABLE(ha)) {
5441 /* This message can be removed from the final version */
5442 ql_log(ql_log_info, vha, 0x506d,
5443 "This board is not MCTP capable\n");
5444 return rval;
5445 }
5446
5447 if (!ha->mctp_dump) {
5448 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
5449 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
5450
5451 if (!ha->mctp_dump) {
5452 ql_log(ql_log_warn, vha, 0x506e,
5453 "Failed to allocate memory for mctp dump\n");
5454 return rval;
5455 }
5456 }
5457
5458#define MCTP_DUMP_STR_ADDR 0x00000000
5459 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
5460 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
5461 if (rval != QLA_SUCCESS) {
5462 ql_log(ql_log_warn, vha, 0x506f,
5463 "Failed to capture mctp dump\n");
5464 } else {
5465 ql_log(ql_log_info, vha, 0x5070,
5466 "Mctp dump capture for host (%ld/%p).\n",
5467 vha->host_no, ha->mctp_dump);
5468 ha->mctp_dumped = 1;
5469 }
5470
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04005471 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04005472 ha->flags.nic_core_reset_hdlr_active = 1;
5473 rval = qla83xx_restart_nic_firmware(vha);
5474 if (rval)
5475 /* NIC Core reset failed. */
5476 ql_log(ql_log_warn, vha, 0x5071,
5477 "Failed to restart nic firmware\n");
5478 else
5479 ql_dbg(ql_dbg_p3p, vha, 0xb084,
5480 "Restarted NIC firmware successfully.\n");
5481 ha->flags.nic_core_reset_hdlr_active = 0;
5482 }
5483
5484 return rval;
5485
5486}
5487
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005488/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005489* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005490* Description: This function will block the new I/Os
5491* Its not aborting any I/Os as context
5492* is not destroyed during quiescence
5493* Arguments: scsi_qla_host_t
5494* return : void
5495*/
5496void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005497qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005498{
5499 struct qla_hw_data *ha = vha->hw;
5500 struct scsi_qla_host *vp;
5501
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005502 ql_dbg(ql_dbg_dpc, vha, 0x401d,
5503 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005504
5505 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
5506 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
5507 atomic_set(&vha->loop_state, LOOP_DOWN);
5508 qla2x00_mark_all_devices_lost(vha, 0);
5509 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005510 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005511 } else {
5512 if (!atomic_read(&vha->loop_down_timer))
5513 atomic_set(&vha->loop_down_timer,
5514 LOOP_DOWN_TIME);
5515 }
5516 /* Wait for pending cmds to complete */
5517 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
5518}
5519
Giridhar Malavalia9083012010-04-12 17:59:55 -07005520void
5521qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
5522{
5523 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005524 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07005525 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08005526 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005527 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005528
Saurav Kashyape46ef002011-02-23 15:27:16 -08005529 /* For ISP82XX, driver waits for completion of the commands.
5530 * online flag should be set.
5531 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005532 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08005533 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005534 ha->flags.chip_reset_done = 0;
5535 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04005536 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005537
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005538 ql_log(ql_log_info, vha, 0x00af,
5539 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07005540
Saurav Kashyape46ef002011-02-23 15:27:16 -08005541 /* For ISP82XX, reset_chip is just disabling interrupts.
5542 * Driver waits for the completion of the commands.
5543 * the interrupts need to be enabled.
5544 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005545 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07005546 ha->isp_ops->reset_chip(vha);
5547
Quinn Tranec7193e2017-03-15 09:48:55 -07005548 ha->flags.n2n_ae = 0;
5549 ha->flags.lip_ae = 0;
5550 ha->current_topology = 0;
5551 ha->flags.fw_started = 0;
5552 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005553 ha->base_qpair->chip_reset++;
5554 for (i = 0; i < ha->max_qpairs; i++) {
5555 if (ha->queue_pair_map[i])
5556 ha->queue_pair_map[i]->chip_reset =
5557 ha->base_qpair->chip_reset;
5558 }
Quinn Tran726b8542017-01-19 22:28:00 -08005559
Giridhar Malavalia9083012010-04-12 17:59:55 -07005560 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
5561 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
5562 atomic_set(&vha->loop_state, LOOP_DOWN);
5563 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07005564
5565 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005566 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07005567 atomic_inc(&vp->vref_count);
5568 spin_unlock_irqrestore(&ha->vport_slock, flags);
5569
Giridhar Malavalia9083012010-04-12 17:59:55 -07005570 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07005571
5572 spin_lock_irqsave(&ha->vport_slock, flags);
5573 atomic_dec(&vp->vref_count);
5574 }
5575 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07005576 } else {
5577 if (!atomic_read(&vha->loop_down_timer))
5578 atomic_set(&vha->loop_down_timer,
5579 LOOP_DOWN_TIME);
5580 }
5581
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08005582 /* Clear all async request states across all VPs. */
5583 list_for_each_entry(fcport, &vha->vp_fcports, list)
5584 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5585 spin_lock_irqsave(&ha->vport_slock, flags);
5586 list_for_each_entry(vp, &ha->vp_list, list) {
5587 atomic_inc(&vp->vref_count);
5588 spin_unlock_irqrestore(&ha->vport_slock, flags);
5589
5590 list_for_each_entry(fcport, &vp->vp_fcports, list)
5591 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5592
5593 spin_lock_irqsave(&ha->vport_slock, flags);
5594 atomic_dec(&vp->vref_count);
5595 }
5596 spin_unlock_irqrestore(&ha->vport_slock, flags);
5597
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07005598 if (!ha->flags.eeh_busy) {
5599 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005600 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08005601 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005602 ql_log(ql_log_info, vha, 0x00b4,
5603 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07005604
Saurav Kashyape46ef002011-02-23 15:27:16 -08005605 /* Done waiting for pending commands.
5606 * Reset the online flag.
5607 */
5608 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07005611 /* Requeue all commands in outstanding command list. */
5612 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
5613 }
Arun Easib6a029e2014-09-25 06:14:52 -04005614 /* memory barrier */
5615 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
5618/*
5619* qla2x00_abort_isp
5620* Resets ISP and aborts all outstanding commands.
5621*
5622* Input:
5623* ha = adapter block pointer.
5624*
5625* Returns:
5626* 0 = success
5627*/
5628int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005629qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
Andrew Vasquez476e8972006-08-23 14:54:55 -07005631 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005633 struct qla_hw_data *ha = vha->hw;
5634 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005635 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07005636 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005638 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07005639 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
Santosh Vernekara61712972012-08-22 14:21:13 -04005641 if (IS_QLA8031(ha)) {
5642 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
5643 "Clearing fcoe driver presence.\n");
5644 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
5645 ql_dbg(ql_dbg_p3p, vha, 0xb073,
5646 "Error while clearing DRV-Presence.\n");
5647 }
5648
Andrew Vasquez85880802009-12-15 21:29:46 -08005649 if (unlikely(pci_channel_offline(ha->pdev) &&
5650 ha->flags.pci_channel_io_perm_failure)) {
5651 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5652 status = 0;
5653 return status;
5654 }
5655
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005656 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08005657
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005658 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005660 if (!qla2x00_restart_isp(vha)) {
5661 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005663 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 /*
5665 * Issue marker command only when we are going
5666 * to start the I/O .
5667 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005668 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
5670
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005671 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
Andrew Vasquezfd34f552007-07-19 15:06:00 -07005673 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07005675 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005676 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07005677
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08005678 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
5679 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08005680 if (ha->fce) {
5681 ha->flags.fce_enabled = 1;
5682 memset(ha->fce, 0,
5683 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005684 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08005685 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
5686 &ha->fce_bufs);
5687 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005688 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08005689 "Unable to reinitialize FCE "
5690 "(%d).\n", rval);
5691 ha->flags.fce_enabled = 0;
5692 }
5693 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07005694
5695 if (ha->eft) {
5696 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005697 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07005698 ha->eft_dma, EFT_NUM_BUFFERS);
5699 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005700 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07005701 "Unable to reinitialize EFT "
5702 "(%d).\n", rval);
5703 }
5704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005706 vha->flags.online = 1;
5707 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005709 ql_log(ql_log_fatal, vha, 0x8035,
5710 "ISP error recover failed - "
5711 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07005712 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 * The next call disables the board
5714 * completely.
5715 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005716 ha->isp_ops->reset_adapter(vha);
5717 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005719 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 status = 0;
5721 } else { /* schedule another ISP abort */
5722 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005723 ql_dbg(ql_dbg_taskm, vha, 0x8020,
5724 "ISP abort - retry remaining %d.\n",
5725 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 status = 1;
5727 }
5728 } else {
5729 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005730 ql_dbg(ql_dbg_taskm, vha, 0x8021,
5731 "ISP error recovery - retrying (%d) "
5732 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005733 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 status = 1;
5735 }
5736 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07005737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 }
5739
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005740 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005741 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Arun Easifeafb7b2010-09-03 14:57:00 -07005742
5743 spin_lock_irqsave(&ha->vport_slock, flags);
5744 list_for_each_entry(vp, &ha->vp_list, list) {
5745 if (vp->vp_idx) {
5746 atomic_inc(&vp->vref_count);
5747 spin_unlock_irqrestore(&ha->vport_slock, flags);
5748
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005749 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07005750
5751 spin_lock_irqsave(&ha->vport_slock, flags);
5752 atomic_dec(&vp->vref_count);
5753 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005754 }
Arun Easifeafb7b2010-09-03 14:57:00 -07005755 spin_unlock_irqrestore(&ha->vport_slock, flags);
5756
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005757 if (IS_QLA8031(ha)) {
5758 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
5759 "Setting back fcoe driver presence.\n");
5760 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
5761 ql_dbg(ql_dbg_p3p, vha, 0xb074,
5762 "Error while setting DRV-Presence.\n");
5763 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005764 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08005765 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
5766 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 }
5768
5769 return(status);
5770}
5771
5772/*
5773* qla2x00_restart_isp
5774* restarts the ISP after a reset
5775*
5776* Input:
5777* ha = adapter block pointer.
5778*
5779* Returns:
5780* 0 = success
5781*/
5782static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005783qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08005785 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005786 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005787 struct req_que *req = ha->req_q_map[0];
5788 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
5790 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005791 if (qla2x00_isp_firmware(vha)) {
5792 vha->flags.online = 0;
5793 status = ha->isp_ops->chip_diag(vha);
5794 if (!status)
5795 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 }
5797
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005798 if (!status && !(status = qla2x00_init_rings(vha))) {
5799 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07005800 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04005801
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005802 /* Initialize the queues in use */
5803 qla25xx_init_queues(ha);
5804
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005805 status = qla2x00_fw_ready(vha);
5806 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005807 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005808 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04005809 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 }
5811
5812 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005813 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 }
5816 return (status);
5817}
5818
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005819static int
5820qla25xx_init_queues(struct qla_hw_data *ha)
5821{
5822 struct rsp_que *rsp = NULL;
5823 struct req_que *req = NULL;
5824 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
5825 int ret = -1;
5826 int i;
5827
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07005828 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005829 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05005830 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005831 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08005832 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005833 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005834 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
5835 "%s Rsp que: %d init failed.\n",
5836 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005837 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005838 ql_dbg(ql_dbg_init, base_vha, 0x0100,
5839 "%s Rsp que: %d inited.\n",
5840 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005841 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07005842 }
5843 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005844 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05005845 if (req && test_bit(i, ha->req_qid_map)) {
5846 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005847 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08005848 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005849 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005850 ql_dbg(ql_dbg_init, base_vha, 0x0101,
5851 "%s Req que: %d init failed.\n",
5852 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005853 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005854 ql_dbg(ql_dbg_init, base_vha, 0x0102,
5855 "%s Req que: %d inited.\n",
5856 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005857 }
5858 }
5859 return ret;
5860}
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862/*
5863* qla2x00_reset_adapter
5864* Reset adapter.
5865*
5866* Input:
5867* ha = adapter block pointer.
5868*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07005869void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005870qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871{
5872 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005873 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07005874 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005876 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07005877 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 spin_lock_irqsave(&ha->hardware_lock, flags);
5880 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
5881 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
5882 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
5883 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
5884 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5885}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005886
5887void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005888qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005889{
5890 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005891 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005892 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
5893
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005894 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07005895 return;
5896
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005897 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07005898 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005899
5900 spin_lock_irqsave(&ha->hardware_lock, flags);
5901 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
5902 RD_REG_DWORD(&reg->hccr);
5903 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
5904 RD_REG_DWORD(&reg->hccr);
5905 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08005906
5907 if (IS_NOPOLLING_TYPE(ha))
5908 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005909}
5910
David Miller4e08df32007-04-16 12:37:43 -07005911/* On sparc systems, obtain port and node WWN from firmware
5912 * properties.
5913 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005914static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
5915 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07005916{
5917#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005918 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07005919 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07005920 struct device_node *dp = pci_device_to_OF_node(pdev);
5921 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07005922 int len;
5923
5924 val = of_get_property(dp, "port-wwn", &len);
5925 if (val && len >= WWN_SIZE)
5926 memcpy(nv->port_name, val, WWN_SIZE);
5927
5928 val = of_get_property(dp, "node-wwn", &len);
5929 if (val && len >= WWN_SIZE)
5930 memcpy(nv->node_name, val, WWN_SIZE);
5931#endif
5932}
5933
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005934int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005935qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005936{
David Miller4e08df32007-04-16 12:37:43 -07005937 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005938 struct init_cb_24xx *icb;
5939 struct nvram_24xx *nv;
5940 uint32_t *dptr;
5941 uint8_t *dptr1, *dptr2;
5942 uint32_t chksum;
5943 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005944 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005945
David Miller4e08df32007-04-16 12:37:43 -07005946 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005947 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07005948 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005949
5950 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05005951 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07005952 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
5953 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
5954 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005955 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08005956 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
5957 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05005958
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07005959 ha->nvram_size = sizeof(struct nvram_24xx);
5960 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005961
Seokmann Ju281afe12007-07-26 13:43:34 -07005962 /* Get VPD data into cache */
5963 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005964 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07005965 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
5966
5967 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005968 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005969 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005970 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05005971 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
5972 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005973
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005974 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
5975 "Contents of NVRAM\n");
5976 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
5977 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005978
5979 /* Bad NVRAM data, set defaults parameters. */
5980 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
5981 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07005982 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005983 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005984 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04005985 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005986 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
5987 ql_log(ql_log_warn, vha, 0x006c,
5988 "Falling back to functioning (yet invalid -- WWPN) "
5989 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07005990
5991 /*
5992 * Set default initialization control block.
5993 */
5994 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07005995 nv->nvram_version = cpu_to_le16(ICB_VERSION);
5996 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04005997 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07005998 nv->execution_throttle = cpu_to_le16(0xFFFF);
5999 nv->exchange_count = cpu_to_le16(0);
6000 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006001 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006002 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006003 nv->port_name[2] = 0x00;
6004 nv->port_name[3] = 0xe0;
6005 nv->port_name[4] = 0x8b;
6006 nv->port_name[5] = 0x1c;
6007 nv->port_name[6] = 0x55;
6008 nv->port_name[7] = 0x86;
6009 nv->node_name[0] = 0x20;
6010 nv->node_name[1] = 0x00;
6011 nv->node_name[2] = 0x00;
6012 nv->node_name[3] = 0xe0;
6013 nv->node_name[4] = 0x8b;
6014 nv->node_name[5] = 0x1c;
6015 nv->node_name[6] = 0x55;
6016 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006017 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006018 nv->login_retry_count = cpu_to_le16(8);
6019 nv->interrupt_delay_timer = cpu_to_le16(0);
6020 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006021 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006022 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6023 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6024 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6025 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6026 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6027 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006028 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006029 nv->max_luns_per_target = cpu_to_le16(128);
6030 nv->port_down_retry_count = cpu_to_le16(30);
6031 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006032
6033 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006034 }
6035
Quinn Tran726b8542017-01-19 22:28:00 -08006036 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006037 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006038 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006039 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006040 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006041 }
6042
6043 qlt_24xx_config_nvram_stage1(vha, nv);
6044
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006045 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006046 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006047
6048 /* Copy 1st segment. */
6049 dptr1 = (uint8_t *)icb;
6050 dptr2 = (uint8_t *)&nv->version;
6051 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6052 while (cnt--)
6053 *dptr1++ = *dptr2++;
6054
6055 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006056 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006057
6058 /* Copy 2nd segment. */
6059 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6060 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6061 cnt = (uint8_t *)&icb->reserved_3 -
6062 (uint8_t *)&icb->interrupt_delay_timer;
6063 while (cnt--)
6064 *dptr1++ = *dptr2++;
6065
6066 /*
6067 * Setup driver NVRAM options.
6068 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006069 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006070 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006071
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006072 qlt_24xx_config_nvram_stage2(vha, icb);
6073
Bart Van Asschead950362015-07-09 07:24:08 -07006074 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006075 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006076 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6077 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6078 }
6079
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006080 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006081 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006082 /*
6083 * Firmware will apply the following mask if the nodename was
6084 * not provided.
6085 */
6086 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6087 icb->node_name[0] &= 0xF0;
6088 }
6089
6090 /* Set host adapter parameters. */
6091 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006092 ha->flags.enable_lip_reset = 0;
6093 ha->flags.enable_lip_full_login =
6094 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6095 ha->flags.enable_target_reset =
6096 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006097 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006098 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006099
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006100 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6101 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006102
6103 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6104 sizeof(ha->fw_seriallink_options24));
6105
6106 /* save HBA serial number */
6107 ha->serial0 = icb->port_name[5];
6108 ha->serial1 = icb->port_name[6];
6109 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006110 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6111 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006112
Bart Van Asschead950362015-07-09 07:24:08 -07006113 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006114
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006115 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6116
6117 /* Set minimum login_timeout to 4 seconds. */
6118 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6119 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6120 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006121 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006122 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006123
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006124 /* Set minimum RATOV to 100 tenths of a second. */
6125 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006126
6127 ha->loop_reset_delay = nv->reset_delay;
6128
6129 /* Link Down Timeout = 0:
6130 *
6131 * When Port Down timer expires we will start returning
6132 * I/O's to OS with "DID_NO_CONNECT".
6133 *
6134 * Link Down Timeout != 0:
6135 *
6136 * The driver waits for the link to come up after link down
6137 * before returning I/Os to OS with "DID_NO_CONNECT".
6138 */
6139 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6140 ha->loop_down_abort_time =
6141 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6142 } else {
6143 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6144 ha->loop_down_abort_time =
6145 (LOOP_DOWN_TIME - ha->link_down_timeout);
6146 }
6147
6148 /* Need enough time to try and get the port back. */
6149 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6150 if (qlport_down_retry)
6151 ha->port_down_retry_count = qlport_down_retry;
6152
6153 /* Set login_retry_count */
6154 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6155 if (ha->port_down_retry_count ==
6156 le16_to_cpu(nv->port_down_retry_count) &&
6157 ha->port_down_retry_count > 3)
6158 ha->login_retry_count = ha->port_down_retry_count;
6159 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6160 ha->login_retry_count = ha->port_down_retry_count;
6161 if (ql2xloginretrycount)
6162 ha->login_retry_count = ql2xloginretrycount;
6163
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006164 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006165 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006166 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6167 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6168 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6169 le16_to_cpu(icb->interrupt_delay_timer): 2;
6170 }
Bart Van Asschead950362015-07-09 07:24:08 -07006171 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006172 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006173 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006174 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006175 ha->zio_mode = QLA_ZIO_MODE_6;
6176
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006177 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006178 "ZIO mode %d enabled; timer delay (%d us).\n",
6179 ha->zio_mode, ha->zio_timer * 100);
6180
6181 icb->firmware_options_2 |= cpu_to_le32(
6182 (uint32_t)ha->zio_mode);
6183 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006184 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006185 }
6186
David Miller4e08df32007-04-16 12:37:43 -07006187 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006188 ql_log(ql_log_warn, vha, 0x0070,
6189 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006190 }
6191 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006192}
6193
Sawan Chandak4243c112016-01-27 12:03:31 -05006194uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6195{
6196 struct qla27xx_image_status pri_image_status, sec_image_status;
6197 uint8_t valid_pri_image, valid_sec_image;
6198 uint32_t *wptr;
6199 uint32_t cnt, chksum, size;
6200 struct qla_hw_data *ha = vha->hw;
6201
6202 valid_pri_image = valid_sec_image = 1;
6203 ha->active_image = 0;
6204 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6205
6206 if (!ha->flt_region_img_status_pri) {
6207 valid_pri_image = 0;
6208 goto check_sec_image;
6209 }
6210
6211 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6212 ha->flt_region_img_status_pri, size);
6213
6214 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6215 ql_dbg(ql_dbg_init, vha, 0x018b,
6216 "Primary image signature (0x%x) not valid\n",
6217 pri_image_status.signature);
6218 valid_pri_image = 0;
6219 goto check_sec_image;
6220 }
6221
6222 wptr = (uint32_t *)(&pri_image_status);
6223 cnt = size;
6224
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006225 for (chksum = 0; cnt--; wptr++)
6226 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006227
Sawan Chandak4243c112016-01-27 12:03:31 -05006228 if (chksum) {
6229 ql_dbg(ql_dbg_init, vha, 0x018c,
6230 "Checksum validation failed for primary image (0x%x)\n",
6231 chksum);
6232 valid_pri_image = 0;
6233 }
6234
6235check_sec_image:
6236 if (!ha->flt_region_img_status_sec) {
6237 valid_sec_image = 0;
6238 goto check_valid_image;
6239 }
6240
6241 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6242 ha->flt_region_img_status_sec, size);
6243
6244 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6245 ql_dbg(ql_dbg_init, vha, 0x018d,
6246 "Secondary image signature(0x%x) not valid\n",
6247 sec_image_status.signature);
6248 valid_sec_image = 0;
6249 goto check_valid_image;
6250 }
6251
6252 wptr = (uint32_t *)(&sec_image_status);
6253 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006254 for (chksum = 0; cnt--; wptr++)
6255 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05006256 if (chksum) {
6257 ql_dbg(ql_dbg_init, vha, 0x018e,
6258 "Checksum validation failed for secondary image (0x%x)\n",
6259 chksum);
6260 valid_sec_image = 0;
6261 }
6262
6263check_valid_image:
6264 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
6265 ha->active_image = QLA27XX_PRIMARY_IMAGE;
6266 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
6267 if (!ha->active_image ||
6268 pri_image_status.generation_number <
6269 sec_image_status.generation_number)
6270 ha->active_image = QLA27XX_SECONDARY_IMAGE;
6271 }
6272
6273 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
6274 ha->active_image == 0 ? "default bootld and fw" :
6275 ha->active_image == 1 ? "primary" :
6276 ha->active_image == 2 ? "secondary" :
6277 "Invalid");
6278
6279 return ha->active_image;
6280}
6281
Adrian Bunk413975a2006-06-30 02:33:06 -07006282static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006283qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
6284 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006285{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006286 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006287 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006288 uint32_t *dcode, dlen;
6289 uint32_t risc_addr;
6290 uint32_t risc_size;
6291 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006292 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006293 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006294
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006295 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08006296 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006297
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006298 rval = QLA_SUCCESS;
6299
6300 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006301 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006302 *srisc_addr = 0;
6303
Sawan Chandak4243c112016-01-27 12:03:31 -05006304 if (IS_QLA27XX(ha) &&
6305 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
6306 faddr = ha->flt_region_fw_sec;
6307
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006308 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006309 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006310 for (i = 0; i < 4; i++)
6311 dcode[i] = be32_to_cpu(dcode[i]);
6312 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
6313 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
6314 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
6315 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006316 ql_log(ql_log_fatal, vha, 0x008c,
6317 "Unable to verify the integrity of flash firmware "
6318 "image.\n");
6319 ql_log(ql_log_fatal, vha, 0x008d,
6320 "Firmware data: %08x %08x %08x %08x.\n",
6321 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006322
6323 return QLA_FUNCTION_FAILED;
6324 }
6325
6326 while (segments && rval == QLA_SUCCESS) {
6327 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006328 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006329
6330 risc_addr = be32_to_cpu(dcode[2]);
6331 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
6332 risc_size = be32_to_cpu(dcode[3]);
6333
6334 fragment = 0;
6335 while (risc_size > 0 && rval == QLA_SUCCESS) {
6336 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
6337 if (dlen > risc_size)
6338 dlen = risc_size;
6339
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006340 ql_dbg(ql_dbg_init, vha, 0x008e,
6341 "Loading risc segment@ risc addr %x "
6342 "number of dwords 0x%x offset 0x%x.\n",
6343 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006344
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006345 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006346 for (i = 0; i < dlen; i++)
6347 dcode[i] = swab32(dcode[i]);
6348
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006349 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006350 dlen);
6351 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006352 ql_log(ql_log_fatal, vha, 0x008f,
6353 "Failed to load segment %d of firmware.\n",
6354 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04006355 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006356 }
6357
6358 faddr += dlen;
6359 risc_addr += dlen;
6360 risc_size -= dlen;
6361 fragment++;
6362 }
6363
6364 /* Next segment. */
6365 segments--;
6366 }
6367
Chad Dupuisf73cb692014-02-26 04:15:06 -05006368 if (!IS_QLA27XX(ha))
6369 return rval;
6370
6371 if (ha->fw_dump_template)
6372 vfree(ha->fw_dump_template);
6373 ha->fw_dump_template = NULL;
6374 ha->fw_dump_template_len = 0;
6375
6376 ql_dbg(ql_dbg_init, vha, 0x0161,
6377 "Loading fwdump template from %x\n", faddr);
6378 qla24xx_read_flash_data(vha, dcode, faddr, 7);
6379 risc_size = be32_to_cpu(dcode[2]);
6380 ql_dbg(ql_dbg_init, vha, 0x0162,
6381 "-> array size %x dwords\n", risc_size);
6382 if (risc_size == 0 || risc_size == ~0)
6383 goto default_template;
6384
6385 dlen = (risc_size - 8) * sizeof(*dcode);
6386 ql_dbg(ql_dbg_init, vha, 0x0163,
6387 "-> template allocating %x bytes...\n", dlen);
6388 ha->fw_dump_template = vmalloc(dlen);
6389 if (!ha->fw_dump_template) {
6390 ql_log(ql_log_warn, vha, 0x0164,
6391 "Failed fwdump template allocate %x bytes.\n", risc_size);
6392 goto default_template;
6393 }
6394
6395 faddr += 7;
6396 risc_size -= 8;
6397 dcode = ha->fw_dump_template;
6398 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
6399 for (i = 0; i < risc_size; i++)
6400 dcode[i] = le32_to_cpu(dcode[i]);
6401
6402 if (!qla27xx_fwdt_template_valid(dcode)) {
6403 ql_log(ql_log_warn, vha, 0x0165,
6404 "Failed fwdump template validate\n");
6405 goto default_template;
6406 }
6407
6408 dlen = qla27xx_fwdt_template_size(dcode);
6409 ql_dbg(ql_dbg_init, vha, 0x0166,
6410 "-> template size %x bytes\n", dlen);
6411 if (dlen > risc_size * sizeof(*dcode)) {
6412 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07006413 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07006414 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006415 goto default_template;
6416 }
6417 ha->fw_dump_template_len = dlen;
6418 return rval;
6419
6420default_template:
6421 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
6422 if (ha->fw_dump_template)
6423 vfree(ha->fw_dump_template);
6424 ha->fw_dump_template = NULL;
6425 ha->fw_dump_template_len = 0;
6426
6427 dlen = qla27xx_fwdt_template_default_size();
6428 ql_dbg(ql_dbg_init, vha, 0x0169,
6429 "-> template allocating %x bytes...\n", dlen);
6430 ha->fw_dump_template = vmalloc(dlen);
6431 if (!ha->fw_dump_template) {
6432 ql_log(ql_log_warn, vha, 0x016a,
6433 "Failed fwdump template allocate %x bytes.\n", risc_size);
6434 goto failed_template;
6435 }
6436
6437 dcode = ha->fw_dump_template;
6438 risc_size = dlen / sizeof(*dcode);
6439 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
6440 for (i = 0; i < risc_size; i++)
6441 dcode[i] = be32_to_cpu(dcode[i]);
6442
6443 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
6444 ql_log(ql_log_warn, vha, 0x016b,
6445 "Failed fwdump template validate\n");
6446 goto failed_template;
6447 }
6448
6449 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
6450 ql_dbg(ql_dbg_init, vha, 0x016c,
6451 "-> template size %x bytes\n", dlen);
6452 ha->fw_dump_template_len = dlen;
6453 return rval;
6454
6455failed_template:
6456 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
6457 if (ha->fw_dump_template)
6458 vfree(ha->fw_dump_template);
6459 ha->fw_dump_template = NULL;
6460 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006461 return rval;
6462}
6463
Giridhar Malavalie9454a82013-02-08 01:57:47 -05006464#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006465
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006466int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006467qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08006468{
6469 int rval;
6470 int i, fragment;
6471 uint16_t *wcode, *fwcode;
6472 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
6473 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006474 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006475 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08006476
6477 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006478 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08006479 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006480 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01006481 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006482 ql_log(ql_log_info, vha, 0x0084,
6483 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08006484 return QLA_FUNCTION_FAILED;
6485 }
6486
6487 rval = QLA_SUCCESS;
6488
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006489 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08006490 *srisc_addr = 0;
6491 fwcode = (uint16_t *)blob->fw->data;
6492 fwclen = 0;
6493
6494 /* Validate firmware image by checking version. */
6495 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006496 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006497 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08006498 blob->fw->size);
6499 goto fail_fw_integrity;
6500 }
6501 for (i = 0; i < 4; i++)
6502 wcode[i] = be16_to_cpu(fwcode[i + 4]);
6503 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
6504 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
6505 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006506 ql_log(ql_log_fatal, vha, 0x0086,
6507 "Unable to verify integrity of firmware image.\n");
6508 ql_log(ql_log_fatal, vha, 0x0087,
6509 "Firmware data: %04x %04x %04x %04x.\n",
6510 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08006511 goto fail_fw_integrity;
6512 }
6513
6514 seg = blob->segs;
6515 while (*seg && rval == QLA_SUCCESS) {
6516 risc_addr = *seg;
6517 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
6518 risc_size = be16_to_cpu(fwcode[3]);
6519
6520 /* Validate firmware image size. */
6521 fwclen += risc_size * sizeof(uint16_t);
6522 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006523 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08006524 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006525 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08006526 goto fail_fw_integrity;
6527 }
6528
6529 fragment = 0;
6530 while (risc_size > 0 && rval == QLA_SUCCESS) {
6531 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
6532 if (wlen > risc_size)
6533 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006534 ql_dbg(ql_dbg_init, vha, 0x0089,
6535 "Loading risc segment@ risc addr %x number of "
6536 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08006537
6538 for (i = 0; i < wlen; i++)
6539 wcode[i] = swab16(fwcode[i]);
6540
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006541 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08006542 wlen);
6543 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006544 ql_log(ql_log_fatal, vha, 0x008a,
6545 "Failed to load segment %d of firmware.\n",
6546 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08006547 break;
6548 }
6549
6550 fwcode += wlen;
6551 risc_addr += wlen;
6552 risc_size -= wlen;
6553 fragment++;
6554 }
6555
6556 /* Next segment. */
6557 seg++;
6558 }
6559 return rval;
6560
6561fail_fw_integrity:
6562 return QLA_FUNCTION_FAILED;
6563}
6564
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006565static int
6566qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006567{
6568 int rval;
6569 int segments, fragment;
6570 uint32_t *dcode, dlen;
6571 uint32_t risc_addr;
6572 uint32_t risc_size;
6573 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08006574 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006575 const uint32_t *fwcode;
6576 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006577 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006578 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006579
Andrew Vasquez54333832005-11-09 15:49:04 -08006580 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006581 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08006582 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006583 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01006584 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006585 ql_log(ql_log_warn, vha, 0x0091,
6586 "Firmware images can be retrieved from: "
6587 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006588
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006589 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006590 }
6591
Chad Dupuiscfb09192011-11-18 09:03:07 -08006592 ql_dbg(ql_dbg_init, vha, 0x0092,
6593 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006594
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006595 rval = QLA_SUCCESS;
6596
6597 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006598 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006599 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08006600 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006601 fwclen = 0;
6602
6603 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08006604 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006605 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006606 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08006607 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05006608 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006609 }
6610 for (i = 0; i < 4; i++)
6611 dcode[i] = be32_to_cpu(fwcode[i + 4]);
6612 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
6613 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
6614 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
6615 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006616 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006617 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006618 blob->fw->size);
6619 ql_log(ql_log_fatal, vha, 0x0095,
6620 "Firmware data: %08x %08x %08x %08x.\n",
6621 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05006622 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006623 }
6624
6625 while (segments && rval == QLA_SUCCESS) {
6626 risc_addr = be32_to_cpu(fwcode[2]);
6627 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
6628 risc_size = be32_to_cpu(fwcode[3]);
6629
6630 /* Validate firmware image size. */
6631 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08006632 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006633 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08006634 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006635 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05006636 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006637 }
6638
6639 fragment = 0;
6640 while (risc_size > 0 && rval == QLA_SUCCESS) {
6641 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
6642 if (dlen > risc_size)
6643 dlen = risc_size;
6644
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006645 ql_dbg(ql_dbg_init, vha, 0x0097,
6646 "Loading risc segment@ risc addr %x "
6647 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006648
6649 for (i = 0; i < dlen; i++)
6650 dcode[i] = swab32(fwcode[i]);
6651
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006652 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08006653 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006654 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006655 ql_log(ql_log_fatal, vha, 0x0098,
6656 "Failed to load segment %d of firmware.\n",
6657 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04006658 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006659 }
6660
6661 fwcode += dlen;
6662 risc_addr += dlen;
6663 risc_size -= dlen;
6664 fragment++;
6665 }
6666
6667 /* Next segment. */
6668 segments--;
6669 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006670
6671 if (!IS_QLA27XX(ha))
6672 return rval;
6673
6674 if (ha->fw_dump_template)
6675 vfree(ha->fw_dump_template);
6676 ha->fw_dump_template = NULL;
6677 ha->fw_dump_template_len = 0;
6678
6679 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04006680 "Loading fwdump template from %x\n",
6681 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006682 risc_size = be32_to_cpu(fwcode[2]);
6683 ql_dbg(ql_dbg_init, vha, 0x172,
6684 "-> array size %x dwords\n", risc_size);
6685 if (risc_size == 0 || risc_size == ~0)
6686 goto default_template;
6687
6688 dlen = (risc_size - 8) * sizeof(*fwcode);
6689 ql_dbg(ql_dbg_init, vha, 0x0173,
6690 "-> template allocating %x bytes...\n", dlen);
6691 ha->fw_dump_template = vmalloc(dlen);
6692 if (!ha->fw_dump_template) {
6693 ql_log(ql_log_warn, vha, 0x0174,
6694 "Failed fwdump template allocate %x bytes.\n", risc_size);
6695 goto default_template;
6696 }
6697
6698 fwcode += 7;
6699 risc_size -= 8;
6700 dcode = ha->fw_dump_template;
6701 for (i = 0; i < risc_size; i++)
6702 dcode[i] = le32_to_cpu(fwcode[i]);
6703
6704 if (!qla27xx_fwdt_template_valid(dcode)) {
6705 ql_log(ql_log_warn, vha, 0x0175,
6706 "Failed fwdump template validate\n");
6707 goto default_template;
6708 }
6709
6710 dlen = qla27xx_fwdt_template_size(dcode);
6711 ql_dbg(ql_dbg_init, vha, 0x0176,
6712 "-> template size %x bytes\n", dlen);
6713 if (dlen > risc_size * sizeof(*fwcode)) {
6714 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07006715 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07006716 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006717 goto default_template;
6718 }
6719 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006720 return rval;
6721
Chad Dupuisf73cb692014-02-26 04:15:06 -05006722default_template:
6723 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
6724 if (ha->fw_dump_template)
6725 vfree(ha->fw_dump_template);
6726 ha->fw_dump_template = NULL;
6727 ha->fw_dump_template_len = 0;
6728
6729 dlen = qla27xx_fwdt_template_default_size();
6730 ql_dbg(ql_dbg_init, vha, 0x0179,
6731 "-> template allocating %x bytes...\n", dlen);
6732 ha->fw_dump_template = vmalloc(dlen);
6733 if (!ha->fw_dump_template) {
6734 ql_log(ql_log_warn, vha, 0x017a,
6735 "Failed fwdump template allocate %x bytes.\n", risc_size);
6736 goto failed_template;
6737 }
6738
6739 dcode = ha->fw_dump_template;
6740 risc_size = dlen / sizeof(*fwcode);
6741 fwcode = qla27xx_fwdt_template_default();
6742 for (i = 0; i < risc_size; i++)
6743 dcode[i] = be32_to_cpu(fwcode[i]);
6744
6745 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
6746 ql_log(ql_log_warn, vha, 0x017b,
6747 "Failed fwdump template validate\n");
6748 goto failed_template;
6749 }
6750
6751 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
6752 ql_dbg(ql_dbg_init, vha, 0x017c,
6753 "-> template size %x bytes\n", dlen);
6754 ha->fw_dump_template_len = dlen;
6755 return rval;
6756
6757failed_template:
6758 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
6759 if (ha->fw_dump_template)
6760 vfree(ha->fw_dump_template);
6761 ha->fw_dump_template = NULL;
6762 ha->fw_dump_template_len = 0;
6763 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006764}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006765
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006766int
6767qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
6768{
6769 int rval;
6770
Andrew Vasqueze337d902009-04-06 22:33:49 -07006771 if (ql2xfwloadbin == 1)
6772 return qla81xx_load_risc(vha, srisc_addr);
6773
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006774 /*
6775 * FW Load priority:
6776 * 1) Firmware via request-firmware interface (.bin file).
6777 * 2) Firmware residing in flash.
6778 */
6779 rval = qla24xx_load_risc_blob(vha, srisc_addr);
6780 if (rval == QLA_SUCCESS)
6781 return rval;
6782
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006783 return qla24xx_load_risc_flash(vha, srisc_addr,
6784 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006785}
6786
6787int
6788qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
6789{
6790 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006791 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006792
Andrew Vasqueze337d902009-04-06 22:33:49 -07006793 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006794 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07006795
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006796 /*
6797 * FW Load priority:
6798 * 1) Firmware residing in flash.
6799 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006800 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006801 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006802 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006803 if (rval == QLA_SUCCESS)
6804 return rval;
6805
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006806try_blob_fw:
6807 rval = qla24xx_load_risc_blob(vha, srisc_addr);
6808 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
6809 return rval;
6810
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006811 ql_log(ql_log_info, vha, 0x0099,
6812 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006813 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
6814 if (rval != QLA_SUCCESS)
6815 return rval;
6816
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006817 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006818 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006819 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006820}
6821
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006822void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006823qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006824{
6825 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006826 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006827
Andrew Vasquez85880802009-12-15 21:29:46 -08006828 if (ha->flags.pci_channel_io_perm_failure)
6829 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07006830 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006831 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07006832 if (!ha->fw_major_version)
6833 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07006834 if (!ha->flags.fw_started)
6835 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006836
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006837 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08006838 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07006839 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006840 ha->isp_ops->reset_chip(vha);
6841 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006842 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006843 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006844 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006845 ql_log(ql_log_info, vha, 0x8015,
6846 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006847 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006848 }
Quinn Tranec7193e2017-03-15 09:48:55 -07006849
Quinn Tran4b60c822017-06-13 20:47:21 -07006850 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07006851 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006852}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006853
6854int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006855qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006856{
6857 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08006858 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006859 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006860 struct qla_hw_data *ha = vha->hw;
6861 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07006862 struct req_que *req;
6863 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006864
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006865 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006866 return -EINVAL;
6867
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006868 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08006869 if (vha->qpair)
6870 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07006871 else
Michael Hernandezd7459522016-12-12 14:40:07 -08006872 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07006873 rsp = req->rsp;
6874
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006875 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006876 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006877 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006878 }
6879
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006880 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006881
6882 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08006883 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
6884 BIT_1);
6885 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
6886 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
6887 ql_dbg(ql_dbg_init, vha, 0x0120,
6888 "Failed SNS login: loop_id=%x, rval2=%d\n",
6889 NPH_SNS, rval2);
6890 else
6891 ql_dbg(ql_dbg_init, vha, 0x0103,
6892 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
6893 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
6894 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006895 return (QLA_FUNCTION_FAILED);
6896 }
6897
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006898 atomic_set(&vha->loop_down_timer, 0);
6899 atomic_set(&vha->loop_state, LOOP_UP);
6900 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6901 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
6902 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006903
6904 return rval;
6905}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006906
6907/* 84XX Support **************************************************************/
6908
6909static LIST_HEAD(qla_cs84xx_list);
6910static DEFINE_MUTEX(qla_cs84xx_mutex);
6911
6912static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006913qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006914{
6915 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006916 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006917
6918 mutex_lock(&qla_cs84xx_mutex);
6919
6920 /* Find any shared 84xx chip. */
6921 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
6922 if (cs84xx->bus == ha->pdev->bus) {
6923 kref_get(&cs84xx->kref);
6924 goto done;
6925 }
6926 }
6927
6928 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
6929 if (!cs84xx)
6930 goto done;
6931
6932 kref_init(&cs84xx->kref);
6933 spin_lock_init(&cs84xx->access_lock);
6934 mutex_init(&cs84xx->fw_update_mutex);
6935 cs84xx->bus = ha->pdev->bus;
6936
6937 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
6938done:
6939 mutex_unlock(&qla_cs84xx_mutex);
6940 return cs84xx;
6941}
6942
6943static void
6944__qla84xx_chip_release(struct kref *kref)
6945{
6946 struct qla_chip_state_84xx *cs84xx =
6947 container_of(kref, struct qla_chip_state_84xx, kref);
6948
6949 mutex_lock(&qla_cs84xx_mutex);
6950 list_del(&cs84xx->list);
6951 mutex_unlock(&qla_cs84xx_mutex);
6952 kfree(cs84xx);
6953}
6954
6955void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006956qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006957{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006958 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006959 if (ha->cs84xx)
6960 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
6961}
6962
6963static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006964qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006965{
6966 int rval;
6967 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006968 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006969
6970 mutex_lock(&ha->cs84xx->fw_update_mutex);
6971
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006972 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006973
6974 mutex_unlock(&ha->cs84xx->fw_update_mutex);
6975
6976 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
6977 QLA_SUCCESS;
6978}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08006979
6980/* 81XX Support **************************************************************/
6981
6982int
6983qla81xx_nvram_config(scsi_qla_host_t *vha)
6984{
6985 int rval;
6986 struct init_cb_81xx *icb;
6987 struct nvram_81xx *nv;
6988 uint32_t *dptr;
6989 uint8_t *dptr1, *dptr2;
6990 uint32_t chksum;
6991 uint16_t cnt;
6992 struct qla_hw_data *ha = vha->hw;
6993
6994 rval = QLA_SUCCESS;
6995 icb = (struct init_cb_81xx *)ha->init_cb;
6996 nv = ha->nvram;
6997
6998 /* Determine NVRAM starting address. */
6999 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007000 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007001 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7002 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007003
7004 /* Get VPD data into cache */
7005 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007006 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7007 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007008
7009 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007010 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007011 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007012 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007013 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7014 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007015
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007016 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7017 "Contents of NVRAM:\n");
7018 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7019 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007020
7021 /* Bad NVRAM data, set defaults parameters. */
7022 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7023 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007024 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007025 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007026 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007027 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007028 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007029 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007030 ql_log(ql_log_info, vha, 0x0074,
7031 "Falling back to functioning (yet invalid -- WWPN) "
7032 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007033
7034 /*
7035 * Set default initialization control block.
7036 */
7037 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007038 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7039 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007040 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007041 nv->execution_throttle = cpu_to_le16(0xFFFF);
7042 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007043 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007044 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007045 nv->port_name[2] = 0x00;
7046 nv->port_name[3] = 0xe0;
7047 nv->port_name[4] = 0x8b;
7048 nv->port_name[5] = 0x1c;
7049 nv->port_name[6] = 0x55;
7050 nv->port_name[7] = 0x86;
7051 nv->node_name[0] = 0x20;
7052 nv->node_name[1] = 0x00;
7053 nv->node_name[2] = 0x00;
7054 nv->node_name[3] = 0xe0;
7055 nv->node_name[4] = 0x8b;
7056 nv->node_name[5] = 0x1c;
7057 nv->node_name[6] = 0x55;
7058 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007059 nv->login_retry_count = cpu_to_le16(8);
7060 nv->interrupt_delay_timer = cpu_to_le16(0);
7061 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007062 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007063 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7064 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7065 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7066 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7067 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7068 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007069 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007070 nv->max_luns_per_target = cpu_to_le16(128);
7071 nv->port_down_retry_count = cpu_to_le16(30);
7072 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007073 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007074 nv->enode_mac[1] = 0xC0;
7075 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007076 nv->enode_mac[3] = 0x04;
7077 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007078 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007079
7080 rval = 1;
7081 }
7082
Arun Easi9e522cd2012-08-22 14:21:31 -04007083 if (IS_T10_PI_CAPABLE(ha))
7084 nv->frame_payload_size &= ~7;
7085
Arun Easiaa230bc2013-01-30 03:34:39 -05007086 qlt_81xx_config_nvram_stage1(vha, nv);
7087
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007088 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007089 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007090
7091 /* Copy 1st segment. */
7092 dptr1 = (uint8_t *)icb;
7093 dptr2 = (uint8_t *)&nv->version;
7094 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7095 while (cnt--)
7096 *dptr1++ = *dptr2++;
7097
7098 icb->login_retry_count = nv->login_retry_count;
7099
7100 /* Copy 2nd segment. */
7101 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7102 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7103 cnt = (uint8_t *)&icb->reserved_5 -
7104 (uint8_t *)&icb->interrupt_delay_timer;
7105 while (cnt--)
7106 *dptr1++ = *dptr2++;
7107
7108 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7109 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7110 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007111 icb->enode_mac[0] = 0x00;
7112 icb->enode_mac[1] = 0xC0;
7113 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007114 icb->enode_mac[3] = 0x04;
7115 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007116 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007117 }
7118
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007119 /* Use extended-initialization control block. */
7120 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7121
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007122 /*
7123 * Setup driver NVRAM options.
7124 */
7125 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007126 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007127
Arun Easiaa230bc2013-01-30 03:34:39 -05007128 qlt_81xx_config_nvram_stage2(vha, icb);
7129
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007130 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007131 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007132 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7133 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7134 }
7135
7136 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007137 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007138 /*
7139 * Firmware will apply the following mask if the nodename was
7140 * not provided.
7141 */
7142 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7143 icb->node_name[0] &= 0xF0;
7144 }
7145
7146 /* Set host adapter parameters. */
7147 ha->flags.disable_risc_code_load = 0;
7148 ha->flags.enable_lip_reset = 0;
7149 ha->flags.enable_lip_full_login =
7150 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7151 ha->flags.enable_target_reset =
7152 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7153 ha->flags.enable_led_scheme = 0;
7154 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7155
7156 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7157 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7158
7159 /* save HBA serial number */
7160 ha->serial0 = icb->port_name[5];
7161 ha->serial1 = icb->port_name[6];
7162 ha->serial2 = icb->port_name[7];
7163 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7164 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7165
Bart Van Asschead950362015-07-09 07:24:08 -07007166 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007167
7168 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7169
7170 /* Set minimum login_timeout to 4 seconds. */
7171 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7172 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7173 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007174 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007175 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007176
7177 /* Set minimum RATOV to 100 tenths of a second. */
7178 ha->r_a_tov = 100;
7179
7180 ha->loop_reset_delay = nv->reset_delay;
7181
7182 /* Link Down Timeout = 0:
7183 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007184 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007185 * I/O's to OS with "DID_NO_CONNECT".
7186 *
7187 * Link Down Timeout != 0:
7188 *
7189 * The driver waits for the link to come up after link down
7190 * before returning I/Os to OS with "DID_NO_CONNECT".
7191 */
7192 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7193 ha->loop_down_abort_time =
7194 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7195 } else {
7196 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7197 ha->loop_down_abort_time =
7198 (LOOP_DOWN_TIME - ha->link_down_timeout);
7199 }
7200
7201 /* Need enough time to try and get the port back. */
7202 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7203 if (qlport_down_retry)
7204 ha->port_down_retry_count = qlport_down_retry;
7205
7206 /* Set login_retry_count */
7207 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7208 if (ha->port_down_retry_count ==
7209 le16_to_cpu(nv->port_down_retry_count) &&
7210 ha->port_down_retry_count > 3)
7211 ha->login_retry_count = ha->port_down_retry_count;
7212 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7213 ha->login_retry_count = ha->port_down_retry_count;
7214 if (ql2xloginretrycount)
7215 ha->login_retry_count = ql2xloginretrycount;
7216
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007217 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007218 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007219 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007220
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007221 /* Enable ZIO. */
7222 if (!vha->flags.init_done) {
7223 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7224 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7225 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7226 le16_to_cpu(icb->interrupt_delay_timer): 2;
7227 }
Bart Van Asschead950362015-07-09 07:24:08 -07007228 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007229 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7230 vha->flags.process_response_queue = 0;
7231 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7232 ha->zio_mode = QLA_ZIO_MODE_6;
7233
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007234 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007235 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007236 ha->zio_mode,
7237 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007238
7239 icb->firmware_options_2 |= cpu_to_le32(
7240 (uint32_t)ha->zio_mode);
7241 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7242 vha->flags.process_response_queue = 1;
7243 }
7244
Quinn Tran41dc5292017-01-19 22:28:03 -08007245 /* enable RIDA Format2 */
7246 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7247 icb->firmware_options_3 |= BIT_0;
7248
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007249 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007250 ql_log(ql_log_warn, vha, 0x0076,
7251 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007252 }
7253 return (rval);
7254}
7255
Giridhar Malavalia9083012010-04-12 17:59:55 -07007256int
7257qla82xx_restart_isp(scsi_qla_host_t *vha)
7258{
7259 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007260 struct qla_hw_data *ha = vha->hw;
7261 struct req_que *req = ha->req_q_map[0];
7262 struct rsp_que *rsp = ha->rsp_q_map[0];
7263 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07007264 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007265
7266 status = qla2x00_init_rings(vha);
7267 if (!status) {
7268 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7269 ha->flags.chip_reset_done = 1;
7270
7271 status = qla2x00_fw_ready(vha);
7272 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07007273 /* Issue a marker after FW becomes ready. */
7274 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007275 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04007276 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007277 }
7278
7279 /* if no cable then assume it's good */
7280 if ((vha->device_flags & DFLG_NO_CABLE))
7281 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007282 }
7283
7284 if (!status) {
7285 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7286
7287 if (!atomic_read(&vha->loop_down_timer)) {
7288 /*
7289 * Issue marker command only when we are going
7290 * to start the I/O .
7291 */
7292 vha->marker_needed = 1;
7293 }
7294
Giridhar Malavalia9083012010-04-12 17:59:55 -07007295 ha->isp_ops->enable_intrs(ha);
7296
7297 ha->isp_abort_cnt = 0;
7298 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7299
Saurav Kashyap53296782011-05-10 11:30:06 -07007300 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07007301 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07007302
Giridhar Malavalia9083012010-04-12 17:59:55 -07007303 if (ha->fce) {
7304 ha->flags.fce_enabled = 1;
7305 memset(ha->fce, 0,
7306 fce_calc_size(ha->fce_bufs));
7307 rval = qla2x00_enable_fce_trace(vha,
7308 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
7309 &ha->fce_bufs);
7310 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007311 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007312 "Unable to reinitialize FCE (%d).\n",
7313 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007314 ha->flags.fce_enabled = 0;
7315 }
7316 }
7317
7318 if (ha->eft) {
7319 memset(ha->eft, 0, EFT_SIZE);
7320 rval = qla2x00_enable_eft_trace(vha,
7321 ha->eft_dma, EFT_NUM_BUFFERS);
7322 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007323 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007324 "Unable to reinitialize EFT (%d).\n",
7325 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007326 }
7327 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07007328 }
7329
7330 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007331 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007332 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07007333
7334 spin_lock_irqsave(&ha->vport_slock, flags);
7335 list_for_each_entry(vp, &ha->vp_list, list) {
7336 if (vp->vp_idx) {
7337 atomic_inc(&vp->vref_count);
7338 spin_unlock_irqrestore(&ha->vport_slock, flags);
7339
Giridhar Malavalia9083012010-04-12 17:59:55 -07007340 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07007341
7342 spin_lock_irqsave(&ha->vport_slock, flags);
7343 atomic_dec(&vp->vref_count);
7344 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07007345 }
Arun Easifeafb7b2010-09-03 14:57:00 -07007346 spin_unlock_irqrestore(&ha->vport_slock, flags);
7347
Giridhar Malavalia9083012010-04-12 17:59:55 -07007348 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007349 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007350 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07007351 }
7352
7353 return status;
7354}
7355
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007356void
Andrew Vasquezae97c912010-02-18 10:07:28 -08007357qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007358{
Andrew Vasquezae97c912010-02-18 10:07:28 -08007359 struct qla_hw_data *ha = vha->hw;
7360
Himanshu Madhanif198caf2016-01-27 12:03:30 -05007361 /* Hold status IOCBs until ABTS response received. */
7362 if (ql2xfwholdabts)
7363 ha->fw_options[3] |= BIT_12;
7364
Giridhar Malavali088d09d2016-07-06 11:14:20 -04007365 /* Set Retry FLOGI in case of P2P connection */
7366 if (ha->operating_mode == P2P) {
7367 ha->fw_options[2] |= BIT_3;
7368 ql_dbg(ql_dbg_disc, vha, 0x2103,
7369 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
7370 __func__, ha->fw_options[2]);
7371 }
7372
Quinn Tran41dc5292017-01-19 22:28:03 -08007373 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
7374 if (ql2xmvasynctoatio) {
7375 if (qla_tgt_mode_enabled(vha) ||
7376 qla_dual_mode_enabled(vha))
7377 ha->fw_options[2] |= BIT_11;
7378 else
7379 ha->fw_options[2] &= ~BIT_11;
7380 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08007381
Quinn Tranf7e761f2017-06-02 09:12:02 -07007382 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07007383 qla_dual_mode_enabled(vha)) {
7384 /* FW auto send SCSI status during */
7385 ha->fw_options[1] |= BIT_8;
7386 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
7387
7388 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07007389 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07007390 } else {
7391 ha->fw_options[1] &= ~BIT_8;
7392 ha->fw_options[10] &= 0x00ff;
7393
Quinn Tranf7e761f2017-06-02 09:12:02 -07007394 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07007395 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07007396
Quinn Tran41dc5292017-01-19 22:28:03 -08007397 if (ql2xetsenable) {
7398 /* Enable ETS Burst. */
7399 memset(ha->fw_options, 0, sizeof(ha->fw_options));
7400 ha->fw_options[2] |= BIT_9;
7401 }
7402
Quinn Tran83548fe2017-06-02 09:12:01 -07007403 ql_dbg(ql_dbg_init, vha, 0x00e9,
7404 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
7405 __func__, ha->fw_options[1], ha->fw_options[2],
7406 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08007407
Andrew Vasquezae97c912010-02-18 10:07:28 -08007408 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007409}
Sarang Radke09ff7012010-03-19 17:03:59 -07007410
7411/*
7412 * qla24xx_get_fcp_prio
7413 * Gets the fcp cmd priority value for the logged in port.
7414 * Looks for a match of the port descriptors within
7415 * each of the fcp prio config entries. If a match is found,
7416 * the tag (priority) value is returned.
7417 *
7418 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007419 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07007420 * fcport = port structure pointer.
7421 *
7422 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07007423 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007424 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07007425 *
7426 * Context:
7427 * Kernel context
7428 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007429static int
Sarang Radke09ff7012010-03-19 17:03:59 -07007430qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
7431{
7432 int i, entries;
7433 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007434 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07007435 uint32_t pid1, pid2;
7436 uint64_t wwn1, wwn2;
7437 struct qla_fcp_prio_entry *pri_entry;
7438 struct qla_hw_data *ha = vha->hw;
7439
7440 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007441 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07007442
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007443 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07007444 entries = ha->fcp_prio_cfg->num_entries;
7445 pri_entry = &ha->fcp_prio_cfg->entry[0];
7446
7447 for (i = 0; i < entries; i++) {
7448 pid_match = wwn_match = 0;
7449
7450 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
7451 pri_entry++;
7452 continue;
7453 }
7454
7455 /* check source pid for a match */
7456 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
7457 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
7458 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
7459 if (pid1 == INVALID_PORT_ID)
7460 pid_match++;
7461 else if (pid1 == pid2)
7462 pid_match++;
7463 }
7464
7465 /* check destination pid for a match */
7466 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
7467 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
7468 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
7469 if (pid1 == INVALID_PORT_ID)
7470 pid_match++;
7471 else if (pid1 == pid2)
7472 pid_match++;
7473 }
7474
7475 /* check source WWN for a match */
7476 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
7477 wwn1 = wwn_to_u64(vha->port_name);
7478 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
7479 if (wwn2 == (uint64_t)-1)
7480 wwn_match++;
7481 else if (wwn1 == wwn2)
7482 wwn_match++;
7483 }
7484
7485 /* check destination WWN for a match */
7486 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
7487 wwn1 = wwn_to_u64(fcport->port_name);
7488 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
7489 if (wwn2 == (uint64_t)-1)
7490 wwn_match++;
7491 else if (wwn1 == wwn2)
7492 wwn_match++;
7493 }
7494
7495 if (pid_match == 2 || wwn_match == 2) {
7496 /* Found a matching entry */
7497 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
7498 priority = pri_entry->tag;
7499 break;
7500 }
7501
7502 pri_entry++;
7503 }
7504
7505 return priority;
7506}
7507
7508/*
7509 * qla24xx_update_fcport_fcp_prio
7510 * Activates fcp priority for the logged in fc port
7511 *
7512 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007513 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07007514 * fcp = port structure pointer.
7515 *
7516 * Return:
7517 * QLA_SUCCESS or QLA_FUNCTION_FAILED
7518 *
7519 * Context:
7520 * Kernel context.
7521 */
7522int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007523qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07007524{
7525 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007526 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07007527 uint16_t mb[5];
7528
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007529 if (fcport->port_type != FCT_TARGET ||
7530 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07007531 return QLA_FUNCTION_FAILED;
7532
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007533 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007534 if (priority < 0)
7535 return QLA_FUNCTION_FAILED;
7536
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007537 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08007538 fcport->fcp_prio = priority & 0xf;
7539 return QLA_SUCCESS;
7540 }
7541
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007542 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08007543 if (ret == QLA_SUCCESS) {
7544 if (fcport->fcp_prio != priority)
7545 ql_dbg(ql_dbg_user, vha, 0x709e,
7546 "Updated FCP_CMND priority - value=%d loop_id=%d "
7547 "port_id=%02x%02x%02x.\n", priority,
7548 fcport->loop_id, fcport->d_id.b.domain,
7549 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08007550 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08007551 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007552 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007553 "Unable to update FCP_CMND priority - ret=0x%x for "
7554 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
7555 fcport->d_id.b.domain, fcport->d_id.b.area,
7556 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07007557 return ret;
7558}
7559
7560/*
7561 * qla24xx_update_all_fcp_prio
7562 * Activates fcp priority for all the logged in ports
7563 *
7564 * Input:
7565 * ha = adapter block pointer.
7566 *
7567 * Return:
7568 * QLA_SUCCESS or QLA_FUNCTION_FAILED
7569 *
7570 * Context:
7571 * Kernel context.
7572 */
7573int
7574qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
7575{
7576 int ret;
7577 fc_port_t *fcport;
7578
7579 ret = QLA_FUNCTION_FAILED;
7580 /* We need to set priority for all logged in ports */
7581 list_for_each_entry(fcport, &vha->vp_fcports, list)
7582 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
7583
7584 return ret;
7585}
Michael Hernandezd7459522016-12-12 14:40:07 -08007586
Quinn Tran82de8022017-06-13 20:47:17 -07007587struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
7588 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08007589{
7590 int rsp_id = 0;
7591 int req_id = 0;
7592 int i;
7593 struct qla_hw_data *ha = vha->hw;
7594 uint16_t qpair_id = 0;
7595 struct qla_qpair *qpair = NULL;
7596 struct qla_msix_entry *msix;
7597
7598 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
7599 ql_log(ql_log_warn, vha, 0x00181,
7600 "FW/Driver is not multi-queue capable.\n");
7601 return NULL;
7602 }
7603
7604 if (ql2xmqsupport) {
7605 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
7606 if (qpair == NULL) {
7607 ql_log(ql_log_warn, vha, 0x0182,
7608 "Failed to allocate memory for queue pair.\n");
7609 return NULL;
7610 }
7611 memset(qpair, 0, sizeof(struct qla_qpair));
7612
7613 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08007614 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07007615 qpair->qp_lock_ptr = &qpair->qp_lock;
7616 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07007617 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08007618
7619 /* Assign available que pair id */
7620 mutex_lock(&ha->mq_lock);
7621 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07007622 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08007623 mutex_unlock(&ha->mq_lock);
7624 ql_log(ql_log_warn, vha, 0x0183,
7625 "No resources to create additional q pair.\n");
7626 goto fail_qid_map;
7627 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07007628 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08007629 set_bit(qpair_id, ha->qpair_qid_map);
7630 ha->queue_pair_map[qpair_id] = qpair;
7631 qpair->id = qpair_id;
7632 qpair->vp_idx = vp_idx;
Quinn Trane326d222017-06-13 20:47:18 -07007633 INIT_LIST_HEAD(&qpair->hints_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007634 qpair->chip_reset = ha->base_qpair->chip_reset;
7635 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
7636 qpair->enable_explicit_conf =
7637 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08007638
7639 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08007640 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08007641 if (msix->in_use)
7642 continue;
7643 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07007644 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08007645 "Vector %x selected for qpair\n", msix->vector);
7646 break;
7647 }
7648 if (!qpair->msix) {
7649 ql_log(ql_log_warn, vha, 0x0184,
7650 "Out of MSI-X vectors!.\n");
7651 goto fail_msix;
7652 }
7653
7654 qpair->msix->in_use = 1;
7655 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07007656 qpair->pdev = ha->pdev;
7657 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
7658 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08007659
7660 mutex_unlock(&ha->mq_lock);
7661
7662 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07007663 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08007664 if (!rsp_id) {
7665 ql_log(ql_log_warn, vha, 0x0185,
7666 "Failed to create response queue.\n");
7667 goto fail_rsp;
7668 }
7669
7670 qpair->rsp = ha->rsp_q_map[rsp_id];
7671
7672 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07007673 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
7674 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08007675 if (!req_id) {
7676 ql_log(ql_log_warn, vha, 0x0186,
7677 "Failed to create request queue.\n");
7678 goto fail_req;
7679 }
7680
7681 qpair->req = ha->req_q_map[req_id];
7682 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07007683 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07007684 /* init qpair to this cpu. Will adjust at run time. */
7685 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08007686
7687 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
7688 if (ha->fw_attributes & BIT_4)
7689 qpair->difdix_supported = 1;
7690 }
7691
7692 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
7693 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007694 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08007695 "Failed to create srb mempool for qpair %d\n",
7696 qpair->id);
7697 goto fail_mempool;
7698 }
7699
7700 /* Mark as online */
7701 qpair->online = 1;
7702
7703 if (!vha->flags.qpairs_available)
7704 vha->flags.qpairs_available = 1;
7705
7706 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
7707 "Request/Response queue pair created, id %d\n",
7708 qpair->id);
7709 ql_dbg(ql_dbg_init, vha, 0x0187,
7710 "Request/Response queue pair created, id %d\n",
7711 qpair->id);
7712 }
7713 return qpair;
7714
7715fail_mempool:
7716fail_req:
7717 qla25xx_delete_rsp_que(vha, qpair->rsp);
7718fail_rsp:
7719 mutex_lock(&ha->mq_lock);
7720 qpair->msix->in_use = 0;
7721 list_del(&qpair->qp_list_elem);
7722 if (list_empty(&vha->qp_list))
7723 vha->flags.qpairs_available = 0;
7724fail_msix:
7725 ha->queue_pair_map[qpair_id] = NULL;
7726 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07007727 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08007728 mutex_unlock(&ha->mq_lock);
7729fail_qid_map:
7730 kfree(qpair);
7731 return NULL;
7732}
7733
7734int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
7735{
Sawan Chandakd65237c2017-06-13 20:47:19 -07007736 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08007737 struct qla_hw_data *ha = qpair->hw;
7738
Sawan Chandakd65237c2017-06-13 20:47:19 -07007739 if (!vha->flags.qpairs_req_created && !vha->flags.qpairs_rsp_created)
7740 goto fail;
7741
Michael Hernandezd7459522016-12-12 14:40:07 -08007742 qpair->delete_in_progress = 1;
7743 while (atomic_read(&qpair->ref_count))
7744 msleep(500);
7745
7746 ret = qla25xx_delete_req_que(vha, qpair->req);
7747 if (ret != QLA_SUCCESS)
7748 goto fail;
7749 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
7750 if (ret != QLA_SUCCESS)
7751 goto fail;
7752
7753 mutex_lock(&ha->mq_lock);
7754 ha->queue_pair_map[qpair->id] = NULL;
7755 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07007756 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08007757 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07007758 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08007759 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07007760 vha->flags.qpairs_req_created = 0;
7761 vha->flags.qpairs_rsp_created = 0;
7762 }
Michael Hernandezd7459522016-12-12 14:40:07 -08007763 mempool_destroy(qpair->srb_mempool);
7764 kfree(qpair);
7765 mutex_unlock(&ha->mq_lock);
7766
7767 return QLA_SUCCESS;
7768fail:
7769 return ret;
7770}