blob: 1d815ab779eadf59deed638f1f63eae166d28606 [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
Quinn Tranb945e772017-06-13 20:47:29 -07002541 if (ha->exchoffld_buf)
2542 dump_size += sizeof(struct qla2xxx_offld_chain) +
2543 ha->exchoffld_size;
2544 if (ha->exlogin_buf)
2545 dump_size += sizeof(struct qla2xxx_offld_chain) +
2546 ha->exlogin_size;
2547
Chad Dupuisf73cb692014-02-26 04:15:06 -05002548allocate:
Andrew Vasquezd4e3e042006-05-17 15:09:50 -07002549 ha->fw_dump = vmalloc(dump_size);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002550 if (!ha->fw_dump) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002551 ql_log(ql_log_warn, vha, 0x00c4,
2552 "Unable to allocate (%d KB) for firmware dump.\n",
2553 dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002554
Madhuranath Iyengare30d1752010-10-15 11:27:46 -07002555 if (ha->fce) {
2556 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2557 ha->fce_dma);
2558 ha->fce = NULL;
2559 ha->fce_dma = 0;
2560 }
2561
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002562 if (ha->eft) {
2563 dma_free_coherent(&ha->pdev->dev, eft_size, ha->eft,
2564 ha->eft_dma);
2565 ha->eft = NULL;
2566 ha->eft_dma = 0;
2567 }
2568 return;
2569 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05002570 ha->fw_dump_len = dump_size;
Chad Dupuiscfb09192011-11-18 09:03:07 -08002571 ql_dbg(ql_dbg_init, vha, 0x00c5,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002572 "Allocated (%d KB) for firmware dump.\n", dump_size / 1024);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002573
Chad Dupuisf73cb692014-02-26 04:15:06 -05002574 if (IS_QLA27XX(ha))
2575 return;
2576
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002577 ha->fw_dump->signature[0] = 'Q';
2578 ha->fw_dump->signature[1] = 'L';
2579 ha->fw_dump->signature[2] = 'G';
2580 ha->fw_dump->signature[3] = 'C';
Bart Van Asschead950362015-07-09 07:24:08 -07002581 ha->fw_dump->version = htonl(1);
Andrew Vasqueza7a167b2006-06-23 16:10:29 -07002582
2583 ha->fw_dump->fixed_size = htonl(fixed_size);
2584 ha->fw_dump->mem_size = htonl(mem_size);
2585 ha->fw_dump->req_q_size = htonl(req_q_size);
2586 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
2587
2588 ha->fw_dump->eft_size = htonl(eft_size);
2589 ha->fw_dump->eft_addr_l = htonl(LSD(ha->eft_dma));
2590 ha->fw_dump->eft_addr_h = htonl(MSD(ha->eft_dma));
2591
2592 ha->fw_dump->header_size =
2593 htonl(offsetof(struct qla2xxx_fw_dump, isp));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002594}
2595
Andrew Vasquez18e75552009-06-03 09:55:30 -07002596static int
2597qla81xx_mpi_sync(scsi_qla_host_t *vha)
2598{
2599#define MPS_MASK 0xe0
2600 int rval;
2601 uint16_t dc;
2602 uint32_t dw;
Andrew Vasquez18e75552009-06-03 09:55:30 -07002603
2604 if (!IS_QLA81XX(vha->hw))
2605 return QLA_SUCCESS;
2606
2607 rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
2608 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002609 ql_log(ql_log_warn, vha, 0x0105,
2610 "Unable to acquire semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002611 goto done;
2612 }
2613
2614 pci_read_config_word(vha->hw->pdev, 0x54, &dc);
2615 rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
2616 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002617 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002618 goto done_release;
2619 }
2620
2621 dc &= MPS_MASK;
2622 if (dc == (dw & MPS_MASK))
2623 goto done_release;
2624
2625 dw &= ~MPS_MASK;
2626 dw |= dc;
2627 rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
2628 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002629 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002630 }
2631
2632done_release:
2633 rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
2634 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002635 ql_log(ql_log_warn, vha, 0x006d,
2636 "Unable to release semaphore.\n");
Andrew Vasquez18e75552009-06-03 09:55:30 -07002637 }
2638
2639done:
2640 return rval;
2641}
2642
Chad Dupuis8d93f552013-01-30 03:34:37 -05002643int
2644qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
2645{
2646 /* Don't try to reallocate the array */
2647 if (req->outstanding_cmds)
2648 return QLA_SUCCESS;
2649
Michael Hernandezd7459522016-12-12 14:40:07 -08002650 if (!IS_FWI2_CAPABLE(ha))
Chad Dupuis8d93f552013-01-30 03:34:37 -05002651 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
2652 else {
Quinn Tran03e8c682015-12-17 14:56:59 -05002653 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
2654 req->num_outstanding_cmds = ha->cur_fw_xcb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002655 else
Quinn Tran03e8c682015-12-17 14:56:59 -05002656 req->num_outstanding_cmds = ha->cur_fw_iocb_count;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002657 }
2658
2659 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
2660 req->num_outstanding_cmds, GFP_KERNEL);
2661
2662 if (!req->outstanding_cmds) {
2663 /*
2664 * Try to allocate a minimal size just so we can get through
2665 * initialization.
2666 */
2667 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
2668 req->outstanding_cmds = kzalloc(sizeof(srb_t *) *
2669 req->num_outstanding_cmds, GFP_KERNEL);
2670
2671 if (!req->outstanding_cmds) {
2672 ql_log(ql_log_fatal, NULL, 0x0126,
2673 "Failed to allocate memory for "
2674 "outstanding_cmds for req_que %p.\n", req);
2675 req->num_outstanding_cmds = 0;
2676 return QLA_FUNCTION_FAILED;
2677 }
2678 }
2679
2680 return QLA_SUCCESS;
2681}
2682
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002683/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 * qla2x00_setup_chip() - Load and start RISC firmware.
2685 * @ha: HA context
2686 *
2687 * Returns 0 on success.
2688 */
2689static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002690qla2x00_setup_chip(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002692 int rval;
2693 uint32_t srisc_address = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002694 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3db06522008-01-31 12:33:49 -08002695 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2696 unsigned long flags;
Andrew Vasquezdda772e2009-03-24 09:08:00 -07002697 uint16_t fw_major_version;
Andrew Vasquez3db06522008-01-31 12:33:49 -08002698
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002699 if (IS_P3P_TYPE(ha)) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07002700 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez14e303d2010-07-23 15:28:29 +05002701 if (rval == QLA_SUCCESS) {
2702 qla2x00_stop_firmware(vha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07002703 goto enable_82xx_npiv;
Andrew Vasquez14e303d2010-07-23 15:28:29 +05002704 } else
Giridhar Malavalib9637522010-05-28 15:08:15 -07002705 goto failed;
Giridhar Malavalia9083012010-04-12 17:59:55 -07002706 }
2707
Andrew Vasquez3db06522008-01-31 12:33:49 -08002708 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
2709 /* Disable SRAM, Instruction RAM and GP RAM parity. */
2710 spin_lock_irqsave(&ha->hardware_lock, flags);
2711 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
2712 RD_REG_WORD(&reg->hccr);
2713 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Andrew Vasquez18e75552009-06-03 09:55:30 -07002716 qla81xx_mpi_sync(vha);
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /* Load firmware sequences */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002719 rval = ha->isp_ops->load_risc(vha, &srisc_address);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002720 if (rval == QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002721 ql_dbg(ql_dbg_init, vha, 0x00c9,
2722 "Verifying Checksum of loaded RISC code.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002724 rval = qla2x00_verify_checksum(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (rval == QLA_SUCCESS) {
2726 /* Start firmware execution. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002727 ql_dbg(ql_dbg_init, vha, 0x00ca,
2728 "Starting firmware.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05002730 if (ql2xexlogins)
2731 ha->flags.exlogins_enabled = 1;
2732
Quinn Tran99e1b682017-06-02 09:12:03 -07002733 if (qla_is_exch_offld_enabled(vha))
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05002734 ha->flags.exchoffld_enabled = 1;
2735
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002736 rval = qla2x00_execute_fw(vha, srisc_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /* Retrieve firmware information. */
Andrew Vasquezdda772e2009-03-24 09:08:00 -07002738 if (rval == QLA_SUCCESS) {
Himanshu Madhanib0d6cab2015-12-17 14:56:56 -05002739 rval = qla2x00_set_exlogins_buffer(vha);
2740 if (rval != QLA_SUCCESS)
2741 goto failed;
2742
Himanshu Madhani2f56a7f2015-12-17 14:56:57 -05002743 rval = qla2x00_set_exchoffld_buffer(vha);
2744 if (rval != QLA_SUCCESS)
2745 goto failed;
2746
Giridhar Malavalia9083012010-04-12 17:59:55 -07002747enable_82xx_npiv:
Andrew Vasquezdda772e2009-03-24 09:08:00 -07002748 fw_major_version = ha->fw_major_version;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002749 if (IS_P3P_TYPE(ha))
Giridhar Malavali31731672011-08-16 11:31:54 -07002750 qla82xx_check_md_needed(vha);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002751 else
2752 rval = qla2x00_get_fw_version(vha);
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07002753 if (rval != QLA_SUCCESS)
2754 goto failed;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002755 ha->flags.npiv_supported = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002756 if (IS_QLA2XXX_MIDTYPE(ha) &&
Mike Hernandez946fb892008-08-13 21:36:59 -07002757 (ha->fw_attributes & BIT_2)) {
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07002758 ha->flags.npiv_supported = 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07002759 if ((!ha->max_npiv_vports) ||
2760 ((ha->max_npiv_vports + 1) %
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002761 MIN_MULTI_ID_FABRIC))
Seokmann Ju4d0ea242007-09-20 14:07:43 -07002762 ha->max_npiv_vports =
Andrew Vasquezeb66dc62007-11-12 10:30:58 -08002763 MIN_MULTI_ID_FABRIC - 1;
Seokmann Ju4d0ea242007-09-20 14:07:43 -07002764 }
Quinn Tran03e8c682015-12-17 14:56:59 -05002765 qla2x00_get_resource_cnts(vha);
Andrew Vasquezd743de62009-03-24 09:08:15 -07002766
Chad Dupuis8d93f552013-01-30 03:34:37 -05002767 /*
2768 * Allocate the array of outstanding commands
2769 * now that we know the firmware resources.
2770 */
2771 rval = qla2x00_alloc_outstanding_cmds(ha,
2772 vha->req);
2773 if (rval != QLA_SUCCESS)
2774 goto failed;
2775
Saurav Kashyapbe5ea3c2011-11-18 09:02:11 -08002776 if (!fw_major_version && ql2xallocfwdump
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002777 && !(IS_P3P_TYPE(ha)))
Giridhar Malavali08de2842011-08-16 11:31:44 -07002778 qla2x00_alloc_fw_dump(vha);
Chad Dupuis3b6e5b92013-10-30 03:38:09 -04002779 } else {
2780 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002783 ql_log(ql_log_fatal, vha, 0x00cd,
2784 "ISP Firmware failed checksum.\n");
2785 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
Andrew Vasquezc74d88a2012-08-22 14:21:19 -04002787 } else
2788 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Andrew Vasquez3db06522008-01-31 12:33:49 -08002790 if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
2791 /* Enable proper parity. */
2792 spin_lock_irqsave(&ha->hardware_lock, flags);
2793 if (IS_QLA2300(ha))
2794 /* SRAM parity */
2795 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
2796 else
2797 /* SRAM, Instruction RAM and GP RAM parity */
2798 WRT_REG_WORD(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
2799 RD_REG_WORD(&reg->hccr);
2800 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2801 }
2802
Chad Dupuisf3982d82014-09-25 05:16:57 -04002803 if (IS_QLA27XX(ha))
2804 ha->flags.fac_supported = 1;
2805 else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07002806 uint32_t size;
2807
2808 rval = qla81xx_fac_get_sector_size(vha, &size);
2809 if (rval == QLA_SUCCESS) {
2810 ha->flags.fac_supported = 1;
2811 ha->fdt_block_size = size << 2;
2812 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002813 ql_log(ql_log_warn, vha, 0x00ce,
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07002814 "Unsupported FAC firmware (%d.%02d.%02d).\n",
2815 ha->fw_major_version, ha->fw_minor_version,
2816 ha->fw_subminor_version);
Joe Carnuccio1ca60e32014-02-26 04:15:02 -05002817
Chad Dupuisf73cb692014-02-26 04:15:06 -05002818 if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08002819 ha->flags.fac_supported = 0;
2820 rval = QLA_SUCCESS;
2821 }
Joe Carnuccio1d2874d2009-03-24 09:08:06 -07002822 }
2823 }
Andrew Vasquezca9e9c32009-06-03 09:55:20 -07002824failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002826 ql_log(ql_log_fatal, vha, 0x00cf,
2827 "Setup chip ****FAILED****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829
2830 return (rval);
2831}
2832
2833/**
2834 * qla2x00_init_response_q_entries() - Initializes response queue entries.
2835 * @ha: HA context
2836 *
2837 * Beginning of request ring has initialization control block already built
2838 * by nvram config routine.
2839 *
2840 * Returns 0 on success.
2841 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08002842void
2843qla2x00_init_response_q_entries(struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 uint16_t cnt;
2846 response_t *pkt;
2847
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07002848 rsp->ring_ptr = rsp->ring;
2849 rsp->ring_index = 0;
2850 rsp->status_srb = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002851 pkt = rsp->ring_ptr;
2852 for (cnt = 0; cnt < rsp->length; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 pkt->signature = RESPONSE_PROCESSED;
2854 pkt++;
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
2858/**
2859 * qla2x00_update_fw_options() - Read and process firmware options.
2860 * @ha: HA context
2861 *
2862 * Returns 0 on success.
2863 */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002864void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002865qla2x00_update_fw_options(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 uint16_t swing, emphasis, tx_sens, rx_sens;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002868 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 memset(ha->fw_options, 0, sizeof(ha->fw_options));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002871 qla2x00_get_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 if (IS_QLA2100(ha) || IS_QLA2200(ha))
2874 return;
2875
2876 /* Serial Link options. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002877 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
2878 "Serial link options.\n");
2879 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
2880 (uint8_t *)&ha->fw_seriallink_options,
2881 sizeof(ha->fw_seriallink_options));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
2884 if (ha->fw_seriallink_options[3] & BIT_2) {
2885 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
2886
2887 /* 1G settings */
2888 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
2889 emphasis = (ha->fw_seriallink_options[2] &
2890 (BIT_4 | BIT_3)) >> 3;
2891 tx_sens = ha->fw_seriallink_options[0] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002892 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 rx_sens = (ha->fw_seriallink_options[0] &
2894 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
2895 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
2896 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
2897 if (rx_sens == 0x0)
2898 rx_sens = 0x3;
2899 ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
2900 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
2901 ha->fw_options[10] |= BIT_5 |
2902 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
2903 (tx_sens & (BIT_1 | BIT_0));
2904
2905 /* 2G settings */
2906 swing = (ha->fw_seriallink_options[2] &
2907 (BIT_7 | BIT_6 | BIT_5)) >> 5;
2908 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
2909 tx_sens = ha->fw_seriallink_options[1] &
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002910 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 rx_sens = (ha->fw_seriallink_options[1] &
2912 (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
2913 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
2914 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
2915 if (rx_sens == 0x0)
2916 rx_sens = 0x3;
2917 ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
2918 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
2919 ha->fw_options[11] |= BIT_5 |
2920 ((rx_sens & (BIT_1 | BIT_0)) << 2) |
2921 (tx_sens & (BIT_1 | BIT_0));
2922 }
2923
2924 /* FCP2 options. */
2925 /* Return command IOCBs without waiting for an ABTS to complete. */
2926 ha->fw_options[3] |= BIT_13;
2927
2928 /* LED scheme. */
2929 if (ha->flags.enable_led_scheme)
2930 ha->fw_options[2] |= BIT_12;
2931
andrew.vasquez@qlogic.com48c02fd2006-03-09 14:27:18 -08002932 /* Detect ISP6312. */
2933 if (IS_QLA6312(ha))
2934 ha->fw_options[2] |= BIT_13;
2935
Giridhar Malavali088d09d2016-07-06 11:14:20 -04002936 /* Set Retry FLOGI in case of P2P connection */
2937 if (ha->operating_mode == P2P) {
2938 ha->fw_options[2] |= BIT_3;
2939 ql_dbg(ql_dbg_disc, vha, 0x2100,
2940 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
2941 __func__, ha->fw_options[2]);
2942 }
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /* Update firmware options. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002945 qla2x00_set_fw_options(vha, ha->fw_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002948void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002949qla24xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002950{
2951 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08002952 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002953
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04002954 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07002955 return;
2956
Himanshu Madhanif198caf2016-01-27 12:03:30 -05002957 /* Hold status IOCBs until ABTS response received. */
2958 if (ql2xfwholdabts)
2959 ha->fw_options[3] |= BIT_12;
2960
Giridhar Malavali088d09d2016-07-06 11:14:20 -04002961 /* Set Retry FLOGI in case of P2P connection */
2962 if (ha->operating_mode == P2P) {
2963 ha->fw_options[2] |= BIT_3;
2964 ql_dbg(ql_dbg_disc, vha, 0x2101,
2965 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
2966 __func__, ha->fw_options[2]);
2967 }
2968
Quinn Tran41dc5292017-01-19 22:28:03 -08002969 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
Quinn Tran3c4810f2017-06-02 09:11:53 -07002970 if (ql2xmvasynctoatio &&
2971 (IS_QLA83XX(ha) || IS_QLA27XX(ha))) {
Quinn Tran41dc5292017-01-19 22:28:03 -08002972 if (qla_tgt_mode_enabled(vha) ||
2973 qla_dual_mode_enabled(vha))
2974 ha->fw_options[2] |= BIT_11;
2975 else
2976 ha->fw_options[2] &= ~BIT_11;
2977 }
2978
Quinn Tranf7e761f2017-06-02 09:12:02 -07002979 if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
2980 /*
2981 * Tell FW to track each exchange to prevent
2982 * driver from using stale exchange.
2983 */
2984 if (qla_tgt_mode_enabled(vha) ||
2985 qla_dual_mode_enabled(vha))
2986 ha->fw_options[2] |= BIT_4;
2987 else
2988 ha->fw_options[2] &= ~BIT_4;
2989 }
2990
Quinn Tran83548fe2017-06-02 09:12:01 -07002991 ql_dbg(ql_dbg_init, vha, 0x00e8,
2992 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
2993 __func__, ha->fw_options[1], ha->fw_options[2],
2994 ha->fw_options[3], vha->host->active_mode);
Quinn Tran3c4810f2017-06-02 09:11:53 -07002995
2996 if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
2997 qla2x00_set_fw_options(vha, ha->fw_options);
Quinn Tran41dc5292017-01-19 22:28:03 -08002998
Andrew Vasquez0107109e2005-07-06 10:31:37 -07002999 /* Update Serial Link options. */
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003000 if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003001 return;
3002
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003003 rval = qla2x00_set_serdes_params(vha,
andrew.vasquez@qlogic.comf94097e2006-01-13 17:05:32 -08003004 le16_to_cpu(ha->fw_seriallink_options24[1]),
3005 le16_to_cpu(ha->fw_seriallink_options24[2]),
3006 le16_to_cpu(ha->fw_seriallink_options24[3]));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003007 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003008 ql_log(ql_log_warn, vha, 0x0104,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003009 "Unable to update Serial Link options (%x).\n", rval);
3010 }
3011}
3012
3013void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003014qla2x00_config_rings(struct scsi_qla_host *vha)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003015{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003016 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003017 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003018 struct req_que *req = ha->req_q_map[0];
3019 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003020
3021 /* Setup ring parameters in initialization control block. */
Bart Van Asschead950362015-07-09 07:24:08 -07003022 ha->init_cb->request_q_outpointer = cpu_to_le16(0);
3023 ha->init_cb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003024 ha->init_cb->request_q_length = cpu_to_le16(req->length);
3025 ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
3026 ha->init_cb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3027 ha->init_cb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3028 ha->init_cb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3029 ha->init_cb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003030
3031 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), 0);
3032 WRT_REG_WORD(ISP_REQ_Q_OUT(ha, reg), 0);
3033 WRT_REG_WORD(ISP_RSP_Q_IN(ha, reg), 0);
3034 WRT_REG_WORD(ISP_RSP_Q_OUT(ha, reg), 0);
3035 RD_REG_WORD(ISP_RSP_Q_OUT(ha, reg)); /* PCI Posting. */
3036}
3037
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003038void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003039qla24xx_config_rings(struct scsi_qla_host *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003040{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003041 struct qla_hw_data *ha = vha->hw;
Bart Van Assche118e2ef2015-07-09 07:24:27 -07003042 device_reg_t *reg = ISP_QUE_REG(ha, 0);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003043 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
3044 struct qla_msix_entry *msix;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003045 struct init_cb_24xx *icb;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003046 uint16_t rid = 0;
3047 struct req_que *req = ha->req_q_map[0];
3048 struct rsp_que *rsp = ha->rsp_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003049
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003050 /* Setup ring parameters in initialization control block. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003051 icb = (struct init_cb_24xx *)ha->init_cb;
Bart Van Asschead950362015-07-09 07:24:08 -07003052 icb->request_q_outpointer = cpu_to_le16(0);
3053 icb->response_q_inpointer = cpu_to_le16(0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003054 icb->request_q_length = cpu_to_le16(req->length);
3055 icb->response_q_length = cpu_to_le16(rsp->length);
3056 icb->request_q_address[0] = cpu_to_le32(LSD(req->dma));
3057 icb->request_q_address[1] = cpu_to_le32(MSD(req->dma));
3058 icb->response_q_address[0] = cpu_to_le32(LSD(rsp->dma));
3059 icb->response_q_address[1] = cpu_to_le32(MSD(rsp->dma));
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003060
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003061 /* Setup ATIO queue dma pointers for target mode */
Bart Van Asschead950362015-07-09 07:24:08 -07003062 icb->atio_q_inpointer = cpu_to_le16(0);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003063 icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
3064 icb->atio_q_address[0] = cpu_to_le32(LSD(ha->tgt.atio_dma));
3065 icb->atio_q_address[1] = cpu_to_le32(MSD(ha->tgt.atio_dma));
3066
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003067 if (IS_SHADOW_REG_CAPABLE(ha))
Bart Van Asschead950362015-07-09 07:24:08 -07003068 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003069
Chad Dupuisf73cb692014-02-26 04:15:06 -05003070 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003071 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
3072 icb->rid = cpu_to_le16(rid);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003073 if (ha->flags.msix_enabled) {
3074 msix = &ha->msix_entries[1];
Quinn Tran83548fe2017-06-02 09:12:01 -07003075 ql_dbg(ql_dbg_init, vha, 0x0019,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003076 "Registering vector 0x%x for base que.\n",
3077 msix->entry);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003078 icb->msix = cpu_to_le16(msix->entry);
3079 }
3080 /* Use alternate PCI bus number */
3081 if (MSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003082 icb->firmware_options_2 |= cpu_to_le32(BIT_19);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003083 /* Use alternate PCI devfn */
3084 if (LSB(rid))
Bart Van Asschead950362015-07-09 07:24:08 -07003085 icb->firmware_options_2 |= cpu_to_le32(BIT_18);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003086
Anirban Chakraborty31557542009-12-02 10:36:55 -08003087 /* Use Disable MSIX Handshake mode for capable adapters */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003088 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
3089 (ha->flags.msix_enabled)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003090 icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003091 ha->flags.disable_msix_handshake = 1;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003092 ql_dbg(ql_dbg_init, vha, 0x00fe,
3093 "MSIX Handshake Disable Mode turned on.\n");
Anirban Chakraborty31557542009-12-02 10:36:55 -08003094 } else {
Bart Van Asschead950362015-07-09 07:24:08 -07003095 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Anirban Chakraborty31557542009-12-02 10:36:55 -08003096 }
Bart Van Asschead950362015-07-09 07:24:08 -07003097 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003098
3099 WRT_REG_DWORD(&reg->isp25mq.req_q_in, 0);
3100 WRT_REG_DWORD(&reg->isp25mq.req_q_out, 0);
3101 WRT_REG_DWORD(&reg->isp25mq.rsp_q_in, 0);
3102 WRT_REG_DWORD(&reg->isp25mq.rsp_q_out, 0);
3103 } else {
3104 WRT_REG_DWORD(&reg->isp24.req_q_in, 0);
3105 WRT_REG_DWORD(&reg->isp24.req_q_out, 0);
3106 WRT_REG_DWORD(&reg->isp24.rsp_q_in, 0);
3107 WRT_REG_DWORD(&reg->isp24.rsp_q_out, 0);
3108 }
Arun Easiaa230bc2013-01-30 03:34:39 -05003109 qlt_24xx_config_rings(vha);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003110
Anirban Chakraborty73208df2008-12-09 16:45:39 -08003111 /* PCI posting */
3112 RD_REG_DWORD(&ioreg->hccr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003113}
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115/**
3116 * qla2x00_init_rings() - Initializes firmware.
3117 * @ha: HA context
3118 *
3119 * Beginning of request ring has initialization control block already built
3120 * by nvram config routine.
3121 *
3122 * Returns 0 on success.
3123 */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003124int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003125qla2x00_init_rings(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
3127 int rval;
3128 unsigned long flags = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003129 int cnt, que;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003130 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003131 struct req_que *req;
3132 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003133 struct mid_init_cb_24xx *mid_init_cb =
3134 (struct mid_init_cb_24xx *) ha->init_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 spin_lock_irqsave(&ha->hardware_lock, flags);
3137
3138 /* Clear outstanding commands array. */
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003139 for (que = 0; que < ha->max_req_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003140 req = ha->req_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003141 if (!req || !test_bit(que, ha->req_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003142 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003143 req->out_ptr = (void *)(req->ring + req->length);
3144 *req->out_ptr = 0;
Chad Dupuis8d93f552013-01-30 03:34:37 -05003145 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003146 req->outstanding_cmds[cnt] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003148 req->current_outstanding_cmd = 1;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003149
3150 /* Initialize firmware. */
3151 req->ring_ptr = req->ring;
3152 req->ring_index = 0;
3153 req->cnt = req->length;
3154 }
3155
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07003156 for (que = 0; que < ha->max_rsp_queues; que++) {
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003157 rsp = ha->rsp_q_map[que];
Quinn Trancb432852016-02-04 11:45:16 -05003158 if (!rsp || !test_bit(que, ha->rsp_qid_map))
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003159 continue;
Joe Carnuccio7c6300e2014-04-11 16:54:37 -04003160 rsp->in_ptr = (void *)(rsp->ring + rsp->length);
3161 *rsp->in_ptr = 0;
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003162 /* Initialize response queue entries */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003163 if (IS_QLAFX00(ha))
3164 qlafx00_init_response_q_entries(rsp);
3165 else
3166 qla2x00_init_response_q_entries(rsp);
Anirban Chakraborty29bdccb2009-01-08 15:41:08 -08003167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003169 ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
3170 ha->tgt.atio_ring_index = 0;
3171 /* Initialize ATIO queue entries */
3172 qlt_init_atio_q_entries(vha);
3173
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003174 ha->isp_ops->config_rings(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3177
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003178 ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
3179
3180 if (IS_QLAFX00(ha)) {
3181 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
3182 goto next_check;
3183 }
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 /* Update any ISP specific firmware options before initialization. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003186 ha->isp_ops->update_fw_options(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003188 if (ha->flags.npiv_supported) {
Saurav Kashyap45980cc2012-08-22 14:21:21 -04003189 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003190 ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
Seokmann Juc48339d2008-01-17 09:02:19 -08003191 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
Lalit Chandivade605aa2b2009-03-05 11:07:01 -08003192 }
3193
Andrew Vasquez24a08132009-03-24 09:08:16 -07003194 if (IS_FWI2_CAPABLE(ha)) {
Bart Van Asschead950362015-07-09 07:24:08 -07003195 mid_init_cb->options = cpu_to_le16(BIT_1);
Andrew Vasquez24a08132009-03-24 09:08:16 -07003196 mid_init_cb->init_cb.execution_throttle =
Quinn Tran03e8c682015-12-17 14:56:59 -05003197 cpu_to_le16(ha->cur_fw_xcb_count);
Joe Carnuccio40f38622016-07-06 11:14:28 -04003198 ha->flags.dport_enabled =
3199 (mid_init_cb->init_cb.firmware_options_1 & BIT_7) != 0;
3200 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
3201 (ha->flags.dport_enabled) ? "enabled" : "disabled");
3202 /* FA-WWPN Status */
Himanshu Madhani2486c622014-09-25 05:17:00 -04003203 ha->flags.fawwpn_enabled =
Joe Carnuccio40f38622016-07-06 11:14:28 -04003204 (mid_init_cb->init_cb.firmware_options_1 & BIT_6) != 0;
Quinn Tran83548fe2017-06-02 09:12:01 -07003205 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
Himanshu Madhani2486c622014-09-25 05:17:00 -04003206 (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
Andrew Vasquez24a08132009-03-24 09:08:16 -07003207 }
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003208
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003209 rval = qla2x00_init_firmware(vha, ha->init_cb_size);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003210next_check:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003212 ql_log(ql_log_fatal, vha, 0x00d2,
3213 "Init Firmware **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003215 ql_dbg(ql_dbg_init, vha, 0x00d3,
3216 "Init Firmware -- success.\n");
Quinn Tran4b60c822017-06-13 20:47:21 -07003217 QLA_FW_STARTED(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
3219
3220 return (rval);
3221}
3222
3223/**
3224 * qla2x00_fw_ready() - Waits for firmware ready.
3225 * @ha: HA context
3226 *
3227 * Returns 0 on success.
3228 */
3229static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003230qla2x00_fw_ready(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231{
3232 int rval;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003233 unsigned long wtime, mtime, cs84xx_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 uint16_t min_wait; /* Minimum wait time if loop is down */
3235 uint16_t wait_time; /* Wait time if loop is coming ready */
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003236 uint16_t state[6];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003237 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04003239 if (IS_QLAFX00(vha->hw))
3240 return qlafx00_fw_ready(vha);
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 rval = QLA_SUCCESS;
3243
Chad Dupuis334614912015-04-09 14:59:57 -04003244 /* Time to wait for loop down */
3245 if (IS_P3P_TYPE(ha))
3246 min_wait = 30;
3247 else
3248 min_wait = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 /*
3251 * Firmware should take at most one RATOV to login, plus 5 seconds for
3252 * our own processing.
3253 */
3254 if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
3255 wait_time = min_wait;
3256 }
3257
3258 /* Min wait time if loop down */
3259 mtime = jiffies + (min_wait * HZ);
3260
3261 /* wait time before firmware ready */
3262 wtime = jiffies + (wait_time * HZ);
3263
3264 /* Wait for ISP to finish LIP */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003265 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003266 ql_log(ql_log_info, vha, 0x801e,
3267 "Waiting for LIP to complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 do {
Andrew Vasquez5b939032012-11-21 02:40:26 -05003270 memset(state, -1, sizeof(state));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003271 rval = qla2x00_get_firmware_state(vha, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (rval == QLA_SUCCESS) {
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003273 if (state[0] < FSTATE_LOSS_OF_SYNC) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003274 vha->device_flags &= ~DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003276 if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003277 ql_dbg(ql_dbg_taskm, vha, 0x801f,
3278 "fw_state=%x 84xx=%x.\n", state[0],
3279 state[2]);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003280 if ((state[2] & FSTATE_LOGGED_IN) &&
3281 (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003282 ql_dbg(ql_dbg_taskm, vha, 0x8028,
3283 "Sending verify iocb.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003284
3285 cs84xx_time = jiffies;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003286 rval = qla84xx_init_chip(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003287 if (rval != QLA_SUCCESS) {
3288 ql_log(ql_log_warn,
Chad Dupuiscfb09192011-11-18 09:03:07 -08003289 vha, 0x8007,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003290 "Init chip failed.\n");
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003291 break;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003292 }
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003293
3294 /* Add time taken to initialize. */
3295 cs84xx_time = jiffies - cs84xx_time;
3296 wtime += cs84xx_time;
3297 mtime += cs84xx_time;
Chad Dupuiscfb09192011-11-18 09:03:07 -08003298 ql_dbg(ql_dbg_taskm, vha, 0x8008,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003299 "Increasing wait time by %ld. "
3300 "New time %ld.\n", cs84xx_time,
3301 wtime);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003302 }
3303 } else if (state[0] == FSTATE_READY) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003304 ql_dbg(ql_dbg_taskm, vha, 0x8037,
3305 "F/W Ready - OK.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003307 qla2x00_get_retry_cnt(vha, &ha->retry_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 &ha->login_timeout, &ha->r_a_tov);
3309
3310 rval = QLA_SUCCESS;
3311 break;
3312 }
3313
3314 rval = QLA_FUNCTION_FAILED;
3315
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003316 if (atomic_read(&vha->loop_down_timer) &&
Harihara Kadayam4d4df192008-04-03 13:13:26 -07003317 state[0] != FSTATE_READY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /* Loop down. Timeout on min_wait for states
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003319 * other than Wait for Login.
3320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (time_after_eq(jiffies, mtime)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003322 ql_log(ql_log_info, vha, 0x8038,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 "Cable is unplugged...\n");
3324
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003325 vha->device_flags |= DFLG_NO_CABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 break;
3327 }
3328 }
3329 } else {
3330 /* Mailbox cmd failed. Timeout on min_wait. */
Santosh Vernekarcdbb0a4f2010-05-28 15:08:25 -07003331 if (time_after_eq(jiffies, mtime) ||
Giridhar Malavali71905752011-02-23 15:27:10 -08003332 ha->flags.isp82xx_fw_hung)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 break;
3334 }
3335
3336 if (time_after_eq(jiffies, wtime))
3337 break;
3338
3339 /* Delay for a while */
3340 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 } while (1);
3342
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003343 ql_dbg(ql_dbg_taskm, vha, 0x803a,
Joe Carnucciob5a340d2014-09-25 05:16:48 -04003344 "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
3345 state[1], state[2], state[3], state[4], state[5], jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Chad Dupuiscfb09192011-11-18 09:03:07 -08003347 if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003348 ql_log(ql_log_warn, vha, 0x803b,
3349 "Firmware ready **** FAILED ****.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
3351
3352 return (rval);
3353}
3354
3355/*
3356* qla2x00_configure_hba
3357* Setup adapter context.
3358*
3359* Input:
3360* ha = adapter state pointer.
3361*
3362* Returns:
3363* 0 = success
3364*
3365* Context:
3366* Kernel context.
3367*/
3368static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003369qla2x00_configure_hba(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
3371 int rval;
3372 uint16_t loop_id;
3373 uint16_t topo;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003374 uint16_t sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 uint8_t al_pa;
3376 uint8_t area;
3377 uint8_t domain;
3378 char connect_type[22];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003379 struct qla_hw_data *ha = vha->hw;
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003380 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran482c9dc2017-03-15 09:48:54 -07003381 port_id_t id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
3383 /* Get host addresses. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003384 rval = qla2x00_get_adapter_id(vha,
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003385 &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (rval != QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003387 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003388 IS_CNA_CAPABLE(ha) ||
Ravi Anand33135aa2005-11-08 14:37:20 -08003389 (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003390 ql_dbg(ql_dbg_disc, vha, 0x2008,
3391 "Loop is in a transition state.\n");
Ravi Anand33135aa2005-11-08 14:37:20 -08003392 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003393 ql_log(ql_log_warn, vha, 0x2009,
3394 "Unable to get host loop ID.\n");
Joe Carnuccio61e1b262013-02-08 01:57:48 -05003395 if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
3396 (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
3397 ql_log(ql_log_warn, vha, 0x1151,
3398 "Doing link init.\n");
3399 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
3400 return rval;
3401 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003402 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Ravi Anand33135aa2005-11-08 14:37:20 -08003403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return (rval);
3405 }
3406
3407 if (topo == 4) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003408 ql_log(ql_log_info, vha, 0x200a,
3409 "Cannot get topology - retrying.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return (QLA_FUNCTION_FAILED);
3411 }
3412
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003413 vha->loop_id = loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
3415 /* initialize */
3416 ha->min_external_loopid = SNS_FIRST_LOOP_ID;
3417 ha->operating_mode = LOOP;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003418 ha->switch_cap = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 switch (topo) {
3421 case 0:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003422 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 ha->current_topology = ISP_CFG_NL;
3424 strcpy(connect_type, "(Loop)");
3425 break;
3426
3427 case 1:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003428 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003429 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 ha->current_topology = ISP_CFG_FL;
3431 strcpy(connect_type, "(FL_Port)");
3432 break;
3433
3434 case 2:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003435 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 ha->operating_mode = P2P;
3437 ha->current_topology = ISP_CFG_N;
3438 strcpy(connect_type, "(N_Port-to-N_Port)");
3439 break;
3440
3441 case 3:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003442 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07003443 ha->switch_cap = sw_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 ha->operating_mode = P2P;
3445 ha->current_topology = ISP_CFG_F;
3446 strcpy(connect_type, "(F_Port)");
3447 break;
3448
3449 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003450 ql_dbg(ql_dbg_disc, vha, 0x200f,
3451 "HBA in unknown topology %x, using NL.\n", topo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 ha->current_topology = ISP_CFG_NL;
3453 strcpy(connect_type, "(Loop)");
3454 break;
3455 }
3456
3457 /* Save Host port and loop ID. */
3458 /* byte order - Big Endian */
Quinn Tran482c9dc2017-03-15 09:48:54 -07003459 id.b.domain = domain;
3460 id.b.area = area;
3461 id.b.al_pa = al_pa;
3462 id.b.rsvd_1 = 0;
3463 qlt_update_host_map(vha, id);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003464
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003465 if (!vha->flags.init_done)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003466 ql_log(ql_log_info, vha, 0x2010,
3467 "Topology - %s, Host Loop address 0x%x.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003468 connect_type, vha->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return(rval);
3471}
3472
Giridhar Malavalia9083012010-04-12 17:59:55 -07003473inline void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003474qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
3475 char *def)
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003476{
3477 char *st, *en;
3478 uint16_t index;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003479 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezab671142009-08-25 11:36:17 -07003480 int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08003481 !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003482
3483 if (memcmp(model, BINZERO, len) != 0) {
3484 strncpy(ha->model_number, model, len);
3485 st = en = ha->model_number;
3486 en += len - 1;
3487 while (en > st) {
3488 if (*en != 0x20 && *en != 0x00)
3489 break;
3490 *en-- = '\0';
3491 }
3492
3493 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07003494 if (use_tbl &&
3495 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003496 index < QLA_MODEL_NAMES)
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003497 strncpy(ha->model_desc,
3498 qla2x00_model_name[index * 2 + 1],
3499 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003500 } else {
3501 index = (ha->pdev->subsystem_device & 0xff);
Andrew Vasquez7d0dba12009-04-06 22:33:45 -07003502 if (use_tbl &&
3503 ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003504 index < QLA_MODEL_NAMES) {
3505 strcpy(ha->model_number,
3506 qla2x00_model_name[index * 2]);
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003507 strncpy(ha->model_desc,
3508 qla2x00_model_name[index * 2 + 1],
3509 sizeof(ha->model_desc) - 1);
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003510 } else {
3511 strcpy(ha->model_number, def);
3512 }
3513 }
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003514 if (IS_FWI2_CAPABLE(ha))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003515 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
Joe Carnuccio1ee27142008-07-10 16:55:53 -07003516 sizeof(ha->model_desc));
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003517}
3518
David Miller4e08df32007-04-16 12:37:43 -07003519/* On sparc systems, obtain port and node WWN from firmware
3520 * properties.
3521 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003522static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
David Miller4e08df32007-04-16 12:37:43 -07003523{
3524#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003525 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07003526 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07003527 struct device_node *dp = pci_device_to_OF_node(pdev);
3528 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07003529 int len;
3530
3531 val = of_get_property(dp, "port-wwn", &len);
3532 if (val && len >= WWN_SIZE)
3533 memcpy(nv->port_name, val, WWN_SIZE);
3534
3535 val = of_get_property(dp, "node-wwn", &len);
3536 if (val && len >= WWN_SIZE)
3537 memcpy(nv->node_name, val, WWN_SIZE);
3538#endif
3539}
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541/*
3542* NVRAM configuration for ISP 2xxx
3543*
3544* Input:
3545* ha = adapter block pointer.
3546*
3547* Output:
3548* initialization control block in response_ring
3549* host adapters parameters in host adapter block
3550*
3551* Returns:
3552* 0 = success.
3553*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07003554int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003555qla2x00_nvram_config(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
David Miller4e08df32007-04-16 12:37:43 -07003557 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003558 uint8_t chksum = 0;
3559 uint16_t cnt;
3560 uint8_t *dptr1, *dptr2;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003561 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003562 init_cb_t *icb = ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07003563 nvram_t *nv = ha->nvram;
3564 uint8_t *ptr = ha->nvram;
Andrew Vasquez3d716442005-07-06 10:30:26 -07003565 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
David Miller4e08df32007-04-16 12:37:43 -07003567 rval = QLA_SUCCESS;
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 /* Determine NVRAM starting address. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003570 ha->nvram_size = sizeof(nvram_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 ha->nvram_base = 0;
3572 if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
3573 if ((RD_REG_WORD(&reg->ctrl_status) >> 14) == 1)
3574 ha->nvram_base = 0x80;
3575
3576 /* Get NVRAM data and calculate checksum. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003577 ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003578 for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
3579 chksum += *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003581 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
3582 "Contents of NVRAM.\n");
3583 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
3584 (uint8_t *)nv, ha->nvram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 /* Bad NVRAM data, set defaults parameters. */
3587 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' ||
3588 nv->id[2] != 'P' || nv->id[3] != ' ' || nv->nvram_version < 1) {
3589 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003590 ql_log(ql_log_warn, vha, 0x0064,
Raul Porcel9e336522012-05-15 14:34:08 -04003591 "Inconsistent NVRAM "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003592 "detected: checksum=0x%x id=%c version=0x%x.\n",
3593 chksum, nv->id[0], nv->nvram_version);
3594 ql_log(ql_log_warn, vha, 0x0065,
3595 "Falling back to "
3596 "functioning (yet invalid -- WWPN) defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07003597
3598 /*
3599 * Set default initialization control block.
3600 */
3601 memset(nv, 0, ha->nvram_size);
3602 nv->parameter_block_version = ICB_VERSION;
3603
3604 if (IS_QLA23XX(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 = 2048;
David Miller4e08df32007-04-16 12:37:43 -07003610 nv->special_options[1] = BIT_7;
3611 } else if (IS_QLA2200(ha)) {
3612 nv->firmware_options[0] = BIT_2 | BIT_1;
3613 nv->firmware_options[1] = BIT_7 | BIT_5;
3614 nv->add_firmware_options[0] = BIT_5;
3615 nv->add_firmware_options[1] = BIT_5 | BIT_4;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003616 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07003617 } else if (IS_QLA2100(ha)) {
3618 nv->firmware_options[0] = BIT_3 | BIT_1;
3619 nv->firmware_options[1] = BIT_5;
Joe Carnuccio98aee702014-09-25 05:16:38 -04003620 nv->frame_payload_size = 1024;
David Miller4e08df32007-04-16 12:37:43 -07003621 }
3622
Bart Van Asschead950362015-07-09 07:24:08 -07003623 nv->max_iocb_allocation = cpu_to_le16(256);
3624 nv->execution_throttle = cpu_to_le16(16);
David Miller4e08df32007-04-16 12:37:43 -07003625 nv->retry_count = 8;
3626 nv->retry_delay = 1;
3627
3628 nv->port_name[0] = 33;
3629 nv->port_name[3] = 224;
3630 nv->port_name[4] = 139;
3631
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003632 qla2xxx_nvram_wwn_from_ofw(vha, nv);
David Miller4e08df32007-04-16 12:37:43 -07003633
3634 nv->login_timeout = 4;
3635
3636 /*
3637 * Set default host adapter parameters
3638 */
3639 nv->host_p[1] = BIT_2;
3640 nv->reset_delay = 5;
3641 nv->port_down_retry_count = 8;
Bart Van Asschead950362015-07-09 07:24:08 -07003642 nv->max_luns_per_target = cpu_to_le16(8);
David Miller4e08df32007-04-16 12:37:43 -07003643 nv->link_down_timeout = 60;
3644
3645 rval = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647
3648#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_SGI_SN2)
3649 /*
3650 * The SN2 does not provide BIOS emulation which means you can't change
3651 * potentially bogus BIOS settings. Force the use of default settings
3652 * for link rate and frame size. Hope that the rest of the settings
3653 * are valid.
3654 */
3655 if (ia64_platform_is("sn2")) {
Joe Carnuccio98aee702014-09-25 05:16:38 -04003656 nv->frame_payload_size = 2048;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (IS_QLA23XX(ha))
3658 nv->special_options[1] = BIT_7;
3659 }
3660#endif
3661
3662 /* Reset Initialization control block */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003663 memset(icb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 /*
3666 * Setup driver NVRAM options.
3667 */
3668 nv->firmware_options[0] |= (BIT_6 | BIT_1);
3669 nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
3670 nv->firmware_options[1] |= (BIT_5 | BIT_0);
3671 nv->firmware_options[1] &= ~BIT_4;
3672
3673 if (IS_QLA23XX(ha)) {
3674 nv->firmware_options[0] |= BIT_2;
3675 nv->firmware_options[0] &= ~BIT_3;
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04003676 nv->special_options[0] &= ~BIT_6;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07003677 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679 if (IS_QLA2300(ha)) {
3680 if (ha->fb_rev == FPM_2310) {
3681 strcpy(ha->model_number, "QLA2310");
3682 } else {
3683 strcpy(ha->model_number, "QLA2300");
3684 }
3685 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003686 qla2x00_set_model_info(vha, nv->model_number,
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08003687 sizeof(nv->model_number), "QLA23xx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 }
3689 } else if (IS_QLA2200(ha)) {
3690 nv->firmware_options[0] |= BIT_2;
3691 /*
3692 * 'Point-to-point preferred, else loop' is not a safe
3693 * connection mode setting.
3694 */
3695 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
3696 (BIT_5 | BIT_4)) {
3697 /* Force 'loop preferred, else point-to-point'. */
3698 nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
3699 nv->add_firmware_options[0] |= BIT_5;
3700 }
3701 strcpy(ha->model_number, "QLA22xx");
3702 } else /*if (IS_QLA2100(ha))*/ {
3703 strcpy(ha->model_number, "QLA2100");
3704 }
3705
3706 /*
3707 * Copy over NVRAM RISC parameter block to initialization control block.
3708 */
3709 dptr1 = (uint8_t *)icb;
3710 dptr2 = (uint8_t *)&nv->parameter_block_version;
3711 cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
3712 while (cnt--)
3713 *dptr1++ = *dptr2++;
3714
3715 /* Copy 2nd half. */
3716 dptr1 = (uint8_t *)icb->add_firmware_options;
3717 cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
3718 while (cnt--)
3719 *dptr1++ = *dptr2++;
3720
Andrew Vasquez5341e862006-05-17 15:09:16 -07003721 /* Use alternate WWN? */
3722 if (nv->host_p[1] & BIT_7) {
3723 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
3724 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
3725 }
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 /* Prepare nodename */
3728 if ((icb->firmware_options[1] & BIT_6) == 0) {
3729 /*
3730 * Firmware will apply the following mask if the nodename was
3731 * not provided.
3732 */
3733 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
3734 icb->node_name[0] &= 0xF0;
3735 }
3736
3737 /*
3738 * Set host adapter parameters.
3739 */
Saurav Kashyap3ce88662011-07-14 12:00:12 -07003740
3741 /*
3742 * BIT_7 in the host-parameters section allows for modification to
3743 * internal driver logging.
3744 */
Andrew Vasquez01819442006-06-23 16:11:10 -07003745 if (nv->host_p[0] & BIT_7)
Chad Dupuiscfb09192011-11-18 09:03:07 -08003746 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
3748 /* Always load RISC code on non ISP2[12]00 chips. */
3749 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
3750 ha->flags.disable_risc_code_load = 0;
3751 ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
3752 ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
3753 ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
Andrew Vasquez06c22bd2005-08-26 19:09:00 -07003754 ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07003755 ha->flags.disable_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757 ha->operating_mode =
3758 (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
3759
3760 memcpy(ha->fw_seriallink_options, nv->seriallink_options,
3761 sizeof(ha->fw_seriallink_options));
3762
3763 /* save HBA serial number */
3764 ha->serial0 = icb->port_name[5];
3765 ha->serial1 = icb->port_name[6];
3766 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003767 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
3768 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Bart Van Asschead950362015-07-09 07:24:08 -07003770 icb->execution_throttle = cpu_to_le16(0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772 ha->retry_count = nv->retry_count;
3773
3774 /* Set minimum login_timeout to 4 seconds. */
Andrew Vasquez5b914902010-05-28 15:08:30 -07003775 if (nv->login_timeout != ql2xlogintimeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 nv->login_timeout = ql2xlogintimeout;
3777 if (nv->login_timeout < 4)
3778 nv->login_timeout = 4;
3779 ha->login_timeout = nv->login_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Andrew Vasquez00a537b2008-02-28 14:06:11 -08003781 /* Set minimum RATOV to 100 tenths of a second. */
3782 ha->r_a_tov = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 ha->loop_reset_delay = nv->reset_delay;
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 /* Link Down Timeout = 0:
3787 *
3788 * When Port Down timer expires we will start returning
3789 * I/O's to OS with "DID_NO_CONNECT".
3790 *
3791 * Link Down Timeout != 0:
3792 *
3793 * The driver waits for the link to come up after link down
3794 * before returning I/Os to OS with "DID_NO_CONNECT".
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 if (nv->link_down_timeout == 0) {
3797 ha->loop_down_abort_time =
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04003798 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 } else {
3800 ha->link_down_timeout = nv->link_down_timeout;
3801 ha->loop_down_abort_time =
3802 (LOOP_DOWN_TIME - ha->link_down_timeout);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07003803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 /*
3806 * Need enough time to try and get the port back.
3807 */
3808 ha->port_down_retry_count = nv->port_down_retry_count;
3809 if (qlport_down_retry)
3810 ha->port_down_retry_count = qlport_down_retry;
3811 /* Set login_retry_count */
3812 ha->login_retry_count = nv->retry_count;
3813 if (ha->port_down_retry_count == nv->port_down_retry_count &&
3814 ha->port_down_retry_count > 3)
3815 ha->login_retry_count = ha->port_down_retry_count;
3816 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
3817 ha->login_retry_count = ha->port_down_retry_count;
3818 if (ql2xloginretrycount)
3819 ha->login_retry_count = ql2xloginretrycount;
3820
Bart Van Asschead950362015-07-09 07:24:08 -07003821 icb->lun_enables = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 icb->command_resource_count = 0;
3823 icb->immediate_notify_resource_count = 0;
Bart Van Asschead950362015-07-09 07:24:08 -07003824 icb->timeout = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3827 /* Enable RIO */
3828 icb->firmware_options[0] &= ~BIT_3;
3829 icb->add_firmware_options[0] &=
3830 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
3831 icb->add_firmware_options[0] |= BIT_2;
3832 icb->response_accumulation_timer = 3;
3833 icb->interrupt_delay_timer = 5;
3834
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003835 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 } else {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003837 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003838 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003839 ha->zio_mode = icb->add_firmware_options[0] &
3840 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
3841 ha->zio_timer = icb->interrupt_delay_timer ?
3842 icb->interrupt_delay_timer: 2;
3843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 icb->add_firmware_options[0] &=
3845 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003846 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003847 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08003848 ha->zio_mode = QLA_ZIO_MODE_6;
3849
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003850 ql_log(ql_log_info, vha, 0x0068,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003851 "ZIO mode %d enabled; timer delay (%d us).\n",
3852 ha->zio_mode, ha->zio_timer * 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07003854 icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
3855 icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003856 vha->flags.process_response_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 }
3858 }
3859
David Miller4e08df32007-04-16 12:37:43 -07003860 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003861 ql_log(ql_log_warn, vha, 0x0069,
3862 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07003863 }
3864 return (rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003867static void
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003868qla2x00_rport_del(void *data)
3869{
3870 fc_port_t *fcport = data;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003871 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08003872 unsigned long flags;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003873
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08003874 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
Andrew Vasquezac280b62009-08-20 11:06:05 -07003875 rport = fcport->drport ? fcport->drport: fcport->rport;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003876 fcport->drport = NULL;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08003877 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
Quinn Tran726b8542017-01-19 22:28:00 -08003878 if (rport) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003879 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
3880 "%s %8phN. rport %p roles %x\n",
3881 __func__, fcport->port_name, rport,
3882 rport->roles);
Quinn Tran726b8542017-01-19 22:28:00 -08003883
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08003884 fc_remote_port_delete(rport);
Quinn Tran726b8542017-01-19 22:28:00 -08003885 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04003886}
3887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888/**
3889 * qla2x00_alloc_fcport() - Allocate a generic fcport.
3890 * @ha: HA context
3891 * @flags: allocation flags
3892 *
3893 * Returns a pointer to the allocated fcport, or NULL, if none available.
3894 */
Giridhar Malavali9a069e12010-01-12 13:02:47 -08003895fc_port_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003896qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897{
3898 fc_port_t *fcport;
3899
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02003900 fcport = kzalloc(sizeof(fc_port_t), flags);
3901 if (!fcport)
3902 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
3904 /* Setup fcport template structure. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003905 fcport->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 fcport->port_type = FCT_UNKNOWN;
3907 fcport->loop_id = FC_NO_LOOP_ID;
Chad Dupuisec426e12011-03-30 11:46:32 -07003908 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07003909 fcport->supported_classes = FC_COS_UNSPECIFIED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Quinn Tran726b8542017-01-19 22:28:00 -08003911 fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
3912 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
Quinn Tran6cb32162017-02-13 12:18:29 -08003913 flags);
Quinn Tran726b8542017-01-19 22:28:00 -08003914 fcport->disc_state = DSC_DELETED;
3915 fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
3916 fcport->deleted = QLA_SESS_DELETED;
3917 fcport->login_retry = vha->hw->login_retry_count;
3918 fcport->login_retry = 5;
3919 fcport->logout_on_delete = 1;
3920
3921 if (!fcport->ct_desc.ct_sns) {
Quinn Tran83548fe2017-06-02 09:12:01 -07003922 ql_log(ql_log_warn, vha, 0xd049,
Quinn Tran726b8542017-01-19 22:28:00 -08003923 "Failed to allocate ct_sns request.\n");
3924 kfree(fcport);
3925 fcport = NULL;
3926 }
3927 INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
3928 INIT_LIST_HEAD(&fcport->gnl_entry);
3929 INIT_LIST_HEAD(&fcport->list);
3930
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02003931 return fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
Quinn Tran726b8542017-01-19 22:28:00 -08003934void
3935qla2x00_free_fcport(fc_port_t *fcport)
3936{
3937 if (fcport->ct_desc.ct_sns) {
3938 dma_free_coherent(&fcport->vha->hw->pdev->dev,
3939 sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
3940 fcport->ct_desc.ct_sns_dma);
3941
3942 fcport->ct_desc.ct_sns = NULL;
3943 }
3944 kfree(fcport);
3945}
3946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947/*
3948 * qla2x00_configure_loop
3949 * Updates Fibre Channel Device Database with what is actually on loop.
3950 *
3951 * Input:
3952 * ha = adapter block pointer.
3953 *
3954 * Returns:
3955 * 0 = success.
3956 * 1 = error.
3957 * 2 = database was full and device was not configured.
3958 */
3959static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003960qla2x00_configure_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 int rval;
3963 unsigned long flags, save_flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003964 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 rval = QLA_SUCCESS;
3966
3967 /* Get Initiator ID */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003968 if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
3969 rval = qla2x00_configure_hba(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003971 ql_dbg(ql_dbg_disc, vha, 0x2013,
3972 "Unable to configure HBA.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return (rval);
3974 }
3975 }
3976
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003977 save_flags = flags = vha->dpc_flags;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07003978 ql_dbg(ql_dbg_disc, vha, 0x2014,
3979 "Configure loop -- dpc flags = 0x%lx.\n", flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 /*
3982 * If we have both an RSCN and PORT UPDATE pending then handle them
3983 * both at the same time.
3984 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08003985 clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
3986 clear_bit(RSCN_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Michael Hernandez3064ff32009-12-15 21:29:44 -08003988 qla2x00_get_data_rate(vha);
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 /* Determine what we need to do */
3991 if (ha->current_topology == ISP_CFG_FL &&
3992 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 set_bit(RSCN_UPDATE, &flags);
3995
3996 } else if (ha->current_topology == ISP_CFG_F &&
3997 (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 set_bit(RSCN_UPDATE, &flags);
4000 clear_bit(LOCAL_LOOP_UPDATE, &flags);
4001
Andrew Vasquez21333b42006-05-17 15:09:56 -07004002 } else if (ha->current_topology == ISP_CFG_N) {
4003 clear_bit(RSCN_UPDATE, &flags);
Quinn Tran41dc5292017-01-19 22:28:03 -08004004 } else if (ha->current_topology == ISP_CFG_NL) {
4005 clear_bit(RSCN_UPDATE, &flags);
4006 set_bit(LOCAL_LOOP_UPDATE, &flags);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004007 } else if (!vha->flags.online ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 (test_bit(ABORT_ISP_ACTIVE, &flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 set_bit(RSCN_UPDATE, &flags);
4010 set_bit(LOCAL_LOOP_UPDATE, &flags);
4011 }
4012
4013 if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004014 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
4015 ql_dbg(ql_dbg_disc, vha, 0x2015,
4016 "Loop resync needed, failing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 rval = QLA_FUNCTION_FAILED;
Chad Dupuis642ef982012-02-09 11:15:57 -08004018 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004019 rval = qla2x00_configure_local_loop(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 }
4021
4022 if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004023 if (LOOP_TRANSITION(vha)) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004024 ql_dbg(ql_dbg_disc, vha, 0x2099,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004025 "Needs RSCN update and loop transition.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 rval = QLA_FUNCTION_FAILED;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004027 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004028 else
4029 rval = qla2x00_configure_fabric(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 }
4031
4032 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004033 if (atomic_read(&vha->loop_down_timer) ||
4034 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 rval = QLA_FUNCTION_FAILED;
4036 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004037 atomic_set(&vha->loop_state, LOOP_READY);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004038 ql_dbg(ql_dbg_disc, vha, 0x2069,
4039 "LOOP READY.\n");
Quinn Tranec7193e2017-03-15 09:48:55 -07004040 ha->flags.fw_init_done = 1;
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004041
4042 /*
4043 * Process any ATIO queue entries that came in
4044 * while we weren't online.
4045 */
Quinn Tranead03852017-01-19 22:28:01 -08004046 if (qla_tgt_mode_enabled(vha) ||
4047 qla_dual_mode_enabled(vha)) {
Dilip Kumar Uppugandla3bb67df2015-12-17 14:57:11 -05004048 if (IS_QLA27XX(ha) || IS_QLA83XX(ha)) {
4049 spin_lock_irqsave(&ha->tgt.atio_lock,
4050 flags);
4051 qlt_24xx_process_atio_queue(vha, 0);
4052 spin_unlock_irqrestore(
4053 &ha->tgt.atio_lock, flags);
4054 } else {
4055 spin_lock_irqsave(&ha->hardware_lock,
4056 flags);
4057 qlt_24xx_process_atio_queue(vha, 1);
4058 spin_unlock_irqrestore(
4059 &ha->hardware_lock, flags);
4060 }
4061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
4063 }
4064
4065 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004066 ql_dbg(ql_dbg_disc, vha, 0x206a,
4067 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004069 ql_dbg(ql_dbg_disc, vha, 0x206b,
4070 "%s: exiting normally.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
4072
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07004073 /* Restore state if a resync event occurred during processing */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004074 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004076 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004077 if (test_bit(RSCN_UPDATE, &save_flags)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004078 set_bit(RSCN_UPDATE, &vha->dpc_flags);
Andrew Vasquezf4658b62009-06-03 09:55:21 -07004079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
4081
4082 return (rval);
4083}
4084
4085
4086
4087/*
4088 * qla2x00_configure_local_loop
4089 * Updates Fibre Channel Device Database with local loop devices.
4090 *
4091 * Input:
4092 * ha = adapter block pointer.
4093 *
4094 * Returns:
4095 * 0 = success.
4096 */
4097static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004098qla2x00_configure_local_loop(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
4100 int rval, rval2;
4101 int found_devs;
4102 int found;
4103 fc_port_t *fcport, *new_fcport;
4104
4105 uint16_t index;
4106 uint16_t entries;
4107 char *id_iter;
4108 uint16_t loop_id;
4109 uint8_t domain, area, al_pa;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004110 struct qla_hw_data *ha = vha->hw;
Quinn Tran41dc5292017-01-19 22:28:03 -08004111 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 found_devs = 0;
4114 new_fcport = NULL;
Chad Dupuis642ef982012-02-09 11:15:57 -08004115 entries = MAX_FIBRE_DEVICES_LOOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 /* Get list of logged in devices. */
Chad Dupuis642ef982012-02-09 11:15:57 -08004118 memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004119 rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 &entries);
4121 if (rval != QLA_SUCCESS)
4122 goto cleanup_allocation;
4123
Quinn Tran83548fe2017-06-02 09:12:01 -07004124 ql_dbg(ql_dbg_disc, vha, 0x2011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004125 "Entries in ID list (%d).\n", entries);
4126 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
4127 (uint8_t *)ha->gid_list,
4128 entries * sizeof(struct gid_list_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004131 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004133 ql_log(ql_log_warn, vha, 0x2012,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004134 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 rval = QLA_MEMORY_ALLOC_FAILED;
4136 goto cleanup_allocation;
4137 }
4138 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4139
4140 /*
4141 * Mark local devices that were present with FCF_DEVICE_LOST for now.
4142 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004143 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (atomic_read(&fcport->state) == FCS_ONLINE &&
4145 fcport->port_type != FCT_BROADCAST &&
4146 (fcport->flags & FCF_FABRIC_DEVICE) == 0) {
4147
Quinn Tran83548fe2017-06-02 09:12:01 -07004148 ql_dbg(ql_dbg_disc, vha, 0x2096,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004149 "Marking port lost loop_id=0x%04x.\n",
4150 fcport->loop_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Quinn Tran41dc5292017-01-19 22:28:03 -08004152 qla2x00_mark_device_lost(vha, fcport, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
4154 }
4155
4156 /* Add devices to port list. */
4157 id_iter = (char *)ha->gid_list;
4158 for (index = 0; index < entries; index++) {
4159 domain = ((struct gid_list_info *)id_iter)->domain;
4160 area = ((struct gid_list_info *)id_iter)->area;
4161 al_pa = ((struct gid_list_info *)id_iter)->al_pa;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004162 if (IS_QLA2100(ha) || IS_QLA2200(ha))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 loop_id = (uint16_t)
4164 ((struct gid_list_info *)id_iter)->loop_id_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004165 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 loop_id = le16_to_cpu(
4167 ((struct gid_list_info *)id_iter)->loop_id);
Andrew Vasquezabbd8872005-07-06 10:30:05 -07004168 id_iter += ha->gid_list_info_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 /* Bypass reserved domain fields. */
4171 if ((domain & 0xf0) == 0xf0)
4172 continue;
4173
4174 /* Bypass if not same domain and area of adapter. */
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004175 if (area && domain &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004176 (area != vha->d_id.b.area || domain != vha->d_id.b.domain))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 continue;
4178
4179 /* Bypass invalid local loop ID. */
4180 if (loop_id > LAST_LOCAL_LOOP_ID)
4181 continue;
4182
Quinn Tran41dc5292017-01-19 22:28:03 -08004183 memset(new_fcport->port_name, 0, WWN_SIZE);
Arun Easi370d5502012-08-22 14:21:10 -04004184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 /* Fill in member data. */
4186 new_fcport->d_id.b.domain = domain;
4187 new_fcport->d_id.b.area = area;
4188 new_fcport->d_id.b.al_pa = al_pa;
4189 new_fcport->loop_id = loop_id;
Quinn Tran41dc5292017-01-19 22:28:03 -08004190
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004191 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (rval2 != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004193 ql_dbg(ql_dbg_disc, vha, 0x2097,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004194 "Failed to retrieve fcport information "
4195 "-- get_port_database=%x, loop_id=0x%04x.\n",
4196 rval2, new_fcport->loop_id);
Quinn Tran83548fe2017-06-02 09:12:01 -07004197 ql_dbg(ql_dbg_disc, vha, 0x2105,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004198 "Scheduling resync.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004199 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 continue;
4201 }
4202
Quinn Tran41dc5292017-01-19 22:28:03 -08004203 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 /* Check for matching device in port list. */
4205 found = 0;
4206 fcport = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004207 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 if (memcmp(new_fcport->port_name, fcport->port_name,
4209 WWN_SIZE))
4210 continue;
4211
Shyam Sundarddb9b122009-03-24 09:08:10 -07004212 fcport->flags &= ~FCF_FABRIC_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 fcport->loop_id = new_fcport->loop_id;
4214 fcport->port_type = new_fcport->port_type;
4215 fcport->d_id.b24 = new_fcport->d_id.b24;
4216 memcpy(fcport->node_name, new_fcport->node_name,
4217 WWN_SIZE);
4218
Quinn Tran41dc5292017-01-19 22:28:03 -08004219 if (!fcport->login_succ) {
4220 vha->fcport_count++;
4221 fcport->login_succ = 1;
4222 fcport->disc_state = DSC_LOGIN_COMPLETE;
4223 }
4224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 found++;
4226 break;
4227 }
4228
4229 if (!found) {
4230 /* New device, add to fcports list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004231 list_add_tail(&new_fcport->list, &vha->vp_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 /* Allocate a new replacement fcport. */
4234 fcport = new_fcport;
Quinn Tran41dc5292017-01-19 22:28:03 -08004235 if (!fcport->login_succ) {
4236 vha->fcport_count++;
4237 fcport->login_succ = 1;
4238 fcport->disc_state = DSC_LOGIN_COMPLETE;
4239 }
4240
4241 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4242
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004243 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Quinn Tran41dc5292017-01-19 22:28:03 -08004244
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004246 ql_log(ql_log_warn, vha, 0xd031,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004247 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 rval = QLA_MEMORY_ALLOC_FAILED;
4249 goto cleanup_allocation;
4250 }
Quinn Tran41dc5292017-01-19 22:28:03 -08004251 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 new_fcport->flags &= ~FCF_FABRIC_DEVICE;
4253 }
4254
Quinn Tran41dc5292017-01-19 22:28:03 -08004255 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4256
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004257 /* Base iIDMA settings on HBA port speed. */
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004258 fcport->fp_speed = ha->link_data_rate;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004259
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004260 qla2x00_update_fcport(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
4262 found_devs++;
4263 }
4264
4265cleanup_allocation:
Jesper Juhlc9475cb2005-11-07 01:01:26 -08004266 kfree(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004269 ql_dbg(ql_dbg_disc, vha, 0x2098,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004270 "Configure local loop error exit: rval=%x.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 }
4272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return (rval);
4274}
4275
4276static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004277qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004278{
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004279 int rval;
Quinn Tran93f2bd62014-09-25 05:16:53 -04004280 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004281 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004282
Andrew Vasquezc76f2c02007-07-19 15:05:57 -07004283 if (!IS_IIDMA_CAPABLE(ha))
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004284 return;
4285
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07004286 if (atomic_read(&fcport->state) != FCS_ONLINE)
4287 return;
4288
Andrew Vasquez39bd9622007-09-20 14:07:34 -07004289 if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
4290 fcport->fp_speed > ha->link_data_rate)
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004291 return;
4292
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004293 rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
Andrew Vasqueza3cbdfa2007-08-13 10:13:18 -07004294 mb);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004295 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004296 ql_dbg(ql_dbg_disc, vha, 0x2004,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004297 "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
4298 fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004299 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004300 ql_dbg(ql_dbg_disc, vha, 0x2005,
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004301 "iIDMA adjusted to %s GB/s on %8phN.\n",
Joe Carnucciod0297c92012-11-21 02:40:40 -05004302 qla2x00_get_link_speed_str(ha, fcport->fp_speed),
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04004303 fcport->port_name);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004304 }
4305}
4306
Quinn Tran726b8542017-01-19 22:28:00 -08004307/* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
Adrian Bunk23be3312006-11-24 02:46:01 +01004308static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004309qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
8482e1182005-04-17 15:04:54 -05004310{
4311 struct fc_rport_identifiers rport_ids;
bdf79622005-04-17 15:06:53 -05004312 struct fc_rport *rport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004313 unsigned long flags;
8482e1182005-04-17 15:04:54 -05004314
Andrew Vasquezf8b02a82005-08-31 15:21:20 -07004315 rport_ids.node_name = wwn_to_u64(fcport->node_name);
4316 rport_ids.port_name = wwn_to_u64(fcport->port_name);
8482e1182005-04-17 15:04:54 -05004317 rport_ids.port_id = fcport->d_id.b.domain << 16 |
4318 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
4319 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004320 fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
Andrew Vasquez77d74142005-07-08 18:00:36 -07004321 if (!rport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004322 ql_log(ql_log_warn, vha, 0x2006,
4323 "Unable to allocate fc remote port.\n");
Andrew Vasquez77d74142005-07-08 18:00:36 -07004324 return;
4325 }
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004326
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004327 spin_lock_irqsave(fcport->vha->host->host_lock, flags);
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004328 *((fc_port_t **)rport->dd_data) = fcport;
Madhuranath Iyengar044d78e2011-01-28 15:17:56 -08004329 spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08004330
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004331 rport->supported_classes = fcport->supported_classes;
Andrew Vasquez77d74142005-07-08 18:00:36 -07004332
4333 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
8482e1182005-04-17 15:04:54 -05004334 if (fcport->port_type == FCT_INITIATOR)
4335 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4336 if (fcport->port_type == FCT_TARGET)
4337 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
Quinn Tran726b8542017-01-19 22:28:00 -08004338
Quinn Tran83548fe2017-06-02 09:12:01 -07004339 ql_dbg(ql_dbg_disc, vha, 0x20ee,
4340 "%s %8phN. rport %p is %s mode\n",
4341 __func__, fcport->port_name, rport,
4342 (fcport->port_type == FCT_TARGET) ? "tgt" : "ini");
Quinn Tran726b8542017-01-19 22:28:00 -08004343
Andrew Vasquez77d74142005-07-08 18:00:36 -07004344 fc_remote_port_rolechg(rport, rport_ids.roles);
8482e1182005-04-17 15:04:54 -05004345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
4347/*
Adrian Bunk23be3312006-11-24 02:46:01 +01004348 * qla2x00_update_fcport
4349 * Updates device on list.
4350 *
4351 * Input:
4352 * ha = adapter block pointer.
4353 * fcport = port structure pointer.
4354 *
4355 * Return:
4356 * 0 - Success
4357 * BIT_0 - error
4358 *
4359 * Context:
4360 * Kernel context.
4361 */
4362void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004363qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
Adrian Bunk23be3312006-11-24 02:46:01 +01004364{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004365 fcport->vha = vha;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004366
Quinn Tran726b8542017-01-19 22:28:00 -08004367 if (IS_SW_RESV_ADDR(fcport->d_id))
4368 return;
4369
Quinn Tran83548fe2017-06-02 09:12:01 -07004370 ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
Quinn Tran726b8542017-01-19 22:28:00 -08004371 __func__, fcport->port_name);
4372
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004373 if (IS_QLAFX00(vha->hw)) {
4374 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004375 goto reg_port;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04004376 }
Adrian Bunk23be3312006-11-24 02:46:01 +01004377 fcport->login_retry = 0;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07004378 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
Quinn Tran726b8542017-01-19 22:28:00 -08004379 fcport->disc_state = DSC_LOGIN_COMPLETE;
4380 fcport->deleted = 0;
4381 fcport->logout_on_delete = 1;
Adrian Bunk23be3312006-11-24 02:46:01 +01004382
Joe Carnuccio1f93da522012-11-21 02:40:38 -05004383 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004384 qla2x00_iidma_fcport(vha, fcport);
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08004385 qla24xx_update_fcport_fcp_prio(vha, fcport);
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004386
4387reg_port:
Quinn Tran726b8542017-01-19 22:28:00 -08004388 switch (vha->host->active_mode) {
4389 case MODE_INITIATOR:
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004390 qla2x00_reg_remote_port(vha, fcport);
Quinn Tran726b8542017-01-19 22:28:00 -08004391 break;
4392 case MODE_TARGET:
4393 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
4394 !vha->vha_tgt.qla_tgt->tgt_stopped)
4395 qlt_fc_port_added(vha, fcport);
4396 break;
4397 case MODE_DUAL:
4398 qla2x00_reg_remote_port(vha, fcport);
4399 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
4400 !vha->vha_tgt.qla_tgt->tgt_stopped)
4401 qlt_fc_port_added(vha, fcport);
4402 break;
4403 default:
4404 break;
Alexei Potashnikd20ed912015-07-14 16:00:47 -04004405 }
Adrian Bunk23be3312006-11-24 02:46:01 +01004406}
4407
4408/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 * qla2x00_configure_fabric
4410 * Setup SNS devices with loop ID's.
4411 *
4412 * Input:
4413 * ha = adapter block pointer.
4414 *
4415 * Returns:
4416 * 0 = success.
4417 * BIT_0 = error
4418 */
4419static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004420qla2x00_configure_fabric(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
Arun Easib3b02e62012-02-09 11:15:39 -08004422 int rval;
Quinn Tran726b8542017-01-19 22:28:00 -08004423 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 uint16_t mb[MAILBOX_REGISTER_COUNT];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004425 uint16_t loop_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 LIST_HEAD(new_fcports);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004427 struct qla_hw_data *ha = vha->hw;
Alexei Potashnikdf673272015-07-14 16:00:46 -04004428 int discovery_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 /* If FL port exists, then SNS is present */
Andrew Vasqueze4289242007-07-19 15:05:56 -07004431 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004432 loop_id = NPH_F_PORT;
4433 else
4434 loop_id = SNS_FL_PORT;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004435 rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004437 ql_dbg(ql_dbg_disc, vha, 0x20a0,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004438 "MBX_GET_PORT_NAME failed, No FL Port.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004440 vha->device_flags &= ~SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 return (QLA_SUCCESS);
4442 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004443 vha->device_flags |= SWITCH_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Quinn Tran41dc5292017-01-19 22:28:03 -08004445
4446 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
4447 rval = qla2x00_send_change_request(vha, 0x3, 0);
4448 if (rval != QLA_SUCCESS)
4449 ql_log(ql_log_warn, vha, 0x121,
4450 "Failed to enable receiving of RSCN requests: 0x%x.\n",
4451 rval);
4452 }
4453
4454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 do {
Quinn Tran726b8542017-01-19 22:28:00 -08004456 qla2x00_mgmt_svr_login(vha);
4457
Andrew Vasquezcca53352005-08-26 19:08:30 -07004458 /* FDMI support. */
4459 if (ql2xfdmienable &&
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004460 test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
4461 qla2x00_fdmi_register(vha);
Andrew Vasquezcca53352005-08-26 19:08:30 -07004462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 /* Ensure we are logged into the SNS. */
Andrew Vasqueze4289242007-07-19 15:05:56 -07004464 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004465 loop_id = NPH_SNS;
4466 else
4467 loop_id = SIMPLE_NAME_SERVER;
Chad Dupuis0b91d112012-02-09 11:15:42 -08004468 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
4469 0xfc, mb, BIT_1|BIT_0);
4470 if (rval != QLA_SUCCESS) {
4471 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Joe Carnuccioe452ceb2013-02-08 01:57:56 -05004472 return rval;
Chad Dupuis0b91d112012-02-09 11:15:42 -08004473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 if (mb[0] != MBS_COMMAND_COMPLETE) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004475 ql_dbg(ql_dbg_disc, vha, 0x20a1,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004476 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x "
4477 "mb[6]=%x mb[7]=%x.\n", loop_id, mb[0], mb[1],
4478 mb[2], mb[6], mb[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return (QLA_SUCCESS);
4480 }
4481
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004482 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
4483 if (qla2x00_rft_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004485 ql_dbg(ql_dbg_disc, vha, 0x20a2,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004486 "Register FC-4 TYPE failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004487 if (test_bit(LOOP_RESYNC_NEEDED,
4488 &vha->dpc_flags))
4489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004491 if (qla2x00_rff_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004493 ql_dbg(ql_dbg_disc, vha, 0x209a,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004494 "Register FC-4 Features failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004495 if (test_bit(LOOP_RESYNC_NEEDED,
4496 &vha->dpc_flags))
4497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004499 if (qla2x00_rnn_id(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004501 ql_dbg(ql_dbg_disc, vha, 0x2104,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004502 "Register Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004503 if (test_bit(LOOP_RESYNC_NEEDED,
4504 &vha->dpc_flags))
4505 break;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004506 } else if (qla2x00_rsnn_nn(vha)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 /* EMPTY */
Quinn Tran83548fe2017-06-02 09:12:01 -07004508 ql_dbg(ql_dbg_disc, vha, 0x209b,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004509 "Register Symobilic Node Name failed.\n");
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004510 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
4513 }
4514
Joe Carnuccio827210b2013-02-08 01:57:57 -05004515 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4516 fcport->scan_state = QLA_FCPORT_SCAN;
4517 }
4518
Alexei Potashnikdf673272015-07-14 16:00:46 -04004519 /* Mark the time right before querying FW for connected ports.
4520 * This process is long, asynchronous and by the time it's done,
4521 * collected information might not be accurate anymore. E.g.
4522 * disconnected port might have re-connected and a brand new
4523 * session has been created. In this case session's generation
4524 * will be newer than discovery_gen. */
4525 qlt_do_generation_tick(vha, &discovery_gen);
4526
Quinn Tran726b8542017-01-19 22:28:00 -08004527 rval = qla2x00_find_all_fabric_devs(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (rval != QLA_SUCCESS)
4529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 } while (0);
4531
Quinn Tran726b8542017-01-19 22:28:00 -08004532 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004533 ql_dbg(ql_dbg_disc, vha, 0x2068,
4534 "Configure fabric error exit rval=%d.\n", rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 return (rval);
4537}
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539/*
4540 * qla2x00_find_all_fabric_devs
4541 *
4542 * Input:
4543 * ha = adapter block pointer.
4544 * dev = database device entry pointer.
4545 *
4546 * Returns:
4547 * 0 = success.
4548 *
4549 * Context:
4550 * Kernel context.
4551 */
4552static int
Quinn Tran726b8542017-01-19 22:28:00 -08004553qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
4555 int rval;
4556 uint16_t loop_id;
Quinn Tran726b8542017-01-19 22:28:00 -08004557 fc_port_t *fcport, *new_fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 int found;
4559
4560 sw_info_t *swl;
4561 int swl_idx;
4562 int first_dev, last_dev;
Mike Waychison1516ef42010-05-04 15:01:31 -07004563 port_id_t wrap = {}, nxt_d_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004564 struct qla_hw_data *ha = vha->hw;
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05004565 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Quinn Tran726b8542017-01-19 22:28:00 -08004566 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568 rval = QLA_SUCCESS;
4569
4570 /* Try GID_PT to get device list, else GAN. */
Andrew Vasquez7a677352012-02-09 11:15:56 -08004571 if (!ha->swl)
Chad Dupuis642ef982012-02-09 11:15:57 -08004572 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
Andrew Vasquez7a677352012-02-09 11:15:56 -08004573 GFP_KERNEL);
4574 swl = ha->swl;
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +02004575 if (!swl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 /*EMPTY*/
Quinn Tran83548fe2017-06-02 09:12:01 -07004577 ql_dbg(ql_dbg_disc, vha, 0x209c,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004578 "GID_PT allocations failed, fallback on GA_NXT.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 } else {
Chad Dupuis642ef982012-02-09 11:15:57 -08004580 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004581 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004583 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4584 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004585 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004587 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4588 return rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004589 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004591 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4592 return rval;
Quinn Tran726b8542017-01-19 22:28:00 -08004593 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
4594 swl = NULL;
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004595 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4596 return rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004598
4599 /* If other queries succeeded probe for FC-4 type */
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004600 if (swl) {
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004601 qla2x00_gff_id(vha, swl);
Quinn Tranb98ae0d2017-06-02 09:12:00 -07004602 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4603 return rval;
4604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
4606 swl_idx = 0;
4607
4608 /* Allocate temporary fcport for any new fcports discovered. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004609 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004611 ql_log(ql_log_warn, vha, 0x209d,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004612 "Failed to allocate memory for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return (QLA_MEMORY_ALLOC_FAILED);
4614 }
4615 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 /* Set start port ID scan at adapter ID. */
4617 first_dev = 1;
4618 last_dev = 0;
4619
4620 /* Starting free loop ID. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004621 loop_id = ha->min_external_loopid;
4622 for (; loop_id <= ha->max_loop_id; loop_id++) {
4623 if (qla2x00_is_reserved_id(vha, loop_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 continue;
4625
Giridhar Malavali3a6478d2010-05-28 15:08:20 -07004626 if (ha->current_topology == ISP_CFG_FL &&
4627 (atomic_read(&vha->loop_down_timer) ||
4628 LOOP_TRANSITION(vha))) {
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08004629 atomic_set(&vha->loop_down_timer, 0);
4630 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
4631 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 break;
Andrew Vasquezbb2d52b2010-02-18 10:07:27 -08004633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635 if (swl != NULL) {
4636 if (last_dev) {
4637 wrap.b24 = new_fcport->d_id.b24;
4638 } else {
4639 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
4640 memcpy(new_fcport->node_name,
4641 swl[swl_idx].node_name, WWN_SIZE);
4642 memcpy(new_fcport->port_name,
4643 swl[swl_idx].port_name, WWN_SIZE);
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004644 memcpy(new_fcport->fabric_port_name,
4645 swl[swl_idx].fabric_port_name, WWN_SIZE);
4646 new_fcport->fp_speed = swl[swl_idx].fp_speed;
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004647 new_fcport->fc4_type = swl[swl_idx].fc4_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
4649 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
4650 last_dev = 1;
4651 }
4652 swl_idx++;
4653 }
4654 } else {
4655 /* Send GA_NXT to the switch */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004656 rval = qla2x00_ga_nxt(vha, new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (rval != QLA_SUCCESS) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004658 ql_log(ql_log_warn, vha, 0x209e,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004659 "SNS scan failed -- assuming "
4660 "zero-entry result.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 rval = QLA_SUCCESS;
4662 break;
4663 }
4664 }
4665
4666 /* If wrap on switch device list, exit. */
4667 if (first_dev) {
4668 wrap.b24 = new_fcport->d_id.b24;
4669 first_dev = 0;
4670 } else if (new_fcport->d_id.b24 == wrap.b24) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004671 ql_dbg(ql_dbg_disc, vha, 0x209f,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004672 "Device wrap (%02x%02x%02x).\n",
4673 new_fcport->d_id.b.domain,
4674 new_fcport->d_id.b.area,
4675 new_fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 break;
4677 }
4678
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004679 /* Bypass if same physical adapter. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004680 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 continue;
4682
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004683 /* Bypass virtual ports of the same host. */
Chad Dupuisbb4cf5b2013-02-08 01:58:01 -05004684 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
4685 continue;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07004686
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004687 /* Bypass if same domain and area of adapter. */
4688 if (((new_fcport->d_id.b24 & 0xffff00) ==
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004689 (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
Andrew Vasquezf7d289f2005-08-26 19:08:40 -07004690 ISP_CFG_FL)
4691 continue;
4692
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 /* Bypass reserved domain fields. */
4694 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
4695 continue;
4696
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004697 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
Chad Dupuis4da26e12010-10-15 11:27:40 -07004698 if (ql2xgffidenable &&
4699 (new_fcport->fc4_type != FC4_TYPE_FCP_SCSI &&
4700 new_fcport->fc4_type != FC4_TYPE_UNKNOWN))
Chad Dupuise8c72ba2010-07-23 15:28:25 +05004701 continue;
4702
Quinn Tran726b8542017-01-19 22:28:00 -08004703 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 /* Locate matching device in database. */
4706 found = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004707 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (memcmp(new_fcport->port_name, fcport->port_name,
4709 WWN_SIZE))
4710 continue;
4711
Joe Carnuccio827210b2013-02-08 01:57:57 -05004712 fcport->scan_state = QLA_FCPORT_FOUND;
Arun Easib3b02e62012-02-09 11:15:39 -08004713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 found++;
4715
Andrew Vasquezd8b45212006-10-02 12:00:43 -07004716 /* Update port state. */
4717 memcpy(fcport->fabric_port_name,
4718 new_fcport->fabric_port_name, WWN_SIZE);
4719 fcport->fp_speed = new_fcport->fp_speed;
4720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 /*
Roland Dreierb2032fd2015-07-14 16:00:42 -04004722 * If address the same and state FCS_ONLINE
4723 * (or in target mode), nothing changed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 */
4725 if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
Roland Dreierb2032fd2015-07-14 16:00:42 -04004726 (atomic_read(&fcport->state) == FCS_ONLINE ||
Quinn Tran726b8542017-01-19 22:28:00 -08004727 (vha->host->active_mode == MODE_TARGET))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 break;
4729 }
4730
4731 /*
4732 * If device was not a fabric device before.
4733 */
4734 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
4735 fcport->d_id.b24 = new_fcport->d_id.b24;
Chad Dupuis5f16b332012-08-22 14:21:00 -04004736 qla2x00_clear_loop_id(fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 fcport->flags |= (FCF_FABRIC_DEVICE |
4738 FCF_LOGIN_NEEDED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 break;
4740 }
4741
4742 /*
4743 * Port ID changed or device was marked to be updated;
4744 * Log it out if still logged in and mark it for
4745 * relogin later.
4746 */
Quinn Tran726b8542017-01-19 22:28:00 -08004747 if (qla_tgt_mode_enabled(base_vha)) {
Roland Dreierb2032fd2015-07-14 16:00:42 -04004748 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
4749 "port changed FC ID, %8phC"
4750 " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
4751 fcport->port_name,
4752 fcport->d_id.b.domain,
4753 fcport->d_id.b.area,
4754 fcport->d_id.b.al_pa,
4755 fcport->loop_id,
4756 new_fcport->d_id.b.domain,
4757 new_fcport->d_id.b.area,
4758 new_fcport->d_id.b.al_pa);
4759 fcport->d_id.b24 = new_fcport->d_id.b24;
4760 break;
4761 }
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 fcport->d_id.b24 = new_fcport->d_id.b24;
4764 fcport->flags |= FCF_LOGIN_NEEDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 break;
4766 }
4767
Quinn Tran726b8542017-01-19 22:28:00 -08004768 if (found) {
4769 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 continue;
Quinn Tran726b8542017-01-19 22:28:00 -08004771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 /* If device was not in our fcports list, then add it. */
Roland Dreierb2032fd2015-07-14 16:00:42 -04004773 new_fcport->scan_state = QLA_FCPORT_FOUND;
Quinn Tran726b8542017-01-19 22:28:00 -08004774 list_add_tail(&new_fcport->list, &vha->vp_fcports);
4775
4776 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 /* Allocate a new replacement fcport. */
4780 nxt_d_id.b24 = new_fcport->d_id.b24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004781 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (new_fcport == NULL) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004783 ql_log(ql_log_warn, vha, 0xd032,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004784 "Memory allocation failed for fcport.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 return (QLA_MEMORY_ALLOC_FAILED);
4786 }
4787 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
4788 new_fcport->d_id.b24 = nxt_d_id.b24;
4789 }
4790
Quinn Tran726b8542017-01-19 22:28:00 -08004791 qla2x00_free_fcport(new_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Quinn Tran726b8542017-01-19 22:28:00 -08004793 /*
4794 * Logout all previous fabric dev marked lost, except FCP2 devices.
4795 */
4796 list_for_each_entry(fcport, &vha->vp_fcports, list) {
4797 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
4798 break;
4799
4800 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0 ||
4801 (fcport->flags & FCF_LOGIN_NEEDED) == 0)
4802 continue;
4803
4804 if (fcport->scan_state == QLA_FCPORT_SCAN) {
4805 if ((qla_dual_mode_enabled(vha) ||
4806 qla_ini_mode_enabled(vha)) &&
4807 atomic_read(&fcport->state) == FCS_ONLINE) {
4808 qla2x00_mark_device_lost(vha, fcport,
4809 ql2xplogiabsentdevice, 0);
4810 if (fcport->loop_id != FC_NO_LOOP_ID &&
4811 (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
4812 fcport->port_type != FCT_INITIATOR &&
4813 fcport->port_type != FCT_BROADCAST) {
Quinn Tran83548fe2017-06-02 09:12:01 -07004814 ql_dbg(ql_dbg_disc, vha, 0x20f0,
Quinn Tran726b8542017-01-19 22:28:00 -08004815 "%s %d %8phC post del sess\n",
4816 __func__, __LINE__,
4817 fcport->port_name);
4818
4819 qlt_schedule_sess_for_deletion_lock
4820 (fcport);
4821 continue;
4822 }
4823 }
4824 }
4825
4826 if (fcport->scan_state == QLA_FCPORT_FOUND)
4827 qla24xx_fcport_handle_login(vha, fcport);
4828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return (rval);
4830}
4831
4832/*
4833 * qla2x00_find_new_loop_id
4834 * Scan through our port list and find a new usable loop ID.
4835 *
4836 * Input:
4837 * ha: adapter state pointer.
4838 * dev: port structure pointer.
4839 *
4840 * Returns:
4841 * qla2x00 local function return status code.
4842 *
4843 * Context:
4844 * Kernel context.
4845 */
Joe Carnuccio03bcfb52011-03-30 11:46:27 -07004846int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004847qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
4849 int rval;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004850 struct qla_hw_data *ha = vha->hw;
Arun Easifeafb7b2010-09-03 14:57:00 -07004851 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 rval = QLA_SUCCESS;
4854
Chad Dupuis5f16b332012-08-22 14:21:00 -04004855 spin_lock_irqsave(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Chad Dupuis5f16b332012-08-22 14:21:00 -04004857 dev->loop_id = find_first_zero_bit(ha->loop_id_map,
4858 LOOPID_MAP_SIZE);
4859 if (dev->loop_id >= LOOPID_MAP_SIZE ||
4860 qla2x00_is_reserved_id(vha, dev->loop_id)) {
4861 dev->loop_id = FC_NO_LOOP_ID;
4862 rval = QLA_FUNCTION_FAILED;
4863 } else
4864 set_bit(dev->loop_id, ha->loop_id_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Chad Dupuis5f16b332012-08-22 14:21:00 -04004866 spin_unlock_irqrestore(&ha->vport_slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Chad Dupuis5f16b332012-08-22 14:21:00 -04004868 if (rval == QLA_SUCCESS)
4869 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
4870 "Assigning new loopid=%x, portid=%x.\n",
4871 dev->loop_id, dev->d_id.b24);
4872 else
4873 ql_log(ql_log_warn, dev->vha, 0x2087,
4874 "No loop_id's available, portid=%x.\n",
4875 dev->d_id.b24);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 return (rval);
4878}
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881/*
4882 * qla2x00_fabric_login
4883 * Issue fabric login command.
4884 *
4885 * Input:
4886 * ha = adapter block pointer.
4887 * device = pointer to FC device type structure.
4888 *
4889 * Returns:
4890 * 0 - Login successfully
4891 * 1 - Login failed
4892 * 2 - Initiator device
4893 * 3 - Fatal error
4894 */
4895int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004896qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 uint16_t *next_loopid)
4898{
4899 int rval;
4900 int retry;
4901 uint16_t tmp_loopid;
4902 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004903 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 retry = 0;
4906 tmp_loopid = 0;
4907
4908 for (;;) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004909 ql_dbg(ql_dbg_disc, vha, 0x2000,
4910 "Trying Fabric Login w/loop id 0x%04x for port "
4911 "%02x%02x%02x.\n",
4912 fcport->loop_id, fcport->d_id.b.domain,
4913 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 /* Login fcport on switch. */
Chad Dupuis0b91d112012-02-09 11:15:42 -08004916 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 fcport->d_id.b.domain, fcport->d_id.b.area,
4918 fcport->d_id.b.al_pa, mb, BIT_0);
Chad Dupuis0b91d112012-02-09 11:15:42 -08004919 if (rval != QLA_SUCCESS) {
4920 return rval;
4921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (mb[0] == MBS_PORT_ID_USED) {
4923 /*
4924 * Device has another loop ID. The firmware team
Andrew Vasquez0107109e2005-07-06 10:31:37 -07004925 * recommends the driver perform an implicit login with
4926 * the specified ID again. The ID we just used is save
4927 * here so we return with an ID that can be tried by
4928 * the next login.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 */
4930 retry++;
4931 tmp_loopid = fcport->loop_id;
4932 fcport->loop_id = mb[1];
4933
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004934 ql_dbg(ql_dbg_disc, vha, 0x2001,
4935 "Fabric Login: port in use - next loop "
4936 "id=0x%04x, port id= %02x%02x%02x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 fcport->loop_id, fcport->d_id.b.domain,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07004938 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
4941 /*
4942 * Login succeeded.
4943 */
4944 if (retry) {
4945 /* A retry occurred before. */
4946 *next_loopid = tmp_loopid;
4947 } else {
4948 /*
4949 * No retry occurred before. Just increment the
4950 * ID value for next login.
4951 */
4952 *next_loopid = (fcport->loop_id + 1);
4953 }
4954
4955 if (mb[1] & BIT_0) {
4956 fcport->port_type = FCT_INITIATOR;
4957 } else {
4958 fcport->port_type = FCT_TARGET;
4959 if (mb[1] & BIT_1) {
Santosh Vernekar8474f3a2009-08-25 11:36:16 -07004960 fcport->flags |= FCF_FCP2_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962 }
4963
Andrew Vasquezad3e0ed2005-08-26 19:08:10 -07004964 if (mb[10] & BIT_0)
4965 fcport->supported_classes |= FC_COS_CLASS2;
4966 if (mb[10] & BIT_1)
4967 fcport->supported_classes |= FC_COS_CLASS3;
4968
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04004969 if (IS_FWI2_CAPABLE(ha)) {
4970 if (mb[10] & BIT_7)
4971 fcport->flags |=
4972 FCF_CONF_COMP_SUPPORTED;
4973 }
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 rval = QLA_SUCCESS;
4976 break;
4977 } else if (mb[0] == MBS_LOOP_ID_USED) {
4978 /*
4979 * Loop ID already used, try next loop ID.
4980 */
4981 fcport->loop_id++;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004982 rval = qla2x00_find_new_loop_id(vha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 if (rval != QLA_SUCCESS) {
4984 /* Ran out of loop IDs to use */
4985 break;
4986 }
4987 } else if (mb[0] == MBS_COMMAND_ERROR) {
4988 /*
4989 * Firmware possibly timed out during login. If NO
4990 * retries are left to do then the device is declared
4991 * dead.
4992 */
4993 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004994 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07004995 fcport->d_id.b.domain, fcport->d_id.b.area,
4996 fcport->d_id.b.al_pa);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08004997 qla2x00_mark_device_lost(vha, fcport, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
4999 rval = 1;
5000 break;
5001 } else {
5002 /*
5003 * unrecoverable / not handled error
5004 */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005005 ql_dbg(ql_dbg_disc, vha, 0x2002,
5006 "Failed=%x port_id=%02x%02x%02x loop_id=%x "
5007 "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
5008 fcport->d_id.b.area, fcport->d_id.b.al_pa,
5009 fcport->loop_id, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 *next_loopid = fcport->loop_id;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005012 ha->isp_ops->fabric_logout(vha, fcport->loop_id,
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07005013 fcport->d_id.b.domain, fcport->d_id.b.area,
5014 fcport->d_id.b.al_pa);
Chad Dupuis5f16b332012-08-22 14:21:00 -04005015 qla2x00_clear_loop_id(fcport);
Andrew Vasquez0eedfcf2005-10-27 11:09:38 -07005016 fcport->login_retry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 rval = 3;
5019 break;
5020 }
5021 }
5022
5023 return (rval);
5024}
5025
5026/*
5027 * qla2x00_local_device_login
5028 * Issue local device login command.
5029 *
5030 * Input:
5031 * ha = adapter block pointer.
5032 * loop_id = loop id of device to login to.
5033 *
5034 * Returns (Where's the #define!!!!):
5035 * 0 - Login successfully
5036 * 1 - Login failed
5037 * 3 - Fatal error
5038 */
5039int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005040qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041{
5042 int rval;
5043 uint16_t mb[MAILBOX_REGISTER_COUNT];
5044
5045 memset(mb, 0, sizeof(mb));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005046 rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 if (rval == QLA_SUCCESS) {
5048 /* Interrogate mailbox registers for any errors */
5049 if (mb[0] == MBS_COMMAND_ERROR)
5050 rval = 1;
5051 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
5052 /* device not in PCB table */
5053 rval = 3;
5054 }
5055
5056 return (rval);
5057}
5058
5059/*
5060 * qla2x00_loop_resync
5061 * Resync with fibre channel devices.
5062 *
5063 * Input:
5064 * ha = adapter block pointer.
5065 *
5066 * Returns:
5067 * 0 = success
5068 */
5069int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005070qla2x00_loop_resync(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005072 int rval = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 uint32_t wait_time;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005074 struct req_que *req;
5075 struct rsp_que *rsp;
5076
Michael Hernandezd7459522016-12-12 14:40:07 -08005077 req = vha->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07005078 rsp = req->rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005080 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5081 if (vha->flags.online) {
5082 if (!(rval = qla2x00_fw_ready(vha))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 /* Wait at most MAX_TARGET RSCNs for a stable link. */
5084 wait_time = 256;
5085 do {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005086 if (!IS_QLAFX00(vha->hw)) {
5087 /*
5088 * Issue a marker after FW becomes
5089 * ready.
5090 */
5091 qla2x00_marker(vha, req, rsp, 0, 0,
5092 MK_SYNC_ALL);
5093 vha->marker_needed = 0;
5094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 /* Remap devices on Loop. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005097 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04005099 if (IS_QLAFX00(vha->hw))
5100 qlafx00_configure_devices(vha);
5101 else
5102 qla2x00_configure_loop(vha);
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 wait_time--;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005105 } while (!atomic_read(&vha->loop_down_timer) &&
5106 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
5107 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
5108 &vha->dpc_flags)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 }
5111
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005112 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return (QLA_FUNCTION_FAILED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005115 if (rval)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005116 ql_dbg(ql_dbg_disc, vha, 0x206c,
5117 "%s *** FAILED ***.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
5119 return (rval);
5120}
5121
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005122/*
5123* qla2x00_perform_loop_resync
5124* Description: This function will set the appropriate flags and call
5125* qla2x00_loop_resync. If successful loop will be resynced
5126* Arguments : scsi_qla_host_t pointer
5127* returm : Success or Failure
5128*/
5129
5130int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
5131{
5132 int32_t rval = 0;
5133
5134 if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
5135 /*Configure the flags so that resync happens properly*/
5136 atomic_set(&ha->loop_down_timer, 0);
5137 if (!(ha->device_flags & DFLG_NO_CABLE)) {
5138 atomic_set(&ha->loop_state, LOOP_UP);
5139 set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
5140 set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
5141 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
5142
5143 rval = qla2x00_loop_resync(ha);
5144 } else
5145 atomic_set(&ha->loop_state, LOOP_DEAD);
5146
5147 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
5148 }
5149
5150 return rval;
5151}
5152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153void
Andrew Vasquez67becc02009-08-25 11:36:20 -07005154qla2x00_update_fcports(scsi_qla_host_t *base_vha)
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005155{
5156 fc_port_t *fcport;
Arun Easifeafb7b2010-09-03 14:57:00 -07005157 struct scsi_qla_host *vha;
5158 struct qla_hw_data *ha = base_vha->hw;
5159 unsigned long flags;
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005160
Arun Easifeafb7b2010-09-03 14:57:00 -07005161 spin_lock_irqsave(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005162 /* Go with deferred removal of rport references. */
Arun Easifeafb7b2010-09-03 14:57:00 -07005163 list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
5164 atomic_inc(&vha->vref_count);
5165 list_for_each_entry(fcport, &vha->vp_fcports, list) {
Dan Carpenter8ae598d2010-12-21 16:00:15 -08005166 if (fcport->drport &&
Arun Easifeafb7b2010-09-03 14:57:00 -07005167 atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
5168 spin_unlock_irqrestore(&ha->vport_slock, flags);
Andrew Vasquez67becc02009-08-25 11:36:20 -07005169 qla2x00_rport_del(fcport);
Alexei Potashnikdf673272015-07-14 16:00:46 -04005170
Arun Easifeafb7b2010-09-03 14:57:00 -07005171 spin_lock_irqsave(&ha->vport_slock, flags);
5172 }
5173 }
5174 atomic_dec(&vha->vref_count);
Joe Carnuccioc4a9b532017-03-15 09:48:43 -07005175 wake_up(&vha->vref_waitq);
Arun Easifeafb7b2010-09-03 14:57:00 -07005176 }
5177 spin_unlock_irqrestore(&ha->vport_slock, flags);
andrew.vasquez@qlogic.comd97994d2006-01-20 14:53:13 -08005178}
5179
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005180/* Assumes idc_lock always held on entry */
5181void
5182qla83xx_reset_ownership(scsi_qla_host_t *vha)
5183{
5184 struct qla_hw_data *ha = vha->hw;
5185 uint32_t drv_presence, drv_presence_mask;
5186 uint32_t dev_part_info1, dev_part_info2, class_type;
5187 uint32_t class_type_mask = 0x3;
5188 uint16_t fcoe_other_function = 0xffff, i;
5189
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005190 if (IS_QLA8044(ha)) {
5191 drv_presence = qla8044_rd_direct(vha,
5192 QLA8044_CRB_DRV_ACTIVE_INDEX);
5193 dev_part_info1 = qla8044_rd_direct(vha,
5194 QLA8044_CRB_DEV_PART_INFO_INDEX);
5195 dev_part_info2 = qla8044_rd_direct(vha,
5196 QLA8044_CRB_DEV_PART_INFO2);
5197 } else {
5198 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5199 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
5200 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
5201 }
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005202 for (i = 0; i < 8; i++) {
5203 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
5204 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5205 (i != ha->portnum)) {
5206 fcoe_other_function = i;
5207 break;
5208 }
5209 }
5210 if (fcoe_other_function == 0xffff) {
5211 for (i = 0; i < 8; i++) {
5212 class_type = ((dev_part_info2 >> (i * 4)) &
5213 class_type_mask);
5214 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
5215 ((i + 8) != ha->portnum)) {
5216 fcoe_other_function = i + 8;
5217 break;
5218 }
5219 }
5220 }
5221 /*
5222 * Prepare drv-presence mask based on fcoe functions present.
5223 * However consider only valid physical fcoe function numbers (0-15).
5224 */
5225 drv_presence_mask = ~((1 << (ha->portnum)) |
5226 ((fcoe_other_function == 0xffff) ?
5227 0 : (1 << (fcoe_other_function))));
5228
5229 /* We are the reset owner iff:
5230 * - No other protocol drivers present.
5231 * - This is the lowest among fcoe functions. */
5232 if (!(drv_presence & drv_presence_mask) &&
5233 (ha->portnum < fcoe_other_function)) {
5234 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
5235 "This host is Reset owner.\n");
5236 ha->flags.nic_core_reset_owner = 1;
5237 }
5238}
5239
Saurav Kashyapfa492632012-11-21 02:40:29 -05005240static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005241__qla83xx_set_drv_ack(scsi_qla_host_t *vha)
5242{
5243 int rval = QLA_SUCCESS;
5244 struct qla_hw_data *ha = vha->hw;
5245 uint32_t drv_ack;
5246
5247 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5248 if (rval == QLA_SUCCESS) {
5249 drv_ack |= (1 << ha->portnum);
5250 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5251 }
5252
5253 return rval;
5254}
5255
Saurav Kashyapfa492632012-11-21 02:40:29 -05005256static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005257__qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
5258{
5259 int rval = QLA_SUCCESS;
5260 struct qla_hw_data *ha = vha->hw;
5261 uint32_t drv_ack;
5262
5263 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5264 if (rval == QLA_SUCCESS) {
5265 drv_ack &= ~(1 << ha->portnum);
5266 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
5267 }
5268
5269 return rval;
5270}
5271
Saurav Kashyapfa492632012-11-21 02:40:29 -05005272static const char *
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005273qla83xx_dev_state_to_string(uint32_t dev_state)
5274{
5275 switch (dev_state) {
5276 case QLA8XXX_DEV_COLD:
5277 return "COLD/RE-INIT";
5278 case QLA8XXX_DEV_INITIALIZING:
5279 return "INITIALIZING";
5280 case QLA8XXX_DEV_READY:
5281 return "READY";
5282 case QLA8XXX_DEV_NEED_RESET:
5283 return "NEED RESET";
5284 case QLA8XXX_DEV_NEED_QUIESCENT:
5285 return "NEED QUIESCENT";
5286 case QLA8XXX_DEV_FAILED:
5287 return "FAILED";
5288 case QLA8XXX_DEV_QUIESCENT:
5289 return "QUIESCENT";
5290 default:
5291 return "Unknown";
5292 }
5293}
5294
5295/* Assumes idc-lock always held on entry */
5296void
5297qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
5298{
5299 struct qla_hw_data *ha = vha->hw;
5300 uint32_t idc_audit_reg = 0, duration_secs = 0;
5301
5302 switch (audit_type) {
5303 case IDC_AUDIT_TIMESTAMP:
5304 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
5305 idc_audit_reg = (ha->portnum) |
5306 (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
5307 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
5308 break;
5309
5310 case IDC_AUDIT_COMPLETION:
5311 duration_secs = ((jiffies_to_msecs(jiffies) -
5312 jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
5313 idc_audit_reg = (ha->portnum) |
5314 (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
5315 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
5316 break;
5317
5318 default:
5319 ql_log(ql_log_warn, vha, 0xb078,
5320 "Invalid audit type specified.\n");
5321 break;
5322 }
5323}
5324
5325/* Assumes idc_lock always held on entry */
Saurav Kashyapfa492632012-11-21 02:40:29 -05005326static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005327qla83xx_initiating_reset(scsi_qla_host_t *vha)
5328{
5329 struct qla_hw_data *ha = vha->hw;
5330 uint32_t idc_control, dev_state;
5331
5332 __qla83xx_get_idc_control(vha, &idc_control);
5333 if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
5334 ql_log(ql_log_info, vha, 0xb080,
5335 "NIC Core reset has been disabled. idc-control=0x%x\n",
5336 idc_control);
5337 return QLA_FUNCTION_FAILED;
5338 }
5339
5340 /* Set NEED-RESET iff in READY state and we are the reset-owner */
5341 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5342 if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
5343 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
5344 QLA8XXX_DEV_NEED_RESET);
5345 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
5346 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
5347 } else {
5348 const char *state = qla83xx_dev_state_to_string(dev_state);
5349 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
5350
5351 /* SV: XXX: Is timeout required here? */
5352 /* Wait for IDC state change READY -> NEED_RESET */
5353 while (dev_state == QLA8XXX_DEV_READY) {
5354 qla83xx_idc_unlock(vha, 0);
5355 msleep(200);
5356 qla83xx_idc_lock(vha, 0);
5357 qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5358 }
5359 }
5360
5361 /* Send IDC ack by writing to drv-ack register */
5362 __qla83xx_set_drv_ack(vha);
5363
5364 return QLA_SUCCESS;
5365}
5366
5367int
5368__qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
5369{
5370 return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
5371}
5372
5373int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005374__qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
5375{
5376 return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
5377}
5378
Saurav Kashyapfa492632012-11-21 02:40:29 -05005379static int
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005380qla83xx_check_driver_presence(scsi_qla_host_t *vha)
5381{
5382 uint32_t drv_presence = 0;
5383 struct qla_hw_data *ha = vha->hw;
5384
5385 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5386 if (drv_presence & (1 << ha->portnum))
5387 return QLA_SUCCESS;
5388 else
5389 return QLA_TEST_FAILED;
5390}
5391
5392int
5393qla83xx_nic_core_reset(scsi_qla_host_t *vha)
5394{
5395 int rval = QLA_SUCCESS;
5396 struct qla_hw_data *ha = vha->hw;
5397
5398 ql_dbg(ql_dbg_p3p, vha, 0xb058,
5399 "Entered %s().\n", __func__);
5400
5401 if (vha->device_flags & DFLG_DEV_FAILED) {
5402 ql_log(ql_log_warn, vha, 0xb059,
5403 "Device in unrecoverable FAILED state.\n");
5404 return QLA_FUNCTION_FAILED;
5405 }
5406
5407 qla83xx_idc_lock(vha, 0);
5408
5409 if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
5410 ql_log(ql_log_warn, vha, 0xb05a,
5411 "Function=0x%x has been removed from IDC participation.\n",
5412 ha->portnum);
5413 rval = QLA_FUNCTION_FAILED;
5414 goto exit;
5415 }
5416
5417 qla83xx_reset_ownership(vha);
5418
5419 rval = qla83xx_initiating_reset(vha);
5420
5421 /*
5422 * Perform reset if we are the reset-owner,
5423 * else wait till IDC state changes to READY/FAILED.
5424 */
5425 if (rval == QLA_SUCCESS) {
5426 rval = qla83xx_idc_state_handler(vha);
5427
5428 if (rval == QLA_SUCCESS)
5429 ha->flags.nic_core_hung = 0;
5430 __qla83xx_clear_drv_ack(vha);
5431 }
5432
5433exit:
5434 qla83xx_idc_unlock(vha, 0);
5435
5436 ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
5437
5438 return rval;
5439}
5440
Saurav Kashyap81178772012-08-22 14:21:04 -04005441int
5442qla2xxx_mctp_dump(scsi_qla_host_t *vha)
5443{
5444 struct qla_hw_data *ha = vha->hw;
5445 int rval = QLA_FUNCTION_FAILED;
5446
5447 if (!IS_MCTP_CAPABLE(ha)) {
5448 /* This message can be removed from the final version */
5449 ql_log(ql_log_info, vha, 0x506d,
5450 "This board is not MCTP capable\n");
5451 return rval;
5452 }
5453
5454 if (!ha->mctp_dump) {
5455 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
5456 MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
5457
5458 if (!ha->mctp_dump) {
5459 ql_log(ql_log_warn, vha, 0x506e,
5460 "Failed to allocate memory for mctp dump\n");
5461 return rval;
5462 }
5463 }
5464
5465#define MCTP_DUMP_STR_ADDR 0x00000000
5466 rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
5467 MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
5468 if (rval != QLA_SUCCESS) {
5469 ql_log(ql_log_warn, vha, 0x506f,
5470 "Failed to capture mctp dump\n");
5471 } else {
5472 ql_log(ql_log_info, vha, 0x5070,
5473 "Mctp dump capture for host (%ld/%p).\n",
5474 vha->host_no, ha->mctp_dump);
5475 ha->mctp_dumped = 1;
5476 }
5477
Saurav Kashyap409ee0f2012-08-22 14:21:29 -04005478 if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
Saurav Kashyap81178772012-08-22 14:21:04 -04005479 ha->flags.nic_core_reset_hdlr_active = 1;
5480 rval = qla83xx_restart_nic_firmware(vha);
5481 if (rval)
5482 /* NIC Core reset failed. */
5483 ql_log(ql_log_warn, vha, 0x5071,
5484 "Failed to restart nic firmware\n");
5485 else
5486 ql_dbg(ql_dbg_p3p, vha, 0xb084,
5487 "Restarted NIC firmware successfully.\n");
5488 ha->flags.nic_core_reset_hdlr_active = 0;
5489 }
5490
5491 return rval;
5492
5493}
5494
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005495/*
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005496* qla2x00_quiesce_io
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005497* Description: This function will block the new I/Os
5498* Its not aborting any I/Os as context
5499* is not destroyed during quiescence
5500* Arguments: scsi_qla_host_t
5501* return : void
5502*/
5503void
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005504qla2x00_quiesce_io(scsi_qla_host_t *vha)
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005505{
5506 struct qla_hw_data *ha = vha->hw;
5507 struct scsi_qla_host *vp;
5508
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005509 ql_dbg(ql_dbg_dpc, vha, 0x401d,
5510 "Quiescing I/O - ha=%p.\n", ha);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005511
5512 atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
5513 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
5514 atomic_set(&vha->loop_state, LOOP_DOWN);
5515 qla2x00_mark_all_devices_lost(vha, 0);
5516 list_for_each_entry(vp, &ha->vp_list, list)
Chad Dupuis8fcd6b82012-08-22 14:21:06 -04005517 qla2x00_mark_all_devices_lost(vp, 0);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005518 } else {
5519 if (!atomic_read(&vha->loop_down_timer))
5520 atomic_set(&vha->loop_down_timer,
5521 LOOP_DOWN_TIME);
5522 }
5523 /* Wait for pending cmds to complete */
5524 qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST);
5525}
5526
Giridhar Malavalia9083012010-04-12 17:59:55 -07005527void
5528qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
5529{
5530 struct qla_hw_data *ha = vha->hw;
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005531 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07005532 unsigned long flags;
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08005533 fc_port_t *fcport;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005534 u16 i;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005535
Saurav Kashyape46ef002011-02-23 15:27:16 -08005536 /* For ISP82XX, driver waits for completion of the commands.
5537 * online flag should be set.
5538 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005539 if (!(IS_P3P_TYPE(ha)))
Saurav Kashyape46ef002011-02-23 15:27:16 -08005540 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005541 ha->flags.chip_reset_done = 0;
5542 clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -04005543 vha->qla_stats.total_isp_aborts++;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005544
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005545 ql_log(ql_log_info, vha, 0x00af,
5546 "Performing ISP error recovery - ha=%p.\n", ha);
Giridhar Malavalia9083012010-04-12 17:59:55 -07005547
Saurav Kashyape46ef002011-02-23 15:27:16 -08005548 /* For ISP82XX, reset_chip is just disabling interrupts.
5549 * Driver waits for the completion of the commands.
5550 * the interrupts need to be enabled.
5551 */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005552 if (!(IS_P3P_TYPE(ha)))
Giridhar Malavalia9083012010-04-12 17:59:55 -07005553 ha->isp_ops->reset_chip(vha);
5554
Quinn Tranec7193e2017-03-15 09:48:55 -07005555 ha->flags.n2n_ae = 0;
5556 ha->flags.lip_ae = 0;
5557 ha->current_topology = 0;
5558 ha->flags.fw_started = 0;
5559 ha->flags.fw_init_done = 0;
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07005560 ha->base_qpair->chip_reset++;
5561 for (i = 0; i < ha->max_qpairs; i++) {
5562 if (ha->queue_pair_map[i])
5563 ha->queue_pair_map[i]->chip_reset =
5564 ha->base_qpair->chip_reset;
5565 }
Quinn Tran726b8542017-01-19 22:28:00 -08005566
Giridhar Malavalia9083012010-04-12 17:59:55 -07005567 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
5568 if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
5569 atomic_set(&vha->loop_state, LOOP_DOWN);
5570 qla2x00_mark_all_devices_lost(vha, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07005571
5572 spin_lock_irqsave(&ha->vport_slock, flags);
Saurav Kashyap579d12b2010-12-21 16:00:14 -08005573 list_for_each_entry(vp, &ha->vp_list, list) {
Arun Easifeafb7b2010-09-03 14:57:00 -07005574 atomic_inc(&vp->vref_count);
5575 spin_unlock_irqrestore(&ha->vport_slock, flags);
5576
Giridhar Malavalia9083012010-04-12 17:59:55 -07005577 qla2x00_mark_all_devices_lost(vp, 0);
Arun Easifeafb7b2010-09-03 14:57:00 -07005578
5579 spin_lock_irqsave(&ha->vport_slock, flags);
5580 atomic_dec(&vp->vref_count);
5581 }
5582 spin_unlock_irqrestore(&ha->vport_slock, flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07005583 } else {
5584 if (!atomic_read(&vha->loop_down_timer))
5585 atomic_set(&vha->loop_down_timer,
5586 LOOP_DOWN_TIME);
5587 }
5588
Andrew Vasquez6aef87b2011-02-23 15:27:13 -08005589 /* Clear all async request states across all VPs. */
5590 list_for_each_entry(fcport, &vha->vp_fcports, list)
5591 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5592 spin_lock_irqsave(&ha->vport_slock, flags);
5593 list_for_each_entry(vp, &ha->vp_list, list) {
5594 atomic_inc(&vp->vref_count);
5595 spin_unlock_irqrestore(&ha->vport_slock, flags);
5596
5597 list_for_each_entry(fcport, &vp->vp_fcports, list)
5598 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5599
5600 spin_lock_irqsave(&ha->vport_slock, flags);
5601 atomic_dec(&vp->vref_count);
5602 }
5603 spin_unlock_irqrestore(&ha->vport_slock, flags);
5604
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07005605 if (!ha->flags.eeh_busy) {
5606 /* Make sure for ISP 82XX IO DMA is complete */
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005607 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali71905752011-02-23 15:27:10 -08005608 qla82xx_chip_reset_cleanup(vha);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005609 ql_log(ql_log_info, vha, 0x00b4,
5610 "Done chip reset cleanup.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07005611
Saurav Kashyape46ef002011-02-23 15:27:16 -08005612 /* Done waiting for pending commands.
5613 * Reset the online flag.
5614 */
5615 vha->flags.online = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07005616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Lalit Chandivadebddd2d62010-09-03 15:20:53 -07005618 /* Requeue all commands in outstanding command list. */
5619 qla2x00_abort_all_cmds(vha, DID_RESET << 16);
5620 }
Arun Easib6a029e2014-09-25 06:14:52 -04005621 /* memory barrier */
5622 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623}
5624
5625/*
5626* qla2x00_abort_isp
5627* Resets ISP and aborts all outstanding commands.
5628*
5629* Input:
5630* ha = adapter block pointer.
5631*
5632* Returns:
5633* 0 = success
5634*/
5635int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005636qla2x00_abort_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
Andrew Vasquez476e8972006-08-23 14:54:55 -07005638 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 uint8_t status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005640 struct qla_hw_data *ha = vha->hw;
5641 struct scsi_qla_host *vp;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005642 struct req_que *req = ha->req_q_map[0];
Arun Easifeafb7b2010-09-03 14:57:00 -07005643 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005645 if (vha->flags.online) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07005646 qla2x00_abort_isp_cleanup(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
Santosh Vernekara61712972012-08-22 14:21:13 -04005648 if (IS_QLA8031(ha)) {
5649 ql_dbg(ql_dbg_p3p, vha, 0xb05c,
5650 "Clearing fcoe driver presence.\n");
5651 if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
5652 ql_dbg(ql_dbg_p3p, vha, 0xb073,
5653 "Error while clearing DRV-Presence.\n");
5654 }
5655
Andrew Vasquez85880802009-12-15 21:29:46 -08005656 if (unlikely(pci_channel_offline(ha->pdev) &&
5657 ha->flags.pci_channel_io_perm_failure)) {
5658 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
5659 status = 0;
5660 return status;
5661 }
5662
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005663 ha->isp_ops->get_flash_version(vha, req->ring);
Andrew Vasquez30c47662007-01-29 10:22:21 -08005664
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005665 ha->isp_ops->nvram_config(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005667 if (!qla2x00_restart_isp(vha)) {
5668 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005670 if (!atomic_read(&vha->loop_down_timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 /*
5672 * Issue marker command only when we are going
5673 * to start the I/O .
5674 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005675 vha->marker_needed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 }
5677
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005678 vha->flags.online = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
Andrew Vasquezfd34f552007-07-19 15:06:00 -07005680 ha->isp_ops->enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07005682 ha->isp_abort_cnt = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005683 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Andrew Vasquez476e8972006-08-23 14:54:55 -07005684
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08005685 if (IS_QLA81XX(ha) || IS_QLA8031(ha))
5686 qla2x00_get_fw_version(vha);
Andrew Vasquezdf613b92008-01-17 09:02:17 -08005687 if (ha->fce) {
5688 ha->flags.fce_enabled = 1;
5689 memset(ha->fce, 0,
5690 fce_calc_size(ha->fce_bufs));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005691 rval = qla2x00_enable_fce_trace(vha,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08005692 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
5693 &ha->fce_bufs);
5694 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005695 ql_log(ql_log_warn, vha, 0x8033,
Andrew Vasquezdf613b92008-01-17 09:02:17 -08005696 "Unable to reinitialize FCE "
5697 "(%d).\n", rval);
5698 ha->flags.fce_enabled = 0;
5699 }
5700 }
Andrew Vasquez436a7b12008-07-10 16:55:54 -07005701
5702 if (ha->eft) {
5703 memset(ha->eft, 0, EFT_SIZE);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005704 rval = qla2x00_enable_eft_trace(vha,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07005705 ha->eft_dma, EFT_NUM_BUFFERS);
5706 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005707 ql_log(ql_log_warn, vha, 0x8034,
Andrew Vasquez436a7b12008-07-10 16:55:54 -07005708 "Unable to reinitialize EFT "
5709 "(%d).\n", rval);
5710 }
5711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 } else { /* failed the ISP abort */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005713 vha->flags.online = 1;
5714 if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 if (ha->isp_abort_cnt == 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005716 ql_log(ql_log_fatal, vha, 0x8035,
5717 "ISP error recover failed - "
5718 "board disabled.\n");
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07005719 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 * The next call disables the board
5721 * completely.
5722 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005723 ha->isp_ops->reset_adapter(vha);
5724 vha->flags.online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 clear_bit(ISP_ABORT_RETRY,
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005726 &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 status = 0;
5728 } else { /* schedule another ISP abort */
5729 ha->isp_abort_cnt--;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005730 ql_dbg(ql_dbg_taskm, vha, 0x8020,
5731 "ISP abort - retry remaining %d.\n",
5732 ha->isp_abort_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 status = 1;
5734 }
5735 } else {
5736 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005737 ql_dbg(ql_dbg_taskm, vha, 0x8021,
5738 "ISP error recovery - retrying (%d) "
5739 "more times.\n", ha->isp_abort_cnt);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005740 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 status = 1;
5742 }
5743 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07005744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 }
5746
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005747 if (!status) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005748 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
Arun Easifeafb7b2010-09-03 14:57:00 -07005749
5750 spin_lock_irqsave(&ha->vport_slock, flags);
5751 list_for_each_entry(vp, &ha->vp_list, list) {
5752 if (vp->vp_idx) {
5753 atomic_inc(&vp->vref_count);
5754 spin_unlock_irqrestore(&ha->vport_slock, flags);
5755
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005756 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07005757
5758 spin_lock_irqsave(&ha->vport_slock, flags);
5759 atomic_dec(&vp->vref_count);
5760 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005761 }
Arun Easifeafb7b2010-09-03 14:57:00 -07005762 spin_unlock_irqrestore(&ha->vport_slock, flags);
5763
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04005764 if (IS_QLA8031(ha)) {
5765 ql_dbg(ql_dbg_p3p, vha, 0xb05d,
5766 "Setting back fcoe driver presence.\n");
5767 if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
5768 ql_dbg(ql_dbg_p3p, vha, 0xb074,
5769 "Error while setting DRV-Presence.\n");
5770 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005771 } else {
Joe Perchesd8424f62011-11-18 09:03:06 -08005772 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
5773 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
5775
5776 return(status);
5777}
5778
5779/*
5780* qla2x00_restart_isp
5781* restarts the ISP after a reset
5782*
5783* Input:
5784* ha = adapter block pointer.
5785*
5786* Returns:
5787* 0 = success
5788*/
5789static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005790qla2x00_restart_isp(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
Andrew Vasquezc6b2fca2009-03-05 11:07:03 -08005792 int status = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005793 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005794 struct req_que *req = ha->req_q_map[0];
5795 struct rsp_que *rsp = ha->rsp_q_map[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 /* If firmware needs to be loaded */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005798 if (qla2x00_isp_firmware(vha)) {
5799 vha->flags.online = 0;
5800 status = ha->isp_ops->chip_diag(vha);
5801 if (!status)
5802 status = qla2x00_setup_chip(vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 }
5804
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005805 if (!status && !(status = qla2x00_init_rings(vha))) {
5806 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Lalit Chandivade2533cf62009-03-24 09:08:07 -07005807 ha->flags.chip_reset_done = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04005808
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005809 /* Initialize the queues in use */
5810 qla25xx_init_queues(ha);
5811
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005812 status = qla2x00_fw_ready(vha);
5813 if (!status) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005814 /* Issue a marker after FW becomes ready. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005815 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Chad Dupuis7108b762014-04-11 16:54:45 -04005816 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 }
5818
5819 /* if no cable then assume it's good */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005820 if ((vha->device_flags & DFLG_NO_CABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 }
5823 return (status);
5824}
5825
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005826static int
5827qla25xx_init_queues(struct qla_hw_data *ha)
5828{
5829 struct rsp_que *rsp = NULL;
5830 struct req_que *req = NULL;
5831 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
5832 int ret = -1;
5833 int i;
5834
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07005835 for (i = 1; i < ha->max_rsp_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005836 rsp = ha->rsp_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05005837 if (rsp && test_bit(i, ha->rsp_qid_map)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005838 rsp->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08005839 ret = qla25xx_init_rsp_que(base_vha, rsp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005840 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005841 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
5842 "%s Rsp que: %d init failed.\n",
5843 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005844 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005845 ql_dbg(ql_dbg_init, base_vha, 0x0100,
5846 "%s Rsp que: %d inited.\n",
5847 __func__, rsp->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005848 }
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07005849 }
5850 for (i = 1; i < ha->max_req_queues; i++) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005851 req = ha->req_q_map[i];
Quinn Trancb432852016-02-04 11:45:16 -05005852 if (req && test_bit(i, ha->req_qid_map)) {
5853 /* Clear outstanding commands array. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005854 req->options &= ~BIT_0;
Anirban Chakraborty618a7522009-02-08 20:50:11 -08005855 ret = qla25xx_init_req_que(base_vha, req);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005856 if (ret != QLA_SUCCESS)
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005857 ql_dbg(ql_dbg_init, base_vha, 0x0101,
5858 "%s Req que: %d init failed.\n",
5859 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005860 else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005861 ql_dbg(ql_dbg_init, base_vha, 0x0102,
5862 "%s Req que: %d inited.\n",
5863 __func__, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08005864 }
5865 }
5866 return ret;
5867}
5868
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869/*
5870* qla2x00_reset_adapter
5871* Reset adapter.
5872*
5873* Input:
5874* ha = adapter block pointer.
5875*/
Andrew Vasquezabbd8872005-07-06 10:30:05 -07005876void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005877qla2x00_reset_adapter(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878{
5879 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005880 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -07005881 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005883 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07005884 ha->isp_ops->disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 spin_lock_irqsave(&ha->hardware_lock, flags);
5887 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
5888 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
5889 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
5890 RD_REG_WORD(&reg->hccr); /* PCI Posting. */
5891 spin_unlock_irqrestore(&ha->hardware_lock, flags);
5892}
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005893
5894void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005895qla24xx_reset_adapter(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005896{
5897 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005898 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005899 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
5900
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04005901 if (IS_P3P_TYPE(ha))
Giridhar Malavalia9083012010-04-12 17:59:55 -07005902 return;
5903
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005904 vha->flags.online = 0;
Andrew Vasquezfd34f552007-07-19 15:06:00 -07005905 ha->isp_ops->disable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005906
5907 spin_lock_irqsave(&ha->hardware_lock, flags);
5908 WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
5909 RD_REG_DWORD(&reg->hccr);
5910 WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
5911 RD_REG_DWORD(&reg->hccr);
5912 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez09ff36d2009-01-22 09:45:30 -08005913
5914 if (IS_NOPOLLING_TYPE(ha))
5915 ha->isp_ops->enable_intrs(ha);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005916}
5917
David Miller4e08df32007-04-16 12:37:43 -07005918/* On sparc systems, obtain port and node WWN from firmware
5919 * properties.
5920 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005921static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
5922 struct nvram_24xx *nv)
David Miller4e08df32007-04-16 12:37:43 -07005923{
5924#ifdef CONFIG_SPARC
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005925 struct qla_hw_data *ha = vha->hw;
David Miller4e08df32007-04-16 12:37:43 -07005926 struct pci_dev *pdev = ha->pdev;
David S. Miller15576bc2007-05-08 00:36:49 -07005927 struct device_node *dp = pci_device_to_OF_node(pdev);
5928 const u8 *val;
David Miller4e08df32007-04-16 12:37:43 -07005929 int len;
5930
5931 val = of_get_property(dp, "port-wwn", &len);
5932 if (val && len >= WWN_SIZE)
5933 memcpy(nv->port_name, val, WWN_SIZE);
5934
5935 val = of_get_property(dp, "node-wwn", &len);
5936 if (val && len >= WWN_SIZE)
5937 memcpy(nv->node_name, val, WWN_SIZE);
5938#endif
5939}
5940
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005941int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005942qla24xx_nvram_config(scsi_qla_host_t *vha)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005943{
David Miller4e08df32007-04-16 12:37:43 -07005944 int rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005945 struct init_cb_24xx *icb;
5946 struct nvram_24xx *nv;
5947 uint32_t *dptr;
5948 uint8_t *dptr1, *dptr2;
5949 uint32_t chksum;
5950 uint16_t cnt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005951 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005952
David Miller4e08df32007-04-16 12:37:43 -07005953 rval = QLA_SUCCESS;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005954 icb = (struct init_cb_24xx *)ha->init_cb;
Seokmann Ju281afe12007-07-26 13:43:34 -07005955 nv = ha->nvram;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005956
5957 /* Determine NVRAM starting address. */
Chad Dupuisf73cb692014-02-26 04:15:06 -05005958 if (ha->port_no == 0) {
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07005959 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
5960 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
5961 } else {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005962 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
andrew.vasquez@qlogic.com6f641792006-03-09 14:27:34 -08005963 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
5964 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05005965
Anirban Chakrabortye5b68a62009-04-06 22:33:50 -07005966 ha->nvram_size = sizeof(struct nvram_24xx);
5967 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005968
Seokmann Ju281afe12007-07-26 13:43:34 -07005969 /* Get VPD data into cache */
5970 ha->vpd = ha->nvram + VPD_OFFSET;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005971 ha->isp_ops->read_nvram(vha, (uint8_t *)ha->vpd,
Seokmann Ju281afe12007-07-26 13:43:34 -07005972 ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
5973
5974 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005975 dptr = (uint32_t *)nv;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08005976 ha->isp_ops->read_nvram(vha, (uint8_t *)dptr, ha->nvram_base,
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005977 ha->nvram_size);
Joe Carnuccioda08ef52016-01-27 12:03:34 -05005978 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
5979 chksum += le32_to_cpu(*dptr);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005980
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005981 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
5982 "Contents of NVRAM\n");
5983 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
5984 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005985
5986 /* Bad NVRAM data, set defaults parameters. */
5987 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
5988 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07005989 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07005990 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005991 ql_log(ql_log_warn, vha, 0x006b,
Raul Porcel9e336522012-05-15 14:34:08 -04005992 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07005993 "version=0x%x.\n", chksum, nv->id[0], nv->nvram_version);
5994 ql_log(ql_log_warn, vha, 0x006c,
5995 "Falling back to functioning (yet invalid -- WWPN) "
5996 "defaults.\n");
David Miller4e08df32007-04-16 12:37:43 -07005997
5998 /*
5999 * Set default initialization control block.
6000 */
6001 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07006002 nv->nvram_version = cpu_to_le16(ICB_VERSION);
6003 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04006004 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07006005 nv->execution_throttle = cpu_to_le16(0xFFFF);
6006 nv->exchange_count = cpu_to_le16(0);
6007 nv->hard_address = cpu_to_le16(124);
David Miller4e08df32007-04-16 12:37:43 -07006008 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006009 nv->port_name[1] = 0x00 + ha->port_no + 1;
David Miller4e08df32007-04-16 12:37:43 -07006010 nv->port_name[2] = 0x00;
6011 nv->port_name[3] = 0xe0;
6012 nv->port_name[4] = 0x8b;
6013 nv->port_name[5] = 0x1c;
6014 nv->port_name[6] = 0x55;
6015 nv->port_name[7] = 0x86;
6016 nv->node_name[0] = 0x20;
6017 nv->node_name[1] = 0x00;
6018 nv->node_name[2] = 0x00;
6019 nv->node_name[3] = 0xe0;
6020 nv->node_name[4] = 0x8b;
6021 nv->node_name[5] = 0x1c;
6022 nv->node_name[6] = 0x55;
6023 nv->node_name[7] = 0x86;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006024 qla24xx_nvram_wwn_from_ofw(vha, nv);
Bart Van Asschead950362015-07-09 07:24:08 -07006025 nv->login_retry_count = cpu_to_le16(8);
6026 nv->interrupt_delay_timer = cpu_to_le16(0);
6027 nv->login_timeout = cpu_to_le16(0);
David Miller4e08df32007-04-16 12:37:43 -07006028 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07006029 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
6030 nv->firmware_options_2 = cpu_to_le32(2 << 4);
6031 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6032 nv->firmware_options_3 = cpu_to_le32(2 << 13);
6033 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
6034 nv->efi_parameters = cpu_to_le32(0);
David Miller4e08df32007-04-16 12:37:43 -07006035 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07006036 nv->max_luns_per_target = cpu_to_le16(128);
6037 nv->port_down_retry_count = cpu_to_le16(30);
6038 nv->link_down_timeout = cpu_to_le16(30);
David Miller4e08df32007-04-16 12:37:43 -07006039
6040 rval = 1;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006041 }
6042
Quinn Tran726b8542017-01-19 22:28:00 -08006043 if (qla_tgt_mode_enabled(vha)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006044 /* Don't enable full login after initial LIP */
Bart Van Asschead950362015-07-09 07:24:08 -07006045 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006046 /* Don't enable LIP full login for initiator */
Bart Van Asschead950362015-07-09 07:24:08 -07006047 nv->host_p &= cpu_to_le32(~BIT_10);
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006048 }
6049
6050 qlt_24xx_config_nvram_stage1(vha, nv);
6051
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006052 /* Reset Initialization control block */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006053 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006054
6055 /* Copy 1st segment. */
6056 dptr1 = (uint8_t *)icb;
6057 dptr2 = (uint8_t *)&nv->version;
6058 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
6059 while (cnt--)
6060 *dptr1++ = *dptr2++;
6061
6062 icb->login_retry_count = nv->login_retry_count;
Andrew Vasquez3ea66e22006-06-23 16:11:27 -07006063 icb->link_down_on_nos = nv->link_down_on_nos;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006064
6065 /* Copy 2nd segment. */
6066 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
6067 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
6068 cnt = (uint8_t *)&icb->reserved_3 -
6069 (uint8_t *)&icb->interrupt_delay_timer;
6070 while (cnt--)
6071 *dptr1++ = *dptr2++;
6072
6073 /*
6074 * Setup driver NVRAM options.
6075 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006076 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Andrew Vasquez9bb9fcf2007-01-29 10:22:24 -08006077 "QLA2462");
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006078
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006079 qlt_24xx_config_nvram_stage2(vha, icb);
6080
Bart Van Asschead950362015-07-09 07:24:08 -07006081 if (nv->host_p & cpu_to_le32(BIT_15)) {
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04006082 /* Use alternate WWN? */
Andrew Vasquez5341e862006-05-17 15:09:16 -07006083 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
6084 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
6085 }
6086
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006087 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07006088 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006089 /*
6090 * Firmware will apply the following mask if the nodename was
6091 * not provided.
6092 */
6093 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
6094 icb->node_name[0] &= 0xF0;
6095 }
6096
6097 /* Set host adapter parameters. */
6098 ha->flags.disable_risc_code_load = 0;
Andrew Vasquez0c8c39a2006-12-13 19:20:30 -08006099 ha->flags.enable_lip_reset = 0;
6100 ha->flags.enable_lip_full_login =
6101 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
6102 ha->flags.enable_target_reset =
6103 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006104 ha->flags.enable_led_scheme = 0;
Andrew Vasquezd4c760c2006-06-23 16:10:39 -07006105 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006106
Andrew Vasquezfd0e7e42006-05-17 15:09:11 -07006107 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
6108 (BIT_6 | BIT_5 | BIT_4)) >> 4;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006109
6110 memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
6111 sizeof(ha->fw_seriallink_options24));
6112
6113 /* save HBA serial number */
6114 ha->serial0 = icb->port_name[5];
6115 ha->serial1 = icb->port_name[6];
6116 ha->serial2 = icb->port_name[7];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006117 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
6118 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006119
Bart Van Asschead950362015-07-09 07:24:08 -07006120 icb->execution_throttle = cpu_to_le16(0xFFFF);
andrew.vasquez@qlogic.combc8fb3c2006-01-13 17:05:42 -08006121
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006122 ha->retry_count = le16_to_cpu(nv->login_retry_count);
6123
6124 /* Set minimum login_timeout to 4 seconds. */
6125 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
6126 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
6127 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07006128 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006129 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006130
Andrew Vasquez00a537b2008-02-28 14:06:11 -08006131 /* Set minimum RATOV to 100 tenths of a second. */
6132 ha->r_a_tov = 100;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006133
6134 ha->loop_reset_delay = nv->reset_delay;
6135
6136 /* Link Down Timeout = 0:
6137 *
6138 * When Port Down timer expires we will start returning
6139 * I/O's to OS with "DID_NO_CONNECT".
6140 *
6141 * Link Down Timeout != 0:
6142 *
6143 * The driver waits for the link to come up after link down
6144 * before returning I/Os to OS with "DID_NO_CONNECT".
6145 */
6146 if (le16_to_cpu(nv->link_down_timeout) == 0) {
6147 ha->loop_down_abort_time =
6148 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
6149 } else {
6150 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
6151 ha->loop_down_abort_time =
6152 (LOOP_DOWN_TIME - ha->link_down_timeout);
6153 }
6154
6155 /* Need enough time to try and get the port back. */
6156 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
6157 if (qlport_down_retry)
6158 ha->port_down_retry_count = qlport_down_retry;
6159
6160 /* Set login_retry_count */
6161 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
6162 if (ha->port_down_retry_count ==
6163 le16_to_cpu(nv->port_down_retry_count) &&
6164 ha->port_down_retry_count > 3)
6165 ha->login_retry_count = ha->port_down_retry_count;
6166 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
6167 ha->login_retry_count = ha->port_down_retry_count;
6168 if (ql2xloginretrycount)
6169 ha->login_retry_count = ql2xloginretrycount;
6170
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006171 /* Enable ZIO. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006172 if (!vha->flags.init_done) {
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006173 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
6174 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
6175 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
6176 le16_to_cpu(icb->interrupt_delay_timer): 2;
6177 }
Bart Van Asschead950362015-07-09 07:24:08 -07006178 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006179 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006180 vha->flags.process_response_queue = 0;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006181 if (ha->zio_mode != QLA_ZIO_DISABLED) {
andrew.vasquez@qlogic.com4a59f712006-03-09 14:27:39 -08006182 ha->zio_mode = QLA_ZIO_MODE_6;
6183
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006184 ql_log(ql_log_info, vha, 0x006f,
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006185 "ZIO mode %d enabled; timer delay (%d us).\n",
6186 ha->zio_mode, ha->zio_timer * 100);
6187
6188 icb->firmware_options_2 |= cpu_to_le32(
6189 (uint32_t)ha->zio_mode);
6190 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006191 vha->flags.process_response_queue = 1;
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07006192 }
6193
David Miller4e08df32007-04-16 12:37:43 -07006194 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006195 ql_log(ql_log_warn, vha, 0x0070,
6196 "NVRAM configuration failed.\n");
David Miller4e08df32007-04-16 12:37:43 -07006197 }
6198 return (rval);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006199}
6200
Sawan Chandak4243c112016-01-27 12:03:31 -05006201uint8_t qla27xx_find_valid_image(struct scsi_qla_host *vha)
6202{
6203 struct qla27xx_image_status pri_image_status, sec_image_status;
6204 uint8_t valid_pri_image, valid_sec_image;
6205 uint32_t *wptr;
6206 uint32_t cnt, chksum, size;
6207 struct qla_hw_data *ha = vha->hw;
6208
6209 valid_pri_image = valid_sec_image = 1;
6210 ha->active_image = 0;
6211 size = sizeof(struct qla27xx_image_status) / sizeof(uint32_t);
6212
6213 if (!ha->flt_region_img_status_pri) {
6214 valid_pri_image = 0;
6215 goto check_sec_image;
6216 }
6217
6218 qla24xx_read_flash_data(vha, (uint32_t *)(&pri_image_status),
6219 ha->flt_region_img_status_pri, size);
6220
6221 if (pri_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6222 ql_dbg(ql_dbg_init, vha, 0x018b,
6223 "Primary image signature (0x%x) not valid\n",
6224 pri_image_status.signature);
6225 valid_pri_image = 0;
6226 goto check_sec_image;
6227 }
6228
6229 wptr = (uint32_t *)(&pri_image_status);
6230 cnt = size;
6231
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006232 for (chksum = 0; cnt--; wptr++)
6233 chksum += le32_to_cpu(*wptr);
Quinn Tran41dc5292017-01-19 22:28:03 -08006234
Sawan Chandak4243c112016-01-27 12:03:31 -05006235 if (chksum) {
6236 ql_dbg(ql_dbg_init, vha, 0x018c,
6237 "Checksum validation failed for primary image (0x%x)\n",
6238 chksum);
6239 valid_pri_image = 0;
6240 }
6241
6242check_sec_image:
6243 if (!ha->flt_region_img_status_sec) {
6244 valid_sec_image = 0;
6245 goto check_valid_image;
6246 }
6247
6248 qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
6249 ha->flt_region_img_status_sec, size);
6250
6251 if (sec_image_status.signature != QLA27XX_IMG_STATUS_SIGN) {
6252 ql_dbg(ql_dbg_init, vha, 0x018d,
6253 "Secondary image signature(0x%x) not valid\n",
6254 sec_image_status.signature);
6255 valid_sec_image = 0;
6256 goto check_valid_image;
6257 }
6258
6259 wptr = (uint32_t *)(&sec_image_status);
6260 cnt = size;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05006261 for (chksum = 0; cnt--; wptr++)
6262 chksum += le32_to_cpu(*wptr);
Sawan Chandak4243c112016-01-27 12:03:31 -05006263 if (chksum) {
6264 ql_dbg(ql_dbg_init, vha, 0x018e,
6265 "Checksum validation failed for secondary image (0x%x)\n",
6266 chksum);
6267 valid_sec_image = 0;
6268 }
6269
6270check_valid_image:
6271 if (valid_pri_image && (pri_image_status.image_status_mask & 0x1))
6272 ha->active_image = QLA27XX_PRIMARY_IMAGE;
6273 if (valid_sec_image && (sec_image_status.image_status_mask & 0x1)) {
6274 if (!ha->active_image ||
6275 pri_image_status.generation_number <
6276 sec_image_status.generation_number)
6277 ha->active_image = QLA27XX_SECONDARY_IMAGE;
6278 }
6279
6280 ql_dbg(ql_dbg_init, vha, 0x018f, "%s image\n",
6281 ha->active_image == 0 ? "default bootld and fw" :
6282 ha->active_image == 1 ? "primary" :
6283 ha->active_image == 2 ? "secondary" :
6284 "Invalid");
6285
6286 return ha->active_image;
6287}
6288
Adrian Bunk413975a2006-06-30 02:33:06 -07006289static int
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006290qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
6291 uint32_t faddr)
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006292{
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006293 int rval = QLA_SUCCESS;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006294 int segments, fragment;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006295 uint32_t *dcode, dlen;
6296 uint32_t risc_addr;
6297 uint32_t risc_size;
6298 uint32_t i;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006299 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006300 struct req_que *req = ha->req_q_map[0];
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006301
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006302 ql_dbg(ql_dbg_init, vha, 0x008b,
Chad Dupuiscfb09192011-11-18 09:03:07 -08006303 "FW: Loading firmware from flash (%x).\n", faddr);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006304
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006305 rval = QLA_SUCCESS;
6306
6307 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006308 dcode = (uint32_t *)req->ring;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006309 *srisc_addr = 0;
6310
Sawan Chandak4243c112016-01-27 12:03:31 -05006311 if (IS_QLA27XX(ha) &&
6312 qla27xx_find_valid_image(vha) == QLA27XX_SECONDARY_IMAGE)
6313 faddr = ha->flt_region_fw_sec;
6314
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006315 /* Validate firmware image by checking version. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006316 qla24xx_read_flash_data(vha, dcode, faddr + 4, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006317 for (i = 0; i < 4; i++)
6318 dcode[i] = be32_to_cpu(dcode[i]);
6319 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
6320 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
6321 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
6322 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006323 ql_log(ql_log_fatal, vha, 0x008c,
6324 "Unable to verify the integrity of flash firmware "
6325 "image.\n");
6326 ql_log(ql_log_fatal, vha, 0x008d,
6327 "Firmware data: %08x %08x %08x %08x.\n",
6328 dcode[0], dcode[1], dcode[2], dcode[3]);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006329
6330 return QLA_FUNCTION_FAILED;
6331 }
6332
6333 while (segments && rval == QLA_SUCCESS) {
6334 /* Read segment's load information. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006335 qla24xx_read_flash_data(vha, dcode, faddr, 4);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006336
6337 risc_addr = be32_to_cpu(dcode[2]);
6338 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
6339 risc_size = be32_to_cpu(dcode[3]);
6340
6341 fragment = 0;
6342 while (risc_size > 0 && rval == QLA_SUCCESS) {
6343 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
6344 if (dlen > risc_size)
6345 dlen = risc_size;
6346
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006347 ql_dbg(ql_dbg_init, vha, 0x008e,
6348 "Loading risc segment@ risc addr %x "
6349 "number of dwords 0x%x offset 0x%x.\n",
6350 risc_addr, dlen, faddr);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006351
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006352 qla24xx_read_flash_data(vha, dcode, faddr, dlen);
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006353 for (i = 0; i < dlen; i++)
6354 dcode[i] = swab32(dcode[i]);
6355
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006356 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006357 dlen);
6358 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006359 ql_log(ql_log_fatal, vha, 0x008f,
6360 "Failed to load segment %d of firmware.\n",
6361 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04006362 return QLA_FUNCTION_FAILED;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006363 }
6364
6365 faddr += dlen;
6366 risc_addr += dlen;
6367 risc_size -= dlen;
6368 fragment++;
6369 }
6370
6371 /* Next segment. */
6372 segments--;
6373 }
6374
Chad Dupuisf73cb692014-02-26 04:15:06 -05006375 if (!IS_QLA27XX(ha))
6376 return rval;
6377
6378 if (ha->fw_dump_template)
6379 vfree(ha->fw_dump_template);
6380 ha->fw_dump_template = NULL;
6381 ha->fw_dump_template_len = 0;
6382
6383 ql_dbg(ql_dbg_init, vha, 0x0161,
6384 "Loading fwdump template from %x\n", faddr);
6385 qla24xx_read_flash_data(vha, dcode, faddr, 7);
6386 risc_size = be32_to_cpu(dcode[2]);
6387 ql_dbg(ql_dbg_init, vha, 0x0162,
6388 "-> array size %x dwords\n", risc_size);
6389 if (risc_size == 0 || risc_size == ~0)
6390 goto default_template;
6391
6392 dlen = (risc_size - 8) * sizeof(*dcode);
6393 ql_dbg(ql_dbg_init, vha, 0x0163,
6394 "-> template allocating %x bytes...\n", dlen);
6395 ha->fw_dump_template = vmalloc(dlen);
6396 if (!ha->fw_dump_template) {
6397 ql_log(ql_log_warn, vha, 0x0164,
6398 "Failed fwdump template allocate %x bytes.\n", risc_size);
6399 goto default_template;
6400 }
6401
6402 faddr += 7;
6403 risc_size -= 8;
6404 dcode = ha->fw_dump_template;
6405 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
6406 for (i = 0; i < risc_size; i++)
6407 dcode[i] = le32_to_cpu(dcode[i]);
6408
6409 if (!qla27xx_fwdt_template_valid(dcode)) {
6410 ql_log(ql_log_warn, vha, 0x0165,
6411 "Failed fwdump template validate\n");
6412 goto default_template;
6413 }
6414
6415 dlen = qla27xx_fwdt_template_size(dcode);
6416 ql_dbg(ql_dbg_init, vha, 0x0166,
6417 "-> template size %x bytes\n", dlen);
6418 if (dlen > risc_size * sizeof(*dcode)) {
6419 ql_log(ql_log_warn, vha, 0x0167,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07006420 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07006421 (size_t)(dlen - risc_size * sizeof(*dcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006422 goto default_template;
6423 }
6424 ha->fw_dump_template_len = dlen;
6425 return rval;
6426
6427default_template:
6428 ql_log(ql_log_warn, vha, 0x0168, "Using default fwdump template\n");
6429 if (ha->fw_dump_template)
6430 vfree(ha->fw_dump_template);
6431 ha->fw_dump_template = NULL;
6432 ha->fw_dump_template_len = 0;
6433
6434 dlen = qla27xx_fwdt_template_default_size();
6435 ql_dbg(ql_dbg_init, vha, 0x0169,
6436 "-> template allocating %x bytes...\n", dlen);
6437 ha->fw_dump_template = vmalloc(dlen);
6438 if (!ha->fw_dump_template) {
6439 ql_log(ql_log_warn, vha, 0x016a,
6440 "Failed fwdump template allocate %x bytes.\n", risc_size);
6441 goto failed_template;
6442 }
6443
6444 dcode = ha->fw_dump_template;
6445 risc_size = dlen / sizeof(*dcode);
6446 memcpy(dcode, qla27xx_fwdt_template_default(), dlen);
6447 for (i = 0; i < risc_size; i++)
6448 dcode[i] = be32_to_cpu(dcode[i]);
6449
6450 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
6451 ql_log(ql_log_warn, vha, 0x016b,
6452 "Failed fwdump template validate\n");
6453 goto failed_template;
6454 }
6455
6456 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
6457 ql_dbg(ql_dbg_init, vha, 0x016c,
6458 "-> template size %x bytes\n", dlen);
6459 ha->fw_dump_template_len = dlen;
6460 return rval;
6461
6462failed_template:
6463 ql_log(ql_log_warn, vha, 0x016d, "Failed default fwdump template\n");
6464 if (ha->fw_dump_template)
6465 vfree(ha->fw_dump_template);
6466 ha->fw_dump_template = NULL;
6467 ha->fw_dump_template_len = 0;
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006468 return rval;
6469}
6470
Giridhar Malavalie9454a82013-02-08 01:57:47 -05006471#define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006472
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006473int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006474qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez54333832005-11-09 15:49:04 -08006475{
6476 int rval;
6477 int i, fragment;
6478 uint16_t *wcode, *fwcode;
6479 uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
6480 struct fw_blob *blob;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006481 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006482 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez54333832005-11-09 15:49:04 -08006483
6484 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006485 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08006486 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006487 ql_log(ql_log_info, vha, 0x0083,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01006488 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006489 ql_log(ql_log_info, vha, 0x0084,
6490 "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
Andrew Vasquez54333832005-11-09 15:49:04 -08006491 return QLA_FUNCTION_FAILED;
6492 }
6493
6494 rval = QLA_SUCCESS;
6495
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006496 wcode = (uint16_t *)req->ring;
Andrew Vasquez54333832005-11-09 15:49:04 -08006497 *srisc_addr = 0;
6498 fwcode = (uint16_t *)blob->fw->data;
6499 fwclen = 0;
6500
6501 /* Validate firmware image by checking version. */
6502 if (blob->fw->size < 8 * sizeof(uint16_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006503 ql_log(ql_log_fatal, vha, 0x0085,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006504 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08006505 blob->fw->size);
6506 goto fail_fw_integrity;
6507 }
6508 for (i = 0; i < 4; i++)
6509 wcode[i] = be16_to_cpu(fwcode[i + 4]);
6510 if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
6511 wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
6512 wcode[2] == 0 && wcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006513 ql_log(ql_log_fatal, vha, 0x0086,
6514 "Unable to verify integrity of firmware image.\n");
6515 ql_log(ql_log_fatal, vha, 0x0087,
6516 "Firmware data: %04x %04x %04x %04x.\n",
6517 wcode[0], wcode[1], wcode[2], wcode[3]);
Andrew Vasquez54333832005-11-09 15:49:04 -08006518 goto fail_fw_integrity;
6519 }
6520
6521 seg = blob->segs;
6522 while (*seg && rval == QLA_SUCCESS) {
6523 risc_addr = *seg;
6524 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
6525 risc_size = be16_to_cpu(fwcode[3]);
6526
6527 /* Validate firmware image size. */
6528 fwclen += risc_size * sizeof(uint16_t);
6529 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006530 ql_log(ql_log_fatal, vha, 0x0088,
Andrew Vasquez54333832005-11-09 15:49:04 -08006531 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006532 "(%zd).\n", blob->fw->size);
Andrew Vasquez54333832005-11-09 15:49:04 -08006533 goto fail_fw_integrity;
6534 }
6535
6536 fragment = 0;
6537 while (risc_size > 0 && rval == QLA_SUCCESS) {
6538 wlen = (uint16_t)(ha->fw_transfer_size >> 1);
6539 if (wlen > risc_size)
6540 wlen = risc_size;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006541 ql_dbg(ql_dbg_init, vha, 0x0089,
6542 "Loading risc segment@ risc addr %x number of "
6543 "words 0x%x.\n", risc_addr, wlen);
Andrew Vasquez54333832005-11-09 15:49:04 -08006544
6545 for (i = 0; i < wlen; i++)
6546 wcode[i] = swab16(fwcode[i]);
6547
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006548 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
Andrew Vasquez54333832005-11-09 15:49:04 -08006549 wlen);
6550 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006551 ql_log(ql_log_fatal, vha, 0x008a,
6552 "Failed to load segment %d of firmware.\n",
6553 fragment);
Andrew Vasquez54333832005-11-09 15:49:04 -08006554 break;
6555 }
6556
6557 fwcode += wlen;
6558 risc_addr += wlen;
6559 risc_size -= wlen;
6560 fragment++;
6561 }
6562
6563 /* Next segment. */
6564 seg++;
6565 }
6566 return rval;
6567
6568fail_fw_integrity:
6569 return QLA_FUNCTION_FAILED;
6570}
6571
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006572static int
6573qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006574{
6575 int rval;
6576 int segments, fragment;
6577 uint32_t *dcode, dlen;
6578 uint32_t risc_addr;
6579 uint32_t risc_size;
6580 uint32_t i;
Andrew Vasquez54333832005-11-09 15:49:04 -08006581 struct fw_blob *blob;
Chad Dupuisf73cb692014-02-26 04:15:06 -05006582 const uint32_t *fwcode;
6583 uint32_t fwclen;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006584 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006585 struct req_que *req = ha->req_q_map[0];
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006586
Andrew Vasquez54333832005-11-09 15:49:04 -08006587 /* Load firmware blob. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006588 blob = qla2x00_request_firmware(vha);
Andrew Vasquez54333832005-11-09 15:49:04 -08006589 if (!blob) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006590 ql_log(ql_log_warn, vha, 0x0090,
Yannick Guerrini94bcf832015-02-26 22:49:34 +01006591 "Firmware image unavailable.\n");
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006592 ql_log(ql_log_warn, vha, 0x0091,
6593 "Firmware images can be retrieved from: "
6594 QLA_FW_URL ".\n");
Andrew Vasquezd1c61902006-05-17 15:09:00 -07006595
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006596 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006597 }
6598
Chad Dupuiscfb09192011-11-18 09:03:07 -08006599 ql_dbg(ql_dbg_init, vha, 0x0092,
6600 "FW: Loading via request-firmware.\n");
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006601
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006602 rval = QLA_SUCCESS;
6603
6604 segments = FA_RISC_CODE_SEGMENTS;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006605 dcode = (uint32_t *)req->ring;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006606 *srisc_addr = 0;
Andrew Vasquez54333832005-11-09 15:49:04 -08006607 fwcode = (uint32_t *)blob->fw->data;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006608 fwclen = 0;
6609
6610 /* Validate firmware image by checking version. */
Andrew Vasquez54333832005-11-09 15:49:04 -08006611 if (blob->fw->size < 8 * sizeof(uint32_t)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006612 ql_log(ql_log_fatal, vha, 0x0093,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006613 "Unable to verify integrity of firmware image (%zd).\n",
Andrew Vasquez54333832005-11-09 15:49:04 -08006614 blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05006615 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006616 }
6617 for (i = 0; i < 4; i++)
6618 dcode[i] = be32_to_cpu(fwcode[i + 4]);
6619 if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
6620 dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
6621 (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
6622 dcode[3] == 0)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006623 ql_log(ql_log_fatal, vha, 0x0094,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006624 "Unable to verify integrity of firmware image (%zd).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006625 blob->fw->size);
6626 ql_log(ql_log_fatal, vha, 0x0095,
6627 "Firmware data: %08x %08x %08x %08x.\n",
6628 dcode[0], dcode[1], dcode[2], dcode[3]);
Chad Dupuisf73cb692014-02-26 04:15:06 -05006629 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006630 }
6631
6632 while (segments && rval == QLA_SUCCESS) {
6633 risc_addr = be32_to_cpu(fwcode[2]);
6634 *srisc_addr = *srisc_addr == 0 ? risc_addr : *srisc_addr;
6635 risc_size = be32_to_cpu(fwcode[3]);
6636
6637 /* Validate firmware image size. */
6638 fwclen += risc_size * sizeof(uint32_t);
Andrew Vasquez54333832005-11-09 15:49:04 -08006639 if (blob->fw->size < fwclen) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006640 ql_log(ql_log_fatal, vha, 0x0096,
Andrew Vasquez54333832005-11-09 15:49:04 -08006641 "Unable to verify integrity of firmware image "
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08006642 "(%zd).\n", blob->fw->size);
Chad Dupuisf73cb692014-02-26 04:15:06 -05006643 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006644 }
6645
6646 fragment = 0;
6647 while (risc_size > 0 && rval == QLA_SUCCESS) {
6648 dlen = (uint32_t)(ha->fw_transfer_size >> 2);
6649 if (dlen > risc_size)
6650 dlen = risc_size;
6651
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006652 ql_dbg(ql_dbg_init, vha, 0x0097,
6653 "Loading risc segment@ risc addr %x "
6654 "number of dwords 0x%x.\n", risc_addr, dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006655
6656 for (i = 0; i < dlen; i++)
6657 dcode[i] = swab32(fwcode[i]);
6658
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006659 rval = qla2x00_load_ram(vha, req->dma, risc_addr,
andrew.vasquez@qlogic.com590f98e2006-01-13 17:05:37 -08006660 dlen);
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006661 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006662 ql_log(ql_log_fatal, vha, 0x0098,
6663 "Failed to load segment %d of firmware.\n",
6664 fragment);
Chad Dupuisf261f7a2014-09-25 05:17:03 -04006665 return QLA_FUNCTION_FAILED;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006666 }
6667
6668 fwcode += dlen;
6669 risc_addr += dlen;
6670 risc_size -= dlen;
6671 fragment++;
6672 }
6673
6674 /* Next segment. */
6675 segments--;
6676 }
Chad Dupuisf73cb692014-02-26 04:15:06 -05006677
6678 if (!IS_QLA27XX(ha))
6679 return rval;
6680
6681 if (ha->fw_dump_template)
6682 vfree(ha->fw_dump_template);
6683 ha->fw_dump_template = NULL;
6684 ha->fw_dump_template_len = 0;
6685
6686 ql_dbg(ql_dbg_init, vha, 0x171,
Chad Dupuis97ea7022014-03-13 14:16:40 -04006687 "Loading fwdump template from %x\n",
6688 (uint32_t)((void *)fwcode - (void *)blob->fw->data));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006689 risc_size = be32_to_cpu(fwcode[2]);
6690 ql_dbg(ql_dbg_init, vha, 0x172,
6691 "-> array size %x dwords\n", risc_size);
6692 if (risc_size == 0 || risc_size == ~0)
6693 goto default_template;
6694
6695 dlen = (risc_size - 8) * sizeof(*fwcode);
6696 ql_dbg(ql_dbg_init, vha, 0x0173,
6697 "-> template allocating %x bytes...\n", dlen);
6698 ha->fw_dump_template = vmalloc(dlen);
6699 if (!ha->fw_dump_template) {
6700 ql_log(ql_log_warn, vha, 0x0174,
6701 "Failed fwdump template allocate %x bytes.\n", risc_size);
6702 goto default_template;
6703 }
6704
6705 fwcode += 7;
6706 risc_size -= 8;
6707 dcode = ha->fw_dump_template;
6708 for (i = 0; i < risc_size; i++)
6709 dcode[i] = le32_to_cpu(fwcode[i]);
6710
6711 if (!qla27xx_fwdt_template_valid(dcode)) {
6712 ql_log(ql_log_warn, vha, 0x0175,
6713 "Failed fwdump template validate\n");
6714 goto default_template;
6715 }
6716
6717 dlen = qla27xx_fwdt_template_size(dcode);
6718 ql_dbg(ql_dbg_init, vha, 0x0176,
6719 "-> template size %x bytes\n", dlen);
6720 if (dlen > risc_size * sizeof(*fwcode)) {
6721 ql_log(ql_log_warn, vha, 0x0177,
Himanshu Madhani4fae52b2017-06-06 13:55:23 -07006722 "Failed fwdump template exceeds array by %zx bytes\n",
Joe Carnuccio383a2982017-06-02 09:11:55 -07006723 (size_t)(dlen - risc_size * sizeof(*fwcode)));
Chad Dupuisf73cb692014-02-26 04:15:06 -05006724 goto default_template;
6725 }
6726 ha->fw_dump_template_len = dlen;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006727 return rval;
6728
Chad Dupuisf73cb692014-02-26 04:15:06 -05006729default_template:
6730 ql_log(ql_log_warn, vha, 0x0178, "Using default fwdump template\n");
6731 if (ha->fw_dump_template)
6732 vfree(ha->fw_dump_template);
6733 ha->fw_dump_template = NULL;
6734 ha->fw_dump_template_len = 0;
6735
6736 dlen = qla27xx_fwdt_template_default_size();
6737 ql_dbg(ql_dbg_init, vha, 0x0179,
6738 "-> template allocating %x bytes...\n", dlen);
6739 ha->fw_dump_template = vmalloc(dlen);
6740 if (!ha->fw_dump_template) {
6741 ql_log(ql_log_warn, vha, 0x017a,
6742 "Failed fwdump template allocate %x bytes.\n", risc_size);
6743 goto failed_template;
6744 }
6745
6746 dcode = ha->fw_dump_template;
6747 risc_size = dlen / sizeof(*fwcode);
6748 fwcode = qla27xx_fwdt_template_default();
6749 for (i = 0; i < risc_size; i++)
6750 dcode[i] = be32_to_cpu(fwcode[i]);
6751
6752 if (!qla27xx_fwdt_template_valid(ha->fw_dump_template)) {
6753 ql_log(ql_log_warn, vha, 0x017b,
6754 "Failed fwdump template validate\n");
6755 goto failed_template;
6756 }
6757
6758 dlen = qla27xx_fwdt_template_size(ha->fw_dump_template);
6759 ql_dbg(ql_dbg_init, vha, 0x017c,
6760 "-> template size %x bytes\n", dlen);
6761 ha->fw_dump_template_len = dlen;
6762 return rval;
6763
6764failed_template:
6765 ql_log(ql_log_warn, vha, 0x017d, "Failed default fwdump template\n");
6766 if (ha->fw_dump_template)
6767 vfree(ha->fw_dump_template);
6768 ha->fw_dump_template = NULL;
6769 ha->fw_dump_template_len = 0;
6770 return rval;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07006771}
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006772
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006773int
6774qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
6775{
6776 int rval;
6777
Andrew Vasqueze337d902009-04-06 22:33:49 -07006778 if (ql2xfwloadbin == 1)
6779 return qla81xx_load_risc(vha, srisc_addr);
6780
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006781 /*
6782 * FW Load priority:
6783 * 1) Firmware via request-firmware interface (.bin file).
6784 * 2) Firmware residing in flash.
6785 */
6786 rval = qla24xx_load_risc_blob(vha, srisc_addr);
6787 if (rval == QLA_SUCCESS)
6788 return rval;
6789
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006790 return qla24xx_load_risc_flash(vha, srisc_addr,
6791 vha->hw->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006792}
6793
6794int
6795qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
6796{
6797 int rval;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006798 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006799
Andrew Vasqueze337d902009-04-06 22:33:49 -07006800 if (ql2xfwloadbin == 2)
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006801 goto try_blob_fw;
Andrew Vasqueze337d902009-04-06 22:33:49 -07006802
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006803 /*
6804 * FW Load priority:
6805 * 1) Firmware residing in flash.
6806 * 2) Firmware via request-firmware interface (.bin file).
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006807 * 3) Golden-Firmware residing in flash -- limited operation.
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006808 */
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006809 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006810 if (rval == QLA_SUCCESS)
6811 return rval;
6812
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006813try_blob_fw:
6814 rval = qla24xx_load_risc_blob(vha, srisc_addr);
6815 if (rval == QLA_SUCCESS || !ha->flt_region_gold_fw)
6816 return rval;
6817
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006818 ql_log(ql_log_info, vha, 0x0099,
6819 "Attempting to fallback to golden firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006820 rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
6821 if (rval != QLA_SUCCESS)
6822 return rval;
6823
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006824 ql_log(ql_log_info, vha, 0x009a, "Update operational firmware.\n");
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006825 ha->flags.running_gold_fw = 1;
Andrew Vasquezcbc8eb62009-06-03 09:55:17 -07006826 return rval;
Andrew Vasquezeaac30b2009-01-22 09:45:32 -08006827}
6828
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006829void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006830qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006831{
6832 int ret, retries;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006833 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006834
Andrew Vasquez85880802009-12-15 21:29:46 -08006835 if (ha->flags.pci_channel_io_perm_failure)
6836 return;
Andrew Vasqueze4289242007-07-19 15:05:56 -07006837 if (!IS_FWI2_CAPABLE(ha))
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006838 return;
Andrew Vasquez75edf812007-05-07 07:43:00 -07006839 if (!ha->fw_major_version)
6840 return;
Quinn Tranec7193e2017-03-15 09:48:55 -07006841 if (!ha->flags.fw_started)
6842 return;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006843
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006844 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez7c7f1f22008-02-28 14:06:09 -08006845 for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
Andrew Vasquezb469a7c2009-04-06 22:33:48 -07006846 ret != QLA_INVALID_COMMAND && retries ; retries--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006847 ha->isp_ops->reset_chip(vha);
6848 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006849 continue;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006850 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006851 continue;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07006852 ql_log(ql_log_info, vha, 0x8015,
6853 "Attempting retry of stop-firmware command.\n");
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006854 ret = qla2x00_stop_firmware(vha);
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006855 }
Quinn Tranec7193e2017-03-15 09:48:55 -07006856
Quinn Tran4b60c822017-06-13 20:47:21 -07006857 QLA_FW_STOPPED(ha);
Quinn Tranec7193e2017-03-15 09:48:55 -07006858 ha->flags.fw_init_done = 0;
Andrew Vasquez18c6c122006-10-13 09:33:38 -07006859}
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006860
6861int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006862qla24xx_configure_vhba(scsi_qla_host_t *vha)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006863{
6864 int rval = QLA_SUCCESS;
Chad Dupuis0b91d112012-02-09 11:15:42 -08006865 int rval2;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006866 uint16_t mb[MAILBOX_REGISTER_COUNT];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006867 struct qla_hw_data *ha = vha->hw;
6868 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07006869 struct req_que *req;
6870 struct rsp_que *rsp;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006871
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006872 if (!vha->vp_idx)
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006873 return -EINVAL;
6874
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006875 rval = qla2x00_fw_ready(base_vha);
Michael Hernandezd7459522016-12-12 14:40:07 -08006876 if (vha->qpair)
6877 req = vha->qpair->req;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07006878 else
Michael Hernandezd7459522016-12-12 14:40:07 -08006879 req = ha->req_q_map[0];
Anirban Chakraborty67c2e932009-04-06 22:33:42 -07006880 rsp = req->rsp;
6881
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006882 if (rval == QLA_SUCCESS) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006883 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08006884 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006885 }
6886
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006887 vha->flags.management_server_logged_in = 0;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006888
6889 /* Login to SNS first */
Chad Dupuis0b91d112012-02-09 11:15:42 -08006890 rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
6891 BIT_1);
6892 if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
6893 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
6894 ql_dbg(ql_dbg_init, vha, 0x0120,
6895 "Failed SNS login: loop_id=%x, rval2=%d\n",
6896 NPH_SNS, rval2);
6897 else
6898 ql_dbg(ql_dbg_init, vha, 0x0103,
6899 "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
6900 "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
6901 NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006902 return (QLA_FUNCTION_FAILED);
6903 }
6904
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006905 atomic_set(&vha->loop_down_timer, 0);
6906 atomic_set(&vha->loop_state, LOOP_UP);
6907 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6908 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
6909 rval = qla2x00_loop_resync(base_vha);
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07006910
6911 return rval;
6912}
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006913
6914/* 84XX Support **************************************************************/
6915
6916static LIST_HEAD(qla_cs84xx_list);
6917static DEFINE_MUTEX(qla_cs84xx_mutex);
6918
6919static struct qla_chip_state_84xx *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006920qla84xx_get_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006921{
6922 struct qla_chip_state_84xx *cs84xx;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006923 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006924
6925 mutex_lock(&qla_cs84xx_mutex);
6926
6927 /* Find any shared 84xx chip. */
6928 list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
6929 if (cs84xx->bus == ha->pdev->bus) {
6930 kref_get(&cs84xx->kref);
6931 goto done;
6932 }
6933 }
6934
6935 cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
6936 if (!cs84xx)
6937 goto done;
6938
6939 kref_init(&cs84xx->kref);
6940 spin_lock_init(&cs84xx->access_lock);
6941 mutex_init(&cs84xx->fw_update_mutex);
6942 cs84xx->bus = ha->pdev->bus;
6943
6944 list_add_tail(&cs84xx->list, &qla_cs84xx_list);
6945done:
6946 mutex_unlock(&qla_cs84xx_mutex);
6947 return cs84xx;
6948}
6949
6950static void
6951__qla84xx_chip_release(struct kref *kref)
6952{
6953 struct qla_chip_state_84xx *cs84xx =
6954 container_of(kref, struct qla_chip_state_84xx, kref);
6955
6956 mutex_lock(&qla_cs84xx_mutex);
6957 list_del(&cs84xx->list);
6958 mutex_unlock(&qla_cs84xx_mutex);
6959 kfree(cs84xx);
6960}
6961
6962void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006963qla84xx_put_chip(struct scsi_qla_host *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006964{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006965 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006966 if (ha->cs84xx)
6967 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
6968}
6969
6970static int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006971qla84xx_init_chip(scsi_qla_host_t *vha)
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006972{
6973 int rval;
6974 uint16_t status[2];
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006975 struct qla_hw_data *ha = vha->hw;
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006976
6977 mutex_lock(&ha->cs84xx->fw_update_mutex);
6978
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08006979 rval = qla84xx_verify_chip(vha, status);
Harihara Kadayam4d4df192008-04-03 13:13:26 -07006980
6981 mutex_unlock(&ha->cs84xx->fw_update_mutex);
6982
6983 return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED:
6984 QLA_SUCCESS;
6985}
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08006986
6987/* 81XX Support **************************************************************/
6988
6989int
6990qla81xx_nvram_config(scsi_qla_host_t *vha)
6991{
6992 int rval;
6993 struct init_cb_81xx *icb;
6994 struct nvram_81xx *nv;
6995 uint32_t *dptr;
6996 uint8_t *dptr1, *dptr2;
6997 uint32_t chksum;
6998 uint16_t cnt;
6999 struct qla_hw_data *ha = vha->hw;
7000
7001 rval = QLA_SUCCESS;
7002 icb = (struct init_cb_81xx *)ha->init_cb;
7003 nv = ha->nvram;
7004
7005 /* Determine NVRAM starting address. */
7006 ha->nvram_size = sizeof(struct nvram_81xx);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007007 ha->vpd_size = FA_NVRAM_VPD_SIZE;
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007008 if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
7009 ha->vpd_size = FA_VPD_SIZE_82XX;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007010
7011 /* Get VPD data into cache */
7012 ha->vpd = ha->nvram + VPD_OFFSET;
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007013 ha->isp_ops->read_optrom(vha, ha->vpd, ha->flt_region_vpd << 2,
7014 ha->vpd_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007015
7016 /* Get NVRAM data into cache and calculate checksum. */
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007017 ha->isp_ops->read_optrom(vha, ha->nvram, ha->flt_region_nvram << 2,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007018 ha->nvram_size);
Andrew Vasquez3d79038f2009-03-24 09:08:14 -07007019 dptr = (uint32_t *)nv;
Joe Carnuccioda08ef52016-01-27 12:03:34 -05007020 for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7021 chksum += le32_to_cpu(*dptr);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007022
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007023 ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
7024 "Contents of NVRAM:\n");
7025 ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
7026 (uint8_t *)nv, ha->nvram_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007027
7028 /* Bad NVRAM data, set defaults parameters. */
7029 if (chksum || nv->id[0] != 'I' || nv->id[1] != 'S' || nv->id[2] != 'P'
7030 || nv->id[3] != ' ' ||
Bart Van Asschead950362015-07-09 07:24:08 -07007031 nv->nvram_version < cpu_to_le16(ICB_VERSION)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007032 /* Reset NVRAM data. */
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007033 ql_log(ql_log_info, vha, 0x0073,
Raul Porcel9e336522012-05-15 14:34:08 -04007034 "Inconsistent NVRAM detected: checksum=0x%x id=%c "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007035 "version=0x%x.\n", chksum, nv->id[0],
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007036 le16_to_cpu(nv->nvram_version));
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007037 ql_log(ql_log_info, vha, 0x0074,
7038 "Falling back to functioning (yet invalid -- WWPN) "
7039 "defaults.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007040
7041 /*
7042 * Set default initialization control block.
7043 */
7044 memset(nv, 0, ha->nvram_size);
Bart Van Asschead950362015-07-09 07:24:08 -07007045 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7046 nv->version = cpu_to_le16(ICB_VERSION);
Joe Carnuccio98aee702014-09-25 05:16:38 -04007047 nv->frame_payload_size = 2048;
Bart Van Asschead950362015-07-09 07:24:08 -07007048 nv->execution_throttle = cpu_to_le16(0xFFFF);
7049 nv->exchange_count = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007050 nv->port_name[0] = 0x21;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007051 nv->port_name[1] = 0x00 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007052 nv->port_name[2] = 0x00;
7053 nv->port_name[3] = 0xe0;
7054 nv->port_name[4] = 0x8b;
7055 nv->port_name[5] = 0x1c;
7056 nv->port_name[6] = 0x55;
7057 nv->port_name[7] = 0x86;
7058 nv->node_name[0] = 0x20;
7059 nv->node_name[1] = 0x00;
7060 nv->node_name[2] = 0x00;
7061 nv->node_name[3] = 0xe0;
7062 nv->node_name[4] = 0x8b;
7063 nv->node_name[5] = 0x1c;
7064 nv->node_name[6] = 0x55;
7065 nv->node_name[7] = 0x86;
Bart Van Asschead950362015-07-09 07:24:08 -07007066 nv->login_retry_count = cpu_to_le16(8);
7067 nv->interrupt_delay_timer = cpu_to_le16(0);
7068 nv->login_timeout = cpu_to_le16(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007069 nv->firmware_options_1 =
Bart Van Asschead950362015-07-09 07:24:08 -07007070 cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7071 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7072 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7073 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7074 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7075 nv->efi_parameters = cpu_to_le32(0);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007076 nv->reset_delay = 5;
Bart Van Asschead950362015-07-09 07:24:08 -07007077 nv->max_luns_per_target = cpu_to_le16(128);
7078 nv->port_down_retry_count = cpu_to_le16(30);
7079 nv->link_down_timeout = cpu_to_le16(180);
Andrew Vasquezeeebcc92009-06-03 09:55:24 -07007080 nv->enode_mac[0] = 0x00;
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007081 nv->enode_mac[1] = 0xC0;
7082 nv->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007083 nv->enode_mac[3] = 0x04;
7084 nv->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007085 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007086
7087 rval = 1;
7088 }
7089
Arun Easi9e522cd2012-08-22 14:21:31 -04007090 if (IS_T10_PI_CAPABLE(ha))
7091 nv->frame_payload_size &= ~7;
7092
Arun Easiaa230bc2013-01-30 03:34:39 -05007093 qlt_81xx_config_nvram_stage1(vha, nv);
7094
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007095 /* Reset Initialization control block */
Andrew Vasquez773120e2011-05-10 11:30:14 -07007096 memset(icb, 0, ha->init_cb_size);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007097
7098 /* Copy 1st segment. */
7099 dptr1 = (uint8_t *)icb;
7100 dptr2 = (uint8_t *)&nv->version;
7101 cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7102 while (cnt--)
7103 *dptr1++ = *dptr2++;
7104
7105 icb->login_retry_count = nv->login_retry_count;
7106
7107 /* Copy 2nd segment. */
7108 dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7109 dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7110 cnt = (uint8_t *)&icb->reserved_5 -
7111 (uint8_t *)&icb->interrupt_delay_timer;
7112 while (cnt--)
7113 *dptr1++ = *dptr2++;
7114
7115 memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
7116 /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
7117 if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
Andrew Vasquez69e5f1e2012-02-09 11:15:35 -08007118 icb->enode_mac[0] = 0x00;
7119 icb->enode_mac[1] = 0xC0;
7120 icb->enode_mac[2] = 0xDD;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007121 icb->enode_mac[3] = 0x04;
7122 icb->enode_mac[4] = 0x05;
Chad Dupuisf73cb692014-02-26 04:15:06 -05007123 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007124 }
7125
Andrew Vasquezb64b0e82009-03-24 09:08:01 -07007126 /* Use extended-initialization control block. */
7127 memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
7128
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007129 /*
7130 * Setup driver NVRAM options.
7131 */
7132 qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
Giridhar Malavalia9083012010-04-12 17:59:55 -07007133 "QLE8XXX");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007134
Arun Easiaa230bc2013-01-30 03:34:39 -05007135 qlt_81xx_config_nvram_stage2(vha, icb);
7136
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007137 /* Use alternate WWN? */
Bart Van Asschead950362015-07-09 07:24:08 -07007138 if (nv->host_p & cpu_to_le32(BIT_15)) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007139 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7140 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7141 }
7142
7143 /* Prepare nodename */
Bart Van Asschead950362015-07-09 07:24:08 -07007144 if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007145 /*
7146 * Firmware will apply the following mask if the nodename was
7147 * not provided.
7148 */
7149 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7150 icb->node_name[0] &= 0xF0;
7151 }
7152
7153 /* Set host adapter parameters. */
7154 ha->flags.disable_risc_code_load = 0;
7155 ha->flags.enable_lip_reset = 0;
7156 ha->flags.enable_lip_full_login =
7157 le32_to_cpu(nv->host_p) & BIT_10 ? 1: 0;
7158 ha->flags.enable_target_reset =
7159 le32_to_cpu(nv->host_p) & BIT_11 ? 1: 0;
7160 ha->flags.enable_led_scheme = 0;
7161 ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1: 0;
7162
7163 ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7164 (BIT_6 | BIT_5 | BIT_4)) >> 4;
7165
7166 /* save HBA serial number */
7167 ha->serial0 = icb->port_name[5];
7168 ha->serial1 = icb->port_name[6];
7169 ha->serial2 = icb->port_name[7];
7170 memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7171 memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7172
Bart Van Asschead950362015-07-09 07:24:08 -07007173 icb->execution_throttle = cpu_to_le16(0xFFFF);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007174
7175 ha->retry_count = le16_to_cpu(nv->login_retry_count);
7176
7177 /* Set minimum login_timeout to 4 seconds. */
7178 if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7179 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7180 if (le16_to_cpu(nv->login_timeout) < 4)
Bart Van Asschead950362015-07-09 07:24:08 -07007181 nv->login_timeout = cpu_to_le16(4);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007182 ha->login_timeout = le16_to_cpu(nv->login_timeout);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007183
7184 /* Set minimum RATOV to 100 tenths of a second. */
7185 ha->r_a_tov = 100;
7186
7187 ha->loop_reset_delay = nv->reset_delay;
7188
7189 /* Link Down Timeout = 0:
7190 *
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007191 * When Port Down timer expires we will start returning
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007192 * I/O's to OS with "DID_NO_CONNECT".
7193 *
7194 * Link Down Timeout != 0:
7195 *
7196 * The driver waits for the link to come up after link down
7197 * before returning I/Os to OS with "DID_NO_CONNECT".
7198 */
7199 if (le16_to_cpu(nv->link_down_timeout) == 0) {
7200 ha->loop_down_abort_time =
7201 (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7202 } else {
7203 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7204 ha->loop_down_abort_time =
7205 (LOOP_DOWN_TIME - ha->link_down_timeout);
7206 }
7207
7208 /* Need enough time to try and get the port back. */
7209 ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7210 if (qlport_down_retry)
7211 ha->port_down_retry_count = qlport_down_retry;
7212
7213 /* Set login_retry_count */
7214 ha->login_retry_count = le16_to_cpu(nv->login_retry_count);
7215 if (ha->port_down_retry_count ==
7216 le16_to_cpu(nv->port_down_retry_count) &&
7217 ha->port_down_retry_count > 3)
7218 ha->login_retry_count = ha->port_down_retry_count;
7219 else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7220 ha->login_retry_count = ha->port_down_retry_count;
7221 if (ql2xloginretrycount)
7222 ha->login_retry_count = ql2xloginretrycount;
7223
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007224 /* if not running MSI-X we need handshaking on interrupts */
Chad Dupuisf73cb692014-02-26 04:15:06 -05007225 if (!vha->hw->flags.msix_enabled && (IS_QLA83XX(ha) || IS_QLA27XX(ha)))
Bart Van Asschead950362015-07-09 07:24:08 -07007226 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08007227
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007228 /* Enable ZIO. */
7229 if (!vha->flags.init_done) {
7230 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7231 (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7232 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7233 le16_to_cpu(icb->interrupt_delay_timer): 2;
7234 }
Bart Van Asschead950362015-07-09 07:24:08 -07007235 icb->firmware_options_2 &= cpu_to_le32(
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007236 ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7237 vha->flags.process_response_queue = 0;
7238 if (ha->zio_mode != QLA_ZIO_DISABLED) {
7239 ha->zio_mode = QLA_ZIO_MODE_6;
7240
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007241 ql_log(ql_log_info, vha, 0x0075,
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007242 "ZIO mode %d enabled; timer delay (%d us).\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007243 ha->zio_mode,
7244 ha->zio_timer * 100);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007245
7246 icb->firmware_options_2 |= cpu_to_le32(
7247 (uint32_t)ha->zio_mode);
7248 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7249 vha->flags.process_response_queue = 1;
7250 }
7251
Quinn Tran41dc5292017-01-19 22:28:03 -08007252 /* enable RIDA Format2 */
7253 if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7254 icb->firmware_options_3 |= BIT_0;
7255
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007256 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007257 ql_log(ql_log_warn, vha, 0x0076,
7258 "NVRAM configuration failed.\n");
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007259 }
7260 return (rval);
7261}
7262
Giridhar Malavalia9083012010-04-12 17:59:55 -07007263int
7264qla82xx_restart_isp(scsi_qla_host_t *vha)
7265{
7266 int status, rval;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007267 struct qla_hw_data *ha = vha->hw;
7268 struct req_que *req = ha->req_q_map[0];
7269 struct rsp_que *rsp = ha->rsp_q_map[0];
7270 struct scsi_qla_host *vp;
Arun Easifeafb7b2010-09-03 14:57:00 -07007271 unsigned long flags;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007272
7273 status = qla2x00_init_rings(vha);
7274 if (!status) {
7275 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7276 ha->flags.chip_reset_done = 1;
7277
7278 status = qla2x00_fw_ready(vha);
7279 if (!status) {
Giridhar Malavalia9083012010-04-12 17:59:55 -07007280 /* Issue a marker after FW becomes ready. */
7281 qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007282 vha->flags.online = 1;
Chad Dupuis7108b762014-04-11 16:54:45 -04007283 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007284 }
7285
7286 /* if no cable then assume it's good */
7287 if ((vha->device_flags & DFLG_NO_CABLE))
7288 status = 0;
Giridhar Malavalia9083012010-04-12 17:59:55 -07007289 }
7290
7291 if (!status) {
7292 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7293
7294 if (!atomic_read(&vha->loop_down_timer)) {
7295 /*
7296 * Issue marker command only when we are going
7297 * to start the I/O .
7298 */
7299 vha->marker_needed = 1;
7300 }
7301
Giridhar Malavalia9083012010-04-12 17:59:55 -07007302 ha->isp_ops->enable_intrs(ha);
7303
7304 ha->isp_abort_cnt = 0;
7305 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7306
Saurav Kashyap53296782011-05-10 11:30:06 -07007307 /* Update the firmware version */
Giridhar Malavali31731672011-08-16 11:31:54 -07007308 status = qla82xx_check_md_needed(vha);
Saurav Kashyap53296782011-05-10 11:30:06 -07007309
Giridhar Malavalia9083012010-04-12 17:59:55 -07007310 if (ha->fce) {
7311 ha->flags.fce_enabled = 1;
7312 memset(ha->fce, 0,
7313 fce_calc_size(ha->fce_bufs));
7314 rval = qla2x00_enable_fce_trace(vha,
7315 ha->fce_dma, ha->fce_bufs, ha->fce_mb,
7316 &ha->fce_bufs);
7317 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007318 ql_log(ql_log_warn, vha, 0x8001,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007319 "Unable to reinitialize FCE (%d).\n",
7320 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007321 ha->flags.fce_enabled = 0;
7322 }
7323 }
7324
7325 if (ha->eft) {
7326 memset(ha->eft, 0, EFT_SIZE);
7327 rval = qla2x00_enable_eft_trace(vha,
7328 ha->eft_dma, EFT_NUM_BUFFERS);
7329 if (rval) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007330 ql_log(ql_log_warn, vha, 0x8010,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007331 "Unable to reinitialize EFT (%d).\n",
7332 rval);
Giridhar Malavalia9083012010-04-12 17:59:55 -07007333 }
7334 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07007335 }
7336
7337 if (!status) {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007338 ql_dbg(ql_dbg_taskm, vha, 0x8011,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007339 "qla82xx_restart_isp succeeded.\n");
Arun Easifeafb7b2010-09-03 14:57:00 -07007340
7341 spin_lock_irqsave(&ha->vport_slock, flags);
7342 list_for_each_entry(vp, &ha->vp_list, list) {
7343 if (vp->vp_idx) {
7344 atomic_inc(&vp->vref_count);
7345 spin_unlock_irqrestore(&ha->vport_slock, flags);
7346
Giridhar Malavalia9083012010-04-12 17:59:55 -07007347 qla2x00_vp_abort_isp(vp);
Arun Easifeafb7b2010-09-03 14:57:00 -07007348
7349 spin_lock_irqsave(&ha->vport_slock, flags);
7350 atomic_dec(&vp->vref_count);
7351 }
Giridhar Malavalia9083012010-04-12 17:59:55 -07007352 }
Arun Easifeafb7b2010-09-03 14:57:00 -07007353 spin_unlock_irqrestore(&ha->vport_slock, flags);
7354
Giridhar Malavalia9083012010-04-12 17:59:55 -07007355 } else {
Chad Dupuiscfb09192011-11-18 09:03:07 -08007356 ql_log(ql_log_warn, vha, 0x8016,
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007357 "qla82xx_restart_isp **** FAILED ****.\n");
Giridhar Malavalia9083012010-04-12 17:59:55 -07007358 }
7359
7360 return status;
7361}
7362
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007363void
Andrew Vasquezae97c912010-02-18 10:07:28 -08007364qla81xx_update_fw_options(scsi_qla_host_t *vha)
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007365{
Andrew Vasquezae97c912010-02-18 10:07:28 -08007366 struct qla_hw_data *ha = vha->hw;
7367
Himanshu Madhanif198caf2016-01-27 12:03:30 -05007368 /* Hold status IOCBs until ABTS response received. */
7369 if (ql2xfwholdabts)
7370 ha->fw_options[3] |= BIT_12;
7371
Giridhar Malavali088d09d2016-07-06 11:14:20 -04007372 /* Set Retry FLOGI in case of P2P connection */
7373 if (ha->operating_mode == P2P) {
7374 ha->fw_options[2] |= BIT_3;
7375 ql_dbg(ql_dbg_disc, vha, 0x2103,
7376 "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
7377 __func__, ha->fw_options[2]);
7378 }
7379
Quinn Tran41dc5292017-01-19 22:28:03 -08007380 /* Move PUREX, ABTS RX & RIDA to ATIOQ */
7381 if (ql2xmvasynctoatio) {
7382 if (qla_tgt_mode_enabled(vha) ||
7383 qla_dual_mode_enabled(vha))
7384 ha->fw_options[2] |= BIT_11;
7385 else
7386 ha->fw_options[2] &= ~BIT_11;
7387 }
Andrew Vasquezae97c912010-02-18 10:07:28 -08007388
Quinn Tranf7e761f2017-06-02 09:12:02 -07007389 if (qla_tgt_mode_enabled(vha) ||
Quinn Tran2da52732017-06-02 09:12:05 -07007390 qla_dual_mode_enabled(vha)) {
7391 /* FW auto send SCSI status during */
7392 ha->fw_options[1] |= BIT_8;
7393 ha->fw_options[10] |= (u16)SAM_STAT_BUSY << 8;
7394
7395 /* FW perform Exchange validation */
Quinn Tranf7e761f2017-06-02 09:12:02 -07007396 ha->fw_options[2] |= BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07007397 } else {
7398 ha->fw_options[1] &= ~BIT_8;
7399 ha->fw_options[10] &= 0x00ff;
7400
Quinn Tranf7e761f2017-06-02 09:12:02 -07007401 ha->fw_options[2] &= ~BIT_4;
Quinn Tran2da52732017-06-02 09:12:05 -07007402 }
Quinn Tranf7e761f2017-06-02 09:12:02 -07007403
Quinn Tran41dc5292017-01-19 22:28:03 -08007404 if (ql2xetsenable) {
7405 /* Enable ETS Burst. */
7406 memset(ha->fw_options, 0, sizeof(ha->fw_options));
7407 ha->fw_options[2] |= BIT_9;
7408 }
7409
Quinn Tran83548fe2017-06-02 09:12:01 -07007410 ql_dbg(ql_dbg_init, vha, 0x00e9,
7411 "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
7412 __func__, ha->fw_options[1], ha->fw_options[2],
7413 ha->fw_options[3], vha->host->active_mode);
Quinn Tran41dc5292017-01-19 22:28:03 -08007414
Andrew Vasquezae97c912010-02-18 10:07:28 -08007415 qla2x00_set_fw_options(vha, ha->fw_options);
Andrew Vasquez3a03eb72009-01-05 11:18:11 -08007416}
Sarang Radke09ff7012010-03-19 17:03:59 -07007417
7418/*
7419 * qla24xx_get_fcp_prio
7420 * Gets the fcp cmd priority value for the logged in port.
7421 * Looks for a match of the port descriptors within
7422 * each of the fcp prio config entries. If a match is found,
7423 * the tag (priority) value is returned.
7424 *
7425 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007426 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07007427 * fcport = port structure pointer.
7428 *
7429 * Return:
Andrew Vasquez6c452a42010-03-19 17:04:02 -07007430 * non-zero (if found)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007431 * -1 (if not found)
Sarang Radke09ff7012010-03-19 17:03:59 -07007432 *
7433 * Context:
7434 * Kernel context
7435 */
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007436static int
Sarang Radke09ff7012010-03-19 17:03:59 -07007437qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
7438{
7439 int i, entries;
7440 uint8_t pid_match, wwn_match;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007441 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07007442 uint32_t pid1, pid2;
7443 uint64_t wwn1, wwn2;
7444 struct qla_fcp_prio_entry *pri_entry;
7445 struct qla_hw_data *ha = vha->hw;
7446
7447 if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007448 return -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07007449
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007450 priority = -1;
Sarang Radke09ff7012010-03-19 17:03:59 -07007451 entries = ha->fcp_prio_cfg->num_entries;
7452 pri_entry = &ha->fcp_prio_cfg->entry[0];
7453
7454 for (i = 0; i < entries; i++) {
7455 pid_match = wwn_match = 0;
7456
7457 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
7458 pri_entry++;
7459 continue;
7460 }
7461
7462 /* check source pid for a match */
7463 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
7464 pid1 = pri_entry->src_pid & INVALID_PORT_ID;
7465 pid2 = vha->d_id.b24 & INVALID_PORT_ID;
7466 if (pid1 == INVALID_PORT_ID)
7467 pid_match++;
7468 else if (pid1 == pid2)
7469 pid_match++;
7470 }
7471
7472 /* check destination pid for a match */
7473 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
7474 pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
7475 pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
7476 if (pid1 == INVALID_PORT_ID)
7477 pid_match++;
7478 else if (pid1 == pid2)
7479 pid_match++;
7480 }
7481
7482 /* check source WWN for a match */
7483 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
7484 wwn1 = wwn_to_u64(vha->port_name);
7485 wwn2 = wwn_to_u64(pri_entry->src_wwpn);
7486 if (wwn2 == (uint64_t)-1)
7487 wwn_match++;
7488 else if (wwn1 == wwn2)
7489 wwn_match++;
7490 }
7491
7492 /* check destination WWN for a match */
7493 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
7494 wwn1 = wwn_to_u64(fcport->port_name);
7495 wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
7496 if (wwn2 == (uint64_t)-1)
7497 wwn_match++;
7498 else if (wwn1 == wwn2)
7499 wwn_match++;
7500 }
7501
7502 if (pid_match == 2 || wwn_match == 2) {
7503 /* Found a matching entry */
7504 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
7505 priority = pri_entry->tag;
7506 break;
7507 }
7508
7509 pri_entry++;
7510 }
7511
7512 return priority;
7513}
7514
7515/*
7516 * qla24xx_update_fcport_fcp_prio
7517 * Activates fcp priority for the logged in fc port
7518 *
7519 * Input:
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007520 * vha = scsi host structure pointer.
Sarang Radke09ff7012010-03-19 17:03:59 -07007521 * fcp = port structure pointer.
7522 *
7523 * Return:
7524 * QLA_SUCCESS or QLA_FUNCTION_FAILED
7525 *
7526 * Context:
7527 * Kernel context.
7528 */
7529int
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007530qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
Sarang Radke09ff7012010-03-19 17:03:59 -07007531{
7532 int ret;
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007533 int priority;
Sarang Radke09ff7012010-03-19 17:03:59 -07007534 uint16_t mb[5];
7535
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007536 if (fcport->port_type != FCT_TARGET ||
7537 fcport->loop_id == FC_NO_LOOP_ID)
Sarang Radke09ff7012010-03-19 17:03:59 -07007538 return QLA_FUNCTION_FAILED;
7539
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007540 priority = qla24xx_get_fcp_prio(vha, fcport);
Andrew Vasquezf28a0a92011-03-30 11:46:18 -07007541 if (priority < 0)
7542 return QLA_FUNCTION_FAILED;
7543
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04007544 if (IS_P3P_TYPE(vha->hw)) {
Saurav Kashyapa00f6292011-11-18 09:03:19 -08007545 fcport->fcp_prio = priority & 0xf;
7546 return QLA_SUCCESS;
7547 }
7548
Madhuranath Iyengar21090cb2010-12-21 16:00:18 -08007549 ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
Chad Dupuiscfb09192011-11-18 09:03:07 -08007550 if (ret == QLA_SUCCESS) {
7551 if (fcport->fcp_prio != priority)
7552 ql_dbg(ql_dbg_user, vha, 0x709e,
7553 "Updated FCP_CMND priority - value=%d loop_id=%d "
7554 "port_id=%02x%02x%02x.\n", priority,
7555 fcport->loop_id, fcport->d_id.b.domain,
7556 fcport->d_id.b.area, fcport->d_id.b.al_pa);
Saurav Kashyapa00f6292011-11-18 09:03:19 -08007557 fcport->fcp_prio = priority & 0xf;
Chad Dupuiscfb09192011-11-18 09:03:07 -08007558 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -07007559 ql_dbg(ql_dbg_user, vha, 0x704f,
Chad Dupuiscfb09192011-11-18 09:03:07 -08007560 "Unable to update FCP_CMND priority - ret=0x%x for "
7561 "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
7562 fcport->d_id.b.domain, fcport->d_id.b.area,
7563 fcport->d_id.b.al_pa);
Sarang Radke09ff7012010-03-19 17:03:59 -07007564 return ret;
7565}
7566
7567/*
7568 * qla24xx_update_all_fcp_prio
7569 * Activates fcp priority for all the logged in ports
7570 *
7571 * Input:
7572 * ha = adapter block pointer.
7573 *
7574 * Return:
7575 * QLA_SUCCESS or QLA_FUNCTION_FAILED
7576 *
7577 * Context:
7578 * Kernel context.
7579 */
7580int
7581qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
7582{
7583 int ret;
7584 fc_port_t *fcport;
7585
7586 ret = QLA_FUNCTION_FAILED;
7587 /* We need to set priority for all logged in ports */
7588 list_for_each_entry(fcport, &vha->vp_fcports, list)
7589 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
7590
7591 return ret;
7592}
Michael Hernandezd7459522016-12-12 14:40:07 -08007593
Quinn Tran82de8022017-06-13 20:47:17 -07007594struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
7595 int vp_idx, bool startqp)
Michael Hernandezd7459522016-12-12 14:40:07 -08007596{
7597 int rsp_id = 0;
7598 int req_id = 0;
7599 int i;
7600 struct qla_hw_data *ha = vha->hw;
7601 uint16_t qpair_id = 0;
7602 struct qla_qpair *qpair = NULL;
7603 struct qla_msix_entry *msix;
7604
7605 if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
7606 ql_log(ql_log_warn, vha, 0x00181,
7607 "FW/Driver is not multi-queue capable.\n");
7608 return NULL;
7609 }
7610
7611 if (ql2xmqsupport) {
7612 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
7613 if (qpair == NULL) {
7614 ql_log(ql_log_warn, vha, 0x0182,
7615 "Failed to allocate memory for queue pair.\n");
7616 return NULL;
7617 }
7618 memset(qpair, 0, sizeof(struct qla_qpair));
7619
7620 qpair->hw = vha->hw;
Joe Carnuccio25ff6af2017-01-19 22:28:04 -08007621 qpair->vha = vha;
Quinn Tran82de8022017-06-13 20:47:17 -07007622 qpair->qp_lock_ptr = &qpair->qp_lock;
7623 spin_lock_init(&qpair->qp_lock);
Quinn Tranaf7bb382017-06-13 20:47:23 -07007624 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
Michael Hernandezd7459522016-12-12 14:40:07 -08007625
7626 /* Assign available que pair id */
7627 mutex_lock(&ha->mq_lock);
7628 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
Sawan Chandakb95b9452017-05-24 18:06:20 -07007629 if (ha->num_qpairs >= ha->max_qpairs) {
Michael Hernandezd7459522016-12-12 14:40:07 -08007630 mutex_unlock(&ha->mq_lock);
7631 ql_log(ql_log_warn, vha, 0x0183,
7632 "No resources to create additional q pair.\n");
7633 goto fail_qid_map;
7634 }
Sawan Chandakb95b9452017-05-24 18:06:20 -07007635 ha->num_qpairs++;
Michael Hernandezd7459522016-12-12 14:40:07 -08007636 set_bit(qpair_id, ha->qpair_qid_map);
7637 ha->queue_pair_map[qpair_id] = qpair;
7638 qpair->id = qpair_id;
7639 qpair->vp_idx = vp_idx;
Quinn Trane326d222017-06-13 20:47:18 -07007640 INIT_LIST_HEAD(&qpair->hints_list);
Quinn Tran7c3f8fd2017-06-13 20:47:22 -07007641 qpair->chip_reset = ha->base_qpair->chip_reset;
7642 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
7643 qpair->enable_explicit_conf =
7644 ha->base_qpair->enable_explicit_conf;
Michael Hernandezd7459522016-12-12 14:40:07 -08007645
7646 for (i = 0; i < ha->msix_count; i++) {
Quinn Tran093df732016-12-12 14:40:09 -08007647 msix = &ha->msix_entries[i];
Michael Hernandezd7459522016-12-12 14:40:07 -08007648 if (msix->in_use)
7649 continue;
7650 qpair->msix = msix;
Quinn Tran83548fe2017-06-02 09:12:01 -07007651 ql_dbg(ql_dbg_multiq, vha, 0xc00f,
Michael Hernandezd7459522016-12-12 14:40:07 -08007652 "Vector %x selected for qpair\n", msix->vector);
7653 break;
7654 }
7655 if (!qpair->msix) {
7656 ql_log(ql_log_warn, vha, 0x0184,
7657 "Out of MSI-X vectors!.\n");
7658 goto fail_msix;
7659 }
7660
7661 qpair->msix->in_use = 1;
7662 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
Quinn Tran8abfa9e2017-06-13 20:47:24 -07007663 qpair->pdev = ha->pdev;
7664 if (IS_QLA27XX(ha) || IS_QLA83XX(ha))
7665 qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
Michael Hernandezd7459522016-12-12 14:40:07 -08007666
7667 mutex_unlock(&ha->mq_lock);
7668
7669 /* Create response queue first */
Quinn Tran82de8022017-06-13 20:47:17 -07007670 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08007671 if (!rsp_id) {
7672 ql_log(ql_log_warn, vha, 0x0185,
7673 "Failed to create response queue.\n");
7674 goto fail_rsp;
7675 }
7676
7677 qpair->rsp = ha->rsp_q_map[rsp_id];
7678
7679 /* Create request queue */
Quinn Tran82de8022017-06-13 20:47:17 -07007680 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
7681 startqp);
Michael Hernandezd7459522016-12-12 14:40:07 -08007682 if (!req_id) {
7683 ql_log(ql_log_warn, vha, 0x0186,
7684 "Failed to create request queue.\n");
7685 goto fail_req;
7686 }
7687
7688 qpair->req = ha->req_q_map[req_id];
7689 qpair->rsp->req = qpair->req;
Quinn Tran82de8022017-06-13 20:47:17 -07007690 qpair->rsp->qpair = qpair;
Quinn Trane326d222017-06-13 20:47:18 -07007691 /* init qpair to this cpu. Will adjust at run time. */
7692 qla_cpu_update(qpair, smp_processor_id());
Michael Hernandezd7459522016-12-12 14:40:07 -08007693
7694 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
7695 if (ha->fw_attributes & BIT_4)
7696 qpair->difdix_supported = 1;
7697 }
7698
7699 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
7700 if (!qpair->srb_mempool) {
Quinn Tran83548fe2017-06-02 09:12:01 -07007701 ql_log(ql_log_warn, vha, 0xd036,
Michael Hernandezd7459522016-12-12 14:40:07 -08007702 "Failed to create srb mempool for qpair %d\n",
7703 qpair->id);
7704 goto fail_mempool;
7705 }
7706
7707 /* Mark as online */
7708 qpair->online = 1;
7709
7710 if (!vha->flags.qpairs_available)
7711 vha->flags.qpairs_available = 1;
7712
7713 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
7714 "Request/Response queue pair created, id %d\n",
7715 qpair->id);
7716 ql_dbg(ql_dbg_init, vha, 0x0187,
7717 "Request/Response queue pair created, id %d\n",
7718 qpair->id);
7719 }
7720 return qpair;
7721
7722fail_mempool:
7723fail_req:
7724 qla25xx_delete_rsp_que(vha, qpair->rsp);
7725fail_rsp:
7726 mutex_lock(&ha->mq_lock);
7727 qpair->msix->in_use = 0;
7728 list_del(&qpair->qp_list_elem);
7729 if (list_empty(&vha->qp_list))
7730 vha->flags.qpairs_available = 0;
7731fail_msix:
7732 ha->queue_pair_map[qpair_id] = NULL;
7733 clear_bit(qpair_id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07007734 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08007735 mutex_unlock(&ha->mq_lock);
7736fail_qid_map:
7737 kfree(qpair);
7738 return NULL;
7739}
7740
7741int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
7742{
Sawan Chandakd65237c2017-06-13 20:47:19 -07007743 int ret = QLA_FUNCTION_FAILED;
Michael Hernandezd7459522016-12-12 14:40:07 -08007744 struct qla_hw_data *ha = qpair->hw;
7745
Sawan Chandakd65237c2017-06-13 20:47:19 -07007746 if (!vha->flags.qpairs_req_created && !vha->flags.qpairs_rsp_created)
7747 goto fail;
7748
Michael Hernandezd7459522016-12-12 14:40:07 -08007749 qpair->delete_in_progress = 1;
7750 while (atomic_read(&qpair->ref_count))
7751 msleep(500);
7752
7753 ret = qla25xx_delete_req_que(vha, qpair->req);
7754 if (ret != QLA_SUCCESS)
7755 goto fail;
7756 ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
7757 if (ret != QLA_SUCCESS)
7758 goto fail;
7759
7760 mutex_lock(&ha->mq_lock);
7761 ha->queue_pair_map[qpair->id] = NULL;
7762 clear_bit(qpair->id, ha->qpair_qid_map);
Sawan Chandakb95b9452017-05-24 18:06:20 -07007763 ha->num_qpairs--;
Michael Hernandezd7459522016-12-12 14:40:07 -08007764 list_del(&qpair->qp_list_elem);
Sawan Chandakd65237c2017-06-13 20:47:19 -07007765 if (list_empty(&vha->qp_list)) {
Michael Hernandezd7459522016-12-12 14:40:07 -08007766 vha->flags.qpairs_available = 0;
Sawan Chandakd65237c2017-06-13 20:47:19 -07007767 vha->flags.qpairs_req_created = 0;
7768 vha->flags.qpairs_rsp_created = 0;
7769 }
Michael Hernandezd7459522016-12-12 14:40:07 -08007770 mempool_destroy(qpair->srb_mempool);
7771 kfree(qpair);
7772 mutex_unlock(&ha->mq_lock);
7773
7774 return QLA_SUCCESS;
7775fail:
7776 return ret;
7777}